IconBorder.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:4k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: IconBorder.java,v 1.4 2005/10/10 18:02:56 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.border;
  22. import java.awt.Component;
  23. import java.awt.ComponentOrientation;
  24. import java.awt.Graphics;
  25. import java.awt.Graphics2D;
  26. import java.awt.Insets;
  27. import java.awt.Rectangle;
  28. import javax.swing.Icon;
  29. import javax.swing.SwingConstants;
  30. import javax.swing.border.Border;
  31. /**
  32.  * @author Amy Fowler
  33.  * @version 1.0
  34.  */
  35. public class IconBorder implements Border {
  36.     private static final int PAD = 4;
  37.     private Icon icon;
  38.     private int iconPosition;
  39.     private Rectangle iconBounds = new Rectangle();
  40.     public IconBorder() {
  41.         this(null);
  42.     }
  43.     public IconBorder(Icon validIcon) {
  44.         this(validIcon, SwingConstants.TRAILING);
  45.     }
  46.     public IconBorder(Icon validIcon, int iconPosition) {
  47.         this.icon = validIcon;
  48.         this.iconPosition = iconPosition;
  49.     }
  50.     public Insets getBorderInsets(Component c) {
  51.         int horizontalInset = icon.getIconWidth() + (2 * PAD);
  52.         int iconPosition = bidiDecodeLeadingTrailing(c.getComponentOrientation(), this.iconPosition);
  53.         if (iconPosition == SwingConstants.EAST) {
  54.             return new Insets(0, 0, 0, horizontalInset);
  55.         }
  56.         return new Insets(0, horizontalInset, 0, 0);
  57.     }
  58.     public void setIcon(Icon validIcon) {
  59.         this.icon = validIcon;
  60.     }
  61.     
  62.     public boolean isBorderOpaque() {
  63.         return false;
  64.     }
  65.     public void paintBorder(Component c, Graphics g, int x, int y, int width,
  66.         int height) {
  67.         Graphics2D g2d = (Graphics2D) g;
  68.         int iconPosition = bidiDecodeLeadingTrailing(c.getComponentOrientation(), this.iconPosition);
  69.         if (iconPosition == SwingConstants.NORTH_EAST) {
  70.             iconBounds.y = y + PAD;
  71.             iconBounds.x = x + width - PAD - icon.getIconWidth();
  72.         } else if (iconPosition == SwingConstants.EAST) {    // EAST
  73.             iconBounds.y = y
  74.                 + ((height - icon.getIconHeight()) / 2);
  75.             iconBounds.x = x + width - PAD - icon.getIconWidth();
  76.         } else if (iconPosition == SwingConstants.WEST) {
  77.             iconBounds.y = y
  78.                 + ((height - icon.getIconHeight()) / 2);
  79.             iconBounds.x = x + PAD;
  80.         }
  81.         iconBounds.width = icon.getIconWidth();
  82.         iconBounds.height = icon.getIconHeight();
  83.         icon.paintIcon(c, g, iconBounds.x, iconBounds.y);
  84.     }
  85.     /**
  86.      * Returns EAST or WEST depending on the ComponentOrientation and 
  87.      * the given postion LEADING/TRAILING this method has no effect for other
  88.      * position values
  89.      */
  90.     private int bidiDecodeLeadingTrailing(ComponentOrientation c, int position) {
  91.         if(position == SwingConstants.TRAILING) {
  92.             if(!c.isLeftToRight()) {
  93.                 return SwingConstants.WEST;
  94.             }
  95.             return SwingConstants.EAST;
  96.         }
  97.         if(position == SwingConstants.LEADING) {
  98.             if(!c.isLeftToRight()) {
  99.                 return SwingConstants.WEST;
  100.             }
  101.             return SwingConstants.EAST;
  102.         }
  103.         return position;
  104.     }
  105. }