001: package net.xoetrope.builder.editor.components;
002:
003: import java.util.Vector;
004: import java.awt.Component;
005: import net.xoetrope.builder.editor.XComponentSizer;
006: import net.xoetrope.xui.style.XStyle;
007: import net.xoetrope.xui.style.XStyleManager;
008: import java.awt.Font;
009: import java.awt.Color;
010: import net.xoetrope.xui.XProjectManager;
011:
012: /**
013: * A helper to help in the manipulation of multiple components
014: * <p> Copyright (c) Xoetrope Ltd., 2002-2003</p>
015: * <p> $Revision: 1.5 $</p>
016: */
017: public class MultipleSelectionHelper extends PropertyHelper {
018: protected final static String propertyName[] = { "Type", "Name",
019: "Left", "Top", "Width", "Height", "Style" };
020: protected final static int numBasicProperties = 7;
021:
022: protected final static int LEFT_VALUE = 0;
023: protected final static int TOP_VALUE = 1;
024: protected final static int WIDTH_VALUE = 2;
025: protected final static int HEIGHT_VALUE = 3;
026:
027: private Vector selectedComponents;
028:
029: /**
030: * A helper for multiple component types
031: */
032: public MultipleSelectionHelper() {
033: }
034:
035: /**
036: * Get the class name for this component type
037: * @return
038: */
039: public String getClassName() {
040: return Vector.class.getName();
041: }
042:
043: /**
044: * Get the friendly type name for this component
045: * @return "Multiple"
046: */
047: public String getComponentType() {
048: return "Multiple";
049: }
050:
051: public void setSelectedComponents(Vector comps) {
052: selectedComponents = comps;
053: }
054:
055: /**
056: * Get the value of the property exposed by the component
057: * @param comp the component instance
058: * @param i the component property index
059: * @return the value
060: */
061: public String getPropertyValue(Component comp, int i) {
062: switch (i) {
063: case 0: {
064: return "Multiple";
065: }
066: case 1:
067: return "Multiple";
068: case 2:
069: return new Integer(getValue(LEFT_VALUE)).toString();
070: case 3:
071: return new Integer(getValue(TOP_VALUE)).toString();
072: case 4:
073: return new Integer(getValue(WIDTH_VALUE)).toString();
074: case 5:
075: return new Integer(getValue(HEIGHT_VALUE)).toString();
076: case 6:
077: return "";
078: }
079: return null;
080: }
081:
082: /**
083: * Set the value of the property exposed by the component
084: * @param comp the component instance
085: * @param i the component property index
086: * @return the value
087: */
088: public void setPropertyValue(Component comp, int i, String value) {
089: switch (i) {
090: case 0:
091: break;
092: case 1:
093: comp.setName(value);
094: break;
095: case 2:
096: comp.setLocation(new Integer(value).intValue(), comp
097: .getLocation().y);
098: break;
099: case 3:
100: comp.setLocation(comp.getLocation().x, new Integer(value)
101: .intValue());
102: break;
103: case 4:
104: comp.setSize(new Integer(value).intValue(),
105: comp.getSize().height);
106: break;
107: case 5:
108: comp.setSize(comp.getSize().width, new Integer(value)
109: .intValue());
110: break;
111: case 6: {
112: XStyle style = XProjectManager.getStyleManager().getStyle(
113: value);
114: int fontStyle = 0;
115: if (style.getStyleAsInt(XStyle.FONT_WEIGHT) == 1)
116: fontStyle = Font.BOLD;
117: if (style.getStyleAsInt(XStyle.FONT_ITALIC) == 1)
118: fontStyle = fontStyle | Font.ITALIC;
119:
120: Font font = new Font(style
121: .getStyleAsString(XStyle.FONT_FACE), fontStyle,
122: style.getStyleAsInt(XStyle.FONT_SIZE));
123: Color fgColor = style.getStyleAsColor(XStyle.COLOR_FORE);
124: Color bkColor = style.getStyleAsColor(XStyle.COLOR_BACK);
125:
126: setForeground(fgColor);
127: setBackground(bkColor);
128: setFont(font);
129: }
130: break;
131: }
132: }
133:
134: private int getValue(int type) {
135: int value = 100000;
136: if ((type == WIDTH_VALUE) || (type == HEIGHT_VALUE))
137: value = -100000;
138:
139: if (selectedComponents == null)
140: return 0;
141:
142: int numComponents = selectedComponents.size();
143: for (int i = 0; i < numComponents; i++) {
144: if (type == LEFT_VALUE)
145: value = Math.min(value,
146: ((XComponentSizer) selectedComponents
147: .elementAt(i)).getTarget()
148: .getLocation().x);
149: else if (type == TOP_VALUE)
150: value = Math.min(value,
151: ((XComponentSizer) selectedComponents
152: .elementAt(i)).getTarget()
153: .getLocation().y);
154: else if (type == WIDTH_VALUE)
155: value = Math
156: .max(value,
157: ((XComponentSizer) selectedComponents
158: .elementAt(i)).getTarget()
159: .getSize().width);
160: else if (type == HEIGHT_VALUE)
161: value = Math
162: .max(value,
163: ((XComponentSizer) selectedComponents
164: .elementAt(i)).getTarget()
165: .getSize().height);
166: }
167:
168: return value;
169: }
170:
171: private void setValue(int type, int newValue) {
172: int numComponents = selectedComponents.size();
173: for (int i = 0; i < numComponents; i++) {
174: XComponentSizer sizer = ((XComponentSizer) selectedComponents
175: .elementAt(i));
176: if (type == LEFT_VALUE)
177: sizer.setLocation(newValue, sizer.getLocation().y);
178: else if (type == TOP_VALUE)
179: sizer.setLocation(sizer.getLocation().x, newValue);
180: else if (type == WIDTH_VALUE)
181: sizer.setSize(newValue, sizer.getSize().height);
182: else if (type == HEIGHT_VALUE)
183: sizer.setSize(sizer.getSize().width, newValue);
184:
185: sizer.repositionTarget();
186: }
187: }
188:
189: private void setForeground(Color fgColor) {
190: int numComponents = selectedComponents.size();
191: for (int i = 0; i < numComponents; i++) {
192: XComponentSizer sizer = ((XComponentSizer) selectedComponents
193: .elementAt(i));
194: sizer.getTarget().setForeground(fgColor);
195: }
196: }
197:
198: private void setBackground(Color bkColor) {
199: int numComponents = selectedComponents.size();
200: for (int i = 0; i < numComponents; i++) {
201: XComponentSizer sizer = ((XComponentSizer) selectedComponents
202: .elementAt(i));
203: sizer.getTarget().setBackground(bkColor);
204: }
205: }
206:
207: private void setFont(Font font) {
208: int numComponents = selectedComponents.size();
209: for (int i = 0; i < numComponents; i++) {
210: XComponentSizer sizer = ((XComponentSizer) selectedComponents
211: .elementAt(i));
212: sizer.getTarget().setFont(font);
213: }
214: }
215: }
|