< prev index next >

src/java.desktop/share/classes/javax/swing/text/DefaultHighlighter.java

Print this page




 314 
 315     /**
 316      * Queues damageRange() call into event dispatch thread
 317      * to be sure that views are in consistent state.
 318      */
 319     private void safeDamageRange(final Position p0, final Position p1) {
 320         safeDamager.damageRange(p0, p1);
 321     }
 322 
 323     /**
 324      * Queues damageRange() call into event dispatch thread
 325      * to be sure that views are in consistent state.
 326      */
 327     private void safeDamageRange(int a0, int a1) throws BadLocationException {
 328         Document doc = component.getDocument();
 329         safeDamageRange(doc.createPosition(a0), doc.createPosition(a1));
 330     }
 331 
 332     /**
 333      * If true, highlights are drawn as the Views draw the text. That is
 334      * the Views will call into <code>paintLayeredHighlight</code> which
 335      * will result in a rectangle being drawn before the text is drawn
 336      * (if the offsets are in a highlighted region that is). For this to
 337      * work the painter supplied must be an instance of
 338      * LayeredHighlightPainter.
 339      * @param newValue the new value
 340      */
 341     public void setDrawsLayeredHighlights(boolean newValue) {
 342         drawsLayeredHighlights = newValue;
 343     }
 344 
 345     /**
 346      * Return the draw layered highlights.
 347      * @return the draw layered highlights
 348      */
 349     public boolean getDrawsLayeredHighlights() {
 350         return drawsLayeredHighlights;
 351     }
 352 
 353     // ---- member variables --------------------------------------------
 354 


 359     private boolean drawsLayeredHighlights;
 360     private SafeDamager safeDamager = new SafeDamager();
 361 
 362 
 363     /**
 364      * Default implementation of LayeredHighlighter.LayerPainter that can
 365      * be used for painting highlights.
 366      * <p>
 367      * As of 1.4 this field is final.
 368      */
 369     public static final LayeredHighlighter.LayerPainter DefaultPainter = new DefaultHighlightPainter(null);
 370 
 371 
 372     /**
 373      * Simple highlight painter that fills a highlighted area with
 374      * a solid color.
 375      */
 376     public static class DefaultHighlightPainter extends LayeredHighlighter.LayerPainter {
 377 
 378         /**
 379          * Constructs a new highlight painter. If <code>c</code> is null,
 380          * the JTextComponent will be queried for its selection color.
 381          *
 382          * @param c the color for the highlight
 383          */
 384         public DefaultHighlightPainter(Color c) {
 385             color = c;
 386         }
 387 
 388         /**
 389          * Returns the color of the highlight.
 390          *
 391          * @return the color
 392          */
 393         public Color getColor() {
 394             return color;
 395         }
 396 
 397         // --- HighlightPainter methods ---------------------------------------
 398 
 399         /**


 569                                     Shape viewBounds, JTextComponent editor,
 570                                     View view) {
 571             int start = getStartOffset();
 572             int end = getEndOffset();
 573             // Restrict the region to what we represent
 574             p0 = Math.max(start, p0);
 575             p1 = Math.min(end, p1);
 576             // Paint the appropriate region using the painter and union
 577             // the effected region with our bounds.
 578             union(((LayeredHighlighter.LayerPainter)painter).paintLayer
 579                   (g, p0, p1, viewBounds, editor, view));
 580         }
 581 
 582         int x;
 583         int y;
 584         int width;
 585         int height;
 586     }
 587 
 588     /**
 589      * This class invokes <code>mapper.damageRange</code> in
 590      * EventDispatchThread. The only one instance per Highlighter
 591      * is cretaed. When a number of ranges should be damaged
 592      * it collects them into queue and damages
 593      * them in consecutive order in <code>run</code>
 594      * call.
 595      */
 596     class SafeDamager implements Runnable {
 597         private Vector<Position> p0 = new Vector<Position>(10);
 598         private Vector<Position> p1 = new Vector<Position>(10);
 599         private Document lastDoc = null;
 600 
 601         /**
 602          * Executes range(s) damage and cleans range queue.
 603          */
 604         public synchronized void run() {
 605             if (component != null) {
 606                 TextUI mapper = component.getUI();
 607                 if (mapper != null && lastDoc == component.getDocument()) {
 608                     // the Document should be the same to properly
 609                     // display highlights
 610                     int len = p0.size();
 611                     for (int i = 0; i < len; i++){
 612                         mapper.damageRange(component,
 613                                 p0.get(i).getOffset(),




 314 
 315     /**
 316      * Queues damageRange() call into event dispatch thread
 317      * to be sure that views are in consistent state.
 318      */
 319     private void safeDamageRange(final Position p0, final Position p1) {
 320         safeDamager.damageRange(p0, p1);
 321     }
 322 
 323     /**
 324      * Queues damageRange() call into event dispatch thread
 325      * to be sure that views are in consistent state.
 326      */
 327     private void safeDamageRange(int a0, int a1) throws BadLocationException {
 328         Document doc = component.getDocument();
 329         safeDamageRange(doc.createPosition(a0), doc.createPosition(a1));
 330     }
 331 
 332     /**
 333      * If true, highlights are drawn as the Views draw the text. That is
 334      * the Views will call into {@code paintLayeredHighlight} which
 335      * will result in a rectangle being drawn before the text is drawn
 336      * (if the offsets are in a highlighted region that is). For this to
 337      * work the painter supplied must be an instance of
 338      * LayeredHighlightPainter.
 339      * @param newValue the new value
 340      */
 341     public void setDrawsLayeredHighlights(boolean newValue) {
 342         drawsLayeredHighlights = newValue;
 343     }
 344 
 345     /**
 346      * Return the draw layered highlights.
 347      * @return the draw layered highlights
 348      */
 349     public boolean getDrawsLayeredHighlights() {
 350         return drawsLayeredHighlights;
 351     }
 352 
 353     // ---- member variables --------------------------------------------
 354 


 359     private boolean drawsLayeredHighlights;
 360     private SafeDamager safeDamager = new SafeDamager();
 361 
 362 
 363     /**
 364      * Default implementation of LayeredHighlighter.LayerPainter that can
 365      * be used for painting highlights.
 366      * <p>
 367      * As of 1.4 this field is final.
 368      */
 369     public static final LayeredHighlighter.LayerPainter DefaultPainter = new DefaultHighlightPainter(null);
 370 
 371 
 372     /**
 373      * Simple highlight painter that fills a highlighted area with
 374      * a solid color.
 375      */
 376     public static class DefaultHighlightPainter extends LayeredHighlighter.LayerPainter {
 377 
 378         /**
 379          * Constructs a new highlight painter. If {@code c} is null,
 380          * the JTextComponent will be queried for its selection color.
 381          *
 382          * @param c the color for the highlight
 383          */
 384         public DefaultHighlightPainter(Color c) {
 385             color = c;
 386         }
 387 
 388         /**
 389          * Returns the color of the highlight.
 390          *
 391          * @return the color
 392          */
 393         public Color getColor() {
 394             return color;
 395         }
 396 
 397         // --- HighlightPainter methods ---------------------------------------
 398 
 399         /**


 569                                     Shape viewBounds, JTextComponent editor,
 570                                     View view) {
 571             int start = getStartOffset();
 572             int end = getEndOffset();
 573             // Restrict the region to what we represent
 574             p0 = Math.max(start, p0);
 575             p1 = Math.min(end, p1);
 576             // Paint the appropriate region using the painter and union
 577             // the effected region with our bounds.
 578             union(((LayeredHighlighter.LayerPainter)painter).paintLayer
 579                   (g, p0, p1, viewBounds, editor, view));
 580         }
 581 
 582         int x;
 583         int y;
 584         int width;
 585         int height;
 586     }
 587 
 588     /**
 589      * This class invokes {@code mapper.damageRange} in
 590      * EventDispatchThread. The only one instance per Highlighter
 591      * is cretaed. When a number of ranges should be damaged
 592      * it collects them into queue and damages
 593      * them in consecutive order in {@code run}
 594      * call.
 595      */
 596     class SafeDamager implements Runnable {
 597         private Vector<Position> p0 = new Vector<Position>(10);
 598         private Vector<Position> p1 = new Vector<Position>(10);
 599         private Document lastDoc = null;
 600 
 601         /**
 602          * Executes range(s) damage and cleans range queue.
 603          */
 604         public synchronized void run() {
 605             if (component != null) {
 606                 TextUI mapper = component.getUI();
 607                 if (mapper != null && lastDoc == component.getDocument()) {
 608                     // the Document should be the same to properly
 609                     // display highlights
 610                     int len = p0.size();
 611                     for (int i = 0; i < len; i++){
 612                         mapper.damageRange(component,
 613                                 p0.get(i).getOffset(),


< prev index next >