1 16 17 package org.mc4j.console.swing.editor.xml; 18 19 import javax.swing.text.Style ; 20 import javax.swing.text.StyleConstants ; 21 import javax.swing.text.StyledDocument ; 22 import java.awt.*; 23 import java.util.LinkedList ; 24 import java.util.regex.Matcher ; 25 import java.util.regex.Pattern ; 26 27 31 public class StyleTokens { 32 33 protected LinkedList tokenList; 34 35 public StyleTokens() { 36 tokenList = new LinkedList (); 37 } 38 39 public void addTokenType(String name, String 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 doc) { 48 for (int i = 0; i < tokenList.size(); i++) { 49 StyleToken styleToken = (StyleToken) tokenList.get(i); 50 Style style = doc.addStyle(styleToken.name,null); 51 StyleConstants.setForeground(style,styleToken.color); 52 StyleConstants.setBold(style, styleToken.bold); 53 } 54 Style style = doc.addStyle("error",null); 57 StyleConstants.setBackground(style, Color.yellow); 58 } 59 60 public String getExpression() { 61 StringBuffer buffer = new StringBuffer (); 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 getPattern() { 73 String expression = getExpression(); 74 return Pattern.compile(expression, Pattern.MULTILINE); 75 } 77 78 public Matcher getMatcher(String text) { 79 return getPattern().matcher(text); 80 } 81 82 public static class StyleToken { 83 protected String name; 84 protected String expression; 85 protected Color color; 86 protected boolean bold; 87 88 public StyleToken(String name, String 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 |