001: package com.xoetrope.carousel.builder;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.Map;
006: import javax.swing.JComponent;
007: import javax.swing.event.TableModelListener;
008: import javax.swing.table.TableModel;
009: import net.xoetrope.xui.validation.XValidator;
010:
011: /**
012: *
013: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
014: * the GNU Public License (GPL), please see license.txt for more details. If
015: * you make commercial use of this software you must purchase a commercial
016: * license from Xoetrope.</p>
017: */
018: // TODO: implementation of TableModel can be removed
019: public class XFormGeneratorInfo implements TableModel {
020: protected ArrayList<HashMap<String, Object>> fieldList;
021: protected HashMap<String, String> defaultProperties;
022: protected HashMap<String, String> generationParameters;
023: protected ArrayList<String> propertyList;
024:
025: /**
026: * Creates a new instance of XFormGeneratorInfo
027: */
028: public XFormGeneratorInfo() {
029: fieldList = new ArrayList<HashMap<String, Object>>();
030: propertyList = new ArrayList<String>();
031:
032: generationParameters = new HashMap<String, String>();
033: generationParameters.put("numColumns", Integer.toString(1));
034:
035: generationParameters.put("includeTitle", "true");
036: generationParameters.put("includeNavigation", "false");
037:
038: generationParameters.put("rowHeight", Integer
039: .toString(XFormGenerator.ROW_HEIGHT));
040: generationParameters.put("colWidth", Integer
041: .toString(XFormGenerator.COL_WIDTH));
042: generationParameters.put("padding", Integer
043: .toString(XFormGenerator.PADDING));
044: generationParameters.put("panelPadding", Integer
045: .toString(XFormGenerator.PANEL_PADDING));
046: generationParameters.put("suffixWidth", Integer
047: .toString(XFormGenerator.SUFFIX_WIDTH));
048: generationParameters.put("panelArc", Integer
049: .toString(XFormGenerator.PANEL_ARC));
050: generationParameters.put("panelOpaque", Boolean
051: .toString(XFormGenerator.PANEL_OPAQUE));
052:
053: defaultProperties = new HashMap<String, String>();
054:
055: defaultProperties.put("visible", "true");
056: defaultProperties.put("enabled", "true");
057: defaultProperties.put("readWrite", "true");
058:
059: defaultProperties.put("component", "Edit");
060: defaultProperties.put("prefixComponent", "Label");
061: defaultProperties.put("suffixComponent", null);
062: defaultProperties.put("validation", null);
063: defaultProperties.put("suffix", null);
064:
065: propertyList.add("name");
066: propertyList.add("prefix");
067: propertyList.add("suffix");
068: propertyList.add("component");
069: propertyList.add("prefixComponent");
070: propertyList.add("suffixComponent");
071: propertyList.add("format");
072: propertyList.add("visible");
073: propertyList.add("enabled");
074: propertyList.add("readWrite");
075: propertyList.add("validation");
076: }
077:
078: public ArrayList<String> getPropertyList() {
079: return propertyList;
080: }
081:
082: public HashMap<String, Object> getFieldProperties(int idx) {
083: return fieldList.get(idx);
084: }
085:
086: /**
087: * Add a new field
088: * @param name the field name
089: * @return true if the new field is added successfully or false otherwise
090: */
091: public boolean addField(String name) {
092: if ((name == null) || (name.trim().length() == 0))
093: return false;
094:
095: for (HashMap<String, Object> properties : fieldList) {
096: if (properties.get("name").equals(name))
097: return false;
098: }
099:
100: HashMap<String, Object> properties = new HashMap<String, Object>();
101: properties.put("name", name);
102:
103: // Add the default properties
104: for (Map.Entry<String, String> prop : defaultProperties
105: .entrySet())
106: properties.put(prop.getKey(), prop.getValue());
107:
108: fieldList.add(properties);
109: return true;
110: }
111:
112: /**
113: * Get the field name
114: * @param idx index
115: * @return the name of the field
116: */
117: public String getFieldName(int idx) {
118: return (String) fieldList.get(idx).get("name");
119: }
120:
121: /**
122: * Remove a field
123: * @param name the field name
124: * @return true if the new field is removed successfully or false otherwise
125: */
126: public boolean removeField(String name) {
127: for (HashMap<String, Object> properties : fieldList) {
128: if (properties.get("name").equals(name)) {
129: fieldList.remove(properties);
130: return true;
131: }
132: }
133:
134: return false;
135: }
136:
137: /**
138: * Moves the properties from position idx1 to idx2 and from idx2
139: * to idx1
140: * @return 1 if the properties have been switched, 0 otherwise
141: */
142: private int switchProperties(int idx1, int idx2) {
143: if ((idx1 < 0) || (idx1 >= fieldList.size()))
144: return 0;
145: if ((idx2 < 0) || (idx2 >= fieldList.size()))
146: return 0;
147: HashMap prop1 = fieldList.get(idx1);
148: HashMap prop2 = fieldList.get(idx2);
149: fieldList.set(idx1, prop2);
150: fieldList.set(idx2, prop1);
151: return 1;
152: }
153:
154: /**
155: * Moves the specified field one position up or down.
156: * @param fieldName the name of the field to be moved
157: * @param moveUp true - move up, false - move down
158: * @return -1 - field not found
159: * 0 - field found but couldn't be moved
160: * 1 - field found and moved
161: */
162: protected int moveFieldHlp(String fieldName, boolean moveUp) {
163: int idx = 0;
164: for (HashMap<String, Object> properties : fieldList) {
165:
166: // we found the right field
167: if (properties.get("name").equals(fieldName))
168: return switchProperties(idx, idx + (moveUp ? -1 : 1));
169:
170: // check the children
171: XFormGeneratorInfo childFormInfo = (XFormGeneratorInfo) properties
172: .get("childPanel");
173: int status = (childFormInfo != null ? childFormInfo
174: .moveFieldHlp(fieldName, moveUp) : -1);
175:
176: // terminate if the field has been found
177: if (status != -1)
178: return status;
179:
180: idx++;
181: }
182: return -1;
183: }
184:
185: /**
186: * Moves the specified field.
187: * @param fieldName the name of the field to be moved
188: * @param moveUp true to move the field up, false to move it down
189: * @return true if the field is moved successfully or false otherwise
190: */
191: public boolean moveField(String fieldName, boolean moveUp) {
192: return (moveFieldHlp(fieldName, moveUp) == 1);
193: }
194:
195: /**
196: * Set the value of a property
197: * @param fieldName the name of a field
198: * @param propertyName the property name
199: * @param value te new property value
200: * @return true if the property is set, otherwise false
201: */
202: public boolean setProperty(String fieldName, String propertyName,
203: Object value) {
204: for (HashMap<String, Object> properties : fieldList) {
205: if (properties.get("name").equals(fieldName)) {
206: properties.put(propertyName, value);
207:
208: if (!propertyList.contains(propertyName))
209: propertyList.add(propertyName);
210:
211: return true;
212: }
213: }
214:
215: return false;
216: }
217:
218: /**
219: * Get the value of a property
220: * @param fieldName the name of a field
221: * @param propertyName the property name
222: * @return the property value or null if no proeprty value is found
223: */
224: public Object getProperty(String fieldName, String propertyName) {
225: for (HashMap<String, Object> properties : fieldList) {
226: if (properties.get("name").equals(fieldName)) {
227: return properties.get(propertyName);
228: }
229: }
230:
231: return null;
232: }
233:
234: /**
235: * Gets the total number of fields
236: * @return the number of fields
237: */
238: public int getNumFields() {
239: return fieldList.size();
240: }
241:
242: /**
243: * Gets the number of enabled fields
244: * @return the number of enabled fields
245: */
246: public int getNumEnabledFields() {
247: int numEnabled = 0;
248: for (HashMap<String, Object> properties : fieldList)
249: if (Boolean
250: .parseBoolean((String) properties.get("enabled")))
251: numEnabled++;
252: return numEnabled;
253: }
254:
255: /**
256: * Get the maximum number of properties for any field
257: * @return the number of properties
258: */
259: public int getNumProperties() {
260: int numProperties = 0;
261: for (HashMap<String, Object> properties : fieldList)
262: numProperties = Math.max(numProperties, properties.size());
263:
264: return numProperties;
265: }
266:
267: /**
268: * Get the number of properties for a given field
269: * @param fieldName the name of the field
270: * @return the number of properties
271: */
272: public int getNumProperties(String fieldName) {
273: for (HashMap<String, Object> properties : fieldList) {
274: if (properties.get("name").equals(fieldName)) {
275: return properties.size();
276: }
277: }
278:
279: return -1;
280: }
281:
282: /**
283: * Set the default properties added when a new field is added
284: * @param props the new set of default properties
285: */
286: public void setDefaultPropeties(HashMap<String, String> props) {
287: defaultProperties = new HashMap<String, String>();
288: for (Map.Entry<String, String> prop : props.entrySet())
289: defaultProperties.put(prop.getKey(), prop.getValue());
290: }
291:
292: /**
293: * Set a form generation parameter
294: * @param key the generation attribute key
295: * @param value the generation attribute
296: */
297: public void setGenerationParameter(String key, String value) {
298: generationParameters.put(key, value);
299: }
300:
301: /**
302: * Get a form generation parameter
303: * @param key the generation attribute key
304: * @return the generation attribute
305: */
306: public String getGenerationParameter(String key) {
307: return generationParameters.get(key);
308: }
309:
310: // TableModel methods --------------------------------------------------------
311: public int getRowCount() {
312: return getNumFields();
313: }
314:
315: public int getColumnCount() {
316: return propertyList.size();
317: }
318:
319: public String getColumnName(int columnIndex) {
320: return propertyList.get(columnIndex);
321: }
322:
323: public boolean isCellEditable(int rowIndex, int columnIndex) {
324: return true;
325: }
326:
327: public Object getValueAt(int rowIndex, int columnIndex) {
328: HashMap<String, Object> properties = fieldList.get(rowIndex);
329: String propertyName = propertyList.get(columnIndex);
330: String value = (String) properties.get(propertyName);
331: if (propertyName.equals("visible")
332: || propertyName.equals("enabled")
333: || propertyName.equals("readWrite"))
334: return Boolean.valueOf(value);
335:
336: return value;
337: }
338:
339: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
340: HashMap<String, Object> properties = fieldList.get(rowIndex);
341: String propertyName = propertyList.get(columnIndex);
342: if (aValue == null)
343: aValue = "";
344: properties.put(propertyName, aValue.toString());
345: }
346:
347: public void addTableModelListener(TableModelListener l) {
348: }
349:
350: public void removeTableModelListener(TableModelListener l) {
351: }
352:
353: public Class<?> getColumnClass(int columnIndex) {
354: String propertyName = propertyList.get(columnIndex);
355: if (propertyName.equals("visible")
356: || propertyName.equals("enabled")
357: || propertyName.equals("readWrite"))
358: return Boolean.class;
359: else if (propertyName.equals("component")
360: || propertyName.equals("prefixComponent")
361: || propertyName.equals("suffixComponent"))
362: return JComponent.class;
363: else if (propertyName.equals("validation"))
364: return XValidator.class;
365:
366: return String.class;
367: }
368:
369: public String toString() {
370: return "internal panel";
371: }
372:
373: }
|