KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bluej > editor > moe > MoeSyntaxDocument


1 /*
2 * MoeSyntaxDocument.java - inherits from
3 * DefaultSyntaxDocument.java - Simple implementation of SyntaxDocument
4 * Copyright (C) 1999 Slava Pestov
5 * modified by Bruce Quig to add Syntax highlighting to the BlueJ
6 * programming environment.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 */

22 package bluej.editor.moe;
23
24 import javax.swing.text.*;
25
26 import java.awt.Color JavaDoc;
27 import bluej.Config;
28
29 import org.syntax.jedit.*;
30 import org.syntax.jedit.tokenmarker.*;
31
32 // For configuration file reading.
33
import java.util.Properties JavaDoc;
34
35 /**
36  * A simple implementation of <code>SyntaxDocument</code> that
37  * inherits from DefaultSyntaxDocument. It takes
38  * care of inserting and deleting lines from the token marker's state.
39  * It adds the ability to handle paragraph attributes on a per line basis.
40  *
41  * @author Bruce Quig
42  * @author Jo Wood (Modified to allow user-defined colours, March 2001)
43  *
44  */

45 public class MoeSyntaxDocument extends SyntaxDocument
46 {
47     public static final String JavaDoc OUTPUT = "output";
48     public static final String JavaDoc ERROR = "error";
49
50     private static Color JavaDoc[] colors = null;
51     
52     public MoeSyntaxDocument()
53     {
54         super(getUserColors());
55          // defaults to 4 if cannot read property
56
int tabSize = Config.getPropInteger("bluej.editor.tabsize", 4);
57          putProperty(tabSizeAttribute, new Integer JavaDoc(tabSize));
58     }
59
60     /**
61      * Sets attributes for a paragraph. This method was added to
62      * provide the ability to replicate DefaultStyledDocument's ability to
63      * set each lines attributes easily.
64      * This is an added method for the BlueJ adaption of jedit's Syntax
65      * package
66      *
67      * @param offset the offset into the paragraph >= 0
68      * @param length the number of characters affected >= 0
69      * @param s the attributes
70      * @param replace whether to replace existing attributes, or merge them
71      */

72     public void setParagraphAttributes(int offset, AttributeSet s)
73     {
74         // modified version of method from DefaultStyleDocument
75
try {
76             writeLock();
77             
78             Element paragraph = getParagraphElement(offset);
79             MutableAttributeSet attr =
80                     (MutableAttributeSet) paragraph.getAttributes();
81             attr.addAttributes(s);
82         } finally {
83             writeUnlock();
84         }
85     }
86     
87     
88     
89     /**
90      * Allows user-defined colours to be set for synax highlighting. The file
91      * containing the colour values is should be 'lib/moe.defs'. If this file is
92      * not found, or not all colours are defined, the BlueJ default colours are
93      * used.
94      *
95      * @author This method was added by Jo Wood (jwo@soi.city.ac.uk), 9th March,
96      * 2001.
97      */

98     private static Color JavaDoc[] getUserColors()
99     {
100         if(colors == null) {
101                 Properties JavaDoc editorProps = Config.moe_user_props;
102                 
103                 // Build colour table.
104
colors = new Color JavaDoc[Token.ID_COUNT];
105                 
106                 // Replace with user-defined colours.
107
String JavaDoc colorStr;
108                 int colorInt;
109                 
110                 // Comments.
111
colorStr = editorProps.getProperty("comment","1a1a80");
112                 try {
113                     colorInt = Integer.parseInt(colorStr,16);
114                 }
115                 catch (NumberFormatException JavaDoc e) {
116                     colorInt = 0x1a1a80;
117                 }
118                 colors[Token.COMMENT1] = new Color JavaDoc(colorInt);
119                 
120                 // Javadoc comments.
121
colorStr = editorProps.getProperty("javadoc","1a1a80");
122                 try {
123                     colorInt = Integer.parseInt(colorStr,16);
124                 }
125                 catch (NumberFormatException JavaDoc e) {
126                     colorInt = 0x1a1a80;
127                 }
128                 colors[Token.COMMENT2] = new Color JavaDoc(colorInt);
129                 
130                 // Stand-out comments (/*#).
131
colorStr = editorProps.getProperty("stand-out","ee00bb");
132                 try {
133                     colorInt = Integer.parseInt(colorStr,16);
134                 }
135                 catch (NumberFormatException JavaDoc e) {
136                     colorInt = 0xee00bb;
137                 }
138                 colors[Token.COMMENT3] = new Color JavaDoc(colorInt);
139                 
140                 // Java keywords.
141
colorStr = editorProps.getProperty("keyword1","660033");
142                 try {
143                     colorInt = Integer.parseInt(colorStr,16);
144                 }
145                 catch (NumberFormatException JavaDoc e) {
146                     colorInt = 0x660033;
147                 }
148                 colors[Token.KEYWORD1] = new Color JavaDoc(colorInt);
149                 
150                 // Class-based keywords.
151
colorStr = editorProps.getProperty("keyword2","cc8033");
152                 try {
153                     colorInt = Integer.parseInt(colorStr,16);
154                 }
155                 catch (NumberFormatException JavaDoc e) {
156                     colorInt = 0xcc8033;
157                 }
158                 colors[Token.KEYWORD2] = new Color JavaDoc(colorInt);
159                 
160                 // Other Java keywords (true, false, this, super).
161
colorStr = editorProps.getProperty("keyword3","006699");
162                 try {
163                     colorInt = Integer.parseInt(colorStr,16);
164                 }
165                 catch (NumberFormatException JavaDoc e) {
166                     colorInt = 0x006699;
167                 }
168                 colors[Token.KEYWORD3] = new Color JavaDoc(colorInt);
169                 
170                 // Primitives.
171
colorStr = editorProps.getProperty("primitive","cc0000");
172                 try {
173                     colorInt = Integer.parseInt(colorStr,16);
174                 }
175                 catch (NumberFormatException JavaDoc e) {
176                     colorInt = 0xcc0000;
177                 }
178                 colors[Token.PRIMITIVE] = new Color JavaDoc(colorInt);
179                 
180                 // String literals.
181
colorStr = editorProps.getProperty("string","339933");
182                 try {
183                     colorInt = Integer.parseInt(colorStr,16);
184                 }
185                 catch (NumberFormatException JavaDoc e) {
186                     colorInt = 0x339933;
187                 }
188                 colors[Token.LITERAL1] = new Color JavaDoc(colorInt);
189                 
190                 // Leave remaining tokens as default.
191
colors[Token.LABEL] = new Color JavaDoc(0x990000);
192                 colors[Token.OPERATOR] = new Color JavaDoc(0xcc9900);
193                 colors[Token.INVALID] = new Color JavaDoc(0xff3300);
194         }
195         return colors;
196     }
197 }
198
Popular Tags