windowsinternalframetitlepane.java

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

JAVA
521
字号
/* * @(#)WindowsInternalFrameTitlePane.java	1.26 06/05/24 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.java.swing.plaf.windows;import sun.swing.SwingUtilities2;import javax.swing.*;import javax.swing.border.*;import javax.swing.UIManager;import javax.swing.plaf.*;import javax.swing.plaf.basic.BasicInternalFrameTitlePane;import java.awt.*;import java.awt.event.*;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.beans.PropertyVetoException;import static com.sun.java.swing.plaf.windows.TMSchema.*;import static com.sun.java.swing.plaf.windows.XPStyle.Skin;public class WindowsInternalFrameTitlePane extends BasicInternalFrameTitlePane {    private Color selectedTitleGradientColor;    private Color notSelectedTitleGradientColor;    private JPopupMenu systemPopupMenu;    private JLabel systemLabel;    private Font titleFont;    private int titlePaneHeight;    private int buttonWidth, buttonHeight;    private boolean hotTrackingOn;    public WindowsInternalFrameTitlePane(JInternalFrame f) {        super(f);    }    protected void addSubComponents() {        add(systemLabel);        add(iconButton);        add(maxButton);        add(closeButton);    }    protected void installDefaults() {        super.installDefaults();	titlePaneHeight = UIManager.getInt("InternalFrame.titlePaneHeight");	buttonWidth     = UIManager.getInt("InternalFrame.titleButtonWidth")  - 4;	buttonHeight    = UIManager.getInt("InternalFrame.titleButtonHeight") - 4;	Object obj      = UIManager.get("InternalFrame.titleButtonToolTipsOn");	hotTrackingOn = (obj instanceof Boolean) ? (Boolean)obj : true;	if (XPStyle.getXP() != null) {	    // Fix for XP bug where sometimes these sizes aren't updated properly            // Assume for now that height is correct and derive width using the             // ratio from the uxtheme part            buttonWidth = buttonHeight;            Dimension d = XPStyle.getPartSize(Part.WP_CLOSEBUTTON, State.NORMAL);            if (d != null && d.width != 0 && d.height != 0) {                buttonWidth = (int) ((float) buttonWidth * d.width / d.height);            }	} else {	    buttonWidth += 2;	    selectedTitleGradientColor =		    UIManager.getColor("InternalFrame.activeTitleGradient");	    notSelectedTitleGradientColor =		    UIManager.getColor("InternalFrame.inactiveTitleGradient");	    Color activeBorderColor =		    UIManager.getColor("InternalFrame.activeBorderColor");	    setBorder(BorderFactory.createLineBorder(activeBorderColor, 1));	}    }    protected void uninstallListeners() {        // Get around protected method in superclass        super.uninstallListeners();    }    protected void createButtons() {	super.createButtons();	if (XPStyle.getXP() != null) {	    iconButton.setContentAreaFilled(false);	    maxButton.setContentAreaFilled(false);	    closeButton.setContentAreaFilled(false);	}    }    protected void setButtonIcons() {	super.setButtonIcons();	if (!hotTrackingOn) {	    iconButton.setToolTipText(null);	    maxButton.setToolTipText(null);	    closeButton.setToolTipText(null);	}    }    public void paintComponent(Graphics g)  {	XPStyle xp = XPStyle.getXP();        paintTitleBackground(g);	String title = frame.getTitle();        if (title != null) {            boolean isSelected = frame.isSelected();            Font oldFont = g.getFont();            Font newFont = (titleFont != null) ? titleFont : getFont();            g.setFont(newFont);            // Center text vertically.            FontMetrics fm = SwingUtilities2.getFontMetrics(frame, g, newFont);            int baseline = (getHeight() + fm.getAscent() - fm.getLeading() -                    fm.getDescent()) / 2;	    int titleX;	    Rectangle r = new Rectangle(0, 0, 0, 0);	    if (frame.isIconifiable())  r = iconButton.getBounds();	    else if (frame.isMaximizable())  r = maxButton.getBounds();	    else if (frame.isClosable())  r = closeButton.getBounds();	    int titleW;	    if(WindowsGraphicsUtils.isLeftToRight(frame) ) {		if (r.x == 0)  r.x = frame.getWidth()-frame.getInsets().right;		    titleX = systemLabel.getX() + systemLabel.getWidth() + 2;		    if (xp != null) {			titleX += 2;		    }		    titleW = r.x - titleX - 3;		    title = getTitle(frame.getTitle(), fm, titleW);	    } else {		titleX = systemLabel.getX() - 2			 - SwingUtilities2.stringWidth(frame,fm,title);	    }	    if (xp != null) {		String shadowType = null;		if (isSelected) {		    shadowType = xp.getString(this, Part.WP_CAPTION,					      State.ACTIVE, Prop.TEXTSHADOWTYPE);		}		if ("single".equalsIgnoreCase(shadowType)) {		    Point shadowOffset = xp.getPoint(this, Part.WP_WINDOW, State.ACTIVE,						     Prop.TEXTSHADOWOFFSET);		    Color shadowColor  = xp.getColor(this, Part.WP_WINDOW, State.ACTIVE,						     Prop.TEXTSHADOWCOLOR, null);		    if (shadowOffset != null && shadowColor != null) {			g.setColor(shadowColor);			SwingUtilities2.drawString(frame, g, title,				     titleX + shadowOffset.x,				     baseline + shadowOffset.y);		    }		}	    }	    g.setColor(isSelected ? selectedTextColor : notSelectedTextColor);            SwingUtilities2.drawString(frame, g, title, titleX, baseline);            g.setFont(oldFont);        }    }    public Dimension getPreferredSize() {	return getMinimumSize();    }    public Dimension getMinimumSize() {	Dimension d = new Dimension(super.getMinimumSize());	d.height = titlePaneHeight + 2;	XPStyle xp = XPStyle.getXP();	if (xp != null) {	    // Note: Don't know how to calculate height on XP,	    // the captionbarheight is 25 but native caption is 30 (maximized 26)	    if (frame.isMaximum()) {		d.height -= 1;	    } else {		d.height += 3;	    }	}	return d;    }    protected void paintTitleBackground(Graphics g) {	XPStyle xp = XPStyle.getXP();	if (xp != null) {	    Part part = frame.isIcon() ? Part.WP_MINCAPTION                                       : (frame.isMaximum() ? Part.WP_MAXCAPTION                                                            : Part.WP_CAPTION);	    State state = frame.isSelected() ? State.ACTIVE : State.INACTIVE;	    Skin skin = xp.getSkin(this, part);	    skin.paintSkin(g, 0,  0, getWidth(), getHeight(), state);	} else {	    Boolean gradientsOn = (Boolean)LookAndFeel.getDesktopPropertyValue(		"win.frame.captionGradientsOn", Boolean.valueOf(false));	    if (gradientsOn.booleanValue() && g instanceof Graphics2D) {		Graphics2D g2 = (Graphics2D)g;		Paint savePaint = g2.getPaint();		boolean isSelected = frame.isSelected();		int w = getWidth();		if (isSelected) {		    GradientPaint titleGradient = new GradientPaint(0,0, 			    selectedTitleColor,			    (int)(w*.75),0, 			    selectedTitleGradientColor);		    g2.setPaint(titleGradient);		} else {		    GradientPaint titleGradient = new GradientPaint(0,0, 			    notSelectedTitleColor,			    (int)(w*.75),0, 			    notSelectedTitleGradientColor);   		    g2.setPaint(titleGradient);		}		g2.fillRect(0, 0, getWidth(), getHeight());		g2.setPaint(savePaint);	    } else {		super.paintTitleBackground(g);	    }	}    }    protected void assembleSystemMenu() {        systemPopupMenu = new JPopupMenu();        addSystemMenuItems(systemPopupMenu);        enableActions();	systemLabel = new JLabel(frame.getFrameIcon()) {	    protected void paintComponent(Graphics g) {		int x = 0;		int y = 0;		int w = getWidth();		int h = getHeight();		g = g.create();  // Create scratch graphics		if (isOpaque()) {		    g.setColor(getBackground());		    g.fillRect(0, 0, w, h);		}		Icon icon = getIcon();		int iconWidth = 0;		int iconHeight = 0;		if (icon != null &&		    (iconWidth = icon.getIconWidth()) > 0 &&		    (iconHeight = icon.getIconHeight()) > 0) {		    // Set drawing scale to make icon scale to our desired size		    double drawScale;		    if (iconWidth > iconHeight) {			// Center icon vertically			y = (h - w*iconHeight/iconWidth) / 2;			drawScale = w / (double)iconWidth;		    } else {			// Center icon horizontally			x = (w - h*iconWidth/iconHeight) / 2;			drawScale = h / (double)iconHeight;		    }		    ((Graphics2D)g).translate(x, y);		    ((Graphics2D)g).scale(drawScale, drawScale);

⌨️ 快捷键说明

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