root / tags / v1_0_2_Build_906 / libraries / libIverUtiles / src / com / iver / utiles / console / jedit / SyntaxUtilities.java @ 10972
History | View | Annotate | Download (4.53 KB)
1 |
package com.iver.utiles.console.jedit; |
---|---|
2 |
/*
|
3 |
* SyntaxUtilities.java - Utility functions used by syntax colorizing
|
4 |
* Copyright (C) 1999 Slava Pestov
|
5 |
*
|
6 |
* You may use and modify this package for any purpose. Redistribution is
|
7 |
* permitted, in both source and binary form, provided that this notice
|
8 |
* remains intact in all source distributions of this package.
|
9 |
*/
|
10 |
|
11 |
import java.awt.Color; |
12 |
import java.awt.Font; |
13 |
import java.awt.Graphics; |
14 |
|
15 |
import javax.swing.text.Segment; |
16 |
import javax.swing.text.TabExpander; |
17 |
import javax.swing.text.Utilities; |
18 |
|
19 |
/**
|
20 |
* Class with several utility functions used by jEdit's syntax colorizing
|
21 |
* subsystem.
|
22 |
*
|
23 |
* @author Slava Pestov
|
24 |
* @version $Id$
|
25 |
*/
|
26 |
public class SyntaxUtilities |
27 |
{ |
28 |
/**
|
29 |
* Checks if a subregion of a <code>Segment</code> is equal to a
|
30 |
* string.
|
31 |
* @param ignoreCase True if case should be ignored, false otherwise
|
32 |
* @param text The segment
|
33 |
* @param offset The offset into the segment
|
34 |
* @param match The string to match
|
35 |
*/
|
36 |
public static boolean regionMatches(boolean ignoreCase, Segment text, |
37 |
int offset, String match) |
38 |
{ |
39 |
int length = offset + match.length();
|
40 |
char[] textArray = text.array; |
41 |
if(length > text.offset + text.count)
|
42 |
return false; |
43 |
for(int i = offset, j = 0; i < length; i++, j++) |
44 |
{ |
45 |
char c1 = textArray[i];
|
46 |
char c2 = match.charAt(j);
|
47 |
if(ignoreCase)
|
48 |
{ |
49 |
c1 = Character.toUpperCase(c1);
|
50 |
c2 = Character.toUpperCase(c2);
|
51 |
} |
52 |
if(c1 != c2)
|
53 |
return false; |
54 |
} |
55 |
return true; |
56 |
} |
57 |
|
58 |
/**
|
59 |
* Checks if a subregion of a <code>Segment</code> is equal to a
|
60 |
* character array.
|
61 |
* @param ignoreCase True if case should be ignored, false otherwise
|
62 |
* @param text The segment
|
63 |
* @param offset The offset into the segment
|
64 |
* @param match The character array to match
|
65 |
*/
|
66 |
public static boolean regionMatches(boolean ignoreCase, Segment text, |
67 |
int offset, char[] match) |
68 |
{ |
69 |
int length = offset + match.length;
|
70 |
char[] textArray = text.array; |
71 |
if(length > text.offset + text.count)
|
72 |
return false; |
73 |
for(int i = offset, j = 0; i < length; i++, j++) |
74 |
{ |
75 |
char c1 = textArray[i];
|
76 |
char c2 = match[j];
|
77 |
if(ignoreCase)
|
78 |
{ |
79 |
c1 = Character.toUpperCase(c1);
|
80 |
c2 = Character.toUpperCase(c2);
|
81 |
} |
82 |
if(c1 != c2)
|
83 |
return false; |
84 |
} |
85 |
return true; |
86 |
} |
87 |
|
88 |
/**
|
89 |
* Returns the default style table. This can be passed to the
|
90 |
* <code>setStyles()</code> method of <code>SyntaxDocument</code>
|
91 |
* to use the default syntax styles.
|
92 |
*/
|
93 |
public static SyntaxStyle[] getDefaultSyntaxStyles() |
94 |
{ |
95 |
SyntaxStyle[] styles = new SyntaxStyle[Token.ID_COUNT]; |
96 |
|
97 |
styles[Token.COMMENT1] = new SyntaxStyle(Color.black,true,false); |
98 |
styles[Token.COMMENT2] = new SyntaxStyle(new Color(0x990033),true,false); |
99 |
styles[Token.KEYWORD1] = new SyntaxStyle(Color.black,false,true); |
100 |
styles[Token.KEYWORD2] = new SyntaxStyle(Color.magenta,false,false); |
101 |
styles[Token.KEYWORD3] = new SyntaxStyle(new Color(0x009600),false,false); |
102 |
styles[Token.LITERAL1] = new SyntaxStyle(new Color(0x650099),false,false); |
103 |
styles[Token.LITERAL2] = new SyntaxStyle(new Color(0x650099),false,true); |
104 |
styles[Token.LABEL] = new SyntaxStyle(new Color(0x990033),false,true); |
105 |
styles[Token.OPERATOR] = new SyntaxStyle(Color.black,false,true); |
106 |
styles[Token.INVALID] = new SyntaxStyle(Color.red,false,true); |
107 |
|
108 |
return styles;
|
109 |
} |
110 |
|
111 |
/**
|
112 |
* Paints the specified line onto the graphics context. Note that this
|
113 |
* method munges the offset and count values of the segment.
|
114 |
* @param line The line segment
|
115 |
* @param tokens The token list for the line
|
116 |
* @param styles The syntax style list
|
117 |
* @param expander The tab expander used to determine tab stops. May
|
118 |
* be null
|
119 |
* @param gfx The graphics context
|
120 |
* @param x The x co-ordinate
|
121 |
* @param y The y co-ordinate
|
122 |
* @return The x co-ordinate, plus the width of the painted string
|
123 |
*/
|
124 |
public static int paintSyntaxLine(Segment line, Token tokens, |
125 |
SyntaxStyle[] styles, TabExpander expander, Graphics gfx, |
126 |
int x, int y) |
127 |
{ |
128 |
Font defaultFont = gfx.getFont();
|
129 |
Color defaultColor = gfx.getColor();
|
130 |
|
131 |
int offset = 0; |
132 |
for(;;)
|
133 |
{ |
134 |
byte id = tokens.id;
|
135 |
if(id == Token.END)
|
136 |
break;
|
137 |
|
138 |
int length = tokens.length;
|
139 |
if(id == Token.NULL)
|
140 |
{ |
141 |
if(!defaultColor.equals(gfx.getColor()))
|
142 |
gfx.setColor(defaultColor); |
143 |
if(!defaultFont.equals(gfx.getFont()))
|
144 |
gfx.setFont(defaultFont); |
145 |
} |
146 |
else
|
147 |
styles[id].setGraphicsFlags(gfx,defaultFont); |
148 |
|
149 |
line.count = length; |
150 |
x = Utilities.drawTabbedText(line,x,y,gfx,expander,0); |
151 |
line.offset += length; |
152 |
offset += length; |
153 |
|
154 |
tokens = tokens.next; |
155 |
} |
156 |
|
157 |
return x;
|
158 |
} |
159 |
|
160 |
// private members
|
161 |
private SyntaxUtilities() {}
|
162 |
} |