001: package net.xoetrope.xui.helper;
002:
003: import java.awt.Component;
004: import java.awt.Container;
005:
006: import net.xoetrope.xui.XPage;
007: import net.xoetrope.xui.style.XStyleFactory;
008:
009: /**
010: * Extends XStyleFactory by adding a caption component whenever a
011: * new component is added. This is intended for use with components such as
012: * TextFields, Choice and so on that normally have an associated label or caption
013: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
014: * License: see license.txt
015: * @version $Revision: 1.18 $
016: */
017: public class BuddyHelper {
018: private XStyleFactory styleFactory;
019: private boolean hasSuffix = false;
020:
021: /**
022: * Constructs a new BuddyHelper.
023: * @param sf the XStyleFactory used to create the new components.
024: */
025: public BuddyHelper(XStyleFactory sf) {
026: styleFactory = sf;
027: }
028:
029: /**
030: * Pass the addNamedComponent call to the XStyleFactory base Class
031: * @param type The type of object being constructed
032: * @param x1 The x coordinate of the new label component
033: * @param x2 The x coordinate of the new primary component
034: * @param y The y coordinate of the new component
035: * @param w The total width of the new components
036: * @param h The height of the new component
037: * @param caption The text of the associated label
038: * @param txt The text/content of the new component
039: * @param style The name of the style to apply
040: */
041: public Component addComponent(String type, int x1, int x2, int y,
042: int w, int h, String caption, String txt, String style) {
043: styleFactory.addComponent(XPage.LABEL, x1, y, x2 - x1 - 2, h,
044: caption, style);
045: return styleFactory.addComponent(type, x2, y, w - (x2 - x1), h,
046: txt, style);
047: }
048:
049: /**
050: * Pass the addNamedComponent call to the XStyleFactory base Class
051: * @param type The type of object being constructed
052: * @param x1 The x coordinate of the new label component
053: * @param x2 The x coordinate of the new primary component
054: * @param x3 The x coordinate of the suffix label
055: * @param y The y coordinate of the new component
056: * @param w The total width of the new components
057: * @param h The height of the new component
058: * @param caption The text of the associated label
059: * @param txt The text/content of the new component
060: * @param style The name of the style to apply
061: */
062: public Component addComponent(String type, int x1, int x2, int x3,
063: int y, int w, int h, String caption, String txt,
064: String suffix, String style) {
065: hasSuffix = true;
066: styleFactory.addComponent(XPage.LABEL, x1, y, x2 - x1 - 2, h,
067: caption, style);
068: Component c = styleFactory.addComponent(type, x2, y, x3 - x2
069: - 2, h, txt, style);
070: styleFactory.addComponent(XPage.LABEL, x3, y, x1 + w - x3, h,
071: suffix, style);
072: return c;
073: }
074:
075: /**
076: * Pass the addNamedComponent call to the XStyleFactory base Class
077: * @param type The type of object being constructed
078: * @param x The x coordinate of the new component
079: * @param y The y coordinate of the new component
080: * @param w The total width of the new components
081: * @param h The height of the new component
082: * @param caption The text of the associated label
083: * @param txt The text/content of the new component
084: * @param style The name of the style to apply
085: */
086: public Component addComponent(String type, int x, int y, int w,
087: int h, String caption, String txt, String style) {
088: styleFactory.addComponent(XPage.LABEL, x, y, w / 2 - 2, h,
089: caption, style);
090: return styleFactory.addComponent(type, x + w / 2, y, w / 2, h,
091: txt, style);
092: }
093:
094: /**
095: * Pass the addNamedComponent call to the XStyleFactory base Class
096: * @param className The name of the class to be constructed
097: * @param x The x coordinate of the new component
098: * @param y The y coordinate of the new component
099: * @param w The total width of the new components
100: * @param h The height of the new component
101: * @param caption The text of the associated label
102: * @param style The name of the style to apply
103: */
104: public Component addComponent(String className, int x, int y,
105: int w, int h, String caption, String style) {
106: styleFactory.addComponent(XPage.LABEL, x, y, w / 2 - 2, h,
107: caption, style);
108: return styleFactory.addClass(className, x + w / 2, y, w / 2, h,
109: style);
110: }
111:
112: /**
113: * Gets the associated buddy label. The buddy is assumed
114: * @param comp the component for which the buddy label was added
115: */
116: public Component getBuddy(Component comp) {
117: try {
118: Container cont = comp.getParent();
119: Component[] comps = cont.getComponents();
120: int numComps = comps.length;
121: for (int i = 1; i < numComps; i++) {
122: if (cont.getComponent(i) == comp)
123: return cont.getComponent(i - 1);
124: }
125: } catch (Exception e) {
126: }
127:
128: return null;
129: }
130:
131: /**
132: * Get a component following the referenced component. This method can be used
133: * to retrieve the suffix of a component added using the addComponent method
134: * for 3 components.
135: * @param comp the main component
136: * @param item the offset, normally 1
137: * @return the suffix component
138: */
139: public Component getBuddySuffix(Component comp, int item) {
140: try {
141: Container cont = comp.getParent();
142: Component[] comps = cont.getComponents();
143: int numComps = comps.length;
144: for (int i = 1; i < numComps; i++) {
145: if (cont.getComponent(i) == comp)
146: return cont.getComponent(i + item);
147: }
148: } catch (Exception e) {
149: }
150:
151: return null;
152: }
153:
154: /**
155: * Set the visible state for the component and its buddy
156: * @param comp the component for which the buddy label was added
157: * @param b true to show the component, false to hide
158: */
159: public void setVisible(Component comp, boolean b) {
160: comp.setVisible(b);
161: getBuddy(comp).setVisible(b);
162: if (hasSuffix) {
163: getBuddySuffix(comp, 1).setVisible(b);
164: }
165: }
166:
167: /**
168: * Set the enabled state for the component and its buddy
169: * @param comp the component for which the buddy label was added
170: * @param b true to enable the component, false to disable
171: */
172: public void setEnabled(Component comp, boolean b) {
173: comp.setEnabled(b);
174: getBuddy(comp).setEnabled(b);
175: if (hasSuffix) {
176: getBuddySuffix(comp, 1).setEnabled(b);
177: }
178: }
179: }
|