KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > sqlnavigator > Syntaxer


1 package org.lucane.applications.sqlnavigator;
2
3 import java.awt.*;
4 import javax.swing.*;
5 import javax.swing.text.*;
6 import java.util.Vector JavaDoc;
7
8
9
10 /**
11  * Syntax colorer
12  */

13 class Syntaxer
14 {
15     SimpleAttributeSet asKeyword1, asKeyword2, asNumber, asText, asStandard;
16     Vector JavaDoc keywords1, keywords2;
17     
18     JTextPane jtaQuery;
19     boolean force;
20
21     /**
22      * Constructor.
23      *
24      * @param jtp the JTextPane containing the query
25      */

26     public Syntaxer(JTextPane jtp)
27     {
28         jtaQuery = jtp;
29         force = false;
30         
31             
32         /* color styles */
33         asKeyword1 = new SimpleAttributeSet();
34         asKeyword1.addAttribute(StyleConstants.ColorConstants.Foreground, Color.blue);
35         asKeyword2 = new SimpleAttributeSet();
36         asKeyword2.addAttribute(StyleConstants.ColorConstants.Foreground, new Color(120, 0, 255));
37         asNumber = new SimpleAttributeSet();
38         asNumber.addAttribute(StyleConstants.ColorConstants.Foreground, Color.red);
39         asText = new SimpleAttributeSet();
40         asText.addAttribute(StyleConstants.ColorConstants.Foreground, Color.gray);
41         asStandard = new SimpleAttributeSet();
42         asStandard.addAttribute(StyleConstants.ColorConstants.Foreground, Color.black);
43         jtaQuery.setCharacterAttributes(asStandard, true);
44         
45         initKeyWords();
46
47     }
48     
49     /**
50      * Set a flag forcing the complete coloring of all the query
51      */

52     public void forcing()
53     {
54         force = true;
55     }
56     
57     /**
58      * Initialize keyword list
59      * keywords1 : language
60      * keywords2 : stantard functions
61      */

62     private void initKeyWords()
63     {
64         this.keywords1 = new Vector JavaDoc();
65         this.keywords1.addElement("select");
66         this.keywords1.addElement("from");
67         this.keywords1.addElement("where");
68         this.keywords1.addElement("order");
69         this.keywords1.addElement("and");
70         this.keywords1.addElement("in");
71         this.keywords1.addElement("not");
72         this.keywords1.addElement("or");
73         this.keywords1.addElement("like");
74         this.keywords1.addElement("all");
75         this.keywords1.addElement("distinct");
76         this.keywords1.addElement("group");
77         this.keywords1.addElement("by");
78         this.keywords1.addElement("having");
79         this.keywords1.addElement("union");
80         this.keywords1.addElement("intersect");
81         this.keywords1.addElement("minus");
82         this.keywords1.addElement("asc");
83         this.keywords1.addElement("desc");
84         this.keywords1.addElement("for");
85         this.keywords1.addElement("update");
86         this.keywords1.addElement("of");
87         this.keywords1.addElement("is");
88         this.keywords1.addElement("null");
89         this.keywords1.addElement("between");
90         this.keywords1.addElement("exists");
91         this.keywords1.addElement("insert");
92         this.keywords1.addElement("into");
93         this.keywords1.addElement("values");
94         this.keywords1.addElement("delete");
95         this.keywords1.addElement("set");
96         this.keywords1.addElement("as");
97  
98
99         this.keywords2 = new Vector JavaDoc();
100         this.keywords2.addElement("abs");
101         this.keywords2.addElement("ceil");
102         this.keywords2.addElement("floor");
103         this.keywords2.addElement("mod");
104         this.keywords2.addElement("power");
105         this.keywords2.addElement("round");
106         this.keywords2.addElement("trunc");
107         this.keywords2.addElement("sqrt");
108         this.keywords2.addElement("least");
109         this.keywords2.addElement("greatest");
110         this.keywords2.addElement("length");
111         this.keywords2.addElement("upper");
112         this.keywords2.addElement("lower");
113         this.keywords2.addElement("substr");
114         this.keywords2.addElement("avg");
115         this.keywords2.addElement("count");
116         this.keywords2.addElement("max");
117         this.keywords2.addElement("min");
118         this.keywords2.addElement("sum");
119         this.keywords2.addElement("stddev");
120         this.keywords2.addElement("variance");
121         this.keywords2.addElement("max");
122     }
123             
124     /**
125      * Colorize the whole query
126      */

127     private void forceColorize()
128     {
129         String JavaDoc txt = jtaQuery.getText();
130         String JavaDoc markers = " \t\r\n,;=<>+-/*()[]";
131
132         int startpos;
133         int endpos;
134         int currentpos;
135         int caretpos = jtaQuery.getCaretPosition();
136         
137         for(int usepos = 0;usepos < txt.length();usepos++)
138         {
139             startpos=0;
140             endpos=txt.length();
141
142             /* search word beginning */
143             for(int i=0;i<markers.length();i++)
144             {
145                 currentpos = txt.lastIndexOf(markers.charAt(i), usepos);
146                 if(currentpos >= startpos)
147                     startpos = currentpos+1;
148             }
149
150
151             /* search word end */
152             for(int i=0;i<markers.length();i++)
153             {
154                 currentpos = txt.indexOf(markers.charAt(i), usepos);
155                 if(currentpos >= 0 && currentpos < endpos)
156                     endpos = currentpos;
157             }
158     
159             if(startpos<endpos)
160             {
161                 jtaQuery.select(startpos, endpos);
162                 
163                 /* coloration */
164                 if(jtaQuery.getSelectedText() != null)
165                 {
166                     boolean kw = false;
167                     for(int k=0;k<keywords1.size() && !kw;k++)
168                     {
169                         if(jtaQuery.getSelectedText().equalsIgnoreCase( (String JavaDoc)keywords1.elementAt(k) ))
170                         {
171                             jtaQuery.setCharacterAttributes(asKeyword1, true);
172                             kw = true;
173                         }
174                     }
175     
176                     for(int k=0;k<keywords2.size() && !kw;k++)
177                     {
178                         if(jtaQuery.getSelectedText().equalsIgnoreCase( (String JavaDoc)keywords2.elementAt(k) ))
179                         {
180                             jtaQuery.setCharacterAttributes(asKeyword2, true);
181                             kw = true;
182                         }
183                     }
184     
185                     if(!kw)
186                     {
187                         try
188                         {
189                             Double JavaDoc d = new Double JavaDoc(jtaQuery.getSelectedText());
190                             jtaQuery.setCharacterAttributes(asNumber, true);
191                         }
192                         catch(Exception JavaDoc e)
193                         {
194                             jtaQuery.setCharacterAttributes(asStandard, true);
195                         }
196                     }
197                 }
198             }
199         }
200     
201         /* search strings */
202         startpos = endpos = 0;
203         while(startpos >= 0 && endpos >= 0)
204         {
205             startpos = txt.indexOf('\'', endpos+1);
206             endpos = -1;
207             if(startpos >= 1)
208             {
209                 endpos = txt.indexOf('\'', startpos+1);
210                 if(endpos < 0)
211                     endpos = txt.length();
212                 
213                 if(startpos<endpos)
214                 {
215                     jtaQuery.select(startpos-1, endpos+1);
216                     jtaQuery.setCharacterAttributes(asText, true);
217                 }
218             }
219         }
220
221         /* reset cursor */
222         jtaQuery.select(caretpos,caretpos);
223         jtaQuery.setCharacterAttributes(asStandard, true);
224     }
225             
226     /**
227      * Colorize the current word
228      */

229     private void colorize()
230     {
231         String JavaDoc txt = jtaQuery.getText();
232         String JavaDoc markers = " \t\r\n,;=<>+-/*()[]";
233         
234         int caretpos = jtaQuery.getCaretPosition();
235         
236         int usepos = caretpos -1;
237         
238         int startpos=0;
239         int endpos=txt.length();
240         int currentpos;
241
242
243         /* go back while we are on a separator */
244         while(usepos > 0 && markers.indexOf(txt.charAt(usepos))>=0)
245             usepos--;
246
247         /* search the end of the word */
248         for(int i=0;i<markers.length();i++)
249         {
250             currentpos = txt.indexOf(markers.charAt(i), usepos);
251             if(currentpos >= 0 && currentpos < endpos)
252                 endpos = currentpos;
253         }
254         
255         
256         /* search the beginning of the word */
257         for(int i=0;i<markers.length();i++)
258         {
259             currentpos = txt.lastIndexOf(markers.charAt(i), usepos);
260             if(currentpos >= startpos)
261                 startpos = currentpos+1;
262         }
263     
264         if(startpos<endpos)
265         {
266             /* text selection */
267             jtaQuery.select(startpos, endpos);
268                            
269             /* coloration */
270             if(jtaQuery.getSelectedText() != null)
271             {
272                 boolean kw = false;
273                 for(int k=0;k<keywords1.size() && !kw;k++)
274                 {
275                     if(jtaQuery.getSelectedText().equalsIgnoreCase( (String JavaDoc)keywords1.elementAt(k) ))
276                     {
277                         jtaQuery.setCharacterAttributes(asKeyword1, true);
278                         kw = true;
279                     }
280                 }
281
282                 for(int k=0;k<keywords2.size() && !kw;k++)
283                 {
284                     if(jtaQuery.getSelectedText().equalsIgnoreCase( (String JavaDoc)keywords2.elementAt(k) ))
285                     {
286                         jtaQuery.setCharacterAttributes(asKeyword2, true);
287                         kw = true;
288                     }
289                 }
290
291                 if(!kw)
292                 {
293                     try
294                     {
295                         Double JavaDoc d = new Double JavaDoc(jtaQuery.getSelectedText());
296                         jtaQuery.setCharacterAttributes(asNumber, true);
297                     }
298                     catch(Exception JavaDoc e)
299                     {
300                         jtaQuery.setCharacterAttributes(asStandard, true);
301                     }
302                 }
303             }
304         }
305
306             /* search strings */
307             startpos = endpos = 0;
308             while(startpos >= 0 && endpos >= 0)
309             {
310                 startpos = txt.indexOf('\'', endpos+1);
311                 endpos = -1;
312                 if(startpos >= 1)
313                 {
314                     endpos = txt.indexOf('\'', startpos+1);
315                     if(endpos < 0)
316                         endpos = txt.length();
317                 
318                     if(startpos<endpos)
319                     {
320                         jtaQuery.select(startpos-1, endpos+1);
321                         jtaQuery.setCharacterAttributes(asText, true);
322                     }
323                 }
324
325             /* reset cursor */
326             jtaQuery.select(caretpos,caretpos);
327             jtaQuery.setCharacterAttributes(asStandard, true);
328         }
329     }
330     
331     /**
332      * Start colorization
333      */

334     public void start()
335     {
336         if(force)
337         {
338             force = false;
339             forceColorize();
340         }
341         else
342             colorize();
343     }
344 }
345
Popular Tags