1 7 8 package com.sun.java.swing.plaf.gtk; 9 10 import java.util.HashMap ; 11 import java.awt.Color ; 12 import java.awt.Insets ; 13 import java.io.*; 14 15 19 abstract class PropertyParser { 20 21 public boolean needSimpleStringsEscaped() { 26 return false; 27 } 28 29 private static final PropertyParser INSETS_PARSER = new PropertyParser() { 30 public Object parse(String source) { 31 GTKScanner scanner = new GTKScanner(); 32 scanner.scanReader(new StringReader(source), source); 33 34 try { 35 int left; 36 int right; 37 int top; 38 int bottom; 39 40 if (scanner.getToken() != GTKScanner.TOKEN_LEFT_CURLY) { 41 return null; 42 } 43 44 if (scanner.getToken() != GTKScanner.TOKEN_INT) { 45 return null; 46 } 47 48 left = (int)scanner.currValue.longVal; 49 50 if (scanner.getToken() != GTKScanner.TOKEN_COMMA) { 51 return null; 52 } 53 54 if (scanner.getToken() != GTKScanner.TOKEN_INT) { 55 return null; 56 } 57 58 right = (int)scanner.currValue.longVal; 59 60 if (scanner.getToken() != GTKScanner.TOKEN_COMMA) { 61 return null; 62 } 63 64 if (scanner.getToken() != GTKScanner.TOKEN_INT) { 65 return null; 66 } 67 68 top = (int)scanner.currValue.longVal; 69 70 if (scanner.getToken() != GTKScanner.TOKEN_COMMA) { 71 return null; 72 } 73 74 if (scanner.getToken() != GTKScanner.TOKEN_INT) { 75 return null; 76 } 77 78 bottom = (int)scanner.currValue.longVal; 79 80 if (scanner.getToken() != GTKScanner.TOKEN_RIGHT_CURLY) { 81 return null; 82 } 83 84 return new Insets (top, left, bottom, right); 85 } catch (IOException ioe) { 86 } finally { 87 scanner.clearScanner(); 88 } 89 90 return null; 91 } 92 }; 93 94 private static final PropertyParser BOOLEAN_PARSER = new PropertyParser() { 95 public Object parse(String source) { 96 source = source.trim(); 97 98 if (source.equals("TRUE")) { 99 return Boolean.TRUE; 100 } else if (source.equals("FALSE")) { 101 return Boolean.FALSE; 102 } 103 104 return null; 105 } 106 }; 107 108 private static final PropertyParser SHADOW_PARSER = new PropertyParser() { 109 public Object parse(String source) { 110 source = source.trim(); 111 112 if (source.equals("SHADOW_IN")) { 113 return new Integer (GTKConstants.SHADOW_IN); 114 } else if (source.equals("SHADOW_OUT")) { 115 return new Integer (GTKConstants.SHADOW_OUT); 116 } else if (source.equals("SHADOW_ETCHED_IN")) { 117 return new Integer (GTKConstants.SHADOW_ETCHED_IN); 118 } else if (source.equals("SHADOW_ETCHED_OUT")) { 119 return new Integer (GTKConstants.SHADOW_ETCHED_OUT); 120 } else if (source.equals("SHADOW_NONE")) { 121 return new Integer (GTKConstants.SHADOW_NONE); 122 } 123 124 return null; 125 } 126 }; 127 128 private static final PropertyParser FOCUS_LINE_PARSER = new PropertyParser() { 129 public Object parse(String source) { 130 int len = source.length(); 131 int[] retVal = new int[len]; 132 133 for (int i = 0; i < len; i++) { 134 retVal[i] = (int)source.charAt(i); 135 } 136 137 if (retVal.length == 0 || 138 retVal.length == 1 && retVal[0] == 0) { 139 retVal = new int[] { Integer.MAX_VALUE }; 141 142 } else if (retVal.length == 1) { 143 int value = retVal[0]; 145 retVal = new int[] { value, value }; 146 } 147 return retVal; 148 } 149 }; 150 151 private static final PropertyParser COLOR_PARSER = new PropertyParser() { 152 public boolean needSimpleStringsEscaped() { 155 return true; 156 } 157 158 public Object parse(String source) { 159 GTKScanner scanner = new GTKScanner(); 160 scanner.scanReader(new StringReader(source), source); 161 162 try { 163 Color [] ret = new Color [1]; 164 int token = GTKParser.parseColor(scanner, ret, 0); 165 if (token == GTKScanner.TOKEN_NONE) { 166 return ret[0]; 167 } 168 } catch (IOException ioe) { 169 } finally { 170 scanner.clearScanner(); 171 } 172 173 return null; 174 } 175 }; 176 private static final HashMap PARSERS = new HashMap (); 177 178 static { 179 PARSERS.put("default-border", INSETS_PARSER); 180 PARSERS.put("interior-focus", BOOLEAN_PARSER); 181 PARSERS.put("shadow-type", SHADOW_PARSER); 182 PARSERS.put("focus-line-pattern", FOCUS_LINE_PARSER); 183 PARSERS.put("cursor-color", COLOR_PARSER); 184 } 185 186 public static PropertyParser getParserFor(String type) { 187 return (PropertyParser)PARSERS.get(type); 188 } 189 190 public abstract Object parse(String source); 191 192 } 193 | Popular Tags |