001: package com.jidesoft.swing;
002:
003: import javax.swing.*;
004: import java.awt.*;
005: import java.util.ArrayList;
006: import java.util.List;
007:
008: /**
009: * This util class has several methods related to <code>Overlayable</code>.
010: */
011: public class OverlayableUtils {
012: /**
013: * Gets the overlayable associated with this component and its parents.
014: * This method will find the first overlayable that contains the component or its parents.
015: *
016: * @param component the component.
017: * @return the overlayable.
018: */
019: public static Overlayable getOverlayable(JComponent component) {
020: Container parent = component;
021: while (true) {
022: Object o = ((JComponent) parent)
023: .getClientProperty(DefaultOverlayable.CLIENT_PROPERTY_OVERLAYABLE);
024: if (o instanceof Overlayable) {
025: return (Overlayable) o;
026: }
027: parent = parent.getParent();
028: if (!(parent instanceof JComponent)) {
029: break;
030: }
031: }
032: return null;
033: }
034:
035: /**
036: * Gets all overlayables associated with this component and its parents.
037: * Different from {@link #getOverlayable(javax.swing.JComponent)}, this method
038: * will find the all overlayables that contain the component or its parents.
039: *
040: * @param component the component
041: * @return all the overlayables.
042: */
043: public static Overlayable[] getAllOverlayables(JComponent component) {
044: List<Overlayable> list = new ArrayList<Overlayable>();
045: Container parent = component;
046: while (true) {
047: Object o = ((JComponent) parent)
048: .getClientProperty(DefaultOverlayable.CLIENT_PROPERTY_OVERLAYABLE);
049: if (o instanceof Overlayable) {
050: if (!list.contains(o)) {
051: list.add((Overlayable) o);
052: }
053: }
054: parent = parent.getParent();
055: if (parent == null) {
056: break;
057: }
058: }
059: return list.toArray(new Overlayable[list.size()]);
060: }
061:
062: /**
063: * Repaints the overlayable component associated with component.
064: * Because the overlay component is shown above the component and its child components,
065: * if any of the components repaint, the overlay component will be coverred if the overlay component
066: * doesn't know to repaint immediately.
067: * Due to way Swing repaintManager works, there isn't any other better way
068: * to solve the issue other than the component has code to trigger the repaint
069: * of the overlay component. That's one reason we provide this way to repaint the overlay component easily.
070: * <p/>
071: * See below for an example of how to prepare the component to be ready for the overlayable.
072: * <pre><code>
073: * public OverlayTextField() {
074: * ...
075: * public void repaint(long tm, int x, int y, int width, int height) {
076: * super.repaint(tm, x, y, width, height);
077: * OverlayableUtils.repaintOverlayable(this);
078: * }
079: * </code></pre>
080: *
081: * @param component the component that has an overyable.
082: */
083: public static void repaintOverlayable(JComponent component) {
084: Overlayable overlayable = getOverlayable(component);
085: if (overlayable != null && overlayable instanceof Component) {
086: ((Component) overlayable).repaint();
087: }
088: }
089:
090: /**
091: * Repaints all the overlayables associated with the component or its parents.
092: *
093: * @param component the component.
094: */
095: public static void repaintAllOverlayables(JComponent component) {
096: Overlayable[] overlayables = getAllOverlayables(component);
097: for (Overlayable overlayable : overlayables) {
098: if (overlayable != null && overlayable instanceof Component) {
099: ((Component) overlayable).repaint();
100: }
101: }
102: }
103:
104: /**
105: * Gets the predefined icon that can be used as the overlay icon for the Swing component.
106: * Available icon names are
107: * <ul>
108: * <li>{@link OverlayableIconsFactory#CORRECT}
109: * <li>{@link OverlayableIconsFactory#ERROR}
110: * <li>{@link OverlayableIconsFactory#ATTENTION}
111: * <li>{@link OverlayableIconsFactory#INFO}
112: * <li>{@link OverlayableIconsFactory#QUESTION}
113: * </ul>
114: *
115: * @param name name defined in {@link com.jidesoft.swing.OverlayableIconsFactory}.
116: * @return the icon
117: */
118: public static Icon getPredefinedOverlayIcon(String name) {
119: return OverlayableIconsFactory.getImageIcon(name);
120: }
121: }
|