001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.swing;
024:
025: import java.awt.Component;
026: import java.awt.Graphics;
027:
028: import javax.swing.Icon;
029:
030: /**
031: * Simple Icon implementation of using two existing icons as one side by side.
032: * <p>
033: *
034: * @author Mark A. Kobold
035: * @version 1.0
036: */
037: public class CompoundIcon implements Icon {
038:
039: private static int lastX = 0;
040: private static int lastY = 0;
041:
042: protected Icon right;
043: protected int iconGap = 2;
044: protected Icon left;
045:
046: /**
047: * Default constructor for this object.
048: * <p>
049: * Either parameter for this method can be null if need be however if both icons are null this won't show much ?
050: *
051: * @param leftIcon icon to be shown on the left side.
052: * @param rightIcon icon to be shown on the right side.
053: */
054: public CompoundIcon(Icon leftIcon, Icon rightIcon) {
055:
056: left = leftIcon;
057: right = rightIcon;
058: }
059:
060: /**
061: * @see javax.swing.Icon#paintIcon(java.awt.Component, java.awt.Graphics, int, int)
062: */
063: public void paintIcon(Component c, Graphics g, int x, int y) {
064:
065: lastX = x;
066: lastY = y;
067: if (left != null)
068: left.paintIcon(c, g, x, y);
069:
070: if (right != null) {
071: int l = (left == null ? 0 : (left.getIconWidth() + iconGap));
072: right.paintIcon(c, g, x + l, y);
073: }
074: }
075:
076: /**
077: * @see javax.swing.Icon#getIconWidth()
078: */
079: public int getIconWidth() {
080:
081: int l = (left == null ? 0 : left.getIconWidth());
082: int r = (right == null ? 0 : right.getIconWidth());
083: return l + r + iconGap;
084: }
085:
086: /**
087: * @see javax.swing.Icon#getIconHeight()
088: */
089: public int getIconHeight() {
090:
091: int l = (left == null ? 0 : left.getIconHeight());
092: int r = (right == null ? 0 : right.getIconHeight());
093:
094: return Math.max(r, l);
095: }
096:
097: /**
098: * Returns the instance of the icon drawn on thr right side.
099: * <p>
100: *
101: * @return Icon shown on the right.
102: */
103: public Icon getRightIcon() {
104:
105: return right;
106: }
107:
108: /**
109: * Returns the instance of the icon drawn on thr left side.
110: * <p>
111: *
112: * @return Icon shown on the left.
113: */
114: public Icon getLeftIcon() {
115:
116: return left;
117: }
118:
119: /**
120: * Replaces the icon on the right side.
121: * <p>
122: *
123: * @param Icon to be shown on the right.
124: */
125: public void setRightIcon(Icon icon) {
126:
127: right = icon;
128: }
129:
130: /**
131: * Replaces the icon on the left side.
132: * <p>
133: *
134: * @param Icon to be shown on the left.
135: */
136: public void setLeftIcon(Icon icon) {
137:
138: left = icon;
139: }
140:
141: /**
142: * This method is used to see if the relative coordinates are contained in the left icon.
143: * <p>
144: * If the left icon is null this method will always return false.
145: *
146: * @param x relative x coordinate of this compond icon.
147: * @param y relative y coordinate of this compond icon.
148: * @return boolean if x and y are within the bounds of the left icon.
149: */
150: public boolean isOverIcon(int x, int y) {
151:
152: int w = (left == null ? 0 : left.getIconWidth());
153: int h = (left == null ? 0 : left.getIconHeight());
154:
155: if (!((x >= lastX) && (x <= (lastX + w))))
156: return false;
157: if (!((y >= lastY) && (y <= (lastX + h))))
158: return false;
159:
160: return true;
161: }
162: }
|