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: ComponentUtil.java,v 1.25 2005/12/04 13:46:04 jesper Exp $
023: package net.infonode.gui;
024:
025: import net.infonode.gui.componentpainter.ComponentPainter;
026: import net.infonode.util.Direction;
027:
028: import javax.swing.*;
029: import java.applet.Applet;
030: import java.awt.*;
031: import java.util.ArrayList;
032:
033: public class ComponentUtil {
034: private ComponentUtil() {
035: }
036:
037: public static final Component getChildAt(Container container,
038: Point p) {
039: Component c = container.getComponentAt(p);
040: return c == null || c.getParent() != container ? null : c;
041: }
042:
043: public static final Component getVisibleChildAt(
044: Container container, Point p) {
045: for (int i = 0; i < container.getComponentCount(); i++) {
046: Component c = container.getComponent(i);
047: if (c.isVisible()
048: && c.contains(p.x - c.getX(), p.y - c.getY()))
049: return c;
050: }
051:
052: return null;
053: }
054:
055: public static final Component getChildAtLine(Container container,
056: Point p, boolean horizontal) {
057: if (horizontal) {
058: for (int i = 0; i < container.getComponentCount(); i++) {
059: Component c = container.getComponent(i);
060: if (p.x >= c.getX() && p.x < c.getX() + c.getWidth())
061: return c;
062: }
063: } else {
064: for (int i = 0; i < container.getComponentCount(); i++) {
065: Component c = container.getComponent(i);
066: if (p.y >= c.getY() && p.y < c.getY() + c.getHeight())
067: return c;
068: }
069: }
070:
071: return null;
072: }
073:
074: public static void getComponentTreePosition(Component c,
075: ArrayList pos) {
076: if (c.getParent() == null) {
077: return;
078: }
079:
080: getComponentTreePosition(c.getParent(), pos);
081:
082: pos.add(new Integer(c.getParent().getComponentCount()
083: - ComponentUtil.getComponentIndex(c)));
084: }
085:
086: public static Component findComponentUnderGlassPaneAt(Point p,
087: Component top) {
088: Component c = null;
089:
090: if (top.isShowing()) {
091: if (top instanceof RootPaneContainer)
092: c = ((RootPaneContainer) top).getLayeredPane()
093: .findComponentAt(
094: SwingUtilities.convertPoint(top, p,
095: ((RootPaneContainer) top)
096: .getLayeredPane()));
097: else
098: c = ((Container) top).findComponentAt(p);
099: }
100:
101: return c;
102: }
103:
104: public static final int getComponentIndex(Component component) {
105: if (component != null && component.getParent() != null) {
106: Container c = component.getParent();
107: for (int i = 0; i < c.getComponentCount(); i++) {
108: if (c.getComponent(i) == component)
109: return i;
110: }
111: }
112:
113: return -1;
114: }
115:
116: public static final String getBorderLayoutOrientation(Direction d) {
117: return d == Direction.UP ? BorderLayout.NORTH
118: : d == Direction.LEFT ? BorderLayout.WEST
119: : d == Direction.DOWN ? BorderLayout.SOUTH
120: : BorderLayout.EAST;
121: }
122:
123: public static Color getBackgroundColor(Component component) {
124: if (component == null)
125: return null;
126:
127: if (component instanceof BackgroundPainter) {
128: ComponentPainter painter = ((BackgroundPainter) component)
129: .getComponentPainter();
130:
131: if (painter != null) {
132: Color c = painter.getColor(component);
133:
134: if (c != null)
135: return c;
136: }
137: }
138:
139: return component.isOpaque() ? component.getBackground()
140: : getBackgroundColor(component.getParent());
141: }
142:
143: public static int countComponents(Container c) {
144: int num = 1;
145: for (int i = 0; i < c.getComponentCount(); i++) {
146: Component comp = c.getComponent(i);
147: if (comp instanceof Container)
148: num += countComponents((Container) comp);
149: else
150: num++;
151: }
152:
153: return num;
154: }
155:
156: public static int getVisibleChildrenCount(Component c) {
157: if (c == null || !(c instanceof Container))
158: return 0;
159:
160: int count = 0;
161: Container container = (Container) c;
162:
163: for (int i = 0; i < container.getComponentCount(); i++)
164: if (container.getComponent(i).isVisible())
165: count++;
166:
167: return count;
168: }
169:
170: public static Component getTopLevelAncestor(Component c) {
171: while (c != null) {
172: if (c instanceof Window || c instanceof Applet)
173: break;
174: c = c.getParent();
175: }
176: return c;
177: }
178:
179: public static boolean hasVisibleChildren(Component c) {
180: return getVisibleChildrenCount(c) > 0;
181: }
182:
183: public static boolean isOnlyVisibleComponent(Component c) {
184: return c != null && c.isVisible()
185: && getVisibleChildrenCount(c.getParent()) == 1;
186: }
187:
188: public static boolean isOnlyVisibleComponents(Component[] c) {
189: if (c != null && c.length > 0) {
190: boolean visible = getVisibleChildrenCount(c[0].getParent()) == c.length;
191: if (visible)
192: for (int i = 0; i < c.length; i++)
193: visible = visible && c[i].isVisible();
194: return visible;
195: }
196: return false;
197: }
198:
199: public static Component findFirstComponentOfType(Component comp,
200: Class c) {
201: if (c.isInstance(comp))
202: return comp;
203:
204: if (comp instanceof Container) {
205: Container container = (Container) comp;
206: for (int i = 0; i < container.getComponentCount(); i++) {
207: Component comp2 = findFirstComponentOfType(container
208: .getComponent(i), c);
209: if (comp2 != null)
210: return comp2;
211: }
212: }
213: return null;
214: }
215:
216: public static boolean isFocusable(Component c) {
217: return c.isFocusable() && c.isDisplayable() && c.isVisible()
218: && c.isEnabled();
219: }
220:
221: /**
222: * Requests focus unless the component already has focus. For some weird
223: * reason calling {@link Component#requestFocusInWindow()}when the
224: * component is focus owner changes focus owner to another component!
225: *
226: * @param component the component to request focus for
227: * @return true if the component has focus or probably will get focus,
228: * otherwise false
229: */
230: public static boolean requestFocus(Component component) {
231: /*
232: * System.out.println("Owner: " +
233: * System.identityHashCode(KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) + ", " +
234: * System.identityHashCode(component) + ", " +
235: * (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() ==
236: * component));
237: */
238: return KeyboardFocusManager.getCurrentKeyboardFocusManager()
239: .getFocusOwner() == component
240: || component.requestFocusInWindow();
241: }
242:
243: /**
244: * Requests focus for a component. If that's not possible it's
245: * {@link FocusTraversalPolicy}is checked. If that doesn't work all it's
246: * children is recursively checked with this method.
247: *
248: * @param component the component to request focus for
249: * @return the component which has focus or probably will obtain focus, null
250: * if no component will receive focus
251: */
252: public static Component smartRequestFocus(Component component) {
253: if (requestFocus(component))
254: return component;
255:
256: if (component instanceof JComponent) {
257: FocusTraversalPolicy policy = ((JComponent) component)
258: .getFocusTraversalPolicy();
259:
260: if (policy != null) {
261: Component focusComponent = policy
262: .getDefaultComponent((Container) component);
263:
264: if (focusComponent != null
265: && requestFocus(focusComponent)) {
266: return focusComponent;
267: }
268: }
269: }
270:
271: if (component instanceof Container) {
272: Component[] children = ((Container) component)
273: .getComponents();
274:
275: for (int i = 0; i < children.length; i++) {
276: component = smartRequestFocus(children[i]);
277:
278: if (component != null)
279: return component;
280: }
281: }
282:
283: return null;
284: }
285:
286: /**
287: * Calculates preferred max height for the given components without checking
288: * isVisible.
289: *
290: * @param components Components to check
291: * @return max height
292: */
293: public static int getPreferredMaxHeight(Component[] components) {
294: int height = 0;
295: for (int i = 0; i < components.length; i++) {
296: int k = (int) components[i].getPreferredSize().getHeight();
297: if (k > height)
298: height = k;
299: }
300: return height;
301: }
302:
303: /**
304: * Calculates preferred max width for the given components without checking
305: * isVisible.
306: *
307: * @param components Components to check
308: * @return max width
309: */
310: public static int getPreferredMaxWidth(Component[] components) {
311: int width = 0;
312: for (int i = 0; i < components.length; i++) {
313: int k = (int) components[i].getPreferredSize().getWidth();
314: if (k > width)
315: width = k;
316: }
317: return width;
318: }
319:
320: public static void setAllOpaque(Container c, boolean opaque) {
321: if (c instanceof JComponent) {
322: ((JComponent) c).setOpaque(opaque);
323: for (int i = 0; i < c.getComponentCount(); i++) {
324: Component comp = c.getComponent(i);
325: if (comp instanceof Container)
326: setAllOpaque((Container) comp, opaque);
327: }
328: }
329: }
330:
331: public static void validate(JComponent c) {
332: c.revalidate();
333: }
334:
335: public static void validate(Component c) {
336: if (c instanceof JComponent)
337: ((JComponent) c).revalidate();
338: else
339: c.validate();
340: }
341: }
|