?? synthparser.java
字號:
/* * @(#)SynthParser.java 1.15 04/04/16 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package javax.swing.plaf.synth;import org.xml.sax.*;import java.awt.*;import java.io.*;import java.lang.reflect.*;import java.net.*;import java.text.*;import java.util.*;import java.util.regex.*;import javax.swing.*;import javax.swing.plaf.*;import javax.xml.parsers.*;import com.sun.beans.ObjectHandler;import sun.swing.plaf.synth.*;/** * @version 1.15, 04/16/04 */class SynthParser extends HandlerBase { // // Known element names // private static final String ELEMENT_SYNTH = "synth"; private static final String ELEMENT_STYLE = "style"; private static final String ELEMENT_STATE = "state"; private static final String ELEMENT_FONT = "font"; private static final String ELEMENT_COLOR = "color"; private static final String ELEMENT_IMAGE_PAINTER = "imagePainter"; private static final String ELEMENT_PAINTER = "painter"; private static final String ELEMENT_PROPERTY = "property"; private static final String ELEMENT_SYNTH_GRAPHICS = "graphicsUtils"; private static final String ELEMENT_IMAGE_ICON = "imageIcon"; private static final String ELEMENT_BIND = "bind"; private static final String ELEMENT_BIND_KEY = "bindKey"; private static final String ELEMENT_INSETS = "insets"; private static final String ELEMENT_OPAQUE = "opaque"; private static final String ELEMENT_DEFAULTS_PROPERTY = "defaultsProperty"; private static final String ELEMENT_INPUT_MAP = "inputMap"; private static final String ELEMENT_BACKGROUND_IMAGE = "backgroundImage"; // // Known attribute names // private static final String ATTRIBUTE_ACTION = "action"; private static final String ATTRIBUTE_ID = "id"; private static final String ATTRIBUTE_IDREF = "idref"; private static final String ATTRIBUTE_CLONE = "clone"; private static final String ATTRIBUTE_VALUE = "value"; private static final String ATTRIBUTE_NAME = "name"; private static final String ATTRIBUTE_STYLE = "style"; private static final String ATTRIBUTE_SIZE = "size"; private static final String ATTRIBUTE_TYPE = "type"; private static final String ATTRIBUTE_TOP = "top"; private static final String ATTRIBUTE_LEFT = "left"; private static final String ATTRIBUTE_BOTTOM = "bottom"; private static final String ATTRIBUTE_RIGHT = "right"; private static final String ATTRIBUTE_KEY = "key"; private static final String ATTRIBUTE_SOURCE_INSETS = "sourceInsets"; private static final String ATTRIBUTE_DEST_INSETS = "destinationInsets"; private static final String ATTRIBUTE_PATH = "path"; private static final String ATTRIBUTE_STRETCH = "stretch"; private static final String ATTRIBUTE_PAINT_CENTER = "paintCenter"; private static final String ATTRIBUTE_METHOD = "method"; private static final String ATTRIBUTE_DIRECTION = "direction"; /** * Lazily created, used for anything we don't understand. */ private ObjectHandler _handler; /** * Indicates the depth of how many elements we've encountered but don't * understand. This is used when forwarding to beans persistance to know * when we hsould stop forwarding. */ private int _depth; /** * Factory that new styles are added to. */ private DefaultSynthStyleFactory _factory; /** * Array of state infos for the current style. These are pushed to the * style when </style> is received. */ private java.util.List _stateInfos; /** * Current style. */ private ParsedSynthStyle _style; /** * Current state info. */ private ParsedSynthStyle.StateInfo _stateInfo; /** * Bindings for the current InputMap */ private java.util.List _inputMapBindings; /** * ID for the input map. This is cached as * the InputMap is created AFTER the inputMapProperty has ended. */ private String _inputMapID; /** * Object references outside the scope of persistance. */ private Map _mapping; /** * Based used to resolve paths. */ private Class _resourceBase; /** * List of ColorTypes. This is populated in startColorType. */ private java.util.List _colorTypes; /** * defaultsPropertys are placed here. */ private Map _defaultsMap; /** * List of SynthStyle.Painters that will be applied to the current style. */ private java.util.List _stylePainters; /** * List of SynthStyle.Painters that will be applied to the current state. */ private java.util.List _statePainters; SynthParser() { _mapping = new HashMap(); _stateInfos = new ArrayList(); _colorTypes = new ArrayList(); _inputMapBindings = new ArrayList(); _stylePainters = new ArrayList(); _statePainters = new ArrayList(); } /** * Parses a set of styles from <code>inputStream</code>, adding the * resulting styles to the passed in DefaultSynthStyleFactory. * * @param inputStream XML document containing the styles to read * @param factory DefaultSynthStyleFactory that new styles are added to * @param resourceBase Class used to resolve any resources, such as Images * @param defaultsMap Map that UIDefaults properties are placed in */ public void parse(InputStream inputStream, DefaultSynthStyleFactory factory, Class resourceBase, Map defaultsMap) throws ParseException, IllegalArgumentException { if (inputStream == null || factory == null || resourceBase == null) { throw new IllegalArgumentException( "You must supply an InputStream;, Class and StyleFactory"); } _factory = factory; _resourceBase = resourceBase; _defaultsMap = defaultsMap; try { try { SAXParser saxParser = SAXParserFactory.newInstance(). newSAXParser(); saxParser.parse(new BufferedInputStream(inputStream), this); } catch (ParserConfigurationException e) { throw new ParseException("Error parsing: " + e, 0); } catch (SAXException se) { throw new ParseException("Error parsing: " + se + " " + se.getException(), 0); } catch (IOException ioe) { throw new ParseException("Error parsing: " + ioe, 0); } } finally { reset(); } } /** * Returns the path to a resource. */ private URL getResource(String path) { return _resourceBase.getResource(path); } /** * Clears our internal state. */ private void reset() { _handler = null; _depth = 0; _mapping.clear(); _stateInfos.clear(); _colorTypes.clear(); _statePainters.clear(); _stylePainters.clear(); } /** * Returns true if we are forwarding to persistance. */ private boolean isForwarding() { return (_depth > 0); } /** * Handles beans persistance. */ private ObjectHandler getHandler() { if (_handler == null) { _handler = new ObjectHandler(); } return _handler; } /** * If <code>value</code> is an instance of <code>type</code> it is * returned, otherwise a SAXException is thrown. */ private Object checkCast(Object value, Class type) throws SAXException { if (!type.isInstance(value)) { throw new SAXException("Expected type " + type + " got " + value.getClass()); } return value; } /** * Returns an object created with id=key. If the object is not of * type type, this will throw an exception. */ private Object lookup(String key, Class type) throws SAXException { Object value = null; if (_handler != null) { if ((value = _handler.lookup(key)) != null) { return checkCast(value, type); } } value = _mapping.get(key); if (value == null) { throw new SAXException("ID " + key + " has not been defined"); } return checkCast(value, type); } /** * Registers an object by name. This will throw an exception if an * object has already been registered under the given name. */ private void register(String key, Object value) throws SAXException { if (key != null) { if (_mapping.get(key) != null || (_handler != null && _handler.lookup(key) != null)) { throw new SAXException("ID " + key + " is already defined"); } _mapping.put(key, value); } } /** * Convenience method to return the next int, or throw if there are no * more valid ints. */ private int nextInt(StringTokenizer tok, String errorMsg) throws SAXException { if (!tok.hasMoreTokens()) { throw new SAXException(errorMsg); } try { return Integer.parseInt(tok.nextToken()); } catch (NumberFormatException nfe) { throw new SAXException(errorMsg); } } /** * Convenience method to return an Insets object. */ private Insets parseInsets(String insets, String errorMsg) throws SAXException { StringTokenizer tokenizer = new StringTokenizer(insets); return new Insets(nextInt(tokenizer, errorMsg), nextInt(tokenizer, errorMsg), nextInt(tokenizer, errorMsg), nextInt(tokenizer, errorMsg)); } // // The following methods are invoked from startElement/stopElement // private void startStyle(AttributeList attributes) throws SAXException { String id = null; _style = null; for(int i = attributes.getLength() - 1; i >= 0; i--) { String key = attributes.getName(i); if (key.equals(ATTRIBUTE_CLONE)) { _style = (ParsedSynthStyle)((ParsedSynthStyle)lookup( attributes.getValue(i), ParsedSynthStyle.class)). clone(); } else if (key.equals(ATTRIBUTE_ID)) { id = attributes.getValue(i); } } if (_style == null) { _style = new ParsedSynthStyle(); } register(id, _style); } private void endStyle() throws SAXException { int size = _stylePainters.size(); if (size > 0) { _style.setPainters((ParsedSynthStyle.PainterInfo[]) _stylePainters.toArray(new ParsedSynthStyle. PainterInfo[size])); _stylePainters.clear(); } size = _stateInfos.size(); if (size > 0) { _style.setStateInfo((ParsedSynthStyle.StateInfo[])_stateInfos. toArray(new ParsedSynthStyle.StateInfo[size])); _stateInfos.clear(); } _style = null; } private void startState(AttributeList attributes) throws SAXException { ParsedSynthStyle.StateInfo stateInfo = null; int state = 0; String id = null; _stateInfo = null; for(int i = attributes.getLength() - 1; i >= 0; i--) { String key = attributes.getName(i); if (key.equals(ATTRIBUTE_ID)) { id = attributes.getValue(i); } else if (key.equals(ATTRIBUTE_IDREF)) { _stateInfo = (ParsedSynthStyle.StateInfo)lookup( attributes.getValue(i), ParsedSynthStyle.StateInfo.class); } else if (key.equals(ATTRIBUTE_CLONE)) { _stateInfo = (ParsedSynthStyle.StateInfo)((ParsedSynthStyle. StateInfo)lookup(attributes.getValue(i), ParsedSynthStyle.StateInfo.class)).clone(); } else if (key.equals(ATTRIBUTE_VALUE)) { StringTokenizer tokenizer = new StringTokenizer( attributes.getValue(i)); while (tokenizer.hasMoreTokens()) { String stateString = tokenizer.nextToken().toUpperCase(). intern(); if (stateString == "ENABLED") { state |= SynthConstants.ENABLED; } else if (stateString == "MOUSE_OVER") { state |= SynthConstants.MOUSE_OVER; } else if (stateString == "PRESSED") { state |= SynthConstants.PRESSED; } else if (stateString == "DISABLED") { state |= SynthConstants.DISABLED; } else if (stateString == "FOCUSED") { state |= SynthConstants.FOCUSED; } else if (stateString == "SELECTED") {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -