KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > swing > editor > xml > StyleTokens


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.mc4j.console.swing.editor.xml;
18
19 import javax.swing.text.Style JavaDoc;
20 import javax.swing.text.StyleConstants JavaDoc;
21 import javax.swing.text.StyledDocument JavaDoc;
22 import java.awt.*;
23 import java.util.LinkedList JavaDoc;
24 import java.util.regex.Matcher JavaDoc;
25 import java.util.regex.Pattern JavaDoc;
26
27 /**
28  * @author Greg Hinkle (ghinkle@users.sourceforge.net), Nov 16, 2004
29  * @version $Revision: 570 $($Author: ghinkl $ / $Date: 2006-04-12 15:14:16 -0400 (Wed, 12 Apr 2006) $)
30  */

31 public class StyleTokens {
32
33     protected LinkedList JavaDoc tokenList;
34
35     public StyleTokens() {
36         tokenList = new LinkedList JavaDoc();
37     }
38
39     public void addTokenType(String JavaDoc name, String JavaDoc expression, Color color, boolean bold) {
40         tokenList.add(new StyleToken(name, expression, color, bold));
41     }
42
43     public StyleToken[] getTokens() {
44         return (StyleToken[]) tokenList.toArray(new StyleToken[tokenList.size()]);
45     }
46
47     public void setStyles(StyledDocument JavaDoc doc) {
48         for (int i = 0; i < tokenList.size(); i++) {
49             StyleToken styleToken = (StyleToken) tokenList.get(i);
50             Style JavaDoc style = doc.addStyle(styleToken.name,null);
51             StyleConstants.setForeground(style,styleToken.color);
52             StyleConstants.setBold(style, styleToken.bold);
53         }
54         // Add a special background colored, yellow
55
// since I'm not sure how to do squiglies under text
56
Style JavaDoc style = doc.addStyle("error",null);
57         StyleConstants.setBackground(style, Color.yellow);
58     }
59
60     public String JavaDoc getExpression() {
61         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
62         for (int i = 0; i < tokenList.size(); i++) {
63             StyleToken styleToken = (StyleToken) tokenList.get(i);
64             if (i > 0) buffer.append('|');
65             buffer.append('(');
66             buffer.append(styleToken.expression);
67             buffer.append(')');
68         }
69         return buffer.toString();
70     }
71
72     public Pattern JavaDoc getPattern() {
73         String JavaDoc expression = getExpression();
74         return Pattern.compile(expression, Pattern.MULTILINE);
75         //Pattern.DOTALL);
76
}
77
78     public Matcher JavaDoc getMatcher(String JavaDoc text) {
79         return getPattern().matcher(text);
80     }
81
82     public static class StyleToken {
83         protected String JavaDoc name;
84         protected String JavaDoc expression;
85         protected Color color;
86         protected boolean bold;
87
88         public StyleToken(String JavaDoc name, String JavaDoc expr, Color color, boolean bold) {
89             this.name = name;
90             this.expression = expr;
91             this.color = color;
92             this.bold = bold;
93         }
94     }
95 }
96
97
Popular Tags