basicinternalframeui.java

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

JAVA
1,653
字号
      Dimension contentDims = frame.getContentPane().getPreferredSize();      if (min)	contentDims.width = contentDims.height = 0;      int nWidth = 0;      int nHeight = 0;      int sWidth = 0;      int sHeight = 0;      int eWidth = 0;      int eHeight = 0;      int wWidth = 0;      int wHeight = 0;      Dimension dims;      if (northPane != null)        {	  dims = northPane.getPreferredSize();	  if (dims != null)	    {	      nWidth = dims.width;	      nHeight = dims.height;	    }        }      if (southPane != null)        {	  dims = southPane.getPreferredSize();	  if (dims != null)	    {	      sWidth = dims.width;	      sHeight = dims.height;	    }        }      if (eastPane != null)        {	  dims = eastPane.getPreferredSize();	  if (dims != null)	    {	      sWidth = dims.width;	      sHeight = dims.height;	    }        }      if (westPane != null)        {	  dims = westPane.getPreferredSize();	  if (dims != null)	    {	      wWidth = dims.width;	      wHeight = dims.height;	    }        }      int width = Math.max(sWidth, nWidth);      width = Math.max(width, contentDims.width + eWidth + wWidth);      int height = Math.max(eHeight, wHeight);      height = Math.max(height, contentDims.height);      height += nHeight + sHeight;      width += insets.left + insets.right;      height += insets.top + insets.bottom;      return new Dimension(width, height);    }    /**     * This method is called when a Component is removed from the     * JInternalFrame.     *     * @param c The Component that was removed.     */    public void removeLayoutComponent(Component c)    {    }  }  /**   * This helper class is used to listen to the JDesktopPane's glassPane for   * MouseEvents. The JInternalFrame can then be selected if a click is   * detected on its children.   */  protected class GlassPaneDispatcher implements MouseInputListener  {    /** The MouseEvent target. */    private transient Component mouseEventTarget;    /** The component pressed. */    private transient Component pressedComponent;    /** The last component entered. */    private transient Component lastComponentEntered;    /** The number of presses. */    private transient int pressCount;    /**     * This method is called when the mouse enters the glass pane.     *     * @param e The MouseEvent.     */    public void mouseEntered(MouseEvent e)    {      handleEvent(e);    }    /**     * This method is called when the mouse is clicked on the glass pane.     *     * @param e The MouseEvent.     */    public void mouseClicked(MouseEvent e)    {      handleEvent(e);    }    /**     * This method is called when the mouse is dragged in the glass pane.     *     * @param e The MouseEvent.     */    public void mouseDragged(MouseEvent e)    {      handleEvent(e);    }    /**     * This method is called when the mouse exits the glass pane.     *     * @param e The MouseEvent.     */    public void mouseExited(MouseEvent e)    {      handleEvent(e);    }    /**     * This method is called when the mouse is moved in the glass pane.     *     * @param e The MouseEvent.     */    public void mouseMoved(MouseEvent e)    {      handleEvent(e);    }    /**     * This method is called when the mouse is  pressed in the glass pane.     *     * @param e The MouseEvent.     */    public void mousePressed(MouseEvent e)    {      activateFrame(frame);      handleEvent(e);    }    /**     * This method is called when the mouse is  released in the glass pane.     *     * @param e The MouseEvent.     */    public void mouseReleased(MouseEvent e)    {      handleEvent(e);    }    /**     * This method acquires a candidate component to dispatch the  MouseEvent     * to.     *     * @param me The MouseEvent to acquire a component for.     */    private void acquireComponentForMouseEvent(MouseEvent me)    {      int x = me.getX();      int y = me.getY();      // Find the candidate which should receive this event.      Component parent = frame.getContentPane();      if (parent == null)	return;      Component candidate = null;      Point p = me.getPoint();      while (candidate == null && parent != null)        {	  candidate = SwingUtilities.getDeepestComponentAt(parent, p.x, p.y);	  if (candidate == null)	    {	      p = SwingUtilities.convertPoint(parent, p.x, p.y,	                                      parent.getParent());	      parent = parent.getParent();	    }        }      // If the only candidate we found was the native container itself,      // don't dispatch any event at all.  We only care about the lightweight      // children here.      if (candidate == frame.getContentPane())	candidate = null;      // If our candidate is new, inform the old target we're leaving.      if (lastComponentEntered != null && lastComponentEntered.isShowing()          && lastComponentEntered != candidate)        {	  Point tp = SwingUtilities.convertPoint(frame.getContentPane(), x, y,	                                         lastComponentEntered);	  MouseEvent exited = new MouseEvent(lastComponentEntered,	                                     MouseEvent.MOUSE_EXITED,	                                     me.getWhen(), me.getModifiersEx(),	                                     tp.x, tp.y, me.getClickCount(),	                                     me.isPopupTrigger(),	                                     me.getButton());	  lastComponentEntered.dispatchEvent(exited);	  lastComponentEntered = null;        }      // If we have a candidate, maybe enter it.      if (candidate != null)        {	  mouseEventTarget = candidate;	  if (candidate.isLightweight() && candidate.isShowing()	      && candidate != frame.getContentPane()	      && candidate != lastComponentEntered)	    {	      lastComponentEntered = mouseEventTarget;	      Point cp = SwingUtilities.convertPoint(frame.getContentPane(),	                                             x, y, lastComponentEntered);	      MouseEvent entered = new MouseEvent(lastComponentEntered,	                                          MouseEvent.MOUSE_ENTERED,	                                          me.getWhen(),	                                          me.getModifiersEx(), cp.x,	                                          cp.y, me.getClickCount(),	                                          me.isPopupTrigger(),	                                          me.getButton());	      lastComponentEntered.dispatchEvent(entered);	    }        }      if (me.getID() == MouseEvent.MOUSE_RELEASED          || me.getID() == MouseEvent.MOUSE_PRESSED && pressCount > 0          || me.getID() == MouseEvent.MOUSE_DRAGGED)	// If any of the following events occur while a button is held down,	// they should be dispatched to the same component to which the	// original MOUSE_PRESSED event was dispatched:	//   - MOUSE_RELEASED	//   - MOUSE_PRESSED: another button pressed while the first is held down	//   - MOUSE_DRAGGED	mouseEventTarget = pressedComponent;      else if (me.getID() == MouseEvent.MOUSE_CLICKED)        {	  // Don't dispatch CLICKED events whose target is not the same as the	  // target for the original PRESSED event.	  if (candidate != pressedComponent)	    mouseEventTarget = null;	  else if (pressCount == 0)	    pressedComponent = null;        }    }    /**     * This is a helper method that dispatches the GlassPane MouseEvents to     * the proper component.     *     * @param e The AWTEvent to be dispatched. Usually an instance of     *        MouseEvent.     */    private void handleEvent(AWTEvent e)    {      if (e instanceof MouseEvent)        {	  MouseEvent me = SwingUtilities.convertMouseEvent(frame.getRootPane()	                                                        .getGlassPane(),	                                                   (MouseEvent) e,	                                                   frame.getRootPane()	                                                        .getGlassPane());	  acquireComponentForMouseEvent(me);	  // Avoid dispatching ENTERED and EXITED events twice.	  if (mouseEventTarget != null && mouseEventTarget.isShowing()	      && e.getID() != MouseEvent.MOUSE_ENTERED	      && e.getID() != MouseEvent.MOUSE_EXITED)	    {	      MouseEvent newEvt = SwingUtilities.convertMouseEvent(frame	                                                           .getContentPane(),	                                                           me,	                                                           mouseEventTarget);	      mouseEventTarget.dispatchEvent(newEvt);	      switch (e.getID())	        {		case MouseEvent.MOUSE_PRESSED:		  if (pressCount++ == 0)		    pressedComponent = mouseEventTarget;		  break;		case MouseEvent.MOUSE_RELEASED:		  // Clear our memory of the original PRESSED event, only if		  // we're not expecting a CLICKED event after this. If		  // there is a CLICKED event after this, it will do clean up.		  if (--pressCount == 0		      && mouseEventTarget != pressedComponent)		    pressedComponent = null;		  break;	        }	    }        }    }  }  /**   * This helper class listens for PropertyChangeEvents from the   * JInternalFrame.   */  public class InternalFramePropertyChangeListener    implements PropertyChangeListener  {    /**     * This method is called when one of the JInternalFrame's properties     * change.     *     * @param evt The PropertyChangeEvent.     */    public void propertyChange(PropertyChangeEvent evt)    {      if (evt.getPropertyName().equals(JInternalFrame.IS_MAXIMUM_PROPERTY))        {	  if (frame.isMaximum())	    maximizeFrame(frame);	  else	    minimizeFrame(frame);        }      else if (evt.getPropertyName().equals(JInternalFrame.IS_CLOSED_PROPERTY))	closeFrame(frame);      else if (evt.getPropertyName().equals(JInternalFrame.IS_ICON_PROPERTY))        {	  if (frame.isIcon())	    iconifyFrame(frame);	  else	    deiconifyFrame(frame);        }      else if (evt.getPropertyName().equals(JInternalFrame.IS_SELECTED_PROPERTY))        {	  if (frame.isSelected())	    activateFrame(frame);	  else	    getDesktopManager().deactivateFrame(frame);        }      else if (evt.getPropertyName().equals(JInternalFrame.ROOT_PANE_PROPERTY)               || evt.getPropertyName().equals(JInternalFrame.GLASS_PANE_PROPERTY))        {	  Component old = (Component) evt.getOldValue();	  old.removeMouseListener(glassPaneDispatcher);	  old.removeMouseMotionListener(glassPaneDispatcher);	  Component newPane = (Component) evt.getNewValue();	  newPane.addMouseListener(glassPaneDispatcher);	  newPane.addMouseMotionListener(glassPaneDispatcher);	  frame.revalidate();        }      /* FIXME: need to add ancestor properties to JComponents.      else if (evt.getPropertyName().equals(JComponent.ANCESTOR_PROPERTY))      {        if (desktopPane != null)          desktopPane.removeComponentListener(componentListener);        desktopPane = frame.getDesktopPane();        if (desktopPane != null)          desktopPane.addComponentListener(componentListener);      }      */    }  }  /**   * This helper class is the border for the JInternalFrame.   */  private class InternalFrameBorder extends AbstractBorder    implements UIResource  {    /** The width of the border. */    private static final int bSize = 5;    /** The size of the corners. */    private static final int offset = 10;    /**     * This method returns whether the border is opaque.     *     * @return Whether the border is opaque.     */    public boolean isBorderOpaque()    {      return true;    }    /**     * This method returns the insets of the border.     *     * @param c The Component to find border insets for.     *     * @return The border insets.     */    public Insets getBorderInsets(Component c)    {      return new Insets(bSize, bSize, bSize, bSize);    }    /**     * This method paints the border.     *     * @param c The Component that owns the border.     * @param g The Graphics object to paint with.     * @param x The x coordinate to paint at.     * @param y The y coordinate to paint at.     * @param width The width of the Component.     * @param height The height of the Component.     */    public void paintBorder(Component c, Graphics g, int x, int y, int width,                            int height)    {      g.translate(x, y);      Color saved = g.getColor();      Rectangle b = frame.getBounds();      Color d = c.getBackground();      g.setColor(d);      g.fillRect(0, 0, bSize, b.height);      g.fillRect(0, 0, b.width, bSize);      g.fillRect(0, b.height - bSize, b.width, bSize);      g.fillRect(b.width - bSize, 0, bSize, b.height);      int x1 = 0;      int x2 = bSize;      int x3 = b.width - bSize;      int x4 = b.width;      int y1 = 0;      int y2 = bSize;      int y3 = b.height - bSize;      int y4 = b.height;      g.setColor(Color.GRAY);      g.fillRect(0, 0, bSize, y4);      g.fillRect(0, 0, x4, bSize);      g.fillRect(0, y3, b.width, bSize);      g.fillRect(x3, 0, bSize, b.height);      g.fill3DRect(0, offset, bSize, b.height - 2 * offset, false);      g.fill3DRect(offset, 0, b.width - 2 * offset, bSize, false);      g.fill3DRect(offset, b.height - bSize, b.width - 2 * offset, bSize, false);      g.fill3DRect(b.width - bSize, offset, bSize, b.height - 2 * offset, false);      g.translate(-x, -y);      g.setColor(saved);    }  }  /**   * The MouseListener that is responsible for dragging and resizing the   * JInternalFrame in response to MouseEvents.   */  protected MouseInputAdapter borderListener;  /**   * The ComponentListener that is responsible for resizing the JInternalFrame   * in response to ComponentEvents from the JDesktopPane.   */  protected ComponentListener componentListener;  /**   * The MouseListener that is responsible for activating the JInternalFrame   * when the mouse press activates one of its descendents.   */  protected MouseInputListener glassPaneDispatcher;  /**   * The PropertyChangeListener that is responsible for listening to   * PropertyChangeEvents from the JInternalFrame.   */  protected PropertyChangeListener propertyChangeListener;  /** The InternalFrameListener that listens to the JInternalFrame. */  private transient BasicInternalFrameListener internalFrameListener;  /** The JComponent placed at the east region of the JInternalFrame. */  protected JComponent eastPane;  /** The JComponent placed at the north region of the JInternalFrame. */  protected JComponent northPane;  /** The JComponent placed at the south region of the JInternalFrame. */  protected JComponent southPane;  /** The JComponent placed at the west region of the JInternalFrame. */  protected JComponent westPane;  /**   * The Keystroke bound to open the menu.   * @deprecated   */  protected KeyStroke openMenuKey;  /** The TitlePane displayed at the top of the JInternalFrame. */  protected BasicInternalFrameTitlePane titlePane;  /** The JInternalFrame this UI is responsible for. */  protected JInternalFrame frame;  /** The LayoutManager used in the JInternalFrame. */  protected LayoutManager internalFrameLayout;  /** The JDesktopPane that is the parent of the JInternalFrame. */  private transient JDesktopPane desktopPane;  /**   * Creates a new BasicInternalFrameUI object.   *   * @param b The JInternalFrame this UI will represent.   */  public BasicInternalFrameUI(JInternalFrame b)  {  }  /**   * This method will create a new BasicInternalFrameUI for the given   * JComponent.   *   * @param b The JComponent to create a BasicInternalFrameUI for.   *   * @return A new BasicInternalFrameUI.   */  public static ComponentUI createUI(JComponent b)  {    return new BasicInternalFrameUI((JInternalFrame) b);  }  /**   * This method installs a UI for the JInternalFrame.   *   * @param c The JComponent to install this UI on.   */  public void installUI(JComponent c)  {    if (c instanceof JInternalFrame)      {	frame = (JInternalFrame) c;	internalFrameLayout = createLayoutManager();	frame.setLayout(internalFrameLayout);

⌨️ 快捷键说明

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