swingutilities.java

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

JAVA
1,323
字号
        break;      case BOTTOM:        verticalAdjustment += (viewR.height - u.height);        break;      case CENTER:        verticalAdjustment += ((viewR.height/2) - (u.height/2));        break;      }    switch (horizontalAlignment)      {      case LEFT:        break;      case RIGHT:        horizontalAdjustment += (viewR.width - u.width);        break;      case CENTER:        horizontalAdjustment += ((viewR.width/2) - (u.width/2));        break;      }    iconR.x += horizontalAdjustment;    iconR.y += verticalAdjustment;    textR.x += horizontalAdjustment;    textR.y += verticalAdjustment;    return text;  }  /**    * Calls {@link java.awt.EventQueue.invokeLater} with the   * specified {@link Runnable}.    */  public static void invokeLater(Runnable doRun)  {    java.awt.EventQueue.invokeLater(doRun);  }  /**    * Calls {@link java.awt.EventQueue.invokeAndWait} with the   * specified {@link Runnable}.    */  public static void invokeAndWait(Runnable doRun)    throws InterruptedException,    InvocationTargetException  {    java.awt.EventQueue.invokeAndWait(doRun);  }  /**    * Calls {@link java.awt.EventQueue.isEventDispatchThread}.   */  public static boolean isEventDispatchThread()  {    return java.awt.EventQueue.isDispatchThread();  }    /**   * This method paints the given component at the given position and size.   * The component will be reparented to the container given.   *    * @param g The Graphics object to draw with.   * @param c The Component to draw   * @param p The Container to reparent to.   * @param x The x coordinate to draw at.   * @param y The y coordinate to draw at.   * @param w The width of the drawing area.   * @param h The height of the drawing area.   */  public static void paintComponent(Graphics g, Component c, Container p,                                     int x, int y, int w, int h)  {           Container parent = c.getParent();    if (parent != null)      parent.remove(c);    if (p != null)      p.add(c);        Shape savedClip = g.getClip();        g.setClip(x, y, w, h);    g.translate(x, y);    c.paint(g);        g.translate(-x, -y);    g.setClip(savedClip);  }  /**   * This method paints the given component in the given rectangle.   * The component will be reparented to the container given.   *    * @param g The Graphics object to draw with.   * @param c The Component to draw   * @param p The Container to reparent to.   * @param r The rectangle that describes the drawing area.   */    public static void paintComponent(Graphics g, Component c,                                     Container p, Rectangle r)  {    paintComponent(g, c, p, r.x, r.y, r.width, r.height);  }    /**   * This method returns the common Frame owner used in JDialogs or   * JWindow when no owner is provided.   *   * @return The common Frame    */  static Frame getOwnerFrame()  {    if (ownerFrame == null)      ownerFrame = new OwnerFrame();    return ownerFrame;  }  /**   * Checks if left mouse button was clicked.   *   * @param event the event to check   *   * @return true if left mouse was clicked, false otherwise.   */  public static boolean isLeftMouseButton(MouseEvent event)  {    return ((event.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK)	     == InputEvent.BUTTON1_DOWN_MASK);  }  /**   * Checks if middle mouse button was clicked.   *   * @param event the event to check   *   * @return true if middle mouse was clicked, false otherwise.   */  public static boolean isMiddleMouseButton(MouseEvent event)  {    return ((event.getModifiersEx() & InputEvent.BUTTON2_DOWN_MASK)	     == InputEvent.BUTTON2_DOWN_MASK);  }  /**   * Checks if right mouse button was clicked.   *   * @param event the event to check   *   * @return true if right mouse was clicked, false otherwise.   */  public static boolean isRightMouseButton(MouseEvent event)  {    return ((event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK)	     == InputEvent.BUTTON3_DOWN_MASK);  }    /**   * This frame should be used when constructing a Window/JDialog without   * a parent. In this case, we are forced to use this frame as a window's   * parent, because we simply cannot pass null instead of parent to Window   * constructor, since doing it will result in NullPointerException.   */  private static class OwnerFrame extends Frame  {    public void setVisible(boolean b)    {      // Do nothing here.     }        public boolean isShowing()    {      return true;    }  }  public static boolean notifyAction(Action action,                                     KeyStroke ks,                                     KeyEvent event,                                     Object sender,                                     int modifiers)  {    if (action != null && action.isEnabled())      {        String name = (String) action.getValue(Action.ACTION_COMMAND_KEY);        if (name == null            && event.getKeyChar() != KeyEvent.CHAR_UNDEFINED)          name = new String(new char[] {event.getKeyChar()});        action.actionPerformed(new ActionEvent(sender,                                               ActionEvent.ACTION_PERFORMED,                                               name, modifiers));        return true;      }    return false;  }  /**   * <p>Change the shared, UI-managed {@link ActionMap} for a given   * component. ActionMaps are arranged in a hierarchy, in order to   * encourage sharing of common actions between components. The hierarchy   * unfortunately places UI-managed ActionMaps at the <em>end</em> of the   * parent-pointer chain, as illustrated:</p>   *   * <pre>   *  [{@link javax.swing.JComponent#getActionMap()}]    *          --&gt; [{@link javax.swing.ActionMap}]    *     parent --&gt; [{@link javax.swing.text.KeymapActionMap}]    *       parent --&gt; [{@link javax.swing.plaf.ActionMapUIResource}]   * </pre>   *   * <p>Our goal with this method is to replace the first ActionMap along   * this chain which is an instance of {@link ActionMapUIResource}, since   * these are the ActionMaps which are supposed to be shared between   * components.</p>   *   * <p>If the provided ActionMap is <code>null</code>, we interpret the   * call as a request to remove the UI-managed ActionMap from the   * component's ActionMap parent chain.</p>   */  public static void replaceUIActionMap(JComponent component,                                         ActionMap uiActionMap)  {    ActionMap child = component.getActionMap();    if (child == null)      component.setActionMap(uiActionMap);    else      {        while(child.getParent() != null              && !(child.getParent() instanceof ActionMapUIResource))          child = child.getParent();        if (child != null)          child.setParent(uiActionMap);      }  }  /**   * <p>Change the shared, UI-managed {@link InputMap} for a given   * component. InputMaps are arranged in a hierarchy, in order to   * encourage sharing of common input mappings between components. The   * hierarchy unfortunately places UI-managed InputMaps at the   * <em>end</em> of the parent-pointer chain, as illustrated:</p>   *   * <pre>   *  [{@link javax.swing.JComponent#getInputMap()}]    *          --&gt; [{@link javax.swing.InputMap}]    *     parent --&gt; [{@link javax.swing.text.KeymapWrapper}]    *       parent --&gt; [{@link javax.swing.plaf.InputMapUIResource}]   * </pre>   *   * <p>Our goal with this method is to replace the first InputMap along   * this chain which is an instance of {@link InputMapUIResource}, since   * these are the InputMaps which are supposed to be shared between   * components.</p>   *   * <p>If the provided InputMap is <code>null</code>, we interpret the   * call as a request to remove the UI-managed InputMap from the   * component's InputMap parent chain.</p>   */  public static void replaceUIInputMap(JComponent component,                                        int condition,                                        InputMap uiInputMap)  {    InputMap child = component.getInputMap(condition);    if (child == null)      component.setInputMap(condition, uiInputMap);    else      {        while(child.getParent() != null              && !(child.getParent() instanceof InputMapUIResource))          child = child.getParent();        if (child != null)          child.setParent(uiInputMap);      }  }  /**   * Subtracts a rectangle from another and return the area as an array   * of rectangles.   * Returns the areas of rectA which are not covered by rectB.   * If the rectangles do not overlap, or if either parameter is   * <code>null</code>, a zero-size array is returned.   * @param rectA The first rectangle   * @param rectB The rectangle to subtract from the first   * @return An array of rectangles representing the area in rectA   * not overlapped by rectB   */  public static Rectangle[] computeDifference(Rectangle rectA, Rectangle rectB)  {    if (rectA == null || rectB == null)      return new Rectangle[0];    Rectangle[] r = new Rectangle[4];    int x1 = rectA.x;    int y1 = rectA.y;    int w1 = rectA.width;    int h1 = rectA.height;    int x2 = rectB.x;    int y2 = rectB.y;    int w2 = rectB.width;    int h2 = rectB.height;    // (outer box = rectA)    // -------------     // |_____0_____|    // |  |rectB|  |    // |_1|_____|_2|    // |     3     |    // -------------    int H0 = (y2 > y1) ? y2 - y1 : 0; // height of box 0    int H3 = (y2 + h2 < y1 + h1) ? y1 + h1 - y2 - h2 : 0; // height box 3    int W1 = (x2 > x1) ? x2 - x1 : 0; // width box 1    int W2 = (x1 + w1 > x2 + w2) ? x1 + w1 - x2 - w2 : 0; // w. box 2    int H12 = (H0 + H3 < h1) ? h1 - H0 - H3 : 0; // height box 1 & 2    if (H0 > 0)      r[0] = new Rectangle(x1, y1, w1, H0);    else      r[0] = null;    if (W1 > 0 && H12 > 0)      r[1] = new Rectangle(x1, y1 + H0, W1, H12);    else      r[1] = null;    if (W2 > 0 && H12 > 0)      r[2] = new Rectangle(x2 + w2, y1 + H0, W2, H12);    else      r[2] = null;    if (H3 > 0)      r[3] = new Rectangle(x1, y1 + H0 + H12, w1, H3);    else      r[3] = null;    // sort out null objects    int n = 0;    for (int i = 0; i < 4; i++)      if (r[i] != null)	n++;    Rectangle[] out = new Rectangle[n];    for (int i = 3; i >= 0; i--)      if (r[i] != null)	out[--n] = r[i];    return out;  }  /**   * Calculates the intersection of two rectangles.   *   * @param x upper-left x coodinate of first rectangle   * @param x upper-left y coodinate of first rectangle   * @param w width of first rectangle   * @param h height of first rectangle   * @param rect a Rectangle object of the second rectangle   * @throws a NullPointerException if rect is null.   *   * @return a rectangle corresponding to the intersection of the   * two rectangles. A zero rectangle is returned if the rectangles   * do not overlap.   */  public static Rectangle computeIntersection(int x, int y, int w, int h,                                              Rectangle rect)  {    int x2 = (int) rect.getX();    int y2 = (int) rect.getY();    int w2 = (int) rect.getWidth();    int h2 = (int) rect.getHeight();    int dx = (x > x2) ? x : x2;    int dy = (y > y2) ? y : y2;    int dw = (x + w < x2 + w2) ? (x + w - dx) : (x2 + w2 - dx);    int dh = (y + h < y2 + h2) ? (y + h - dy) : (y2 + h2 - dy);    if (dw >= 0 && dh >= 0)      return new Rectangle(dx, dy, dw, dh);    return new Rectangle(0, 0, 0, 0);  }    /**   * Calculates the width of a given string.   *   * @param fm the <code>FontMetrics</code> object to use   * @param str the string   *    * @return the width of the the string.   */  public static int computeStringWidth(FontMetrics fm, String str)  {    return fm.stringWidth(str);  }  /**   * Calculates the union of two rectangles.   *   * @param x upper-left x coodinate of first rectangle   * @param x upper-left y coodinate of first rectangle   * @param w width of first rectangle   * @param h height of first rectangle   * @param rect a Rectangle object of the second rectangle   * @throws a NullPointerException if rect is null.   *   * @return a rectangle corresponding to the union of the   * two rectangles. A rectangle encompassing both is returned if the   * rectangles do not overlap.   */  public static Rectangle computeUnion(int x, int y, int w, int h,                                       Rectangle rect)  {    int x2 = (int) rect.getX();    int y2 = (int) rect.getY();    int w2 = (int) rect.getWidth();    int h2 = (int) rect.getHeight();    int dx = (x < x2) ? x : x2;    int dy = (y < y2) ? y : y2;    int dw = (x + w > x2 + w2) ? (x + w - dx) : (x2 + w2 - dx);    int dh = (y + h > y2 + h2) ? (y + h - dy) : (y2 + h2 - dy);    if (dw >= 0 && dh >= 0)      return new Rectangle(dx, dy, dw, dh);    return new Rectangle(0, 0, 0, 0);  }  /**   * Tests if a rectangle contains another.   * @param a first rectangle   * @param b second rectangle   * @return true if a contains b, false otherwise   * @throws NullPointerException   */  public static boolean isRectangleContainingRectangle(Rectangle a, Rectangle b)  {    // Note: zero-size rects inclusive, differs from Rectangle.contains()    return b.width >= 0 && b.height >= 0 && b.width >= 0 && b.height >= 0           && b.x >= a.x && b.x + b.width <= a.x + a.width && b.y >= a.y           && b.y + b.height <= a.y + a.height;  }}

⌨️ 快捷键说明

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