FileDocCategorySizeDatePackage
SynthInternalFrameTitlePane.javaAPI DocJava SE 6 API17247Tue Jun 10 00:26:54 BST 2008javax.swing.plaf.synth

SynthInternalFrameTitlePane

public class SynthInternalFrameTitlePane extends BasicInternalFrameTitlePane implements sun.swing.plaf.synth.SynthUI, PropertyChangeListener
The class that manages a synth title bar
version
1.27 11/30/05
author
David Kloba
author
Joshua Outwater
author
Steve Wilson

Fields Summary
protected JPopupMenu
systemPopupMenu
protected JButton
menuButton
private SynthStyle
style
private int
titleSpacing
private int
buttonSpacing
private int
titleAlignment
Constructors Summary
public SynthInternalFrameTitlePane(JInternalFrame f)

        super(f);
    
Methods Summary
protected voidaddSubComponents()

	menuButton.setName("InternalFrameTitlePane.menuButton");
        iconButton.setName("InternalFrameTitlePane.iconifyButton");
        maxButton.setName("InternalFrameTitlePane.maximizeButton");
        closeButton.setName("InternalFrameTitlePane.closeButton");

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

        // PENDING: this should all be localizable!
        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");
	menu.add(new JSeparator());
	mi = (JMenuItem)menu.add(closeAction);
	mi.setMnemonic('C");
    
protected voidassembleSystemMenu()

        systemPopupMenu = new JPopupMenuUIResource();
        addSystemMenuItems(systemPopupMenu);
	enableActions();
        menuButton = createNoFocusButton();
        updateMenuIcon();
        menuButton.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                try {
                    frame.setSelected(true);
                } catch(PropertyVetoException pve) {
                }
                showSystemMenu();
            }
        });
	JPopupMenu p = frame.getComponentPopupMenu();
	if (p == null || p instanceof UIResource) {
	    frame.setComponentPopupMenu(systemPopupMenu);
	}
	if (frame.getDesktopIcon() != null) {
	    p = frame.getDesktopIcon().getComponentPopupMenu();
	    if (p == null || p instanceof UIResource) {
		frame.getDesktopIcon().setComponentPopupMenu(systemPopupMenu);
	    }
	}
	setInheritsPopupMenu(true);
    
protected java.awt.LayoutManagercreateLayout()

        SynthContext context = getContext(this);
	LayoutManager lm =
	    (LayoutManager)style.get(context, "InternalFrameTitlePane.titlePaneLayout");
	context.dispose();
        return (lm != null) ? lm : new SynthTitlePaneLayout();
    
private javax.swing.JButtoncreateNoFocusButton()

        JButton button = new JButton();
        button.setFocusable(false);
        button.setMargin(new Insets(0,0,0,0));
        return button;
    
private intgetComponentState(javax.swing.JComponent c)

        if (frame != null) {
            if (frame.isSelected()) {
                return SELECTED;
            }
        }
        return SynthLookAndFeel.getComponentState(c);
    
public javax.swing.plaf.synth.SynthContextgetContext(javax.swing.JComponent c)

        return getContext(c, getComponentState(c));
    
public javax.swing.plaf.synth.SynthContextgetContext(javax.swing.JComponent c, int state)

        return SynthContext.getContext(SynthContext.class, c,
                    SynthLookAndFeel.getRegion(c), style, state);
    
private javax.swing.plaf.synth.RegiongetRegion(javax.swing.JComponent c)

        return SynthLookAndFeel.getRegion(c);
    
public java.lang.StringgetUIClassID()

        return "InternalFrameTitlePaneUI";
    
protected voidinstallDefaults()

        super.installDefaults();
        updateStyle(this);
    
protected voidinstallListeners()

        super.installListeners();
        frame.addPropertyChangeListener(this);
        addPropertyChangeListener(this);
    
protected voidpaint(javax.swing.plaf.synth.SynthContext context, java.awt.Graphics g)

        String title = frame.getTitle();

	if (title != null) {
            SynthStyle style = context.getStyle();

            g.setColor(style.getColor(context, ColorType.TEXT_FOREGROUND));
            g.setFont(style.getFont(context));

            // Center text vertically.
	    FontMetrics fm = SwingUtilities2.getFontMetrics(frame, g);
            int baseline = (getHeight() + fm.getAscent() - fm.getLeading() -
                            fm.getDescent()) / 2;
            JButton lastButton = null;
            if (frame.isIconifiable()) {
                lastButton = iconButton;
            }
            else if (frame.isMaximizable()) {
                lastButton = maxButton;
            }
            else if (frame.isClosable()) {
                lastButton = closeButton;
            }
            int maxX;
            int minX;
            boolean ltr = SynthLookAndFeel.isLeftToRight(frame);
            int titleAlignment = this.titleAlignment;
            if (ltr) {
                if (lastButton != null) {
                    maxX = lastButton.getX() - titleSpacing;
                }
                else {
                    maxX = frame.getWidth() - frame.getInsets().right -
                           titleSpacing;
                }
                minX = menuButton.getX() + menuButton.getWidth() +
                       titleSpacing;
            }
            else {
                if (lastButton != null) {
                    minX = lastButton.getX() + lastButton.getWidth() +
                           titleSpacing;
                }
                else {
                    minX = frame.getInsets().left + titleSpacing;
                }
                maxX = menuButton.getX() - titleSpacing;
                if (titleAlignment == SwingConstants.LEADING) {
                    titleAlignment = SwingConstants.TRAILING;
                }
                else if (titleAlignment == SwingConstants.TRAILING) {
                    titleAlignment = SwingConstants.LEADING;
                }
            }
            String clippedTitle = getTitle(title, fm, maxX - minX);
            if (clippedTitle == title) {
                // String fit, align as necessary.
                if (titleAlignment == SwingConstants.TRAILING) {
                    minX = maxX - style.getGraphicsUtils(context).
                        computeStringWidth(context, g.getFont(), fm, title);
                }
                else if (titleAlignment == SwingConstants.CENTER) {
                    int width = style.getGraphicsUtils(context).
                           computeStringWidth(context, g.getFont(), fm, title);
                    minX = Math.max(minX, (getWidth() - width) / 2);
                    minX = Math.min(maxX - width, minX);
                }
            }
            style.getGraphicsUtils(context).paintText(
                context, g, clippedTitle, minX, baseline - fm.getAscent(), -1);
	}
    
public voidpaintBorder(javax.swing.plaf.synth.SynthContext context, java.awt.Graphics g, int x, int y, int w, int h)

        context.getPainter().paintInternalFrameTitlePaneBorder(context,
                                                            g, x, y, w, h);
    
public voidpaintComponent(java.awt.Graphics g)

        SynthContext context = getContext(this);
        SynthLookAndFeel.update(context, g);
        context.getPainter().paintInternalFrameTitlePaneBackground(context,
                          g, 0, 0, getWidth(), getHeight());
        paint(context, g);
        context.dispose();
    
public voidpropertyChange(java.beans.PropertyChangeEvent evt)

        if (evt.getSource() == this) {
            if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
                updateStyle(this);
            }
        }
        else {
            // Changes for the internal frame
            if (evt.getPropertyName() == JInternalFrame.FRAME_ICON_PROPERTY) {
                updateMenuIcon();
            }
        }
    
protected voidshowSystemMenu()

        Insets insets = frame.getInsets();
        if (!frame.isIcon()) {
            systemPopupMenu.show(frame, insets.left, getY() + getHeight());
        } else {
            systemPopupMenu.show(menuButton,
                getX() - insets.left - insets.right,
                getY() - systemPopupMenu.getPreferredSize().height -
                    insets.bottom - insets.top);
        }
    
protected voiduninstallDefaults()

        SynthContext context = getContext(this, ENABLED);
        style.uninstallDefaults(context);
        context.dispose();
        style = null;
        JInternalFrame.JDesktopIcon di = frame.getDesktopIcon();
        if(di != null && di.getComponentPopupMenu() == systemPopupMenu) {
            // Release link to systemMenu from the JInternalFrame
            di.setComponentPopupMenu(null);
        }
        super.uninstallDefaults();
    
protected voiduninstallListeners()

        frame.removePropertyChangeListener(this);
        removePropertyChangeListener(this);
        super.uninstallListeners();
    
private voidupdateMenuIcon()
Resets the menuButton icon to match that of the frame.

        Icon frameIcon = frame.getFrameIcon();
        SynthContext context = getContext(this);
        if (frameIcon != null) {
            Dimension maxSize = (Dimension)context.getStyle().get(context,
                                "InternalFrameTitlePane.maxFrameIconSize");
            int maxWidth = 16;
            int maxHeight = 16;
            if (maxSize != null) {
                maxWidth = maxSize.width;
                maxHeight = maxSize.height;
            }
            if ((frameIcon.getIconWidth() > maxWidth ||
                     frameIcon.getIconHeight() > maxHeight) && 
                    (frameIcon instanceof ImageIcon)) {
                frameIcon = new ImageIcon(((ImageIcon)frameIcon).
                             getImage().getScaledInstance(maxWidth, maxHeight,
                             Image.SCALE_SMOOTH));
            }
        }
        context.dispose();
        menuButton.setIcon(frameIcon);
    
private voidupdateStyle(javax.swing.JComponent c)

        SynthContext context = getContext(this, ENABLED);
        SynthStyle oldStyle = style;
        style = SynthLookAndFeel.updateStyle(context, this);
        if (style != oldStyle) {
            maxIcon =
                style.getIcon(context,"InternalFrameTitlePane.maximizeIcon");
            minIcon =
                style.getIcon(context,"InternalFrameTitlePane.minimizeIcon");
            iconIcon =
                style.getIcon(context,"InternalFrameTitlePane.iconifyIcon");
            closeIcon =
                style.getIcon(context,"InternalFrameTitlePane.closeIcon");
            titleSpacing = style.getInt(context,
                              "InternalFrameTitlePane.titleSpacing", 2);
            buttonSpacing = style.getInt(context,
                              "InternalFrameTitlePane.buttonSpacing", 2);
            String alignString = (String)style.get(context,
                              "InternalFrameTitlePane.titleAlignment");
            titleAlignment = SwingConstants.LEADING;
            if (alignString != null) {
                alignString = alignString.toUpperCase();
                if (alignString.equals("TRAILING")) {
                    titleAlignment = SwingConstants.TRAILING;
                }
                else if (alignString.equals("CENTER")) {
                    titleAlignment = SwingConstants.CENTER;
                }
            }
        }
        context.dispose();