svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / console / jedit / SyntaxUtilities.java @ 40561
History | View | Annotate | Download (5.46 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* 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, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.utils.console.jedit; |
25 |
/*
|
26 |
* SyntaxUtilities.java - Utility functions used by syntax colorizing
|
27 |
* Copyright (C) 1999 Slava Pestov
|
28 |
*
|
29 |
* You may use and modify this package for any purpose. Redistribution is
|
30 |
* permitted, in both source and binary form, provided that this notice
|
31 |
* remains intact in all source distributions of this package.
|
32 |
*/
|
33 |
|
34 |
import java.awt.Color; |
35 |
import java.awt.Font; |
36 |
import java.awt.Graphics; |
37 |
|
38 |
import javax.swing.text.Segment; |
39 |
import javax.swing.text.TabExpander; |
40 |
import javax.swing.text.Utilities; |
41 |
|
42 |
/**
|
43 |
* Class with several utility functions used by jEdit's syntax colorizing
|
44 |
* subsystem.
|
45 |
*
|
46 |
* @author Slava Pestov
|
47 |
* @version $Id$
|
48 |
*/
|
49 |
public class SyntaxUtilities |
50 |
{ |
51 |
/**
|
52 |
* Checks if a subregion of a <code>Segment</code> is equal to a
|
53 |
* string.
|
54 |
* @param ignoreCase True if case should be ignored, false otherwise
|
55 |
* @param text The segment
|
56 |
* @param offset The offset into the segment
|
57 |
* @param match The string to match
|
58 |
*/
|
59 |
public static boolean regionMatches(boolean ignoreCase, Segment text, |
60 |
int offset, String match) |
61 |
{ |
62 |
int length = offset + match.length();
|
63 |
char[] textArray = text.array; |
64 |
if(length > text.offset + text.count)
|
65 |
return false; |
66 |
for(int i = offset, j = 0; i < length; i++, j++) |
67 |
{ |
68 |
char c1 = textArray[i];
|
69 |
char c2 = match.charAt(j);
|
70 |
if(ignoreCase)
|
71 |
{ |
72 |
c1 = Character.toUpperCase(c1);
|
73 |
c2 = Character.toUpperCase(c2);
|
74 |
} |
75 |
if(c1 != c2)
|
76 |
return false; |
77 |
} |
78 |
return true; |
79 |
} |
80 |
|
81 |
/**
|
82 |
* Checks if a subregion of a <code>Segment</code> is equal to a
|
83 |
* character array.
|
84 |
* @param ignoreCase True if case should be ignored, false otherwise
|
85 |
* @param text The segment
|
86 |
* @param offset The offset into the segment
|
87 |
* @param match The character array to match
|
88 |
*/
|
89 |
public static boolean regionMatches(boolean ignoreCase, Segment text, |
90 |
int offset, char[] match) |
91 |
{ |
92 |
int length = offset + match.length;
|
93 |
char[] textArray = text.array; |
94 |
if(length > text.offset + text.count)
|
95 |
return false; |
96 |
for(int i = offset, j = 0; i < length; i++, j++) |
97 |
{ |
98 |
char c1 = textArray[i];
|
99 |
char c2 = match[j];
|
100 |
if(ignoreCase)
|
101 |
{ |
102 |
c1 = Character.toUpperCase(c1);
|
103 |
c2 = Character.toUpperCase(c2);
|
104 |
} |
105 |
if(c1 != c2)
|
106 |
return false; |
107 |
} |
108 |
return true; |
109 |
} |
110 |
|
111 |
/**
|
112 |
* Returns the default style table. This can be passed to the
|
113 |
* <code>setStyles()</code> method of <code>SyntaxDocument</code>
|
114 |
* to use the default syntax styles.
|
115 |
*/
|
116 |
public static SyntaxStyle[] getDefaultSyntaxStyles() |
117 |
{ |
118 |
SyntaxStyle[] styles = new SyntaxStyle[Token.ID_COUNT]; |
119 |
|
120 |
styles[Token.COMMENT1] = new SyntaxStyle(Color.black,true,false); |
121 |
styles[Token.COMMENT2] = new SyntaxStyle(new Color(0x990033),true,false); |
122 |
styles[Token.KEYWORD1] = new SyntaxStyle(Color.black,false,true); |
123 |
styles[Token.KEYWORD2] = new SyntaxStyle(Color.magenta,false,false); |
124 |
styles[Token.KEYWORD3] = new SyntaxStyle(new Color(0x009600),false,false); |
125 |
styles[Token.LITERAL1] = new SyntaxStyle(new Color(0x650099),false,false); |
126 |
styles[Token.LITERAL2] = new SyntaxStyle(new Color(0x650099),false,true); |
127 |
styles[Token.LABEL] = new SyntaxStyle(new Color(0x990033),false,true); |
128 |
styles[Token.OPERATOR] = new SyntaxStyle(Color.black,false,true); |
129 |
styles[Token.INVALID] = new SyntaxStyle(Color.red,false,true); |
130 |
|
131 |
return styles;
|
132 |
} |
133 |
|
134 |
/**
|
135 |
* Paints the specified line onto the graphics context. Note that this
|
136 |
* method munges the offset and count values of the segment.
|
137 |
* @param line The line segment
|
138 |
* @param tokens The token list for the line
|
139 |
* @param styles The syntax style list
|
140 |
* @param expander The tab expander used to determine tab stops. May
|
141 |
* be null
|
142 |
* @param gfx The graphics context
|
143 |
* @param x The x co-ordinate
|
144 |
* @param y The y co-ordinate
|
145 |
* @return The x co-ordinate, plus the width of the painted string
|
146 |
*/
|
147 |
public static int paintSyntaxLine(Segment line, Token tokens, |
148 |
SyntaxStyle[] styles, TabExpander expander, Graphics gfx, |
149 |
int x, int y) |
150 |
{ |
151 |
Font defaultFont = gfx.getFont();
|
152 |
Color defaultColor = gfx.getColor();
|
153 |
|
154 |
int offset = 0; |
155 |
for(;;)
|
156 |
{ |
157 |
byte id = tokens.id;
|
158 |
if(id == Token.END)
|
159 |
break;
|
160 |
|
161 |
int length = tokens.length;
|
162 |
if(id == Token.NULL)
|
163 |
{ |
164 |
if(!defaultColor.equals(gfx.getColor()))
|
165 |
gfx.setColor(defaultColor); |
166 |
if(!defaultFont.equals(gfx.getFont()))
|
167 |
gfx.setFont(defaultFont); |
168 |
} |
169 |
else
|
170 |
styles[id].setGraphicsFlags(gfx,defaultFont); |
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 |
} |