001: /*
002: * $Id: IconBorder.java,v 1.6 2006/12/01 12:32:49 kleopatra Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021:
022: package org.jdesktop.swingx.border;
023:
024: import java.awt.Component;
025: import java.awt.ComponentOrientation;
026: import java.awt.Graphics;
027: import java.awt.Insets;
028: import java.awt.Rectangle;
029: import java.io.Serializable;
030:
031: import javax.swing.Icon;
032: import javax.swing.SwingConstants;
033: import javax.swing.border.Border;
034:
035: /**
036: * @author Amy Fowler
037: * @version 1.0
038: */
039: public class IconBorder implements Border, Serializable {
040:
041: private static final int PAD = 4;
042: private Icon icon;
043: private int iconPosition;
044: private Rectangle iconBounds = new Rectangle();
045:
046: public IconBorder() {
047: this (null);
048: }
049:
050: public IconBorder(Icon validIcon) {
051: this (validIcon, SwingConstants.TRAILING);
052: }
053:
054: public IconBorder(Icon validIcon, int iconPosition) {
055: this .icon = validIcon;
056: this .iconPosition = iconPosition;
057: }
058:
059: public Insets getBorderInsets(Component c) {
060: int horizontalInset = icon.getIconWidth() + (2 * PAD);
061: int iconPosition = bidiDecodeLeadingTrailing(c
062: .getComponentOrientation(), this .iconPosition);
063: if (iconPosition == SwingConstants.EAST) {
064: return new Insets(0, 0, 0, horizontalInset);
065: }
066: return new Insets(0, horizontalInset, 0, 0);
067: }
068:
069: public void setIcon(Icon validIcon) {
070: this .icon = validIcon;
071: }
072:
073: public boolean isBorderOpaque() {
074: return false;
075: }
076:
077: public void paintBorder(Component c, Graphics g, int x, int y,
078: int width, int height) {
079: int iconPosition = bidiDecodeLeadingTrailing(c
080: .getComponentOrientation(), this .iconPosition);
081: if (iconPosition == SwingConstants.NORTH_EAST) {
082: iconBounds.y = y + PAD;
083: iconBounds.x = x + width - PAD - icon.getIconWidth();
084: } else if (iconPosition == SwingConstants.EAST) { // EAST
085: iconBounds.y = y + ((height - icon.getIconHeight()) / 2);
086: iconBounds.x = x + width - PAD - icon.getIconWidth();
087: } else if (iconPosition == SwingConstants.WEST) {
088: iconBounds.y = y + ((height - icon.getIconHeight()) / 2);
089: iconBounds.x = x + PAD;
090: }
091: iconBounds.width = icon.getIconWidth();
092: iconBounds.height = icon.getIconHeight();
093: icon.paintIcon(c, g, iconBounds.x, iconBounds.y);
094: }
095:
096: /**
097: * Returns EAST or WEST depending on the ComponentOrientation and
098: * the given postion LEADING/TRAILING this method has no effect for other
099: * position values
100: */
101: private int bidiDecodeLeadingTrailing(ComponentOrientation c,
102: int position) {
103: if (position == SwingConstants.TRAILING) {
104: if (!c.isLeftToRight()) {
105: return SwingConstants.WEST;
106: }
107: return SwingConstants.EAST;
108: }
109: if (position == SwingConstants.LEADING) {
110: if (!c.isLeftToRight()) {
111: return SwingConstants.WEST;
112: }
113: return SwingConstants.EAST;
114: }
115: return position;
116: }
117:
118: }
|