BasicInternalFrameTitlePane.java [plain text]
package javax.swing.plaf.basic;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
public class BasicInternalFrameTitlePane extends JComponent
{
public class CloseAction extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
if (frame.isClosable())
{
try
{
frame.setClosed(true);
}
catch (PropertyVetoException pve)
{
}
}
}
}
public class IconifyAction extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
if (frame.isIconifiable() && ! frame.isIcon())
{
try
{
frame.setIcon(true);
}
catch (PropertyVetoException pve)
{
}
}
}
}
public class MaximizeAction extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
try
{
if (frame.isMaximizable() && ! frame.isMaximum())
frame.setMaximum(true);
else if (frame.isMaximum())
frame.setMaximum(false);
}
catch (PropertyVetoException pve)
{
}
}
}
public class MoveAction extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
}
}
public class RestoreAction extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
if (frame.isMaximum())
{
try
{
frame.setMaximum(false);
}
catch (PropertyVetoException pve)
{
}
}
}
}
public class SizeAction extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
}
}
protected class PropertyChangeHandler implements PropertyChangeListener
{
public void propertyChange(PropertyChangeEvent evt)
{
enableActions();
}
}
public class SystemMenuBar extends JMenuBar
{
public boolean isFocusTransversable()
{
return true;
}
public boolean isOpaque()
{
return true;
}
public void paint(Graphics g)
{
Icon frameIcon = frame.getFrameIcon();
if (frameIcon == null)
frameIcon = BasicDesktopIconUI.defaultIcon;
frameIcon.paintIcon(this, g, 0, 0);
}
public void requestFocus()
{
super.requestFocus();
}
}
protected class TitlePaneLayout implements LayoutManager
{
public TitlePaneLayout()
{
}
public void addLayoutComponent(String name, Component c)
{
}
public void layoutContainer(Container c)
{
enableActions();
Insets insets = c.getInsets();
int width = c.getBounds().width - insets.left - insets.right;
int height = c.getBounds().height - insets.top - insets.bottom;
Dimension menupref = menuBar.getPreferredSize();
menuBar.setBounds(insets.left, insets.top, menupref.width, height);
int loc = width + insets.left;
Insets i = closeButton.getInsets();
Dimension prefs = new Dimension(iconSize + i.left + i.right,
iconSize + i.top + i.bottom);
int top = insets.top + (height - prefs.height) / 2;
if (closeAction.isEnabled())
{
loc -= prefs.width;
closeButton.setVisible(true);
closeButton.setBounds(loc, top, prefs.width, prefs.height);
}
else
closeButton.setVisible(false);
if (maximizeAction.isEnabled())
{
loc -= prefs.width;
maxButton.setVisible(true);
maxButton.setBounds(loc, top, prefs.width, prefs.height);
}
else
maxButton.setVisible(false);
if (iconifyAction.isEnabled())
{
loc -= prefs.width;
iconButton.setVisible(true);
iconButton.setBounds(loc, top, prefs.width, prefs.height);
}
else
iconButton.setVisible(false);
if (title != null)
title.setBounds(insets.left + menupref.width, insets.top,
loc - menupref.width - insets.left, height);
}
public Dimension minimumLayoutSize(Container c)
{
return preferredLayoutSize(c);
}
public Dimension preferredLayoutSize(Container c)
{
Insets frameInsets = frame.getInsets();
int height = 0;
int width = 0;
Dimension d;
Component[] components = BasicInternalFrameTitlePane.this.getComponents();
for (int i = 0; i < components.length; i++)
{
d = components[i].getPreferredSize();
height = Math.max(height, d.height);
width += d.width;
}
Insets insets = BasicInternalFrameTitlePane.this.getInsets();
height += insets.top + insets.bottom;
return new Dimension(width, height);
}
public void removeLayoutComponent(Component c)
{
}
}
private class PaneButton extends JButton
{
public PaneButton(Action a)
{
super(a);
setMargin(new Insets(0, 0, 0, 0));
setBorder(null);
}
public boolean isFocusable()
{
return false;
}
}
protected static final String CLOSE_CMD = "Close";
protected static final String ICONIFY_CMD = "Minimize";
protected static final String MAXIMIZE_CMD = "Maximize";
protected static final String MOVE_CMD = "Move";
protected static final String RESTORE_CMD = "Restore";
protected static final String SIZE_CMD = "Size";
protected Action closeAction;
protected Action iconifyAction;
protected Action maximizeAction;
protected Action moveAction;
protected Action restoreAction;
protected Action sizeAction;
protected JButton closeButton;
protected JButton iconButton;
protected JButton maxButton;
protected Color activeBGColor;
protected Color activeFGColor;
protected Color inactiveBGColor;
protected Color inactiveFGColor;
private static final int iconSize = 16;
protected Icon closeIcon = new Icon()
{
public int getIconHeight()
{
return iconSize;
}
public int getIconWidth()
{
return iconSize;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
g.translate(x, y);
Color saved = g.getColor();
g.setColor(Color.BLACK);
int four = iconSize / 4;
int six = iconSize * 6 / 16;
int ten = iconSize * 10 / 16;
int twelve = iconSize * 12 / 16;
Polygon a = new Polygon(new int[] { four, six, ten, twelve },
new int[] { six, four, twelve, ten }, 4);
Polygon b = new Polygon(new int[] { four, six, ten, twelve },
new int[] { ten, twelve, four, six }, 4);
g.fillPolygon(a);
g.fillPolygon(b);
g.setColor(saved);
g.translate(-x, -y);
}
};
protected Icon minIcon;
protected Icon maxIcon = new Icon()
{
public int getIconHeight()
{
return iconSize;
}
public int getIconWidth()
{
return iconSize;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
g.translate(x, y);
Color saved = g.getColor();
g.setColor(Color.BLACK);
int four = iconSize / 4;
int two = four / 2;
int six = iconSize * 6 / 16;
int eight = four * 2;
g.fillRect(four, four, eight, two);
g.drawRect(four, six, eight, six);
g.setColor(saved);
g.translate(-x, -y);
}
};
protected Icon iconIcon = new Icon()
{
public int getIconHeight()
{
return iconSize;
}
public int getIconWidth()
{
return iconSize;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
g.translate(x, y);
Color saved = g.getColor();
g.setColor(Color.BLACK);
g.fillRect(iconSize / 4, iconSize * 10 / 16, iconSize / 2, iconSize / 8);
g.setColor(saved);
g.translate(-x, -y);
}
};
protected JInternalFrame frame;
protected JMenuBar menuBar;
protected JMenu windowMenu;
protected Color notSelectedTextColor;
protected Color notSelectedTitleColor;
protected Color selectedTextColor;
protected Color selectedTitleColor;
protected PropertyChangeListener propertyChangeListener;
private transient JLabel title;
public BasicInternalFrameTitlePane(JInternalFrame f)
{
frame = f;
setLayout(createLayout());
title = new JLabel();
title.setHorizontalAlignment(SwingConstants.LEFT);
title.setHorizontalTextPosition(SwingConstants.LEFT);
title.setOpaque(false);
setOpaque(true);
setBackground(Color.LIGHT_GRAY);
installTitlePane();
}
protected void installTitlePane()
{
installDefaults();
installListeners();
createActions();
assembleSystemMenu();
createButtons();
setButtonIcons();
addSubComponents();
enableActions();
}
protected void addSubComponents()
{
add(menuBar);
add(closeButton);
add(iconButton);
add(maxButton);
}
protected void createActions()
{
closeAction = new CloseAction();
closeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, CLOSE_CMD);
iconifyAction = new IconifyAction();
iconifyAction.putValue(AbstractAction.ACTION_COMMAND_KEY, ICONIFY_CMD);
maximizeAction = new MaximizeAction();
maximizeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, MAXIMIZE_CMD);
sizeAction = new SizeAction();
sizeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, SIZE_CMD);
restoreAction = new RestoreAction();
restoreAction.putValue(AbstractAction.ACTION_COMMAND_KEY, RESTORE_CMD);
moveAction = new MoveAction();
moveAction.putValue(AbstractAction.ACTION_COMMAND_KEY, MOVE_CMD);
}
protected void installListeners()
{
propertyChangeListener = new PropertyChangeHandler();
frame.addPropertyChangeListener(propertyChangeListener);
}
protected void uninstallListeners()
{
frame.removePropertyChangeListener(propertyChangeListener);
propertyChangeListener = null;
}
protected void installDefaults()
{
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
setFont(defaults.getFont("InternalFrame.titleFont"));
activeFGColor = defaults.getColor("InternalFrame.activeTitleForeground");
activeBGColor = defaults.getColor("InternalFrame.activeTitleBackground");
inactiveFGColor = defaults.getColor("InternalFrame.inactiveTitleForeground");
inactiveBGColor = defaults.getColor("InternalFrame.inactiveTitleBackground");
}
protected void uninstallDefaults()
{
setFont(null);
activeFGColor = null;
activeBGColor = null;
inactiveFGColor = null;
inactiveBGColor = null;
}
protected void createButtons()
{
closeButton = new PaneButton(closeAction);
closeButton.setOpaque(false);
iconButton = new PaneButton(iconifyAction);
iconButton.setOpaque(false);
maxButton = new PaneButton(maximizeAction);
maxButton.setOpaque(false);
}
protected void setButtonIcons()
{
closeButton.setIcon(closeIcon);
iconButton.setIcon(iconIcon);
maxButton.setIcon(maxIcon);
}
protected void assembleSystemMenu()
{
menuBar = createSystemMenuBar();
windowMenu = createSystemMenu();
menuBar.add(windowMenu);
addSystemMenuItems(windowMenu);
enableActions();
}
protected void addSystemMenuItems(JMenu systemMenu)
{
JMenuItem tmp;
tmp = new JMenuItem(RESTORE_CMD);
tmp.addActionListener(restoreAction);
tmp.setMnemonic(KeyEvent.VK_R);
systemMenu.add(tmp);
tmp = new JMenuItem(MOVE_CMD);
tmp.addActionListener(moveAction);
tmp.setMnemonic(KeyEvent.VK_M);
systemMenu.add(tmp);
tmp = new JMenuItem(SIZE_CMD);
tmp.addActionListener(sizeAction);
tmp.setMnemonic(KeyEvent.VK_S);
systemMenu.add(tmp);
tmp = new JMenuItem(ICONIFY_CMD);
tmp.addActionListener(iconifyAction);
tmp.setMnemonic(KeyEvent.VK_N);
systemMenu.add(tmp);
tmp = new JMenuItem(MAXIMIZE_CMD);
tmp.addActionListener(maximizeAction);
tmp.setMnemonic(KeyEvent.VK_X);
systemMenu.add(tmp);
systemMenu.addSeparator();
tmp = new JMenuItem(CLOSE_CMD);
tmp.addActionListener(closeAction);
tmp.setMnemonic(KeyEvent.VK_C);
systemMenu.add(tmp);
}
protected JMenuBar createSystemMenuBar()
{
if (menuBar == null)
menuBar = new SystemMenuBar();
menuBar.removeAll();
return menuBar;
}
protected JMenu createSystemMenu()
{
if (windowMenu == null)
windowMenu = new JMenu();
windowMenu.removeAll();
return windowMenu;
}
protected void showSystemMenu()
{
menuBar.getMenu(1).getPopupMenu().show();
}
public void paintComponent(Graphics g)
{
paintTitleBackground(g);
Font f = g.getFont();
FontMetrics fm = g.getFontMetrics(f);
if (frame.getTitle() != null && title != null)
{
Color saved = g.getColor();
if (frame.isSelected())
g.setColor(activeFGColor);
else
g.setColor(inactiveFGColor);
title.setText(getTitle(frame.getTitle(), fm, title.getBounds().width));
SwingUtilities.paintComponent(g, title, null, title.getBounds());
g.setColor(saved);
}
}
protected void paintTitleBackground(Graphics g)
{
Color saved = g.getColor();
Dimension dims = getSize();
Color bg = getBackground();
if (frame.isSelected())
bg = activeBGColor;
else
bg = inactiveBGColor;
g.setColor(bg);
g.fillRect(0, 0, dims.width, dims.height);
g.setColor(saved);
}
protected String getTitle(String text, FontMetrics fm, int availableWidth)
{
Rectangle vr = new Rectangle(0, 0, availableWidth, fm.getHeight());
Rectangle ir = new Rectangle();
Rectangle tr = new Rectangle();
String value = SwingUtilities.layoutCompoundLabel(this, fm, text, null,
SwingConstants.CENTER,
SwingConstants.LEFT,
SwingConstants.CENTER,
SwingConstants.LEFT, vr,
ir, tr, 0);
return value;
}
protected void postClosingEvent(JInternalFrame frame)
{
}
protected void enableActions()
{
closeAction.setEnabled(frame.isClosable());
iconifyAction.setEnabled(frame.isIconifiable());
maximizeAction.setEnabled(frame.isMaximizable());
restoreAction.setEnabled(frame.isMaximum());
sizeAction.setEnabled(frame.isResizable());
moveAction.setEnabled(false);
}
protected PropertyChangeListener createPropertyChangeListener()
{
return new PropertyChangeHandler();
}
protected LayoutManager createLayout()
{
return new TitlePaneLayout();
}
}