1 19 20 package org.netbeans.swing.plaf.util; 21 22 import org.xml.sax.InputSource ; 23 import org.xml.sax.Locator ; 24 import org.xml.sax.Parser ; 25 import org.xml.sax.SAXException ; 26 import org.xml.sax.helpers.XMLReaderAdapter ; 27 28 import javax.swing.*; 29 import javax.swing.border.BevelBorder ; 30 import javax.swing.border.EtchedBorder ; 31 import javax.swing.plaf.*; 32 import javax.swing.plaf.metal.MetalTheme ; 33 import javax.xml.parsers.ParserConfigurationException ; 34 import javax.xml.parsers.SAXParserFactory ; 35 import java.awt.*; 36 import java.net.URL ; 37 import java.util.HashSet ; 38 import java.util.StringTokenizer ; 39 40 57 public class NbTheme extends MetalTheme implements org.xml.sax.DocumentHandler { 58 59 60 public static final String THEMEFILE_NAME = "themes.xml"; private static final String THEMESET_TAG = "themeset"; private static final String ACTIVE_ATTR = "active"; private static final String THEME_TAG = "theme"; private static final String BOOL_TAG = "boolean"; private static final String DIM_TAG = "dimension"; private static final String FONT_TAG = "font"; private static final String INSETS_TAG = "insets"; private static final String ETCHEDBORDER_TAG = "etchedborder"; 70 private static final String EMPTYBORDER_TAG = "emptyborder"; 71 private static final String BEVELBORDER_TAG = "bevelborder"; 72 private static final String LINEBORDER_TAG = "lineborder"; 73 private static final String COLOR_ATTR = "color"; private static final String KEY_ATTR = "key"; private static final String METRIC_TAG = "metric"; private static final String STRING_TAG = "string"; private static final String NAME_ATTR = "name"; private static final String FONTSTYLE_ATTR = "style"; private static final String FONTSIZE_ATTR = "size"; private static final String VALUE_ATTR = "value"; private static final String WIDTH_ATTR = "width"; private static final String HEIGHT_ATTR = "height"; private static final String RED_ATTR = "r"; private static final String GREEN_ATTR = "g"; private static final String BLUE_ATTR = "b"; private static final String LEFT_ATTR = "left"; private static final String TOP_ATTR = "top"; private static final String RIGHT_ATTR = "right"; private static final String BOTTOM_ATTR = "bottom"; private static final String TYPE_ATTR = "type"; private static final String REFERENCE_ATTR = "reference"; private static final String FONTSTYLE_BOLD = "bold"; private static final String FONTSTYLE_ITALIC = "italic"; private static final String TYPE_LOWERED = "lowered"; 99 private static final String CONTROLFONT = "controlFont"; private static final String SYSTEMFONT = "systemFont"; private static final String USERFONT = "userFont"; private static final String MENUFONT = "menuFont"; private static final String WINDOWTITLEFONT = "windowTitleFont"; private static final String SUBFONT = "subFont"; 107 private static final String PRIMARY1 = "primary1"; private static final String PRIMARY2 = "primary2"; private static final String PRIMARY3 = "primary3"; private static final String SECONDARY1 = "secondary1"; private static final String SECONDARY2 = "secondary2"; private static final String SECONDARY3 = "secondary3"; private static final String WHITE = "white"; private static final String BLACK = "black"; 118 private HashSet <String > activeThemes=null; 119 private boolean inActiveTheme=false; 120 private URL themeURL = null; 121 122 private UIDefaults defaults; 123 public String getName(){ return "NetBeans XML Theme"; } 125 public NbTheme(URL themeURL, LookAndFeel lf) { 126 this.themeURL = themeURL; 127 defaults = lf.getDefaults(); 128 initThemeDefaults(); 129 parseTheme(); 130 UIManager.getDefaults().putAll (defaults); 131 } 132 133 134 void initThemeDefaults () { 135 defaults.put(PRIMARY1, new ColorUIResource(102, 102, 153)); 136 defaults.put(PRIMARY2, new ColorUIResource(153, 153, 204)); 137 defaults.put(PRIMARY3, new ColorUIResource(204, 204, 255)); 138 139 defaults.put(SECONDARY1, new ColorUIResource(102, 102, 102)); 140 defaults.put(SECONDARY2, new ColorUIResource(153, 153, 153)); 141 defaults.put(SECONDARY3, new ColorUIResource(204, 204, 204)); 142 143 defaults.put(WHITE, new ColorUIResource(255,255,255)); 144 defaults.put(BLACK, new ColorUIResource(0,0,0)); 145 } 146 147 private void parseTheme(){ 148 try{ 149 SAXParserFactory factory = SAXParserFactory.newInstance(); 150 factory.setValidating(false); 151 factory.setNamespaceAware(false); 152 153 Parser p = new XMLReaderAdapter (factory.newSAXParser().getXMLReader()); 154 p.setDocumentHandler(this); 155 String externalForm = themeURL.toExternalForm(); 156 InputSource is = new InputSource (externalForm); 157 p.parse(is); 158 activeThemes=null; locator = null; 160 } 161 catch(java.io.IOException ie){ 162 System.err.println ("IO exception reading theme file"); } catch(org.xml.sax.SAXException se){ 164 System.err.println ("Error parsing theme file " + (locator != null ? "line " + locator.getLineNumber() : "")); } catch (ParserConfigurationException e) { 166 System.err.println ("Couldn't create XML parser for theme file"); } 168 } 169 170 public void endDocument() throws org.xml.sax.SAXException { 171 } 172 173 public void startDocument() throws org.xml.sax.SAXException { 174 } 175 176 public void startElement(java.lang.String p1,org.xml.sax.AttributeList atts) throws org.xml.sax.SAXException { 177 if (p1.equals(THEMESET_TAG)) { 179 String themes = atts.getValue (ACTIVE_ATTR); 182 if (themes != null) { 183 StringTokenizer tok = new StringTokenizer (themes, ","); activeThemes = new HashSet <String > (tok.countTokens() + 1); 185 while (tok.hasMoreTokens()) { 186 activeThemes.add(tok.nextToken().trim()); 187 } 188 } 189 } 190 else{ 191 if (p1.equals(THEME_TAG) && (activeThemes != null)) { 192 String themeName = atts.getValue (NAME_ATTR); 194 inActiveTheme = activeThemes.contains(themeName); 195 } else { 196 if (inActiveTheme) { 197 if (handleReference(atts)) { 198 return; 199 } 200 if (p1.equals (COLOR_ATTR)) { 201 handleColor (atts); 202 return; 203 } 204 if (p1.equals (FONT_TAG)) { 205 handleFont (atts); 206 return; 207 } 208 if (p1.equals (EMPTYBORDER_TAG)) { 209 handleEmptyBorder (atts); 210 return; 211 } 212 if (p1.equals (METRIC_TAG)) { 213 handleMetric (atts); 214 return; 215 } 216 if (p1.equals (STRING_TAG)) { 217 handleString (atts); 218 return; 219 } 220 if (p1.equals (INSETS_TAG)) { 221 handleInsets (atts); 222 return; 223 } 224 if (p1.equals (BOOL_TAG)) { 225 handleBool (atts); 226 return; 227 } 228 if (p1.equals (DIM_TAG)) { 229 handleDim (atts); 230 return; 231 } 232 if (p1.equals (ETCHEDBORDER_TAG)) { 233 handleEtchedBorder (atts); 234 return; 235 } 236 if (p1.equals (LINEBORDER_TAG)) { 237 handleLineBorder (atts); 238 return; 239 } 240 if (p1.equals (BEVELBORDER_TAG)) { 241 handleBevelBorder (atts); 242 return; 243 } 244 System.err.println("UNRECOGNIZED " + 245 "THEME ENTRY " + p1 + "\" " + atts.toString()); } 247 } 248 } 249 } 250 251 private boolean handleReference(org.xml.sax.AttributeList atts) throws SAXException { 252 String key = atts.getValue (KEY_ATTR); 253 String reference = atts.getValue(REFERENCE_ATTR); 254 if (reference != null) { 255 Object res = defaults.get(reference); 256 if (res != null) { 257 defaults.put(key, res); 258 return true; 259 } 260 } 261 return false; 262 } 263 264 private final void handleFont (org.xml.sax.AttributeList atts) throws SAXException { 265 String key = atts.getValue (KEY_ATTR); 266 String fontname = atts.getValue (NAME_ATTR); 267 String fontstylename = atts.getValue (FONTSTYLE_ATTR); 268 int fontsize = intFromAttr (atts, FONTSIZE_ATTR); 269 int fontstyle = Font.PLAIN; 270 if(fontstylename.equals(FONTSTYLE_BOLD)) { 271 fontstyle = Font.BOLD; 272 } else { 273 if(fontstylename.equals(FONTSTYLE_ITALIC)) fontstyle = Font.ITALIC; 274 } 275 276 FontUIResource resource = new FontUIResource (fontname, fontstyle, fontsize); 277 defaults.put (key, resource); 278 } 279 280 private final void handleColor (org.xml.sax.AttributeList atts) throws SAXException { 281 int r = intFromAttr (atts, RED_ATTR); 282 int g = intFromAttr (atts, GREEN_ATTR); 283 int b = intFromAttr (atts, BLUE_ATTR); 284 String key = atts.getValue(KEY_ATTR); 285 ColorUIResource resource = new ColorUIResource (r,g,b); 286 defaults.put (key, resource); 287 } 288 289 private final void handleMetric (org.xml.sax.AttributeList atts) throws SAXException { 290 String key = atts.getValue(KEY_ATTR); 291 Integer resource = Integer.valueOf (atts.getValue(VALUE_ATTR)); 292 defaults.put (key, resource); 293 } 294 295 private final void handleString (org.xml.sax.AttributeList atts) throws SAXException { 296 String key = atts.getValue (KEY_ATTR); 297 String resource = atts.getValue (VALUE_ATTR); 298 defaults.put (key, resource); 299 } 300 301 private final void handleBool (org.xml.sax.AttributeList atts) throws SAXException { 302 String key = atts.getValue (KEY_ATTR); 303 Boolean resource = Boolean.valueOf (key); 304 defaults.put (key, resource); 305 } 306 307 private final void handleDim (org.xml.sax.AttributeList atts) throws SAXException { 308 String key = atts.getValue (KEY_ATTR); 309 int width = intFromAttr (atts, WIDTH_ATTR); 310 int height = intFromAttr (atts, HEIGHT_ATTR); 311 DimensionUIResource resource = new DimensionUIResource (width, height); 312 defaults.put (key, resource); 313 } 314 315 private final void handleInsets (org.xml.sax.AttributeList atts) throws SAXException { 316 String key = atts.getValue (KEY_ATTR); 317 int top = intFromAttr (atts, TOP_ATTR); 318 int left = intFromAttr (atts, LEFT_ATTR); 319 int bottom = intFromAttr (atts, BOTTOM_ATTR); 320 int right = intFromAttr (atts, RIGHT_ATTR); 321 InsetsUIResource resource = new InsetsUIResource (top, left, bottom, 322 right); 323 defaults.put (key, resource); 324 } 325 326 private final void handleEtchedBorder (org.xml.sax.AttributeList atts) { 327 String key = atts.getValue (KEY_ATTR); 328 int i = EtchedBorder.LOWERED; 329 String type = atts.getValue (TYPE_ATTR); 330 if (type != null) 331 i = type.equals (TYPE_LOWERED) ? EtchedBorder.LOWERED : EtchedBorder.RAISED; 332 BorderUIResource.EtchedBorderUIResource resource = new BorderUIResource.EtchedBorderUIResource (i); 333 defaults.put (key, resource); 334 } 335 336 private final void handleBevelBorder (org.xml.sax.AttributeList atts) { 337 String key = atts.getValue (KEY_ATTR); 338 int i = BevelBorder.LOWERED; 339 String type = atts.getValue (TYPE_ATTR); 340 if (type != null) 341 i = type.equals (TYPE_LOWERED) ? BevelBorder.LOWERED : BevelBorder.RAISED; 342 BorderUIResource.BevelBorderUIResource resource = new BorderUIResource.BevelBorderUIResource (i); 343 defaults.put (key, resource); 344 } 345 346 private final void handleEmptyBorder (org.xml.sax.AttributeList atts) throws SAXException { 347 String key = atts.getValue (KEY_ATTR); 348 int top = intFromAttr (atts, TOP_ATTR); 349 int left = intFromAttr (atts, LEFT_ATTR); 350 int bottom = intFromAttr (atts, BOTTOM_ATTR); 351 int right = intFromAttr (atts, RIGHT_ATTR); 352 BorderUIResource.EmptyBorderUIResource resource = new BorderUIResource.EmptyBorderUIResource (top, left, bottom, right); 353 defaults.put (key, resource); 354 } 355 356 private final void handleLineBorder (org.xml.sax.AttributeList atts) throws SAXException { 357 String key = atts.getValue (KEY_ATTR); 358 int r = intFromAttr (atts, RED_ATTR); 359 int g = intFromAttr (atts, GREEN_ATTR); 360 int b = intFromAttr (atts, BLUE_ATTR); 361 int width = 1; 362 if (atts.getValue(WIDTH_ATTR) != null) { 363 width = intFromAttr (atts, WIDTH_ATTR); 364 } 365 Color c = new Color (r,g,b); 366 BorderUIResource.LineBorderUIResource resource = new BorderUIResource.LineBorderUIResource (c); 367 defaults.put (key, resource); 368 } 369 370 private final int intFromAttr (final org.xml.sax.AttributeList atts, final String key) throws SAXException { 371 try { 372 return Integer.valueOf (atts.getValue (key)).intValue(); 373 } catch (NumberFormatException nfe) { 374 throw new SAXException (atts.getValue(key) + " is not an integer"); 375 } 376 } 377 378 public void characters(char[] p1,int p2,int p3) throws org.xml.sax.SAXException { 379 } 380 381 Locator locator = null; 382 public void setDocumentLocator(org.xml.sax.Locator locator) { 383 this.locator = locator; 384 } 385 386 public void endElement(java.lang.String p1) throws org.xml.sax.SAXException { 387 if(p1.equals(THEME_TAG)){ 388 inActiveTheme=false; 389 } 390 } 391 392 public void ignorableWhitespace(char[] p1,int p2,int p3) throws org.xml.sax.SAXException { 393 } 394 395 public void processingInstruction(java.lang.String p1,java.lang.String p2) throws org.xml.sax.SAXException { 396 } 397 398 private final ColorUIResource getColor(String key) { 399 return (ColorUIResource) defaults.get (key); 400 } 401 402 private final FontUIResource getFont(String key) { 403 return (FontUIResource) defaults.get (key); 404 } 405 406 public FontUIResource getControlTextFont() { return getFont (CONTROLFONT); } 407 public FontUIResource getSystemTextFont() { return getFont (SYSTEMFONT); } 408 public FontUIResource getUserTextFont() { return getFont (USERFONT); } 409 public FontUIResource getMenuTextFont() { return getFont (MENUFONT); } 410 public FontUIResource getWindowTitleFont() { return getFont (WINDOWTITLEFONT); } 411 public FontUIResource getSubTextFont() { return getFont (SUBFONT); } 412 protected ColorUIResource getPrimary1() { return getColor (PRIMARY1); } 413 protected ColorUIResource getPrimary2() { return getColor (PRIMARY2); } 414 protected ColorUIResource getPrimary3() { return getColor (PRIMARY3); } 415 protected ColorUIResource getSecondary1() { return getColor (SECONDARY1); } 416 protected ColorUIResource getSecondary2() { return getColor (SECONDARY2); } 417 protected ColorUIResource getSecondary3() { return getColor (SECONDARY3); } 418 protected ColorUIResource getWhite() { return getColor (WHITE); } 419 protected ColorUIResource getBlack() { return getColor (BLACK); } 420 421 } 422 | Popular Tags |