001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: IconUtil.java,v 1.5 2005/02/16 11:28:11 jesper Exp $
023: package net.infonode.gui.icon;
024:
025: import javax.swing.*;
026: import java.awt.*;
027:
028: /**
029: * @author $Author: jesper $
030: * @version $Revision: 1.5 $
031: */
032: public class IconUtil {
033: private IconUtil() {
034: }
035:
036: public static final Icon SMALL_ICON = new Icon() {
037: public int getIconHeight() {
038: return 1;
039: }
040:
041: public int getIconWidth() {
042: return 1;
043: }
044:
045: public void paintIcon(Component c, Graphics g, int x, int y) {
046: }
047: };
048:
049: public static Icon copy(final Icon icon) {
050: return new Icon() {
051: public void paintIcon(Component c, Graphics g, int x, int y) {
052: icon.paintIcon(c, g, x, y);
053: }
054:
055: public int getIconWidth() {
056: return icon.getIconWidth();
057: }
058:
059: public int getIconHeight() {
060: return icon.getIconHeight();
061: }
062: };
063: }
064:
065: public static Icon getIcon(Object object) {
066: return object == null ? null
067: : object instanceof AbstractButton ? (Icon) ((AbstractButton) object)
068: .getIcon()
069: : object instanceof Action ? (Icon) ((Action) object)
070: .getValue(Action.SMALL_ICON)
071: : object instanceof IconProvider ? ((IconProvider) object)
072: .getIcon()
073: : null;
074: }
075:
076: public static int getIconWidth(Object object) {
077: Icon icon = getIcon(object);
078: return icon == null ? 0 : icon.getIconWidth();
079: }
080:
081: public static int getIconHeight(Object object) {
082: Icon icon = getIcon(object);
083: return icon == null ? 0 : icon.getIconHeight();
084: }
085:
086: public static int getMaxIconWidth(Object[] objects) {
087: int max = 0;
088:
089: for (int i = 0; i < objects.length; i++) {
090: int width = getIconWidth(objects[i]);
091:
092: if (width > max) {
093: max = width;
094: }
095: }
096:
097: return max;
098: }
099:
100: }
|