001: package org.gui4j.component.factory;
002:
003: import java.util.ArrayList;
004: import java.util.Arrays;
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Map;
009: import java.util.StringTokenizer;
010:
011: import org.dom4j.LElement;
012:
013: import org.gui4j.component.Gui4jJComponent;
014: import org.gui4j.component.Gui4jTableLayout;
015: import org.gui4j.core.Gui4jComponentContainer;
016: import org.gui4j.core.Gui4jComponentContainerManager;
017: import org.gui4j.core.Gui4jQualifiedComponent;
018: import org.gui4j.core.definition.Attribute;
019: import org.gui4j.core.definition.AttributeTypeEnumeration;
020: import org.gui4j.core.definition.AttributeTypeID;
021: import org.gui4j.core.definition.AttributeTypeInteger;
022: import org.gui4j.core.definition.AttributeTypeString;
023: import org.gui4j.core.swing.TableLayoutConstants;
024: import org.gui4j.core.swing.TableLayoutConstraints;
025: import org.gui4j.exception.Gui4jUncheckedException;
026: import org.gui4j.util.Filter;
027:
028: /**
029: * Syntax:<br>
030: * <tableLayout rows=$rowStr col=$colStr><br>
031: * <placeTl row="0" col="0" hAlign="left" vAlign="top" id="xxx"/><br>
032: * </tableLayout><br>
033: * rowStr="p,f,m,100.5"<br>
034: * colStr="preferred,fill,minimum,100.5"<br>
035: *
036: */
037: public final class Gui4jTableLayoutFactory extends
038: Gui4jJComponentFactory {
039: private static final String NAME = "tableLayout";
040: private static final String PLACEMENT = "placeTl";
041: private static final String ROWS = "rows";
042: private static final String COLS = "cols";
043: private static final String HSPACING = "hSpacing";
044: private static final String VSPACING = "vSpacing";
045: private static final String ROW = "row";
046: private static final String COL = "col";
047: private static final String WIDTH = "width";
048: private static final String HEIGHT = "height";
049: private static final String HALIGN = "hAlign";
050: private static final String VALIGN = "vAlign";
051: private static final String ID = "id";
052:
053: private static final Map mHAlign;
054: private static final Map mVAlign;
055:
056: static {
057: mHAlign = new HashMap();
058: mVAlign = new HashMap();
059: mHAlign.put("left", new Integer(TableLayoutConstants.LEFT));
060: mHAlign.put("center", new Integer(TableLayoutConstants.CENTER));
061: mHAlign.put("full", new Integer(TableLayoutConstants.FULL));
062: mHAlign.put("right", new Integer(TableLayoutConstants.RIGHT));
063: mVAlign.put("top", new Integer(TableLayoutConstants.TOP));
064: mVAlign.put("center", new Integer(TableLayoutConstants.CENTER));
065: mVAlign.put("full", new Integer(TableLayoutConstants.FULL));
066: mVAlign.put("bottom", new Integer(TableLayoutConstants.BOTTOM));
067: }
068:
069: /* (non-Javadoc)
070: * @see org.gui4j.component.factory.Gui4jJComponentFactory#defineGui4jJComponentBy(org.gui4j.core.Gui4jComponentContainer, java.lang.String, org.dom4j.LElement)
071: */
072: protected Gui4jJComponent defineGui4jJComponentBy(
073: Gui4jComponentContainer gui4jComponentContainer, String id,
074: LElement e) {
075: String rowStr = gui4jComponentContainer.getAttrValueReplaceAll(
076: e, ROWS);
077: String colStr = gui4jComponentContainer.getAttrValueReplaceAll(
078: e, COLS);
079: double[][] size = new double[2][];
080: size[0] = parseRowColStr(colStr, gui4jComponentContainer
081: .getConfigurationName(), Gui4jComponentContainerManager
082: .getLineNumber(e.attribute(COLS)));
083: size[1] = parseRowColStr(rowStr, gui4jComponentContainer
084: .getConfigurationName(), Gui4jComponentContainerManager
085: .getLineNumber(e.attribute(ROWS)));
086: Gui4jTableLayout gui4jTableLayout = new Gui4jTableLayout(
087: gui4jComponentContainer, size, id);
088: gui4jTableLayout.definePropertySetter(HSPACING,
089: getGui4jAccessInstance(Integer.TYPE, gui4jTableLayout,
090: e, HSPACING));
091: gui4jTableLayout.definePropertySetter(VSPACING,
092: getGui4jAccessInstance(Integer.TYPE, gui4jTableLayout,
093: e, VSPACING));
094:
095: List children = e.elements();
096: List errorList = new ArrayList();
097: for (Iterator it = children.iterator(); it.hasNext();) {
098: try {
099: LElement child = (LElement) it.next();
100: gui4jComponentContainer.autoExtend(child);
101: int row = getIntValue(gui4jComponentContainer, child,
102: ROW);
103: int col = getIntValue(gui4jComponentContainer, child,
104: COL);
105: int width = getIntValue(gui4jComponentContainer, child,
106: WIDTH, 1);
107: int height = getIntValue(gui4jComponentContainer,
108: child, HEIGHT, 1);
109:
110: int hAlign = getMapValue(gui4jComponentContainer,
111: child, HALIGN, mHAlign,
112: TableLayoutConstants.FULL);
113: int vAlign = getMapValue(gui4jComponentContainer,
114: child, VALIGN, mVAlign,
115: TableLayoutConstants.FULL);
116:
117: String gui4jId = child.attributeValue(ID);
118: Gui4jQualifiedComponent gui4jQualifiedComponent;
119: if (gui4jId == null) {
120: if (child.elements().isEmpty()) {
121: Object[] args = { PLACEMENT };
122: throw new Gui4jUncheckedException.ResourceError(
123: gui4jComponentContainer
124: .getConfigurationName(),
125: Gui4jComponentContainerManager
126: .getLineNumber(child),
127: RESOURCE_ERROR_element_must_contain_gui4jComponent,
128: args);
129: }
130: LElement gui4jElement = (LElement) child.elements()
131: .iterator().next();
132: gui4jQualifiedComponent = gui4jComponentContainer
133: .extractGui4jComponent(gui4jElement);
134: } else {
135: gui4jQualifiedComponent = gui4jComponentContainer
136: .getGui4jQualifiedComponent(gui4jId);
137: }
138:
139: gui4jTableLayout.addPlacement(
140: new TableLayoutConstraints(col, row, col
141: + width - 1, row + height - 1, hAlign,
142: vAlign), gui4jQualifiedComponent);
143: } catch (Throwable t) {
144: errorList.add(t);
145: }
146: }
147: checkErrorList(errorList);
148: return gui4jTableLayout;
149: }
150:
151: private double[] parseRowColStr(String str,
152: String configurationName, int line) {
153: StringTokenizer tokenizer = new StringTokenizer(str, ",");
154: List values = new ArrayList();
155: boolean ok = true;
156: while (tokenizer.hasMoreTokens()) {
157: String token = tokenizer.nextToken().trim();
158: if ("preferred".equalsIgnoreCase(token)
159: || "p".equalsIgnoreCase(token)) {
160: values.add(new Double(TableLayoutConstants.PREFERRED));
161: } else if ("minimum".equalsIgnoreCase(token)
162: || "m".equalsIgnoreCase(token)) {
163: values.add(new Double(TableLayoutConstants.MINIMUM));
164: } else if ("fill".equalsIgnoreCase(token)
165: || "f".equalsIgnoreCase(token)) {
166: values.add(new Double(TableLayoutConstants.FILL));
167: } else if (token.length() > 0) {
168: try {
169: values.add(Double.valueOf(token));
170: } catch (Throwable t) {
171: ok = false;
172: }
173: }
174: }
175: double[] val = new double[values.size()];
176: for (int i = 0; i < values.size(); i++) {
177: val[i] = ((Double) values.get(i)).doubleValue();
178: }
179: if (!ok) {
180: Object[] args = { str };
181: throw new Gui4jUncheckedException.ResourceError(
182: configurationName, line,
183: RESOURCE_ERROR_tableLayout_invalid_col_row_str,
184: args);
185: }
186: return val;
187: }
188:
189: public SubElement getSubElement(String elementName) {
190: if (PLACEMENT.equals(elementName)) {
191: return SubElement.optional(SubElement.gui4jComponent());
192: }
193: if (NAME.equals(elementName)) {
194: SubElement place = SubElement.getInstance(PLACEMENT);
195: return SubElement.star(place);
196: }
197: return null;
198: }
199:
200: public void addInnerAttributes(String elementName, List list) {
201: if (PLACEMENT.equals(elementName)) {
202: Attribute[] attributesSubElement = {
203: new Attribute(ROW, new AttributeTypeInteger(),
204: REQUIRED, false),
205: new Attribute(COL, new AttributeTypeInteger(),
206: REQUIRED, false),
207: new Attribute(WIDTH, new AttributeTypeInteger(),
208: IMPLIED, false),
209: new Attribute(HEIGHT, new AttributeTypeInteger(),
210: IMPLIED, false),
211: new Attribute(HALIGN, new AttributeTypeEnumeration(
212: mHAlign), IMPLIED, false),
213: new Attribute(VALIGN, new AttributeTypeEnumeration(
214: mVAlign), IMPLIED, false),
215: new Attribute(ID, new AttributeTypeID(), IMPLIED,
216: false), };
217: list.addAll(Arrays.asList(attributesSubElement));
218: }
219: }
220:
221: public void addToplevelAttributes(List attrList, Filter filter) {
222: super .addToplevelAttributes(attrList, filter);
223: if (filter == null
224: || filter.takeIt(Gui4jTableLayoutFactory.class)) {
225: attrList.add(new Attribute(ROWS, new AttributeTypeString(),
226: REQUIRED, false));
227: attrList.add(new Attribute(COLS, new AttributeTypeString(),
228: REQUIRED, false));
229: attrList.add(new Attribute(HSPACING,
230: new AttributeTypeInteger(), IMPLIED, false));
231: attrList.add(new Attribute(VSPACING,
232: new AttributeTypeInteger(), IMPLIED, false));
233: }
234: }
235:
236: public String[] getInnerElements() {
237: String[] elems = { PLACEMENT };
238: return elems;
239: }
240:
241: /**
242: * @see org.gui4j.core.Gui4jComponentFactory#getName()
243: */
244: public String getName() {
245: return NAME;
246: }
247:
248: }
|