metacity.java

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

JAVA
1,988
字号
		    userHome = System.getProperty("user.home");		    String theme = System.getProperty("swing.metacitythemename");		    if (theme != null) {			return theme;		    }		    // Note: this is a small file (< 1024 bytes) so it's not worth		    // starting an XML parser or even to use a buffered reader.		    URL url = new URL(new File(userHome).toURL(),				      ".gconf/apps/metacity/general/%25gconf.xml");		    // Pending: verify character encoding spec for gconf		    Reader reader = new InputStreamReader(url.openStream(), "ISO-8859-1");		    char[] buf = new char[1024];		    StringBuffer strBuf = new StringBuffer();		    int n;		    while ((n = reader.read(buf)) >= 0) {			strBuf.append(buf, 0, n);		    }	    		    reader.close();		    String str = strBuf.toString();		    if (str != null) {			String strLowerCase = str.toLowerCase();			int i = strLowerCase.indexOf("<entry name=\"theme\"");			if (i >= 0) {			    i = strLowerCase.indexOf("<stringvalue>", i);			    if (i > 0) {				i += "<stringvalue>".length();				int i2 = str.indexOf("<", i);				return str.substring(i, i2);			    }			}		    }		} catch (MalformedURLException ex) {		    // OK to just ignore. We'll use a fallback theme.		} catch (IOException ex) {		    // OK to just ignore. We'll use a fallback theme.		}		return null;	    } else if (type == GET_IMAGE) {		return new ImageIcon((URL)arg).getImage();	    } else {		return null;	    }	}    }    private static URL getThemeDir(String themeName) {	return (URL)new Privileged().doPrivileged(Privileged.GET_THEME_DIR, themeName);    }    private static String getUserTheme() {	return (String)new Privileged().doPrivileged(Privileged.GET_USER_THEME, null);    }    protected void tileImage(Graphics g, Image image, int x0, int y0, int w, int h, float[] alphas) {	Graphics2D g2 = (Graphics2D)g;	Composite oldComp = g2.getComposite();	int sw = image.getWidth(null);	int sh = image.getHeight(null);	int y = y0;	while (y < y0 + h) {	    sh = Math.min(sh, y0 + h - y);	    int x = x0;	    while (x < x0 + w) {		float f = (alphas.length - 1.0F) * x / (x0 + w);		int i = (int)f;		f -= (int)f;		float alpha = (1-f) * alphas[i];		if (i+1 < alphas.length) {		    alpha += f * alphas[i+1];		}		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));		int swm = Math.min(sw, x0 + w - x);		g.drawImage(image, x, y, x+swm, y+sh, 0, 0, swm, sh, null);		x += swm;	    }	    y += sh;	}	g2.setComposite(oldComp);    }    private HashMap<String, Image> images = new HashMap();    protected Image getImage(String key, Color c) {	Image image = images.get(key+"-"+c.getRGB());	if (image == null) {	    image = imageFilter.colorize(getImage(key), c);	    if (image != null) {		images.put(key+"-"+c.getRGB(), image);	    }	}	return image;    }    protected Image getImage(String key) {	Image image = images.get(key);	if (image == null) {	    if (themeDir != null) {		try {		    URL url = new URL(themeDir, key);		    image = (Image)new Privileged().doPrivileged(Privileged.GET_IMAGE, url);		} catch (MalformedURLException ex) {		    //log("Bad image url: "+ themeDir + "/" + key);		}	    }	    if (image != null) {		images.put(key, image);	    }	}	return image;    }    private class ColorizeImageFilter extends RGBImageFilter {	double cr, cg, cb;	public ColorizeImageFilter() {	    canFilterIndexColorModel = true;	}	public void setColor(Color color) {	    cr = color.getRed()   / 255.0;	    cg = color.getGreen() / 255.0;	    cb = color.getBlue()  / 255.0;	}	public Image colorize(Image fromImage, Color c) {	    setColor(c);	    ImageProducer producer = new FilteredImageSource(fromImage.getSource(), this);	    return new ImageIcon(context.getComponent().createImage(producer)).getImage();	}	public int filterRGB(int x, int y, int rgb) {	    // Assume all rgb values are shades of gray	    double grayLevel = 2 * (rgb & 0xff) / 255.0;	    double r, g, b;	    if (grayLevel <= 1.0) {		r = cr * grayLevel;		g = cg * grayLevel;		b = cb * grayLevel;            } else {		grayLevel -= 1.0;		r = cr + (1.0 - cr) * grayLevel;		g = cg + (1.0 - cg) * grayLevel;		b = cb + (1.0 - cb) * grayLevel;            }	    return ((rgb & 0xff000000) +		    (((int)(r * 255)) << 16) +		    (((int)(g * 255)) << 8) +		    (int)(b * 255));	}    }    protected static JComponent findChild(JComponent parent, String name) {	int n = parent.getComponentCount();	for (int i = 0; i < n; i++) {	    JComponent c = (JComponent)parent.getComponent(i);	    if (name.equals(c.getName())) {		return c;	    }	}	return null;    }    protected class TitlePaneLayout implements LayoutManager {        public void addLayoutComponent(String name, Component c) {}        public void removeLayoutComponent(Component c) {}            public Dimension preferredLayoutSize(Container c)  {	    return minimumLayoutSize(c);	}            public Dimension minimumLayoutSize(Container c) {	    JComponent titlePane = (JComponent)c;	    Container titlePaneParent = titlePane.getParent();	    JInternalFrame frame;	    if (titlePaneParent instanceof JInternalFrame) {		frame = (JInternalFrame)titlePaneParent;	    } else if (titlePaneParent instanceof JInternalFrame.JDesktopIcon) {		frame = ((JInternalFrame.JDesktopIcon)titlePaneParent).getInternalFrame();	    } else {		return null;	    }	    Dimension buttonDim = calculateButtonSize(titlePane);	    Insets title_border  = (Insets)getFrameGeometry().get("title_border");	    Insets button_border = (Insets)getFrameGeometry().get("button_border");            // Calculate width.            int width = getInt("left_titlebar_edge") + buttonDim.width + getInt("right_titlebar_edge");	    if (title_border != null) {		width += title_border.left + title_border.right;	    }            if (frame.isClosable()) {                width += buttonDim.width;            }            if (frame.isMaximizable()) {                width += buttonDim.width;            }            if (frame.isIconifiable()) {                width += buttonDim.width;            }            FontMetrics fm = frame.getFontMetrics(titlePane.getFont());            String frameTitle = frame.getTitle();            int title_w = frameTitle != null ? SwingUtilities2.stringWidth(                               frame, fm, frameTitle) : 0;            int title_length = frameTitle != null ? frameTitle.length() : 0;            // Leave room for three characters in the title.            if (title_length > 3) {                int subtitle_w = SwingUtilities2.stringWidth(                    frame, fm, frameTitle.substring(0, 3) + "...");                width += (title_w < subtitle_w) ? title_w : subtitle_w;            } else {                width += title_w;            }            // Calculate height.	    int titleHeight = fm.getHeight() + getInt("title_vertical_pad");	    if (title_border != null) {		titleHeight += title_border.top + title_border.bottom;	    }	    int buttonHeight = buttonDim.height;	    if (button_border != null) {		buttonHeight += button_border.top + button_border.bottom;	    }            int height = Math.max(buttonHeight, titleHeight);            return new Dimension(width, height);	}            public void layoutContainer(Container c) {	    JComponent titlePane = (JComponent)c;	    Container titlePaneParent = titlePane.getParent();	    JInternalFrame frame;	    if (titlePaneParent instanceof JInternalFrame) {		frame = (JInternalFrame)titlePaneParent;	    } else if (titlePaneParent instanceof JInternalFrame.JDesktopIcon) {		frame = ((JInternalFrame.JDesktopIcon)titlePaneParent).getInternalFrame();	    } else {		return;	    }	    Map gm = getFrameGeometry();            int w = titlePane.getWidth();            int h = titlePane.getHeight();	    JComponent menuButton     = findChild(titlePane, "InternalFrameTitlePane.menuButton");	    JComponent minimizeButton = findChild(titlePane, "InternalFrameTitlePane.iconifyButton");	    JComponent maximizeButton = findChild(titlePane, "InternalFrameTitlePane.maximizeButton");	    JComponent closeButton    = findChild(titlePane, "InternalFrameTitlePane.closeButton");	    int buttonGap = 0;	    Insets button_border = (Insets)gm.get("button_border");	    Dimension buttonDim = calculateButtonSize(titlePane);            int x = getInt("left_titlebar_edge");	    int y = (button_border != null) ? button_border.top : 0;            menuButton.setBounds(x, y, buttonDim.width, buttonDim.height);            x = w - buttonDim.width - getInt("right_titlebar_edge");	    if (button_border != null) {		x -= button_border.right;	    }            if (frame.isClosable()) {                closeButton.setBounds(x, y, buttonDim.width, buttonDim.height);                x -= (buttonDim.width + buttonGap);            }             if (frame.isMaximizable()) {                maximizeButton.setBounds(x, y, buttonDim.width, buttonDim.height);                x -= (buttonDim.width + buttonGap);            }            if (frame.isIconifiable()) {                minimizeButton.setBounds(x, y, buttonDim.width, buttonDim.height);            }         }    } // end TitlePaneLayout    protected Map getFrameGeometry() {	return frameGeometry;    }    protected void setFrameGeometry(JComponent titlePane, Map gm) {	this.frameGeometry = gm;	if (getInt("top_height") == 0) {	    gm.put("top_height", new Integer(titlePane.getHeight()));	}    }    protected int getInt(String key) {	Integer i = (Integer)frameGeometry.get(key);	if (i == null) {	    i = variables.get(key);	}	return (i != null) ? i.intValue() : 0;    }    protected boolean getBoolean(String key, boolean fallback) {	Boolean b = (Boolean)frameGeometry.get(key);	return (b != null) ? b.booleanValue() : fallback;    }    protected void drawArc(Node node, Graphics g) {	NamedNodeMap attrs = node.getAttributes();	Color color = parseColor(getStringAttr(attrs, "color"));	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 start_angle = aee.evaluate(getStringAttr(attrs, "start_angle"));	int extent_angle = aee.evaluate(getStringAttr(attrs, "extent_angle"));	boolean filled = getBooleanAttr(node, "filled", false);	if (getInt("width") == -1) {	    x -= w;	}	if (getInt("height") == -1) {	    y -= h;	}	g.setColor(color);	if (filled) {	    g.fillArc(x, y, w, h, start_angle, extent_angle);	} else {	    g.drawArc(x, y, w, h, start_angle, extent_angle);	}    }    protected void drawLine(Node node, Graphics g) {	NamedNodeMap attrs = node.getAttributes();	Color color = parseColor(getStringAttr(attrs, "color"));	int x1 = aee.evaluate(getStringAttr(attrs, "x1"));	int y1 = aee.evaluate(getStringAttr(attrs, "y1"));	int x2 = aee.evaluate(getStringAttr(attrs, "x2"));	int y2 = aee.evaluate(getStringAttr(attrs, "y2"));	int lineWidth = aee.evaluate(getStringAttr(attrs, "width"), 1);	g.setColor(color);	if (lineWidth != 1) {	    Graphics2D g2d = (Graphics2D)g;	    Stroke stroke = g2d.getStroke();	    g2d.setStroke(new BasicStroke((float)lineWidth));	    g2d.drawLine(x1, y1, x2, y2);	    g2d.setStroke(stroke);	} else {	    g.drawLine(x1, y1, x2, y2);	}    }    protected void drawRectangle(Node node, Graphics g) {	NamedNodeMap attrs = node.getAttributes();	Color color = parseColor(getStringAttr(attrs, "color"));	boolean filled = getBooleanAttr(node, "filled", false);	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"));	g.setColor(color);	if (getInt("width") == -1) {	    x -= w;	}	if (getInt("height") == -1) {	    y -= h;	}	if (filled) {	    g.fillRect(x, y, w, h);	} else {	    g.drawRect(x, y, w, h);	}    }    protected void drawTile(Node node, Graphics g, JInternalFrame jif) {	NamedNodeMap attrs = node.getAttributes();	int x0 = aee.evaluate(getStringAttr(attrs, "x"));	int y0 = aee.evaluate(getStringAttr(attrs, "y"));	int w = aee.evaluate(getStringAttr(attrs, "width"));	int h = aee.evaluate(getStringAttr(attrs, "height"));	int tw = aee.evaluate(getStringAttr(attrs, "tile_width"));	int th = aee.evaluate(getStringAttr(attrs, "tile_height"));	int width  = getInt("width");	int height = getInt("height");	if (width == -1) {	    x0 -= w;	}	if (height == -1) {	    y0 -= h;	}	Shape oldClip = g.getClip();	if (g instanceof Graphics2D) {	    ((Graphics2D)g).clip(new Rectangle(x0, y0, w, h));	}	variables.put("width",  tw);	variables.put("height", th);	Node draw_ops = getNode("draw_ops", new String[] { "name", getStringAttr(node, "name") });		int y = y0;	while (y < y0 + h) {	    int x = x0;	    while (x < x0 + w) {		g.translate(x, y);		draw(draw_ops, g, jif);		g.translate(-x, -y);		x += tw;	    }	    y += th;	}	variables.put("width",  width);	variables.put("height", height);	g.setClip(oldClip);    }    protected void drawTint(Node node, Graphics g) {	NamedNodeMap attrs = node.getAttributes();	Color color = parseColor(getStringAttr(attrs, "color"));	float alpha = Float.parseFloat(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 (g instanceof Graphics2D) {	    Graphics2D g2 = (Graphics2D)g;	    Composite oldComp = g2.getComposite();	    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);	    g2.setComposite(ac);	    g2.setColor(color);	    g2.fillRect(x, y, w, h);	    g2.setComposite(oldComp);	}    }    protected void drawTitle(Node node, Graphics g, JInternalFrame jif) {	NamedNodeMap attrs = node.getAttributes();	String colorStr = getStringAttr(attrs, "color");	int i = colorStr.indexOf("gtk:fg[");	if (i > 0) {	    colorStr = colorStr.substring(0, i) + "gtk:text[" + colorStr.substring(i+7);	}	Color color = parseColor(colorStr);	int x = aee.evaluate(getStringAttr(attrs, "x"));	int y = aee.evaluate(getStringAttr(attrs, "y"));	String title = jif.getTitle();        if (title != null) {            FontMetrics fm = SwingUtilities2.getFontMetrics(jif, g);	    if (jif.getComponentOrientation().isLeftToRight()) {		title = SwingUtilities2.clipStringIfNecessary(jif, fm, title,                             calculateTitleTextWidth(g, jif));	    }	    g.setColor(color);            SwingUtilities2.drawString(jif, g, title, x, y + fm.getAscent());        }    }    protected Dimension calculateButtonSize(JComponent titlePane) {	int buttonHeight = getInt("button_height");	if (buttonHeight == 0) {	    buttonHeight = titlePane.getHeight();	    if (buttonHeight == 0) {		buttonHeight = 13;	    } else {		Insets button_border = (Insets)frameGeometry.get("button_border");		if (button_border != null) {		    buttonHeight -= (button_border.top + button_border.bottom);		}	    }	}	int buttonWidth = getInt("button_width");	if (buttonWidth == 0) {	    buttonWidth = buttonHeight;	    Float aspect_ratio = (Float)frameGeometry.get("aspect_ratio");	    if (aspect_ratio != null) {		buttonWidth = (int)(buttonHeight / aspect_ratio.floatValue());	    }	}	return new Dimension(buttonWidth, buttonHeight);    }    protected Rectangle calculateTitleArea(JInternalFrame jif) {	JComponent titlePane = findChild(jif, "InternalFrame.northPane");	Dimension buttonDim = calculateButtonSize(titlePane);	Insets title_border = (Insets)frameGeometry.get("title_border");	Rectangle r = new Rectangle();	r.x = getInt("left_titlebar_edge") + buttonDim.width;

⌨️ 快捷键说明

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