1
2
3
4
5
6
7
8
9 package com.ochafik.swing.syntaxcoloring;
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
21
22
23
24
25
26 public class SyntaxUtilities
27 {
28
29
30
31
32
33
34
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
60
61
62
63
64
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
90
91
92
93 public static SyntaxStyle[] getDefaultSyntaxStyles()
94 {
95 SyntaxStyle[] styles = new SyntaxStyle[Token.ID_COUNT];
96
97 styles[Token.COMMENT1] = new SyntaxStyle(Color.green,true,false);
98 styles[Token.COMMENT2] = new SyntaxStyle(Color.blue,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(Color.blue,false,false);
104
105 styles[Token.LABEL] = new SyntaxStyle(new Color(0x990033),false,true);
106 styles[Token.OPERATOR] = new SyntaxStyle(Color.black,false,true);
107 styles[Token.INVALID] = new SyntaxStyle(Color.red,false,true);
108
109 return styles;
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 public static int paintSyntaxLine(Segment line, Token tokens,
126 SyntaxStyle[] styles, TabExpander expander, Graphics gfx,
127 int x, int y)
128 {
129 Font defaultFont = gfx.getFont();
130 Color defaultColor = gfx.getColor();
131
132 int offset = 0;
133 for(;;)
134 {
135 byte id = tokens.id;
136 if(id == Token.END)
137 break;
138
139 int length = tokens.length;
140 if(id == Token.NULL)
141 {
142 if(!defaultColor.equals(gfx.getColor()))
143 gfx.setColor(defaultColor);
144 if(!defaultFont.equals(gfx.getFont()))
145 gfx.setFont(defaultFont);
146 }
147 else
148 styles[id].setGraphicsFlags(gfx,defaultFont);
149
150 line.count = length;
151 x = Utilities.drawTabbedText(line,x,y,gfx,expander,0);
152 line.offset += length;
153 offset += length;
154
155 tokens = tokens.next;
156 }
157
158 return x;
159 }
160
161
162 private SyntaxUtilities() {}
163 }