001: package com.xoetrope.carousel.builder;
002:
003: import java.awt.Component;
004: import java.awt.Dimension;
005: import java.awt.Point;
006: import javax.swing.JComponent;
007: import javax.swing.SwingUtilities;
008: import net.xoetrope.editor.project.XEditorProject;
009: import net.xoetrope.editor.project.pages.PageDesigner;
010: import net.xoetrope.editor.project.pages.XPageResource;
011: import net.xoetrope.editor.project.pages.components.ComponentHelper;
012: import net.xoetrope.editor.project.pages.components.PropertyHelper;
013: import net.xoetrope.editor.project.pages.components.proxy.XProxy;
014: import net.xoetrope.swing.XPanel;
015: import net.xoetrope.xui.XAttributedComponent;
016:
017: /**
018: * A page generator for creating form elements within the editor
019: * <p> Copyright (c) Xoetrope Ltd., 2002-2007</p>
020: * <p> $Revision: 1.77 $</p>
021: * <p> License: see License.txt</p>
022: */
023: public class XFormGenerator {
024: protected XEditorProject currentProject;
025: protected PageDesigner pageDesigner;
026:
027: // Deliberate package scope
028: static int ROW_HEIGHT = 24;
029: static int COL_WIDTH = 200;
030: static int PADDING = 4;
031: static int PANEL_PADDING = 6;
032: static int SUFFIX_WIDTH = 30;
033: static int PANEL_ARC = 10;
034: static boolean PANEL_OPAQUE = false;
035:
036: /**
037: * Creates a new instance of XFormGenerator
038: */
039: public XFormGenerator(XEditorProject editorProject, PageDesigner pd) {
040: currentProject = editorProject;
041: pageDesigner = pd;
042: }
043:
044: public boolean generatePanel(XFormGeneratorInfo genInfo,
045: JComponent sourceComponent, Point p) {
046: return generatePanel(genInfo, sourceComponent, p,
047: new Dimension());
048: }
049:
050: /**
051: * Indicates whether the specified field is enabled
052: * @param genInfo XFormGeneratorInfo object
053: * @param fieldName the name of the field to be queried
054: * @return true if enabled, false otherwise
055: */
056: private boolean isEnabled(XFormGeneratorInfo genInfo,
057: String fieldName) {
058: return Boolean.parseBoolean((String) genInfo.getProperty(
059: fieldName, "enabled"));
060: }
061:
062: /** @param size the size of the generated panel will be stored here */
063: // size of the generated panel will be stored in the passed size variable.
064: protected boolean generatePanel(XFormGeneratorInfo genInfo,
065: JComponent sourceComponent, Point p, Dimension size) {
066: int rowHeight = Integer.parseInt(genInfo
067: .getGenerationParameter("rowHeight"));
068: int colWidth = Integer.parseInt(genInfo
069: .getGenerationParameter("colWidth"));
070: int padding = Integer.parseInt(genInfo
071: .getGenerationParameter("padding"));
072: int suffixWidth = Integer.parseInt(genInfo
073: .getGenerationParameter("suffixWidth"));
074: int panelPadding = Integer.parseInt(genInfo
075: .getGenerationParameter("panelPadding"));
076: int panelArc = Integer.parseInt(genInfo
077: .getGenerationParameter("panelArc"));
078: boolean panelOpaque = Boolean.parseBoolean(genInfo
079: .getGenerationParameter("panelOpaque"));
080:
081: Component target = sourceComponent;
082: if (target instanceof PageDesigner) {
083: target = pageDesigner.getContentPage();
084: target = SwingUtilities.getDeepestComponentAt(target, p.x,
085: p.y);
086: }
087:
088: int ww = target.getSize().width;
089: int hh = target.getSize().height;
090:
091: XPanel cont = (XPanel) pageDesigner.addNewComponent(target,
092: new Point(p.x + 10, p.y + 10), false, "Panel");
093:
094: cont.setOpaque(panelOpaque);
095: if (panelArc > 0) {
096: cont.setAttribute("arc", Integer.toString(panelArc));
097: cont.setAttribute("border", "1");
098: }
099:
100: boolean hasTitle = Boolean.parseBoolean(genInfo
101: .getGenerationParameter("includeTitle"));
102: int x = 0;
103: int y = (hasTitle ? rowHeight : 0) + panelPadding;
104: int yStart = y;
105:
106: // int numFields = genInfo.getNumFields();
107: // int numColumns = Integer.parseInt( genInfo.getGenerationParameter( "numColumns" ));
108: // int numRows = (int)Math.ceil( (double)numFields / numColumns );
109:
110: int numFields = genInfo.getNumFields();
111: int numEnabledFields = genInfo.getNumEnabledFields();
112: int numColumns = Integer.parseInt(genInfo
113: .getGenerationParameter("numColumns"));
114: int numRows = (int) Math.ceil((double) numEnabledFields
115: / numColumns);
116:
117: cont.setSize(numColumns * COL_WIDTH + (numColumns + 1)
118: * PANEL_PADDING, numRows * ROW_HEIGHT + y
119: + PANEL_PADDING - PADDING);
120:
121: boolean hasPrefix = false;
122: boolean hasSuffix = false;
123: for (int i = 0; i < numFields; i++) {
124: String fieldName = genInfo.getFieldName(i);
125: if (!isEnabled(genInfo, fieldName))
126: continue;
127:
128: String prefixComponent = (String) genInfo.getProperty(
129: fieldName, "prefixComponent");
130: String suffixComponent = (String) genInfo.getProperty(
131: fieldName, "suffixComponent");
132: if (prefixComponent != null) {
133: hasPrefix = true;
134: if (hasSuffix)
135: break;
136: }
137:
138: if (suffixComponent != null) {
139: hasSuffix = true;
140: if (hasPrefix)
141: break;
142: }
143: }
144:
145: int[] colWidths = new int[3];
146: int[] locations = new int[3];
147: locations[0] = panelPadding;
148: if (hasSuffix) {
149: colWidths[2] = suffixWidth;
150: if (hasPrefix) {
151: colWidths[0] = colWidths[1] = (colWidth - 2 * padding - suffixWidth) / 2;
152: locations[1] = panelPadding + colWidths[0] + padding;
153: locations[2] = panelPadding + colWidths[0] + padding
154: + colWidths[1] + padding;
155: } else {
156: locations[1] = panelPadding;
157: locations[2] = panelPadding + colWidths[1] + padding;
158: }
159: } else if (hasPrefix) {
160: colWidths[0] = colWidths[1] = (colWidth - padding) / 2;
161: locations[1] = panelPadding + colWidths[0] + padding;
162: } else {
163: colWidths[1] = colWidth;
164: locations[1] = panelPadding;
165: }
166:
167: if (hasTitle) {
168: String title = genInfo.getGenerationParameter("title");
169: Point location = new Point(panelPadding, panelPadding);
170: Component comp = pageDesigner.addNewComponent(cont,
171: location, false, "Label");
172: comp.setName(title + "Title");
173: comp.setSize(x + colWidths[0], rowHeight - padding * 2);
174: if (comp instanceof XAttributedComponent) {
175: ((XAttributedComponent) comp).setAttribute("align",
176: "left");
177: ((XAttributedComponent) comp).setAttribute("content",
178: title);
179: }
180: }
181:
182: // stores the highest value of the y variable in order
183: // to determine the size of the generated panel
184: int maxY = 0;
185:
186: XPageResource pageRes = pageDesigner.getPageResource();
187: for (int i = 0; i < numFields; i++) {
188: String fieldName = genInfo.getFieldName(i);
189: if (!isEnabled(genInfo, fieldName))
190: continue;
191:
192: Object enabled = genInfo.getProperty(fieldName, "enabled");
193: Object visible = genInfo.getProperty(fieldName, "visible");
194:
195: // child panel
196: XFormGeneratorInfo childInfo = (XFormGeneratorInfo) genInfo
197: .getProperty(fieldName, "childPanel");
198:
199: // if a child panel
200: if (childInfo != null) {
201: Point location = new Point(x, y);
202: Dimension childSize = new Dimension();
203: generatePanel(childInfo, cont, location, childSize);
204:
205: maxY = Math.max(maxY, y + childSize.height + padding);
206:
207: if ((i % numRows) == (numRows - 1)) {
208: y = yStart;
209: x += (colWidth + panelPadding);
210: } else {
211: y += (childSize.height + padding + 2 * panelPadding);
212: }
213:
214: }
215: // if a single component
216: else {
217: String prefixComponent = (String) genInfo.getProperty(
218: fieldName, "prefixComponent");
219: String suffixComponent = (String) genInfo.getProperty(
220: fieldName, "suffixComponent");
221:
222: if (prefixComponent != null) {
223: Point location = new Point(x + locations[0], y);
224: Component comp = pageDesigner.addNewComponent(cont,
225: location, false, prefixComponent);
226: comp.setName(fieldName + "Prefix");
227: comp.setSize(colWidths[0], rowHeight - padding * 2);
228: comp.setVisible(Boolean
229: .parseBoolean((String) genInfo.getProperty(
230: fieldName, "visible")));
231: comp.setEnabled(Boolean
232: .parseBoolean((String) genInfo.getProperty(
233: fieldName, "enabled")));
234: if (comp instanceof XAttributedComponent) {
235: ((XAttributedComponent) comp).setAttribute(
236: "align", "right");
237: String caption = (String) genInfo.getProperty(
238: fieldName, "prefix");
239: if ((caption == null)
240: || (caption.length() == 0))
241: caption = fieldName;
242: ((XAttributedComponent) comp).setAttribute(
243: "content", caption);
244: }
245: }
246:
247: {
248: Point location = new Point(x + locations[1], y);
249: Component comp = pageDesigner.addNewComponent(cont,
250: location, false,
251: (String) genInfo.getProperty(fieldName,
252: "component"));
253: comp.setName(fieldName + "Input");
254: comp.setSize(colWidths[1], rowHeight - padding * 2);
255: comp.setVisible(Boolean
256: .parseBoolean((String) genInfo.getProperty(
257: fieldName, "visible")));
258: comp.setEnabled(Boolean
259: .parseBoolean((String) genInfo.getProperty(
260: fieldName, "enabled")));
261:
262: addValidation(pageRes, comp, (String) genInfo
263: .getProperty(fieldName, "validation"));
264: }
265:
266: if (suffixComponent != null) {
267: Point location = new Point(x + locations[2], y);
268: Component comp = pageDesigner.addNewComponent(cont,
269: location, false, suffixComponent);
270: comp.setName(fieldName + "Suffix");
271: comp.setSize(colWidths[2], rowHeight - padding * 2);
272: }
273:
274: maxY = Math.max(maxY, y + rowHeight);
275:
276: if ((i % numRows) == (numRows - 1)) {
277: y = yStart;
278: x += colWidth + panelPadding;
279: } else
280: y += rowHeight;
281: }
282: }
283:
284: p = new Point(x, maxY);
285: p.y += panelPadding + ROW_HEIGHT;
286:
287: size.height = p.y;
288: size.width = numColumns * colWidth + (numColumns + 1)
289: * panelPadding;
290:
291: // amend the panel's size
292: cont.setSize(size.width, size.height);
293:
294: pageDesigner.selectComponent(cont, true);
295: return false;
296: }
297:
298: /**
299: * Add a validation rule
300: */
301: public void addValidation(XPageResource pageRes, Component comp,
302: String validationRule) {
303: if ((validationRule == null)
304: || (validationRule.trim().length() == 0))
305: return;
306:
307: try {
308: if (comp instanceof XProxy)
309: comp = ((XProxy) comp).getProxiedComponent();
310:
311: PropertyHelper helper = ComponentHelper
312: .getPropertyHelper(comp);
313: helper.setPropertyValue(pageRes, comp, "Validation",
314: validationRule);
315: } catch (Exception e) {
316: e.printStackTrace();
317: }
318: }
319: }
|