metacity.java

来自「JAVA 所有包」· Java 代码 · 共 2,097 行 · 第 1/5 页

JAVA
2,097
字号
	int w = aee.evaluate(getStringAttr(attrs, "width"));	int h = aee.evaluate(getStringAttr(attrs, "height"));	if (getInt("width") == -1) {	    x -= w;	}	if (getInt("height") == -1) {	    y -= h;	}	if (alpha != null) {	    float a = Float.parseFloat(alpha);	    if (g instanceof Graphics2D) {		Graphics2D g2 = (Graphics2D)g;		Composite oldComp = g2.getComposite();		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, a));		icon.paintIcon(jif, g, x, y);		g2.setComposite(oldComp);	    }	} else {	    icon.paintIcon(jif, g, x, y);	}    }    protected void drawInclude(Node node, Graphics g, JInternalFrame jif) {	int oldWidth  = getInt("width");	int oldHeight = getInt("height");	NamedNodeMap attrs = node.getAttributes();	int x = aee.evaluate(getStringAttr(attrs, "x"),       0);	int y = aee.evaluate(getStringAttr(attrs, "y"),       0);	int w = aee.evaluate(getStringAttr(attrs, "width"),  -1);	int h = aee.evaluate(getStringAttr(attrs, "height"), -1);	if (w != -1) {	    variables.put("width",  w);	}	if (h != -1) {	    variables.put("height", h);	}	Node draw_ops = getNode("draw_ops", new String[] {	    "name", getStringAttr(node, "name")	});	g.translate(x, y);	draw(draw_ops, g, jif);	g.translate(-x, -y);	if (w != -1) {	    variables.put("width",  oldWidth);	}	if (h != -1) {	    variables.put("height", oldHeight);	}    }        protected void draw(Node draw_ops, Graphics g, JInternalFrame jif) {	if (draw_ops != null) {	    NodeList nodes = draw_ops.getChildNodes();	    if (nodes != null) {		Shape oldClip = g.getClip();		for (int i = 0; i < nodes.getLength(); i++) {		    Node child = nodes.item(i);		    if (child.getNodeType() == Node.ELEMENT_NODE) {			try {			    String name = child.getNodeName();			    if ("include".equals(name)) {				drawInclude(child, g, jif);			    } else if ("arc".equals(name)) {				drawArc(child, g);			    } else if ("clip".equals(name)) {				setClip(child, g);			    } else if ("gradient".equals(name)) {				drawGradient(child, g);			    } else if ("gtk_arrow".equals(name)) {				drawGTKArrow(child, g);			    } else if ("gtk_box".equals(name)) {				drawGTKBox(child, g);			    } else if ("gtk_vline".equals(name)) {				drawGTKVLine(child, g);			    } else if ("image".equals(name)) {				drawImage(child, g);			    } else if ("icon".equals(name)) {				drawIcon(child, g, jif);			    } else if ("line".equals(name)) {				drawLine(child, g);			    } else if ("rectangle".equals(name)) {				drawRectangle(child, g);			    } else if ("tint".equals(name)) {				drawTint(child, g);			    } else if ("tile".equals(name)) {				drawTile(child, g, jif);			    } else if ("title".equals(name)) {				drawTitle(child, g, jif);			    } else {				System.err.println("Unknown Metacity drawing op: "+child);			    }			} catch (NumberFormatException ex) {			    logError(themeName, ex);			}		    }		}		g.setClip(oldClip);	    }	}    }    protected void drawPiece(Node frame_style, Graphics g, String position, int x, int y,			     int width, int height, JInternalFrame jif) {	Node piece = getNode(frame_style, "piece", new String[] { "position", position });	if (piece != null) {	    Node draw_ops;	    String draw_ops_name = getStringAttr(piece, "draw_ops");	    if (draw_ops_name != null) {		draw_ops = getNode("draw_ops", new String[] { "name", draw_ops_name });	    } else {		draw_ops = getNode(piece, "draw_ops", null);	    }	    variables.put("width",  width);	    variables.put("height", height);	    g.translate(x, y);	    draw(draw_ops, g, jif);	    g.translate(-x, -y);	}    }    Insets getBorderInsets(SynthContext context, Insets insets) {	updateFrameGeometry(context);	if (insets == null) {	    insets = new Insets(0, 0, 0, 0);	}	insets.top    = ((Insets)frameGeometry.get("title_border")).top;	insets.bottom = getInt("bottom_height");	insets.left   = getInt("left_width");	insets.right  = getInt("right_width");	return insets;    }    private void updateFrameGeometry(SynthContext context) {        this.context = context;        JComponent comp = context.getComponent();        JComponent titlePane = findChild(comp, "InternalFrame.northPane");        JInternalFrame jif = null;        if (comp instanceof JInternalFrame) {            jif = (JInternalFrame)comp;        } else if (comp instanceof JInternalFrame.JDesktopIcon) {            jif = ((JInternalFrame.JDesktopIcon)comp).getInternalFrame();        } else {	    assert false : "component is not JInternalFrame or JInternalFrame.JDesktopIcon";            return;        }        if (frame_style_set == null) {            Node window = getNode("window", new String[]{"type", "normal"});                         if (window != null) {                frame_style_set = getNode("frame_style_set",                         new String[] {"name", getStringAttr(window, "style_set")});            }                         if (frame_style_set == null) {                frame_style_set = getNode("frame_style_set", new String[] {"name", "normal"});            }        }                if (frame_style_set != null) {            Node frame = getNode(frame_style_set, "frame", new String[] {                "focus", (jif.isSelected() ? "yes" : "no"),                "state", (jif.isMaximum() ? "maximized" : "normal")            });            if (frame != null) {                Node frame_style = getNode("frame_style", new String[] {                    "name", getStringAttr(frame, "style")                });                if (frame_style != null) {                    Map gm = frameGeometries.get(getStringAttr(frame_style, "geometry"));                    setFrameGeometry(titlePane, gm);                }            }        }    }    protected static void logError(String themeName, Exception ex) {	logError(themeName, ex.toString());    }    protected static void logError(String themeName, String msg) {	if (!errorLogged) {	    System.err.println("Exception in Metacity for theme \""+themeName+"\": "+msg);	    errorLogged = true;	}    }    // XML Parsing    protected static Document getXMLDoc(final URL xmlFile)				throws IOException,                                       ParserConfigurationException,                                       SAXException {	if (documentBuilder == null) {	    documentBuilder =                DocumentBuilderFactory.newInstance().newDocumentBuilder();	}	InputStream inputStream =            (InputStream)AccessController.doPrivileged(new PrivilegedAction() {                public Object run() {                    try {                         return new BufferedInputStream(xmlFile.openStream());                    } catch (IOException ex) {                        return null;                    }                }            });        Document doc = null;        if (inputStream != null) {	    doc = documentBuilder.parse(inputStream);        }        return doc;    }    protected Node[] getNodesByName(Node parent, String name) {	NodeList nodes = parent.getChildNodes(); // ElementNode	int n = nodes.getLength();	ArrayList<Node> list = new ArrayList();	for (int i=0; i < n; i++) {	    Node node = nodes.item(i);	    if (name.equals(node.getNodeName())) {		list.add(node);	    }	}	return list.toArray(new Node[list.size()]);    }    protected Node getNode(String tagName, String[] attrs) {	NodeList nodes = xmlDoc.getElementsByTagName(tagName);	return (nodes != null) ? getNode(nodes, tagName, attrs) : null;    }    protected Node getNode(Node parent, String name, String[] attrs) {	Node node = null;	NodeList nodes = parent.getChildNodes();	if (nodes != null) {	    node = getNode(nodes, name, attrs);	}	if (node == null) {	    String inheritFrom = getStringAttr(parent, "parent");	    if (inheritFrom != null) {		Node inheritFromNode = getNode(parent.getParentNode(),					       parent.getNodeName(),					       new String[] { "name", inheritFrom });		if (inheritFromNode != null) {		    node = getNode(inheritFromNode, name, attrs);		}	    }	}	return node;    }    protected Node getNode(NodeList nodes, String name, String[] attrs) {	int n = nodes.getLength();	for (int i=0; i < n; i++) {	    Node node = nodes.item(i);	    if (name.equals(node.getNodeName())) {		if (attrs != null) {		    NamedNodeMap nodeAttrs = node.getAttributes();		    if (nodeAttrs != null) {			boolean matches = true;			int nAttrs = attrs.length / 2;			for (int a = 0; a < nAttrs; a++) {			    String aName  = attrs[a * 2];			    String aValue = attrs[a * 2 + 1];			    Node attr = nodeAttrs.getNamedItem(aName);			    if (attr == null ||                                 aValue != null && !aValue.equals((String)attr.getNodeValue())) {				matches = false;				break;			    }			}			if (matches) {			    return node;			}		    }		} else {		    return node;		}	    }	}	return null;    }    protected String getStringAttr(Node node, String name) {	String value = null;	NamedNodeMap attrs = node.getAttributes();	if (attrs != null) {	    value = getStringAttr(attrs, name);	    if (value == null) {		String inheritFrom = getStringAttr(attrs, "parent");		if (inheritFrom != null) {		    Node inheritFromNode = getNode(node.getParentNode(),						   node.getNodeName(),						   new String[] { "name", inheritFrom });		    if (inheritFromNode != null) {			value = getStringAttr(inheritFromNode, name);		    }		}	    }	}	return value;    }    protected String getStringAttr(NamedNodeMap attrs, String name) {	Node item = attrs.getNamedItem(name);	return (item != null) ? (String)item.getNodeValue() : null;    }    protected boolean getBooleanAttr(Node node, String name, boolean fallback) {	String str = getStringAttr(node, name);	if (str != null) {	    return Boolean.valueOf(str).booleanValue();	}	return fallback;    }    protected int getIntAttr(Node node, String name, int fallback) {	String str = getStringAttr(node, name);	int value = fallback;	if (str != null) {	    try {		value = Integer.parseInt(str);	    } catch (NumberFormatException ex) {		logError(themeName, ex);	    }	}	return value;    }    protected float getFloatAttr(Node node, String name, float fallback) {	String str = getStringAttr(node, name);	float value = fallback;	if (str != null) {	    try {		value = Float.parseFloat(str);	    } catch (NumberFormatException ex) {		logError(themeName, ex);	    }	}	return value;    }    protected Color parseColor(String str) {	StringTokenizer tokenizer = new StringTokenizer(str, "/");	int n = tokenizer.countTokens();	if (n > 1) {	    String function = tokenizer.nextToken();	    if ("shade".equals(function)) {		assert (n == 3);		Color c = parseColor2(tokenizer.nextToken());		float alpha = Float.parseFloat(tokenizer.nextToken());		return GTKColorType.adjustColor(c, 1.0F, alpha, alpha);	    } else if ("blend".equals(function)) {		assert (n == 4);                Color  bg = parseColor2(tokenizer.nextToken());                Color  fg = parseColor2(tokenizer.nextToken());                float alpha = Float.parseFloat(tokenizer.nextToken());                if (alpha > 1.0f) {                    alpha = 1.0f / alpha;                }                		return new Color((int)(bg.getRed() + ((fg.getRed() - bg.getRed()) * alpha)),				 (int)(bg.getRed() + ((fg.getRed() - bg.getRed()) * alpha)),				 (int)(bg.getRed() + ((fg.getRed() - bg.getRed()) * alpha)));	    } else {		System.err.println("Unknown Metacity color function="+str);		return null;	    }	} else {	    return parseColor2(str);	}    }    protected Color parseColor2(String str) {	Color c = null;	if (str.startsWith("gtk:")) {	    int i1 = str.indexOf('[');	    if (i1 > 3) {		String typeStr = str.substring(4, i1).toLowerCase();		int i2 = str.indexOf(']');		if (i2 > i1+1) {		    String stateStr = str.substring(i1+1, i2).toUpperCase();		    int state = -1;		    if ("ACTIVE".equals(stateStr)) {			state = PRESSED;		    } else if ("INSENSITIVE".equals(stateStr)) {			state = DISABLED;		    } else if ("NORMAL".equals(stateStr)) {			state = ENABLED;		    } else if ("PRELIGHT".equals(stateStr)) {			state = MOUSE_OVER;		    } else if ("SELECTED".equals(stateStr)) {			state = SELECTED;		    }		    ColorType type = null;		    if ("fg".equals(typeStr)) {			type = GTKColorType.FOREGROUND;		    } else if ("bg".equals(typeStr)) {			type = GTKColorType.BACKGROUND;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?