FileDocCategorySizeDatePackage
WindowsInternalFrameTitlePane.javaAPI DocJava SE 5 API14503Fri Aug 26 14:54:50 BST 2005com.sun.java.swing.plaf.windows

WindowsInternalFrameTitlePane

public class WindowsInternalFrameTitlePane extends BasicInternalFrameTitlePane

Fields Summary
private Color
selectedTitleGradientColor
private Color
notSelectedTitleGradientColor
private JPopupMenu
systemPopupMenu
private JLabel
systemLabel
private Font
titleFont
private int
titlePaneHeight
private int
buttonWidth
private int
buttonHeight
Constructors Summary
public WindowsInternalFrameTitlePane(JInternalFrame f)

        super(f);
    
Methods Summary
protected voidaddSubComponents()

        add(systemLabel);
        add(iconButton);
        add(maxButton);
        add(closeButton);
    
protected voidaddSystemMenuItems(javax.swing.JPopupMenu menu)

        JMenuItem mi = (JMenuItem)menu.add(restoreAction);
        mi.setMnemonic('R");
        mi = (JMenuItem)menu.add(moveAction);
        mi.setMnemonic('M");
        mi = (JMenuItem)menu.add(sizeAction);
        mi.setMnemonic('S");
        mi = (JMenuItem)menu.add(iconifyAction);
        mi.setMnemonic('n");
        mi = (JMenuItem)menu.add(maximizeAction);
        mi.setMnemonic('x");
        systemPopupMenu.add(new JSeparator());
        mi = (JMenuItem)menu.add(closeAction);
        mi.setMnemonic('C");
    
protected voidassembleSystemMenu()

        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);
		    icon.paintIcon(this, g, 0, 0);
		}
		g.dispose();
	    }
	};
        systemLabel.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
		showSystemPopupMenu(e.getComponent());
            }
        });
    
protected voidcreateButtons()

	super.createButtons();
	if (XPStyle.getXP() != null) {
	    iconButton.setContentAreaFilled(false);
	    maxButton.setContentAreaFilled(false);
	    closeButton.setContentAreaFilled(false);
	}
    
protected java.awt.LayoutManagercreateLayout()

        return new WindowsTitlePaneLayout();
    
protected java.beans.PropertyChangeListenercreatePropertyChangeListener()

        return new WindowsPropertyChangeHandler();
    
public java.awt.DimensiongetMinimumSize()

	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;
    
public java.awt.DimensiongetPreferredSize()

	return getMinimumSize();
    
protected voidinstallDefaults()

        super.installDefaults();

	titlePaneHeight = UIManager.getInt("InternalFrame.titlePaneHeight");
	buttonWidth     = UIManager.getInt("InternalFrame.titleButtonWidth")  - 4;
	buttonHeight    = UIManager.getInt("InternalFrame.titleButtonHeight") - 4;

	if (XPStyle.getXP() != null) {
	    // Fix for XP bug where sometimes these sizes aren't updated properly
	    // Assume for now that XP buttons are always square
	    buttonWidth = buttonHeight;
	} else {
	    buttonWidth += 2;
	    selectedTitleGradientColor =
		    UIManager.getColor("InternalFrame.activeTitleGradient");
	    notSelectedTitleGradientColor =
		    UIManager.getColor("InternalFrame.inactiveTitleGradient");
	    Color activeBorderColor =
		    UIManager.getColor("InternalFrame.activeBorderColor");
	    setBorder(BorderFactory.createLineBorder(activeBorderColor, 1));
	}
    
public voidpaintComponent(java.awt.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("window.caption", "active", "textshadowtype");
		}
		if ("single".equalsIgnoreCase(shadowType)) {
		    Point shadowOffset = xp.getPoint("window.textshadowoffset");
		    Color shadowColor  = xp.getColor("window.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);
        }
    
protected voidpaintTitleBackground(java.awt.Graphics g)

	XPStyle xp = XPStyle.getXP();
	if (xp != null) {
	    XPStyle.Skin skin = xp.getSkin(frame.isIcon() ? "window.mincaption"
				      : (frame.isMaximum() ? "window.maxcaption"
							   : "window.caption"));

	    skin.paintSkin(g, 0,  0, getSize().width, getSize().height, frame.isSelected() ? 0 : 1);
	} 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 voidshowSystemMenu()

	showSystemPopupMenu(systemLabel);
    
private voidshowSystemPopupMenu(java.awt.Component invoker)

	Dimension dim = new Dimension();
	Border border = frame.getBorder();
	if (border != null) {
	    dim.width += border.getBorderInsets(frame).left +
		border.getBorderInsets(frame).right;
	    dim.height += border.getBorderInsets(frame).bottom +
		border.getBorderInsets(frame).top;
	}
	if (!frame.isIcon()) {
	    systemPopupMenu.show(invoker,
                getX() - dim.width,
                getY() + getHeight() - dim.height);
	} else {
	    systemPopupMenu.show(invoker,
                getX() - dim.width,
                getY() - systemPopupMenu.getPreferredSize().height -
		     dim.height);
	}
    
protected voiduninstallListeners()

        // Get around protected method in superclass
        super.uninstallListeners();