Codebase list jd-gui / beb5dc6 services / src / main / java / org / jd / gui / view / component / RoundMarkErrorStrip.java
beb5dc6

Tree @beb5dc6 (Download .tar.gz)

RoundMarkErrorStrip.java @beb5dc6raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
/*
 * Copyright (c) 2008-2019 Emmanuel Dupuy.
 * This project is distributed under the GPLv3 license.
 * This is a Copyleft license that gives the user the right to use,
 * copy and modify the code freely for non-commercial purposes.
 */

package org.jd.gui.view.component;

import org.fife.ui.rsyntaxtextarea.DocumentRange;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.parser.Parser;
import org.fife.ui.rsyntaxtextarea.parser.ParserNotice;
import org.fife.ui.rsyntaxtextarea.parser.TaskTagParser.TaskNotice;
import org.fife.ui.rtextarea.RTextArea;

import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.BadLocationException;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.MessageFormat;
import java.util.*;
import java.util.List;

/*
 * 'private' access prohibit all changes ==> Copy "ErrorStrip" to JD-GUI project just to change the marker.
 *
 * JD-GUI uses two workarounds for RSyntaxTextArea:
 * - org.fife.ui.rtextarea.Marker
 * - org.jd.gui.view.component.RoundMarkErrorStrip
 */

/*
 * 08/10/2009
 *
 * ErrorStrip.java - A component that can visually show Parser messages (syntax
 * errors, etc.) in an RSyntaxTextArea.
 *
 * This library is distributed under a modified BSD license.  See the included
 * RSyntaxTextArea.License.txt file for details.
 */

/**
 * A component to sit alongside an {@link RSyntaxTextArea} that displays
 * colored markers for locations of interest (parser errors, marked
 * occurrences, etc.).<p>
 *
 * <code>ErrorStrip</code>s display <code>ParserNotice</code>s from
 * {@link Parser}s.  Currently, the only way to get lines flagged in this
 * component is to register a <code>Parser</code> on an RSyntaxTextArea and
 * return <code>ParserNotice</code>s for each line to display an icon for.
 * The severity of each notice must be at least the threshold set by
 * {@link #setLevelThreshold(ParserNotice.Level)}
 * to be displayed in this error strip.  The default threshold is
 * {@link ParserNotice.Level#WARNING}.<p>
 *
 * An <code>ErrorStrip</code> can be added to a UI like so:
 * <pre>
 * textArea = createTextArea();
 * textArea.addParser(new MyParser(textArea)); // Identifies lines to display
 * scrollPane = new RTextScrollPane(textArea, true);
 * ErrorStrip es = new ErrorStrip(textArea);
 * JPanel temp = new JPanel(new BorderLayout());
 * temp.add(scrollPane);
 * temp.add(es, BorderLayout.LINE_END);
 * </pre>
 *
 * @author Robert Futrell
 * @version 0.5
 */
/*
 * Possible improvements:
 *    1. Handle marked occurrence changes & "mark all" changes separately from
 *       parser changes. For each property change, call a method that removes
 *       the notices being reloaded from the Markers (removing any Markers that
 *       are now "empty").
 */
public class RoundMarkErrorStrip extends JComponent {

    /**
     * The text area.
     */
    private RSyntaxTextArea textArea;

    /**
     * Listens for events in this component.
     */
    private Listener listener;

    /**
     * Whether "marked occurrences" in the text area should be shown in this
     * error strip.
     */
    private boolean showMarkedOccurrences;

    /**
     * Whether markers for "mark all" highlights should be shown in this
     * error strip.
     */
    private boolean showMarkAll;

    /**
     * Mapping of colors to brighter colors.  This is kept to prevent
     * unnecessary creation of the same Colors over and over.
     */
    private Map<Color, Color> brighterColors;

    /**
     * Added for JD-GUI.
     *
     * Mapping of colors to darker colors.  This is kept to prevent
     * unnecessary creation of the same Colors over and over.
     */
    private Map<Color, Color> darkerColors;

    /**
     * Only notices of this severity (or worse) will be displayed in this
     * error strip.
     */
    private ParserNotice.Level levelThreshold;

    /**
     * Whether the caret marker's location should be rendered.
     */
    private boolean followCaret;

    /**
     * The color to use for the caret marker.
     */
    private Color caretMarkerColor;

    /**
     * Where we paint the caret marker.
     */
    private int caretLineY;

    /**
     * The last location of the caret marker.
     */
    private int lastLineY;

    /**
     * The preferred width of this component.
     */
    private static final int PREFERRED_WIDTH = 14;

    private static final String MSG = "org.fife.ui.rsyntaxtextarea.ErrorStrip";
    private static final ResourceBundle msg = ResourceBundle.getBundle(MSG);

    /**
     * Constructor.
     *
     * @param textArea The text area we are examining.
     */
    public RoundMarkErrorStrip(RSyntaxTextArea textArea) {
        this.textArea = textArea;
        listener = new Listener();
        ToolTipManager.sharedInstance().registerComponent(this);
        setLayout(null); // Manually layout Markers as they can overlap
        addMouseListener(listener);
        setShowMarkedOccurrences(true);
        setShowMarkAll(true);
        setLevelThreshold(ParserNotice.Level.WARNING);
        setFollowCaret(true);
        setCaretMarkerColor(new Color(0x96c5fe));
    }


    /**
     * Overridden so we only start listening for parser notices when this
     * component (and presumably the text area) are visible.
     */
    @Override
    public void addNotify() {
        super.addNotify();
        textArea.addCaretListener(listener);
        textArea.addPropertyChangeListener(
                RSyntaxTextArea.PARSER_NOTICES_PROPERTY, listener);
        textArea.addPropertyChangeListener(
                RSyntaxTextArea.MARK_OCCURRENCES_PROPERTY, listener);
        textArea.addPropertyChangeListener(
                RSyntaxTextArea.MARKED_OCCURRENCES_CHANGED_PROPERTY, listener);
        textArea.addPropertyChangeListener(
                RSyntaxTextArea.MARK_ALL_OCCURRENCES_CHANGED_PROPERTY, listener);
        refreshMarkers();
    }


    /**
     * Manually manages layout since this component uses no layout manager.
     */
    @Override
    public void doLayout() {
        for (int i=0; i<getComponentCount(); i++) {
            Marker m = (Marker)getComponent(i);
            m.updateLocation();
        }
        listener.caretUpdate(null); // Force recalculation of caret line pos
    }


    /**
     * Returns a "brighter" color.
     *
     * @param c The color.
     * @return A brighter color.
     */
    private Color getBrighterColor(Color c) {
        if (brighterColors==null) {
            brighterColors = new HashMap<Color, Color>(5); // Usually small
        }
        Color brighter = brighterColors.get(c);
        if (brighter==null) {
            // Don't use c.brighter() as it doesn't work well for blue, and
            // also doesn't return something brighter "enough."
            int r = possiblyBrighter(c.getRed());
            int g = possiblyBrighter(c.getGreen());
            int b = possiblyBrighter(c.getBlue());
            brighter = new Color(r, g, b);
            brighterColors.put(c, brighter);
        }
        return brighter;
    }


    /**
     * Added for JD-GUI.
     *
     * Returns a "brighter" color.
     *
     * @param c The color.
     * @return A brighter color.
     */
    private Color getDarkerColor(Color c) {
        if (darkerColors==null) {
            darkerColors = new HashMap<Color, Color>(5); // Usually small
        }
        Color darker = darkerColors.get(c);
        if (darker==null) {
            // Don't use c.brighter() as it doesn't work well for blue, and
            // also doesn't return something brighter "enough."
            int r = possiblyDarker(c.getRed());
            int g = possiblyDarker(c.getGreen());
            int b = possiblyDarker(c.getBlue());
            darker = new Color(r, g, b);
            darkerColors.put(c, darker);
        }
        return darker;
    }


    /**
     * returns the color to use when painting the caret marker.
     *
     * @return The caret marker color.
     * @see #setCaretMarkerColor(Color)
     */
    public Color getCaretMarkerColor() {
        return caretMarkerColor;
    }


    /**
     * Returns whether the caret's position should be drawn.
     *
     * @return Whether the caret's position should be drawn.
     * @see #setFollowCaret(boolean)
     */
    public boolean getFollowCaret() {
        return followCaret;
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public Dimension getPreferredSize() {
        int height = textArea.getPreferredScrollableViewportSize().height;
        return new Dimension(PREFERRED_WIDTH, height);
    }


    /**
     * Returns the minimum severity a parser notice must be for it to be
     * displayed in this error strip.  This will be one of the constants
     * defined in the <code>ParserNotice</code> class.
     *
     * @return The minimum severity.
     * @see #setLevelThreshold(ParserNotice.Level)
     */
    public ParserNotice.Level getLevelThreshold() {
        return levelThreshold;
    }


    /**
     * Returns whether "mark all" highlights are shown in this error strip.
     *
     * @return Whether markers are shown for "mark all" highlights.
     * @see #setShowMarkAll(boolean)
     */
    public boolean getShowMarkAll() {
        return showMarkAll;
    }


    /**
     * Returns whether marked occurrences are shown in this error strip.
     *
     * @return Whether marked occurrences are shown.
     * @see #setShowMarkedOccurrences(boolean)
     */
    public boolean getShowMarkedOccurrences() {
        return showMarkedOccurrences;
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public String getToolTipText(MouseEvent e) {
        String text = null;
        int line = yToLine(e.getY());
        if (line>-1) {
            text = msg.getString("Line");
            text = MessageFormat.format(text, Integer.valueOf(line+1));
        }
        return text;
    }


    /**
     * Returns the y-offset in this component corresponding to a line in the
     * text component.
     *
     * @param line The line.
     * @return The y-offset.
     * @see #yToLine(int)
     */
    private int lineToY(int line) {
        int h = textArea.getVisibleRect().height;
        float lineCount = textArea.getLineCount();
        return (int)(((line-1)/(lineCount-1)) * h) - 2;
    }


    /**
     * Overridden to (possibly) draw the caret's position.
     *
     * @param g The graphics context.
     */
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (caretLineY>-1) {
            g.setColor(getCaretMarkerColor());
            g.fillRect(0, caretLineY, getWidth(), 2);
        }
    }


    /**
     * Returns a possibly brighter component for a color.
     *
     * @param i An RGB component for a color (0-255).
     * @return A possibly brighter value for the component.
     */
    private static final int possiblyBrighter(int i) {
        if (i<255) {
            i += (int)((255-i)*0.6f);
        }
        return i;
    }


    /**
     * Returns a possibly darker component for a color.
     *
     * @param i An RGB component for a color (0-255).
     * @return A possibly brighter value for the component.
     */
    private static final int possiblyDarker(int i) {
        return i -= (int)(i*0.4f);
    }


    /**
     * Refreshes the markers displayed in this error strip.
     */
    private void refreshMarkers() {

        removeAll(); // listener is removed in Marker.removeNotify()
        Map<Integer, Marker> markerMap = new HashMap<Integer, Marker>();

        List<ParserNotice> notices = textArea.getParserNotices();
        for (ParserNotice notice : notices) {
            if (notice.getLevel().isEqualToOrWorseThan(levelThreshold) ||
                    (notice instanceof TaskNotice)) {
                Integer key = Integer.valueOf(notice.getLine());
                Marker m = markerMap.get(key);
                if (m==null) {
                    m = new Marker(notice);
                    m.addMouseListener(listener);
                    markerMap.put(key, m);
                    add(m);
                }
                else {
                    m.addNotice(notice);
                }
            }
        }

        if (getShowMarkedOccurrences() && textArea.getMarkOccurrences()) {
            List<DocumentRange> occurrences = textArea.getMarkedOccurrences();
            addMarkersForRanges(occurrences, markerMap, textArea.getMarkOccurrencesColor());
        }

        if (getShowMarkAll() /*&& textArea.getMarkAll()*/) {
            Color markAllColor = textArea.getMarkAllHighlightColor();
            List<DocumentRange> ranges = textArea.getMarkAllHighlightRanges();
            addMarkersForRanges(ranges, markerMap, markAllColor);
        }

        revalidate();
        repaint();

    }


    /**
     * Adds markers for a list of ranges in the document.
     *
     * @param ranges The list of ranges in the document.
     * @param markerMap A mapping from line number to <code>Marker</code>.
     * @param color The color to use for the markers.
     */
    private void addMarkersForRanges(List<DocumentRange> ranges,
                                     Map<Integer, Marker> markerMap, Color color) {
        for (DocumentRange range : ranges) {
            int line = 0;
            try {
                line = textArea.getLineOfOffset(range.getStartOffset());
            } catch (BadLocationException ble) { // Never happens
                continue;
            }
            ParserNotice notice = new MarkedOccurrenceNotice(range, color);
            Integer key = Integer.valueOf(line);
            Marker m = markerMap.get(key);
            if (m==null) {
                m = new Marker(notice);
                m.addMouseListener(listener);
                markerMap.put(key, m);
                add(m);
            }
            else {
                if (!m.containsMarkedOccurence()) {
                    m.addNotice(notice);
                }
            }
        }
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public void removeNotify() {
        super.removeNotify();
        textArea.removeCaretListener(listener);
        textArea.removePropertyChangeListener(
                RSyntaxTextArea.PARSER_NOTICES_PROPERTY, listener);
        textArea.removePropertyChangeListener(
                RSyntaxTextArea.MARK_OCCURRENCES_PROPERTY, listener);
        textArea.removePropertyChangeListener(
                RSyntaxTextArea.MARKED_OCCURRENCES_CHANGED_PROPERTY, listener);
        textArea.removePropertyChangeListener(
                RSyntaxTextArea.MARK_ALL_OCCURRENCES_CHANGED_PROPERTY, listener);
    }


    /**
     * Sets the color to use when painting the caret marker.
     *
     * @param color The new caret marker color.
     * @see #getCaretMarkerColor()
     */
    public void setCaretMarkerColor(Color color) {
        if (color!=null) {
            caretMarkerColor = color;
            listener.caretUpdate(null); // Force repaint
        }
    }


    /**
     * Toggles whether the caret's current location should be drawn.
     *
     * @param follow Whether the caret's current location should be followed.
     * @see #getFollowCaret()
     */
    public void setFollowCaret(boolean follow) {
        if (followCaret!=follow) {
            if (followCaret) {
                repaint(0,caretLineY, getWidth(),2); // Erase
            }
            caretLineY = -1;
            lastLineY = -1;
            followCaret = follow;
            listener.caretUpdate(null); // Possibly repaint
        }
    }


    /**
     * Sets the minimum severity a parser notice must be for it to be displayed
     * in this error strip.  This should be one of the constants defined in
     * the <code>ParserNotice</code> class.  The default value is
     * {@link ParserNotice.Level#WARNING}.
     *
     * @param level The new severity threshold.
     * @see #getLevelThreshold()
     * @see ParserNotice
     */
    public void setLevelThreshold(ParserNotice.Level level) {
        levelThreshold = level;
        if (isDisplayable()) {
            refreshMarkers();
        }
    }


    /**
     * Sets whether "mark all" highlights are shown in this error strip.
     *
     * @param show Whether to show markers for "mark all" highlights.
     * @see #getShowMarkAll()
     */
    public void setShowMarkAll(boolean show) {
        if (show!=showMarkAll) {
            showMarkAll = show;
            if (isDisplayable()) { // Skip this when we're first created
                refreshMarkers();
            }
        }
    }


    /**
     * Sets whether marked occurrences are shown in this error strip.
     *
     * @param show Whether to show marked occurrences.
     * @see #getShowMarkedOccurrences()
     */
    public void setShowMarkedOccurrences(boolean show) {
        if (show!=showMarkedOccurrences) {
            showMarkedOccurrences = show;
            if (isDisplayable()) { // Skip this when we're first created
                refreshMarkers();
            }
        }
    }


    /**
     * Returns the line in the text area corresponding to a y-offset in this
     * component.
     *
     * @param y The y-offset.
     * @return The line.
     * @see #lineToY(int)
     */
    private final int yToLine(int y) {
        int line = -1;
        int h = textArea.getVisibleRect().height;
        if (y<h) {
            float at = y/(float)h;
            line = Math.round((textArea.getLineCount()-1)*at);
        }
        return line;
    }


    /**
     * Listens for events in the error strip and its markers.
     */
    private class Listener extends MouseAdapter
            implements PropertyChangeListener, CaretListener {

        private Rectangle visibleRect = new Rectangle();

        public void caretUpdate(CaretEvent e) {
            if (getFollowCaret()) {
                int line = textArea.getCaretLineNumber();
                float percent = line / (float)(textArea.getLineCount()-1);
                textArea.computeVisibleRect(visibleRect);
                caretLineY = (int)(visibleRect.height*percent);
                if (caretLineY!=lastLineY) {
                    repaint(0,lastLineY, getWidth(), 2); // Erase old position
                    repaint(0,caretLineY, getWidth(), 2);
                    lastLineY = caretLineY;
                }
            }
        }

        @Override
        public void mouseClicked(MouseEvent e) {

            Component source = (Component)e.getSource();
            if (source instanceof Marker) {
                ((Marker)source).mouseClicked(e);
                return;
            }

            int line = yToLine(e.getY());
            if (line>-1) {
                try {
                    int offs = textArea.getLineStartOffset(line);
                    textArea.setCaretPosition(offs);
                } catch (BadLocationException ble) { // Never happens
                    UIManager.getLookAndFeel().provideErrorFeedback(textArea);
                }
            }

        }

        public void propertyChange(PropertyChangeEvent e) {

            String propName = e.getPropertyName();

            // If they change whether marked occurrences are visible in editor
            if (RSyntaxTextArea.MARK_OCCURRENCES_PROPERTY.equals(propName)) {
                if (getShowMarkedOccurrences()) {
                    refreshMarkers();
                }
            }

            // If parser notices changed.
            // TODO: Don't update "mark all/occurrences" markers.
            else if (RSyntaxTextArea.PARSER_NOTICES_PROPERTY.equals(propName)) {
                refreshMarkers();
            }

            // If marked occurrences changed.
            // TODO: Only update "mark occurrences" markers, not all of them.
            else if (RSyntaxTextArea.MARKED_OCCURRENCES_CHANGED_PROPERTY.
                    equals(propName)) {
                if (getShowMarkedOccurrences()) {
                    refreshMarkers();
                }
            }

            // If "mark all" occurrences changed.
            // TODO: Only update "mark all" markers, not all of them.
            else if (RTextArea.MARK_ALL_OCCURRENCES_CHANGED_PROPERTY.
                    equals(propName)) {
                if (getShowMarkAll()) {
                    refreshMarkers();
                }
            }

        }

    }


    /**
     * A notice that wraps a "marked occurrence."
     */
    private class MarkedOccurrenceNotice implements ParserNotice {

        private DocumentRange range;
        private Color color;

        public MarkedOccurrenceNotice(DocumentRange range, Color color) {
            this.range = range;
            this.color = color;
        }

        public int compareTo(ParserNotice other) {
            return 0; // Value doesn't matter
        }

        public boolean containsPosition(int pos) {
            return pos>=range.getStartOffset() && pos<range.getEndOffset();
        }

        @Override
        public boolean equals(Object o) {
            // FindBugs - Define equals() when defining compareTo()
            if (!(o instanceof ParserNotice)) {
                return false;
            }
            return compareTo((ParserNotice)o)==0;
        }

        public Color getColor() {
            return color;
        }

        /**
         * {@inheritDoc}
         */
        public boolean getKnowsOffsetAndLength() {
            return true;
        }

        public int getLength() {
            return range.getEndOffset() - range.getStartOffset();
        }

        public Level getLevel() {
            return Level.INFO; // Won't matter
        }

        public int getLine() {
            try {
                return textArea.getLineOfOffset(range.getStartOffset())+1;
            } catch (BadLocationException ble) {
                return 0;
            }
        }

        public String getMessage() {
            String text = null;
            try {
                String word = textArea.getText(range.getStartOffset(),
                        getLength());
                text = msg.getString("OccurrenceOf");
                text = MessageFormat.format(text, word);
            } catch (BadLocationException ble) {
                UIManager.getLookAndFeel().provideErrorFeedback(textArea);
            }
            return text;
        }

        public int getOffset() {
            return range.getStartOffset();
        }

        public Parser getParser() {
            return null;
        }

        public boolean getShowInEditor() {
            return false; // Value doesn't matter
        }

        public String getToolTipText() {
            return null;
        }

        @Override
        public int hashCode() { // FindBugs, since we override equals()
            return 0; // Value doesn't matter for us.
        }

    }


    /**
     * A "marker" in this error strip, representing one or more notices.
     */
    private class Marker extends JComponent {

        private List<ParserNotice> notices;

        public Marker(ParserNotice notice) {
            notices = new ArrayList<ParserNotice>(1); // Usually just 1
            addNotice(notice);
            setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            setSize(getPreferredSize());
            ToolTipManager.sharedInstance().registerComponent(this);
        }

        public void addNotice(ParserNotice notice) {
            notices.add(notice);
        }

        public boolean containsMarkedOccurence() {
            boolean result = false;
            for (int i=0; i<notices.size(); i++) {
                if (notices.get(i) instanceof MarkedOccurrenceNotice) {
                    result = true;
                    break;
                }
            }
            return result;
        }

        public Color getColor() {
            // Return the color for the highest-level parser.
            Color c = null;
            int lowestLevel = Integer.MAX_VALUE; // ERROR is 0
            for (ParserNotice notice : notices) {
                if (notice.getLevel().getNumericValue()<lowestLevel) {
                    lowestLevel = notice.getLevel().getNumericValue();
                    c = notice.getColor();
                }
            }
            return c;
        }

        @Override
        public Dimension getPreferredSize() {
            int w = PREFERRED_WIDTH - 4; // 2-pixel empty border
            return new Dimension(w, 5);
        }

        @Override
        public String getToolTipText() {

            String text = null;

            if (notices.size()==1) {
                text = notices.get(0).getMessage();
            }
            else { // > 1
                StringBuilder sb = new StringBuilder("<html>");
                sb.append(msg.getString("MultipleMarkers"));
                sb.append("<br>");
                for (int i=0; i<notices.size(); i++) {
                    ParserNotice pn = notices.get(i);
                    sb.append("&nbsp;&nbsp;&nbsp;- ");
                    sb.append(pn.getMessage());
                    sb.append("<br>");
                }
                text = sb.toString();
            }

            return text;

        }

        protected void mouseClicked(MouseEvent e) {
            ParserNotice pn = notices.get(0);
            int offs = pn.getOffset();
            int len = pn.getLength();
            if (offs>-1 && len>-1) { // These values are optional
                textArea.setSelectionStart(offs);
                textArea.setSelectionEnd(offs+len);
            }
            else {
                int line = pn.getLine();
                try {
                    offs = textArea.getLineStartOffset(line);
                    textArea.setCaretPosition(offs);
                } catch (BadLocationException ble) { // Never happens
                    UIManager.getLookAndFeel().provideErrorFeedback(textArea);
                }
            }
        }

        @Override
        protected void paintComponent(Graphics g) {

            // TODO: Give "priorities" and always pick color of a notice with
            // highest priority (e.g. parsing errors will usually be red).

            Color color = getColor();
            if (color==null) {
                color = Color.GRAY;
            }

            Color brighterColor = getBrighterColor(color);
            Color darkerColor = getDarkerColor(color);

            int w = getWidth();
            int h = getHeight();

            // Draw background
            g.setColor(color);
            g.fillRect(0,0, w,h);

            // Draw border
            w--;
            h--;

            g.setColor(darkerColor);
            g.drawLine(w,0, w,h);
            g.drawRect(0,h, w,h);

            g.setColor(brighterColor);
            g.drawLine(0,0, w,0);
            g.drawRect(0,0, 0,h);
        }

        @Override
        public void removeNotify() {
            super.removeNotify();
            ToolTipManager.sharedInstance().unregisterComponent(this);
            removeMouseListener(listener);
        }

        public void updateLocation() {
            int line = notices.get(0).getLine();
            int y = lineToY(line);
            setLocation(2, y);
        }

    }


}