KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > swing > JavaTextPane


1 package com.opensymphony.workflow.designer.swing;
2
3 import java.awt.*;
4 import java.io.IOException JavaDoc;
5 import java.io.Reader JavaDoc;
6 import java.util.*;
7 import javax.swing.*;
8 import javax.swing.text.*;
9
10 import com.Ostermiller.Syntax.Lexer.JavaLexer;
11 import com.Ostermiller.Syntax.Lexer.Lexer;
12 import com.Ostermiller.Syntax.Lexer.Token;
13
14 /**
15  * User: Hani Suleiman
16  * Date: Jan 6, 2004
17  * Time: 4:19:18 PM
18  */

19 public class JavaTextPane extends JTextPane
20 {
21     /**
22       * A hash table containing the text styles.
23       * Simple attribute sets are hashed by name (String)
24       */

25      private static StyleContext styleContext = new StyleContext();
26      static
27      {
28          initStyles();
29      }
30     /**
31      * the styled document that is the model for
32      * the textPane
33      */

34     protected HighLightedDocument document;
35     /**
36      * A reader wrapped around the document
37      * so that the document can be fed into
38      * the lexer.
39      */

40     protected DocumentReader documentReader;
41     /**
42      * The lexer that tells us what colors different
43      * words should be.
44      */

45     protected Lexer syntaxLexer;
46     /** A thread that handles the actual coloring. */
47     protected Colorer colorer;
48     /**
49      * A lock for modifying the document, or for
50      * actions that depend on the document not being
51      * modified.
52      */

53     private Object JavaDoc doclock = new Object JavaDoc();
54
55     public JavaTextPane()
56     {
57         super();
58         document = new HighLightedDocument(styleContext);
59         setDocument(document);
60         setCaretPosition(0);
61         setMargin(new Insets(5, 5, 5, 5));
62         colorer = new Colorer();
63         colorer.start();
64         // create the new document.
65
documentReader = new DocumentReader(this.document);
66
67         syntaxLexer = new JavaLexer(documentReader);
68     }
69
70     /**
71      * Run the Syntax Highlighting as a separate thread.
72      * Things that need to be colored are messaged to the
73      * thread and put in a list.
74      */

75     private class Colorer extends Thread JavaDoc
76     {
77
78         /**
79          * Keep a list of places in the file that it is safe to restart the
80          * highlighting. This happens whenever the lexer reports that it has
81          * returned to its initial state. Since this list needs to be sorted
82          * and we need to be able to retrieve ranges from it, it is stored in a
83          * balanced tree.
84          */

85         private TreeSet iniPositions = new TreeSet(new DocPositionComparator());
86
87         /**
88          * As we go through and remove invalid positions we will also be finding
89          * new valid positions.
90          * Since the position list cannot be deleted from and written to at the same
91          * time, we will keep a list of the new positions and simply add it to the
92          * list of positions once all the old positions have been removed.
93          */

94         private HashSet newPositions = new HashSet();
95
96         /**
97          * A simple wrapper representing something that needs to be colored.
98          * Placed into an object so that it can be stored in a Vector.
99          */

100         private class RecolorEvent
101         {
102             public int position;
103             public int adjustment;
104
105             public RecolorEvent(int position, int adjustment)
106             {
107                 this.position = position;
108                 this.adjustment = adjustment;
109             }
110         }
111
112         /** Vector that stores the communication between the two threads. */
113         private volatile Vector v = new Vector();
114
115         /**
116          * The amount of change that has occurred before the place in the
117          * document that we are currently highlighting (lastPosition).
118          */

119         private volatile int change = 0;
120
121         /** The last position colored */
122         private volatile int lastPosition = -1;
123
124         private volatile boolean asleep = false;
125
126         /**
127          * When accessing the vector, we need to create a critical section.
128          * we will synchronize on this object to ensure that we don't get
129          * unsafe thread behavior.
130          */

131         private Object JavaDoc lock = new Object JavaDoc();
132
133         /**
134          * Tell the Syntax Highlighting thread to take another look at this
135          * section of the document. It will process this as a FIFO.
136          * This method should be done inside a doclock.
137          */

138         public void color(int position, int adjustment)
139         {
140             // figure out if this adjustment effects the current run.
141
// if it does, then adjust the place in the document
142
// that gets highlighted.
143
if(position < lastPosition)
144             {
145                 if(lastPosition < position - adjustment)
146                 {
147                     change -= lastPosition - position;
148                 }
149                 else
150                 {
151                     change += adjustment;
152                 }
153             }
154             synchronized(lock)
155             {
156                 v.add(new RecolorEvent(position, adjustment));
157                 if(asleep)
158                 {
159                     this.interrupt();
160                 }
161             }
162         }
163
164         /**
165          * The colorer runs forever and may sleep for long
166          * periods of time. It should be interrupted every
167          * time there is something for it to do.
168          */

169         public void run()
170         {
171             int position = -1;
172             int adjustment = 0;
173             // if we just finish, we can't go to sleep until we
174
// ensure there is nothing else for us to do.
175
// use try again to keep track of this.
176
boolean tryAgain = false;
177             for(; ;)
178             { // forever
179
synchronized(lock)
180                 {
181                     if(v.size() > 0)
182                     {
183                         RecolorEvent re = (RecolorEvent)(v.elementAt(0));
184                         v.removeElementAt(0);
185                         position = re.position;
186                         adjustment = re.adjustment;
187                     }
188                     else
189                     {
190                         tryAgain = false;
191                         position = -1;
192                         adjustment = 0;
193                     }
194                 }
195                 if(position != -1)
196                 {
197                     SortedSet workingSet;
198                     Iterator workingIt;
199                     DocPosition startRequest = new DocPosition(position);
200                     DocPosition endRequest = new DocPosition(position + ((adjustment >= 0) ? adjustment : -adjustment));
201                     DocPosition dp;
202                     DocPosition dpStart = null;
203                     DocPosition dpEnd = null;
204
205                     // find the starting position. We must start at least one
206
// token before the current position
207
try
208                     {
209                         // all the good positions before
210
workingSet = iniPositions.headSet(startRequest);
211                         // the last of the stuff before
212
dpStart = ((DocPosition)workingSet.last());
213                     }
214                     catch(NoSuchElementException x)
215                     {
216                         // if there were no good positions before the requested start,
217
// we can always start at the very beginning.
218
dpStart = new DocPosition(0);
219                     }
220
221                     // if stuff was removed, take any removed positions off the list.
222
if(adjustment < 0)
223                     {
224                         workingSet = iniPositions.subSet(startRequest, endRequest);
225                         workingIt = workingSet.iterator();
226                         while(workingIt.hasNext())
227                         {
228                             workingIt.next();
229                             workingIt.remove();
230                         }
231                     }
232
233                     // adjust the positions of everything after the insertion/removal.
234
workingSet = iniPositions.tailSet(startRequest);
235                     workingIt = workingSet.iterator();
236                     while(workingIt.hasNext())
237                     {
238                         ((DocPosition)workingIt.next()).adjustPosition(adjustment);
239                     }
240
241                     // now go through and highlight as much as needed
242
workingSet = iniPositions.tailSet(dpStart);
243                     workingIt = workingSet.iterator();
244                     dp = null;
245                     if(workingIt.hasNext())
246                     {
247                         dp = (DocPosition)workingIt.next();
248                     }
249                     try
250                     {
251                         Token t;
252                         boolean done = false;
253                         dpEnd = dpStart;
254                         synchronized(doclock)
255                         {
256                             // we are playing some games with the lexer for efficiency.
257
// we could just create a new lexer each time here, but instead,
258
// we will just reset it so that it thinks it is starting at the
259
// beginning of the document but reporting a funny start position.
260
// Reseting the lexer causes the close() method on the reader
261
// to be called but because the close() method has no effect on the
262
// DocumentReader, we can do this.
263
syntaxLexer.reset(documentReader, 0, dpStart.getPosition(), 0);
264                             // After the lexer has been set up, scroll the reader so that it
265
// is in the correct spot as well.
266
documentReader.seek(dpStart.getPosition());
267                             // we will highlight tokens until we reach a good stopping place.
268
// the first obvious stopping place is the end of the document.
269
// the lexer will return null at the end of the document and wee
270
// need to stop there.
271
t = syntaxLexer.getNextToken();
272                         }
273                         newPositions.add(dpStart);
274                         while(!done && t != null)
275                         {
276                             // this is the actual command that colors the stuff.
277
// Color stuff with the description of the style matched
278
// to the hash table that has been set up ahead of time.
279
synchronized(doclock)
280                             {
281                                 if(t.getCharEnd() <= document.getLength())
282                                 {
283                                     Style style = getStyle(t.getDescription());
284                                     document.setCharacterAttributes(t.getCharBegin() + change, t.getCharEnd() - t.getCharBegin(), style, true);
285                                     // record the position of the last bit of text that we colored
286
dpEnd = new DocPosition(t.getCharEnd());
287                                 }
288                                 lastPosition = (t.getCharEnd() + change);
289                             }
290                             // The other more complicated reason for doing no more highlighting
291
// is that all the colors are the same from here on out anyway.
292
// We can detect this by seeing if the place that the lexer returned
293
// to the initial state last time we highlighted is the same as the
294
// place that returned to the initial state this time.
295
// As long as that place is after the last changed text, everything
296
// from there on is fine already.
297
if(t.getState() == Token.INITIAL_STATE)
298                             {
299                                 //System.out.println(t);
300
// look at all the positions from last time that are less than or
301
// equal to the current position
302
while(dp != null && dp.getPosition() <= t.getCharEnd())
303                                 {
304                                     if(dp.getPosition() == t.getCharEnd() && dp.getPosition() >= endRequest.getPosition())
305                                     {
306                                         // we have found a state that is the same
307
done = true;
308                                         dp = null;
309                                     }
310                                     else if(workingIt.hasNext())
311                                     {
312                                         // didn't find it, try again.
313
dp = (DocPosition)workingIt.next();
314                                     }
315                                     else
316                                     {
317                                         // didn't find it, and there is no more info from last
318
// time. This means that we will just continue
319
// until the end of the document.
320
dp = null;
321                                     }
322                                 }
323                                 // so that we can do this check next time, record all the
324
// initial states from this time.
325
newPositions.add(dpEnd);
326                             }
327                             synchronized(doclock)
328                             {
329                                 t = syntaxLexer.getNextToken();
330                             }
331                         }
332
333                         // remove all the old initial positions from the place where
334
// we started doing the highlighting right up through the last
335
// bit of text we touched.
336
workingIt = iniPositions.subSet(dpStart, dpEnd).iterator();
337                         while(workingIt.hasNext())
338                         {
339                             workingIt.next();
340                             workingIt.remove();
341                         }
342
343                         // Remove all the positions that are after the end of the file.:
344
workingIt = iniPositions.tailSet(new DocPosition(document.getLength())).iterator();
345                         while(workingIt.hasNext())
346                         {
347                             workingIt.next();
348                             workingIt.remove();
349                         }
350
351                         // and put the new initial positions that we have found on the list.
352
iniPositions.addAll(newPositions);
353                         newPositions.clear();
354
355                         /*workingIt = iniPositions.iterator();
356                         while (workingIt.hasNext()){
357                             System.out.println(workingIt.next());
358                         }
359
360                         System.out.println("Started: " + dpStart.getPosition() + " Ended: " + dpEnd.getPosition());*/

361                     }
362                     catch(IOException JavaDoc x)
363                     {
364                     }
365                     synchronized(doclock)
366                     {
367                         lastPosition = -1;
368                         change = 0;
369                     }
370                     // since we did something, we should check that there is
371
// nothing else to do before going back to sleep.
372
tryAgain = true;
373                 }
374                 asleep = true;
375                 if(!tryAgain)
376                 {
377                     try
378                     {
379                         sleep(0xffffff);
380                     }
381                     catch(InterruptedException JavaDoc x)
382                     {
383                     }
384
385                 }
386                 asleep = false;
387             }
388         }
389     }
390
391     /**
392      * Color or recolor the entire document
393      */

394     public void colorAll()
395     {
396         color(0, document.getLength());
397     }
398
399     /**
400      * Color a section of the document.
401      * The actual coloring will start somewhere before
402      * the requested position and continue as long
403      * as needed.
404      *
405      * @param position the starting point for the coloring.
406      * @param adjustment amount of text inserted or removed
407      * at the starting point.
408      */

409     public void color(int position, int adjustment)
410     {
411         colorer.color(position, adjustment);
412     }
413
414     /**
415      * Just like a DefaultStyledDocument but intercepts inserts and
416      * removes to color them.
417      */

418     private class HighLightedDocument extends DefaultStyledDocument
419     {
420         public HighLightedDocument(StyleContext styles)
421         {
422             super(styles);
423         }
424
425         public void insertString(int offs, String JavaDoc str, AttributeSet a) throws BadLocationException
426         {
427             synchronized(doclock)
428             {
429                 super.insertString(offs, str, a);
430                 color(offs, str.length());
431                 documentReader.update(offs, str.length());
432             }
433         }
434
435         public void remove(int offs, int len) throws BadLocationException
436         {
437             synchronized(doclock)
438             {
439                 super.remove(offs, len);
440                 color(offs, -len);
441                 documentReader.update(offs, -len);
442             }
443         }
444     }
445
446     /**
447      * A wrapper for a position in a document appropriate for storing
448      * in a collection.
449      */

450     class DocPosition
451     {
452
453         /** The actual position */
454         private int position;
455
456         /**
457          * Get the position represented by this DocPosition
458          *
459          * @return the position
460          */

461         int getPosition()
462         {
463             return position;
464         }
465
466         /**
467          * Construct a DocPosition from the given offset into the document.
468          *
469          * @param position The position this DocObject will represent
470          */

471         public DocPosition(int position)
472         {
473             this.position = position;
474         }
475
476         /**
477          * Adjust this position.
478          * This is useful in cases that an amount of text is inserted
479          * or removed before this position.
480          *
481          * @param adjustment amount (either positive or negative) to adjust this position.
482          * @return the DocPosition, adjusted properly.
483          */

484         public DocPosition adjustPosition(int adjustment)
485         {
486             position += adjustment;
487             return this;
488         }
489
490         /**
491          * Two DocPositions are equal iff they have the same internal position.
492          *
493          * @return if this DocPosition represents the same position as another.
494          */

495         public boolean equals(Object JavaDoc obj)
496         {
497             if(obj instanceof DocPosition)
498             {
499                 DocPosition d = (DocPosition)(obj);
500                 if(this.position == d.position)
501                 {
502                     return true;
503                 }
504                 else
505                 {
506                     return false;
507                 }
508             }
509             else
510             {
511                 return false;
512             }
513         }
514
515         /**
516          * A string representation useful for debugging.
517          *
518          * @return A string representing the position.
519          */

520         public String JavaDoc toString()
521         {
522             return "" + position;
523         }
524     }
525
526     /**
527      * A comparator appropriate for use with Collections of
528      * DocPositions.
529      */

530     class DocPositionComparator implements Comparator
531     {
532         /**
533          * Does this Comparator equal another?
534          * Since all DocPositionComparators are the same, they
535          * are all equal.
536          *
537          * @return true for DocPositionComparators, false otherwise.
538          */

539         public boolean equals(Object JavaDoc obj)
540         {
541             if(obj instanceof DocPositionComparator)
542             {
543                 return true;
544             }
545             else
546             {
547                 return false;
548             }
549         }
550
551         /**
552          * Compare two DocPositions
553          *
554          * @param o1 first DocPosition
555          * @param o2 second DocPosition
556          * @return negative if first < second, 0 if equal, positive if first > second
557          */

558         public int compare(Object JavaDoc o1, Object JavaDoc o2)
559         {
560             if(o1 instanceof DocPosition && o2 instanceof DocPosition)
561             {
562                 DocPosition d1 = (DocPosition)(o1);
563                 DocPosition d2 = (DocPosition)(o2);
564                 return (d1.getPosition() - d2.getPosition());
565             }
566             else if(o1 instanceof DocPosition)
567             {
568                 return -1;
569             }
570             else if(o2 instanceof DocPosition)
571             {
572                 return 1;
573             }
574             else if(o1.hashCode() < o2.hashCode())
575             {
576                 return -1;
577             }
578             else if(o2.hashCode() > o1.hashCode())
579             {
580                 return 1;
581             }
582             else
583             {
584                 return 0;
585             }
586         }
587     }
588
589     /**
590      * A reader interface for an abstract document. Since
591      * the syntax highlighting packages only accept Stings and
592      * Readers, this must be used.
593      * Since the close() method does nothing and a seek() method
594      * has been added, this allows us to get some performance
595      * improvements through reuse. It can be used even after the
596      * lexer explicitly closes it by seeking to the place that
597      * we want to read next, and reseting the lexer.
598      */

599     class DocumentReader extends Reader JavaDoc
600     {
601
602         /**
603          * Modifying the document while the reader is working is like
604          * pulling the rug out from under the reader. Alerting the
605          * reader with this method (in a nice thread safe way, this
606          * should not be called at the same time as a read) allows
607          * the reader to compensate.
608          */

609         public void update(int position, int adjustment)
610         {
611             if(position < this.position)
612             {
613                 if(this.position < position - adjustment)
614                 {
615                     this.position = position;
616                 }
617                 else
618                 {
619                     this.position += adjustment;
620                 }
621             }
622         }
623
624         /**
625          * Current position in the document. Incremented
626          * whenever a character is read.
627          */

628         private long position = 0;
629
630         /** Saved position used in the mark and reset methods. */
631         private long mark = -1;
632
633         /** The document that we are working with. */
634         private AbstractDocument document;
635
636         /**
637          * Construct a reader on the given document.
638          *
639          * @param document the document to be read.
640          */

641         public DocumentReader(AbstractDocument document)
642         {
643             this.document = document;
644         }
645
646         /**
647          * Has no effect. This reader can be used even after
648          * it has been closed.
649          */

650         public void close()
651         {
652         }
653
654         /**
655          * Save a position for reset.
656          *
657          * @param readAheadLimit ignored.
658          */

659         public void mark(int readAheadLimit)
660         {
661             mark = position;
662         }
663
664         /**
665          * This reader support mark and reset.
666          *
667          * @return true
668          */

669         public boolean markSupported()
670         {
671             return true;
672         }
673
674         /**
675          * Read a single character.
676          *
677          * @return the character or -1 if the end of the document has been reached.
678          */

679         public int read()
680         {
681             if(position < document.getLength())
682             {
683                 try
684                 {
685                     char c = document.getText((int)position, 1).charAt(0);
686                     position++;
687                     return c;
688                 }
689                 catch(BadLocationException x)
690                 {
691                     return -1;
692                 }
693             }
694             else
695             {
696                 return -1;
697             }
698         }
699
700         /**
701          * Read and fill the buffer.
702          * This method will always fill the buffer unless the end of the document is reached.
703          *
704          * @param cbuf the buffer to fill.
705          * @return the number of characters read or -1 if no more characters are available in the document.
706          */

707         public int read(char[] cbuf)
708         {
709             return read(cbuf, 0, cbuf.length);
710         }
711
712         /**
713          * Read and fill the buffer.
714          * This method will always fill the buffer unless the end of the document is reached.
715          *
716          * @param cbuf the buffer to fill.
717          * @param off offset into the buffer to begin the fill.
718          * @param len maximum number of characters to put in the buffer.
719          * @return the number of characters read or -1 if no more characters are available in the document.
720          */

721         public int read(char[] cbuf, int off, int len)
722         {
723             if(position < document.getLength())
724             {
725                 int length = len;
726                 if(position + length >= document.getLength())
727                 {
728                     length = document.getLength() - (int)position;
729                 }
730                 if(off + length >= cbuf.length)
731                 {
732                     length = cbuf.length - off;
733                 }
734                 try
735                 {
736                     String JavaDoc s = document.getText((int)position, length);
737                     position += length;
738                     for(int i = 0; i < length; i++)
739                     {
740                         cbuf[off + i] = s.charAt(i);
741                     }
742                     return length;
743                 }
744                 catch(BadLocationException x)
745                 {
746                     return -1;
747                 }
748             }
749             else
750             {
751                 return -1;
752             }
753         }
754
755         /**
756          * @return true
757          */

758         public boolean ready()
759         {
760             return true;
761         }
762
763         /**
764          * Reset this reader to the last mark, or the beginning of the document if a mark has not been set.
765          */

766         public void reset()
767         {
768             if(mark == -1)
769             {
770                 position = 0;
771             }
772             else
773             {
774                 position = mark;
775             }
776             mark = -1;
777         }
778
779         /**
780          * Skip characters of input.
781          * This method will always skip the maximum number of characters unless
782          * the end of the file is reached.
783          *
784          * @param n number of characters to skip.
785          * @return the actual number of characters skipped.
786          */

787         public long skip(long n)
788         {
789             if(position + n <= document.getLength())
790             {
791                 position += n;
792                 return n;
793             }
794             else
795             {
796                 long oldPos = position;
797                 position = document.getLength();
798                 return (document.getLength() - oldPos);
799             }
800         }
801
802         /**
803          * Seek to the given position in the document.
804          *
805          * @param n the offset to which to seek.
806          */

807         public void seek(long n)
808         {
809             if(n <= document.getLength())
810             {
811                 position = n;
812             }
813             else
814             {
815                 position = document.getLength();
816             }
817         }
818     }
819
820     private static void initStyles()
821     {
822         Style style;
823         style = styleContext.addStyle("body", null);
824         StyleConstants.setFontFamily(style, "Monospaced");
825         StyleConstants.setFontSize(style, 12);
826         StyleConstants.setBackground(style, Color.white);
827         StyleConstants.setForeground(style, Color.black);
828         StyleConstants.setBold(style, false);
829         StyleConstants.setItalic(style, false);
830
831         style = styleContext.addStyle("tag", null);
832         StyleConstants.setFontFamily(style, "Monospaced");
833         StyleConstants.setFontSize(style, 12);
834         StyleConstants.setBackground(style, Color.white);
835         StyleConstants.setForeground(style, Color.blue);
836         StyleConstants.setBold(style, true);
837         StyleConstants.setItalic(style, false);
838
839         style = styleContext.addStyle("endtag", null);
840         StyleConstants.setFontFamily(style, "Monospaced");
841         StyleConstants.setFontSize(style, 12);
842         StyleConstants.setBackground(style, Color.white);
843         StyleConstants.setForeground(style, Color.blue);
844         StyleConstants.setBold(style, false);
845         StyleConstants.setItalic(style, false);
846
847         style = styleContext.addStyle("reference", null);
848         StyleConstants.setFontFamily(style, "Monospaced");
849         StyleConstants.setFontSize(style, 12);
850         StyleConstants.setBackground(style, Color.white);
851         StyleConstants.setForeground(style, Color.black);
852         StyleConstants.setBold(style, false);
853         StyleConstants.setItalic(style, false);
854
855         style = styleContext.addStyle("name", null);
856         StyleConstants.setFontFamily(style, "Monospaced");
857         StyleConstants.setFontSize(style, 12);
858         StyleConstants.setBackground(style, Color.white);
859         StyleConstants.setForeground(style, new Color(0xB03060)/*Color.maroon*/);
860         StyleConstants.setBold(style, true);
861         StyleConstants.setItalic(style, false);
862
863         style = styleContext.addStyle("value", null);
864         StyleConstants.setFontFamily(style, "Monospaced");
865         StyleConstants.setFontSize(style, 12);
866         StyleConstants.setBackground(style, Color.white);
867         StyleConstants.setForeground(style, new Color(0xB03060)/*Color.maroon*/);
868         StyleConstants.setBold(style, false);
869         StyleConstants.setItalic(style, true);
870
871         style = styleContext.addStyle("tag", null);
872         StyleConstants.setFontFamily(style, "Monospaced");
873         StyleConstants.setFontSize(style, 12);
874         StyleConstants.setBackground(style, Color.white);
875         StyleConstants.setForeground(style, Color.black);
876         StyleConstants.setBold(style, true);
877         StyleConstants.setItalic(style, false);
878
879         style = styleContext.addStyle("reservedWord", null);
880         StyleConstants.setFontFamily(style, "Monospaced");
881         StyleConstants.setFontSize(style, 12);
882         StyleConstants.setBackground(style, Color.white);
883         StyleConstants.setForeground(style, Color.blue);
884         StyleConstants.setBold(style, false);
885         StyleConstants.setItalic(style, false);
886
887         style = styleContext.addStyle("identifier", null);
888         StyleConstants.setFontFamily(style, "Monospaced");
889         StyleConstants.setFontSize(style, 12);
890         StyleConstants.setBackground(style, Color.white);
891         StyleConstants.setForeground(style, Color.black);
892         StyleConstants.setBold(style, false);
893         StyleConstants.setItalic(style, false);
894
895         style = styleContext.addStyle("literal", null);
896         StyleConstants.setFontFamily(style, "Monospaced");
897         StyleConstants.setFontSize(style, 12);
898         StyleConstants.setBackground(style, Color.white);
899         StyleConstants.setForeground(style, new Color(0xB03060)/*Color.maroon*/);
900         StyleConstants.setBold(style, false);
901         StyleConstants.setItalic(style, false);
902
903         style = styleContext.addStyle("separator", null);
904         StyleConstants.setFontFamily(style, "Monospaced");
905         StyleConstants.setFontSize(style, 12);
906         StyleConstants.setBackground(style, Color.white);
907         StyleConstants.setForeground(style, new Color(0x000080)/*Color.navy*/);
908         StyleConstants.setBold(style, false);
909         StyleConstants.setItalic(style, false);
910
911         style = styleContext.addStyle("operator", null);
912         StyleConstants.setFontFamily(style, "Monospaced");
913         StyleConstants.setFontSize(style, 12);
914         StyleConstants.setBackground(style, Color.white);
915         StyleConstants.setForeground(style, Color.black);
916         StyleConstants.setBold(style, true);
917         StyleConstants.setItalic(style, false);
918
919         style = styleContext.addStyle("comment", null);
920         StyleConstants.setFontFamily(style, "Monospaced");
921         StyleConstants.setFontSize(style, 12);
922         StyleConstants.setBackground(style, Color.white);
923         StyleConstants.setForeground(style, Color.green.darker());
924         StyleConstants.setBold(style, false);
925         StyleConstants.setItalic(style, false);
926
927         style = styleContext.addStyle("preprocessor", null);
928         StyleConstants.setFontFamily(style, "Monospaced");
929         StyleConstants.setFontSize(style, 12);
930         StyleConstants.setBackground(style, Color.white);
931         StyleConstants.setForeground(style, new Color(0xA020F0).darker()/*Color.purple*/);
932         StyleConstants.setBold(style, false);
933         StyleConstants.setItalic(style, false);
934
935         style = styleContext.addStyle("whitespace", null);
936         StyleConstants.setFontFamily(style, "Monospaced");
937         StyleConstants.setFontSize(style, 12);
938         StyleConstants.setBackground(style, Color.white);
939         StyleConstants.setForeground(style, Color.black);
940         StyleConstants.setBold(style, false);
941         StyleConstants.setItalic(style, false);
942
943         style = styleContext.addStyle("error", null);
944         StyleConstants.setFontFamily(style, "Monospaced");
945         StyleConstants.setFontSize(style, 12);
946         StyleConstants.setBackground(style, Color.white);
947         StyleConstants.setForeground(style, Color.red);
948         StyleConstants.setBold(style, false);
949         StyleConstants.setItalic(style, false);
950
951         style = styleContext.addStyle("unknown", null);
952         StyleConstants.setFontFamily(style, "Monospaced");
953         StyleConstants.setFontSize(style, 12);
954         StyleConstants.setBackground(style, Color.white);
955         StyleConstants.setForeground(style, Color.orange);
956         StyleConstants.setBold(style, false);
957         StyleConstants.setItalic(style, false);
958     }
959 }
960
Popular Tags