KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > syntax > jedit > SyntaxUtilities


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * SyntaxUtilities.java
28  *
29  */

30
31 package org.syntax.jedit;
32
33 import org.syntax.jedit.tokenmarker.*;
34 import javax.swing.text.*;
35 import java.awt.*;
36
37 /**
38  * Class with several utility functions used by jEdit's syntax colorizing
39  * subsystem.
40  *
41  * @author Slava Pestov
42  * @version $Id: SyntaxUtilities.java 932 2006-10-20 09:32:45Z gtoffoli $
43  */

44 public class SyntaxUtilities
45 {
46     /**
47      * Checks if a subregion of a <code>Segment</code> is equal to a
48      * string.
49      * @param ignoreCase True if case should be ignored, false otherwise
50      * @param text The segment
51      * @param offset The offset into the segment
52      * @param match The string to match
53      */

54     public static boolean regionMatches(boolean ignoreCase, Segment text,
55                         int offset, String JavaDoc match)
56     {
57         int length = offset + match.length();
58         char[] textArray = text.array;
59         if(length > text.offset + text.count)
60             return false;
61         for(int i = offset, j = 0; i < length; i++, j++)
62         {
63             char c1 = textArray[i];
64             char c2 = match.charAt(j);
65             if(ignoreCase)
66             {
67                 c1 = Character.toUpperCase(c1);
68                 c2 = Character.toUpperCase(c2);
69             }
70             if(c1 != c2)
71                 return false;
72         }
73         return true;
74     }
75     
76     /**
77      * Checks if a subregion of a <code>Segment</code> is equal to a
78      * character array.
79      * @param ignoreCase True if case should be ignored, false otherwise
80      * @param text The segment
81      * @param offset The offset into the segment
82      * @param match The character array to match
83      */

84     public static boolean regionMatches(boolean ignoreCase, Segment text,
85                         int offset, char[] match)
86     {
87         int length = offset + match.length;
88         char[] textArray = text.array;
89         if(length > text.offset + text.count)
90             return false;
91         for(int i = offset, j = 0; i < length; i++, j++)
92         {
93             char c1 = textArray[i];
94             char c2 = match[j];
95             if(ignoreCase)
96             {
97                 c1 = Character.toUpperCase(c1);
98                 c2 = Character.toUpperCase(c2);
99             }
100             if(c1 != c2)
101                 return false;
102         }
103         return true;
104     }
105
106     /**
107      * Returns the default style table. This can be passed to the
108      * <code>setStyles()</code> method of <code>SyntaxDocument</code>
109      * to use the default syntax styles.
110      */

111     public static SyntaxStyle[] getDefaultSyntaxStyles()
112     {
113         SyntaxStyle[] styles = new SyntaxStyle[Token.ID_COUNT];
114
115         styles[Token.COMMENT1] = new SyntaxStyle(Color.black,true,false);
116         styles[Token.COMMENT2] = new SyntaxStyle(new Color(0x990033),true,false);
117         styles[Token.KEYWORD1] = new SyntaxStyle(Color.black,false,true);
118         styles[Token.KEYWORD2] = new SyntaxStyle(Color.magenta,false,false);
119         styles[Token.KEYWORD3] = new SyntaxStyle(new Color(0x009600),false,false);
120         styles[Token.LITERAL1] = new SyntaxStyle(new Color(0x650099),false,false);
121         styles[Token.LITERAL2] = new SyntaxStyle(new Color(0x650099),false,true);
122         styles[Token.LABEL] = new SyntaxStyle(new Color(0x990033),false,true);
123         styles[Token.OPERATOR] = new SyntaxStyle(Color.black,false,true);
124         styles[Token.INVALID] = new SyntaxStyle(Color.red,false,true);
125
126         styles[Token.PARAMETER] = new SyntaxStyle(Color.blue,false,true);
127         styles[Token.PARAMETER_OK] = new SyntaxStyle(new Color(0x009b1f),false,true);
128
129         return styles;
130     }
131
132     /**
133      * Paints the specified line onto the graphics context. Note that this
134      * method munges the offset and count values of the segment.
135      * @param line The line segment
136      * @param tokens The token list for the line
137      * @param styles The syntax style list
138      * @param expander The tab expander used to determine tab stops. May
139      * be null
140      * @param gfx The graphics context
141      * @param x The x co-ordinate
142      * @param y The y co-ordinate
143      * @return The x co-ordinate, plus the width of the painted string
144      */

145     public static int paintSyntaxLine(Segment line, Token tokens,
146         SyntaxStyle[] styles, TabExpander expander, Graphics gfx,
147         int x, int y)
148     {
149         Font defaultFont = gfx.getFont();
150         Color defaultColor = gfx.getColor();
151
152         int offset = 0;
153         for(;;)
154         {
155             byte id = tokens.id;
156             if(id == Token.END)
157                 break;
158
159             int length = tokens.length;
160             if(id == Token.NULL)
161             {
162                 if(!defaultColor.equals(gfx.getColor()))
163                     gfx.setColor(defaultColor);
164                 if(!defaultFont.equals(gfx.getFont()))
165                     gfx.setFont(defaultFont);
166             }
167             else
168                         {
169                 styles[id].setGraphicsFlags(gfx,defaultFont);
170                         }
171
172             line.count = length;
173             x = Utilities.drawTabbedText(line,x,y,gfx,expander,0);
174             line.offset += length;
175             offset += length;
176
177             tokens = tokens.next;
178         }
179
180         return x;
181     }
182
183     // private members
184
private SyntaxUtilities() {}
185 }
186
Popular Tags