metacity.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,988 行 · 第 1/4 页

JAVA
1,988
字号
	r.y = 0;	r.height = titlePane.getHeight();	if (title_border != null) {	    r.x += title_border.left;	    r.y += title_border.top;	    r.height -= (title_border.top + title_border.bottom);	}	r.width = titlePane.getWidth() - r.x - getInt("right_titlebar_edge");	if (jif.isClosable()) {	    r.width -= buttonDim.width;	}	if (jif.isMaximizable()) {	    r.width -= buttonDim.width;	}	if (jif.isIconifiable()) {	    r.width -= buttonDim.width;	}	if (title_border != null) {	    r.width -= title_border.right;	}	return r;    }    protected int calculateTitleTextWidth(Graphics g, JInternalFrame jif) {	String title = jif.getTitle();	if (title != null) {	    Rectangle r = calculateTitleArea(jif);	    return Math.min(SwingUtilities2.stringWidth(jif,                     SwingUtilities2.getFontMetrics(jif, g), title), r.width);	}	return 0;    }    protected void setClip(Node node, Graphics g) {	NamedNodeMap attrs = node.getAttributes();	int x = aee.evaluate(getStringAttr(attrs, "x"));	int y = aee.evaluate(getStringAttr(attrs, "y"));	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 (g instanceof Graphics2D) {	    ((Graphics2D)g).clip(new Rectangle(x, y, w, h));	}    }    protected void drawGTKArrow(Node node, Graphics g) {	NamedNodeMap attrs = node.getAttributes();	String arrow    = getStringAttr(attrs, "arrow");	String shadow   = getStringAttr(attrs, "shadow");	String stateStr = getStringAttr(attrs, "state").toUpperCase();	int x = aee.evaluate(getStringAttr(attrs, "x"));	int y = aee.evaluate(getStringAttr(attrs, "y"));	int w = aee.evaluate(getStringAttr(attrs, "width"));	int h = aee.evaluate(getStringAttr(attrs, "height"));	int state = -1;	if ("NORMAL".equals(stateStr)) {	    state = ENABLED;	} else if ("SELECTED".equals(stateStr)) {	    state = SELECTED;	} else if ("INSENSITIVE".equals(stateStr)) {	    state = DISABLED;	} else if ("PRELIGHT".equals(stateStr)) {	    state = MOUSE_OVER;	}	int shadowType = -1;	if ("in".equals(shadow)) {	    shadowType = GTKConstants.SHADOW_IN;	} else if ("out".equals(shadow)) {	    shadowType = GTKConstants.SHADOW_OUT;	} else if ("etched_in".equals(shadow)) {	    shadowType = GTKConstants.SHADOW_ETCHED_IN;	} else if ("etched_out".equals(shadow)) {	    shadowType = GTKConstants.SHADOW_ETCHED_OUT;	} else if ("none".equals(shadow)) {	    shadowType = GTKConstants.SHADOW_NONE;	}	int direction = -1;	if ("up".equals(arrow)) {	    direction = GTKConstants.ARROW_UP;	} else if ("down".equals(arrow)) {	    direction = GTKConstants.ARROW_DOWN;	} else if ("left".equals(arrow)) {	    direction = GTKConstants.ARROW_LEFT;	} else if ("right".equals(arrow)) {	    direction = GTKConstants.ARROW_RIGHT;	}	GTKEngine engine = ((GTKStyle)context.getStyle()).getEngine(context);	engine.paintArrow(context, g, state, shadowType,			  direction, null, x, y, w, h);    }    protected void drawGTKBox(Node node, Graphics g) {	NamedNodeMap attrs = node.getAttributes();	String shadow   = getStringAttr(attrs, "shadow");	String stateStr = getStringAttr(attrs, "state").toUpperCase();	int x = aee.evaluate(getStringAttr(attrs, "x"));	int y = aee.evaluate(getStringAttr(attrs, "y"));	int w = aee.evaluate(getStringAttr(attrs, "width"));	int h = aee.evaluate(getStringAttr(attrs, "height"));	int state = -1;	if ("NORMAL".equals(stateStr)) {	    state = ENABLED;	} else if ("SELECTED".equals(stateStr)) {	    state = SELECTED;	} else if ("INSENSITIVE".equals(stateStr)) {	    state = DISABLED;	} else if ("PRELIGHT".equals(stateStr)) {	    state = MOUSE_OVER;	}	int shadowType = -1;	if ("in".equals(shadow)) {	    shadowType = GTKConstants.SHADOW_IN;	} else if ("out".equals(shadow)) {	    shadowType = GTKConstants.SHADOW_OUT;	} else if ("etched_in".equals(shadow)) {	    shadowType = GTKConstants.SHADOW_ETCHED_IN;	} else if ("etched_out".equals(shadow)) {	    shadowType = GTKConstants.SHADOW_ETCHED_OUT;	} else if ("none".equals(shadow)) {	    shadowType = GTKConstants.SHADOW_NONE;	}	GTKEngine.INSTANCE.paintBox(context, g, state, shadowType,				    null, x, y, w, h);    }    protected void drawGTKVLine(Node node, Graphics g) {	NamedNodeMap attrs = node.getAttributes();	String stateStr = getStringAttr(attrs, "state").toUpperCase();	int x  = aee.evaluate(getStringAttr(attrs, "x"));	int y1 = aee.evaluate(getStringAttr(attrs, "y1"));	int y2 = aee.evaluate(getStringAttr(attrs, "y2"));	int state = -1;	if ("NORMAL".equals(stateStr)) {	    state = ENABLED;	} else if ("SELECTED".equals(stateStr)) {	    state = SELECTED;	} else if ("INSENSITIVE".equals(stateStr)) {	    state = DISABLED;	} else if ("PRELIGHT".equals(stateStr)) {	    state = MOUSE_OVER;	}	GTKEngine.INSTANCE.paintVline(context, g, state, null, x, y1, 1, y2-y1);    }    protected void drawGradient(Node node, Graphics g) {	NamedNodeMap attrs = node.getAttributes();	String type = getStringAttr(attrs, "type");	float alpha = getFloatAttr(node, "alpha", -1F);	int x = aee.evaluate(getStringAttr(attrs, "x"));	int y = aee.evaluate(getStringAttr(attrs, "y"));	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;	}	// Get colors from child nodes	Node[] colorNodes = getNodesByName(node, "color");	Color[] colors = new Color[colorNodes.length];	for (int i = 0; i < colorNodes.length; i++) {	    colors[i] = parseColor(getStringAttr(colorNodes[i], "value"));	}	boolean horizontal = ("diagonal".equals(type) || "horizontal".equals(type));	boolean vertical   = ("diagonal".equals(type) || "vertical".equals(type));	if (g instanceof Graphics2D) {	    Graphics2D g2 = (Graphics2D)g;	    Composite oldComp = g2.getComposite();	    if (alpha >= 0F) {		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));	    }	    int n = colors.length - 1;	    for (int i = 0; i < n; i++) {		g2.setPaint(new GradientPaint(x + (horizontal ? (i*w/n) : 0),					      y + (vertical   ? (i*h/n) : 0),					      colors[i],					      x + (horizontal ? ((i+1)*w/n) : 0),					      y + (vertical   ? ((i+1)*h/n) : 0),					      colors[i+1]));		g2.fillRect(x + (horizontal ? (i*w/n) : 0),			    y + (vertical   ? (i*h/n) : 0),			    (horizontal ? (w/n) : w),			    (vertical   ? (h/n) : h));	    }	    g2.setComposite(oldComp);	}    }    protected void drawImage(Node node, Graphics g) {	NamedNodeMap attrs = node.getAttributes();	String filename = getStringAttr(attrs, "filename");	String colorizeStr = getStringAttr(attrs, "colorize");	Color colorize = (colorizeStr != null) ? parseColor(colorizeStr) : null;	String alpha = getStringAttr(attrs, "alpha");	Image object = (colorize != null) ? getImage(filename, colorize) : getImage(filename);	variables.put("object_width",  object.getWidth(null));	variables.put("object_height", object.getHeight(null));	String fill_type = getStringAttr(attrs, "fill_type"); 	int x = aee.evaluate(getStringAttr(attrs, "x"));	int y = aee.evaluate(getStringAttr(attrs, "y"));	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) {	    if ("tile".equals(fill_type)) {		StringTokenizer tokenizer = new StringTokenizer(alpha, ":");		float[] alphas = new float[tokenizer.countTokens()];		for (int i = 0; i < alphas.length; i++) {		    alphas[i] = Float.parseFloat(tokenizer.nextToken());		}		tileImage(g, object, x, y, w, h, alphas);	    } else {		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));		    g2.drawImage(object, x, y, w, h, null);		    g2.setComposite(oldComp);		}	    }	} else {	    g.drawImage(object, x, y, w, h, null);	}    }    protected void drawIcon(Node node, Graphics g, JInternalFrame jif) {	Icon icon = jif.getFrameIcon();	if (icon == null) {	    return;	}	NamedNodeMap attrs = node.getAttributes();	String alpha = getStringAttr(attrs, "alpha");	int x = aee.evaluate(getStringAttr(attrs, "x"));	int y = aee.evaluate(getStringAttr(attrs, "y"));	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) {	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;    }    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.equals((String)attr.getNodeValue())) {				matches = false;				break;

⌨️ 快捷键说明

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