basicinternalframetitlepane.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,014 行 · 第 1/2 页

JAVA
1,014
字号
  /** The size of the icons in the buttons. */  private static final int iconSize = 16;  /** The icon displayed in the close button. */  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);      }    };  // FIXME: Create new icon.  /** The icon displayed in the restore button. */  protected Icon minIcon;  /** The icon displayed in the maximize button. */  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);      }    };  /** The icon displayed in the iconify button. */  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);      }    };  /** The JInternalFrame that this TitlePane is used in. */  protected JInternalFrame frame;  /** The JMenuBar that is located at the top left of the Title Pane. */  protected JMenuBar menuBar;  /** The JMenu inside the menuBar. */  protected JMenu windowMenu;  /**   * The text color of the TitlePane when the JInternalFrame is not selected.   */  protected Color notSelectedTextColor;  /**   * The background color of the TitlePane when the JInternalFrame is not   * selected.   */  protected Color notSelectedTitleColor;  /** The text color of the titlePane when the JInternalFrame is selected. */  protected Color selectedTextColor;  /**   * The background color of the TitlePane when the JInternalFrame is   * selected.   */  protected Color selectedTitleColor;  /** The Property Change listener that listens to the JInternalFrame. */  protected PropertyChangeListener propertyChangeListener;  /**   * The label used to display the title. This label is not added to the   * TitlePane.   */  private transient JLabel title;  /**   * Creates a new BasicInternalFrameTitlePane object that is used in the   * given JInternalFrame.   *   * @param f The JInternalFrame this BasicInternalFrameTitlePane will be used   *        in.   */  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();  }  /**   * This method installs the TitlePane onto the JInternalFrameTitlePane. It   * also creates any children components that need to be created and adds   * listeners to the appropriate components.   */  protected void installTitlePane()  {    installDefaults();    installListeners();    createActions();    assembleSystemMenu();    createButtons();    setButtonIcons();    addSubComponents();    enableActions();  }  /**   * This method adds the sub components to the TitlePane.   */  protected void addSubComponents()  {    add(menuBar);    add(closeButton);    add(iconButton);    add(maxButton);  }  /**   * This method creates the actions that are used to manipulate the   * JInternalFrame.   */  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);  }  /**   * This method is used to install the listeners.   */  protected void installListeners()  {    propertyChangeListener = new PropertyChangeHandler();    frame.addPropertyChangeListener(propertyChangeListener);  }  /**   * This method is used to uninstall the listeners.   */  protected void uninstallListeners()  {    frame.removePropertyChangeListener(propertyChangeListener);    propertyChangeListener = null;  }  /**   * This method installs the defaults determined by the look and feel.   */  protected void installDefaults()  {    // FIXME: move icons to defaults.    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");  }  /**   * This method uninstalls the defaults.   */  protected void uninstallDefaults()  {    setFont(null);    activeFGColor = null;    activeBGColor = null;    inactiveFGColor = null;    inactiveBGColor = null;  }  /**   * This method creates the buttons used in the TitlePane.   */  protected void createButtons()  {    closeButton = new PaneButton(closeAction);    closeButton.setOpaque(false);    iconButton = new PaneButton(iconifyAction);    iconButton.setOpaque(false);    maxButton = new PaneButton(maximizeAction);    maxButton.setOpaque(false);  }  /**   * This method sets the icons in the buttons.   */  protected void setButtonIcons()  {    closeButton.setIcon(closeIcon);    iconButton.setIcon(iconIcon);    maxButton.setIcon(maxIcon);  }  /**   * This method creates the MenuBar used in the TitlePane.   */  protected void assembleSystemMenu()  {    menuBar = createSystemMenuBar();    windowMenu = createSystemMenu();    menuBar.add(windowMenu);    addSystemMenuItems(windowMenu);    enableActions();  }  /**   * This method adds the MenuItems to the given JMenu.   *   * @param systemMenu The JMenu to add MenuItems to.   */  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);  }  /**   * This method creates a new JMenubar.   *   * @return A new JMenuBar.   */  protected JMenuBar createSystemMenuBar()  {    if (menuBar == null)      menuBar = new SystemMenuBar();    menuBar.removeAll();    return menuBar;  }  /**   * This method creates a new JMenu.   *   * @return A new JMenu.   */  protected JMenu createSystemMenu()  {    if (windowMenu == null)      windowMenu = new JMenu();    windowMenu.removeAll();    return windowMenu;  }  /**   * This method programmatically shows the JMenu.   */  protected void showSystemMenu()  {    // FIXME: Untested as KeyEvents are not hooked up.    menuBar.getMenu(1).getPopupMenu().show();  }  /**   * This method paints the TitlePane.   *   * @param g The Graphics object to paint with.   */  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);      }  }  /**   * This method paints the TitlePane's background.   *   * @param g The Graphics object to paint with.   */  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);  }  /**   * This method returns the title string based on the available width and the   * font metrics.   *   * @param text The desired title.   * @param fm The FontMetrics of the font used.   * @param availableWidth The available width.   *   * @return The allowable string.   */  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;  }  /**   * This method fires something similar to a WINDOW_CLOSING event.   *   * @param frame The JInternalFrame that is being closed.   */  protected void postClosingEvent(JInternalFrame frame)  {    // FIXME: Implement postClosingEvent when I figure out what    // it's supposed to do.    // It says that this fires an WINDOW_CLOSING like event.     // So the closest thing is some kind of InternalFrameEvent.    // But none is fired.    // Can't see it called or anything.  }  /**   * This method enables the actions for the TitlePane given the frame's   * properties.   */  protected void enableActions()  {    closeAction.setEnabled(frame.isClosable());    iconifyAction.setEnabled(frame.isIconifiable());    // The maximize action is responsible for restoring it    // as well, if clicked from the button    maximizeAction.setEnabled(frame.isMaximizable());    // The restoring action is only active when selected    // from the menu.    restoreAction.setEnabled(frame.isMaximum());    sizeAction.setEnabled(frame.isResizable());    // FIXME: Tie MoveAction enabled status to a variable.    moveAction.setEnabled(false);  }  /**   * This method creates a new PropertyChangeListener.   *   * @return A new PropertyChangeListener.   */  protected PropertyChangeListener createPropertyChangeListener()  {    return new PropertyChangeHandler();  }  /**   * This method creates a new LayoutManager for the TitlePane.   *   * @return A new LayoutManager.   */  protected LayoutManager createLayout()  {    return new TitlePaneLayout();  }}

⌨️ 快捷键说明

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