001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: * Simon Epstein <anyone@simondo.com>
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or (at your option) any later version.
012:
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public
019: * License along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
021: */
022:
023: package org.beryl.gui.builder;
024:
025: import java.util.StringTokenizer;
026:
027: import org.beryl.gui.Controller;
028: import org.beryl.gui.GUIEvent;
029: import org.beryl.gui.GUIException;
030: import org.beryl.gui.LayoutFactory;
031: import org.beryl.gui.MessageDialog;
032: import org.beryl.gui.Widget;
033: import org.beryl.gui.XMLUtils;
034: import org.beryl.gui.model.MapDataModel;
035: import org.beryl.gui.model.TableDataModel;
036: import org.beryl.gui.model.TableRow;
037: import org.beryl.gui.table.TableEditor;
038: import org.beryl.gui.validators.IntegerValidator;
039: import org.beryl.gui.widgets.Dialog;
040: import org.beryl.gui.widgets.Frame;
041: import org.beryl.gui.widgets.Panel;
042: import org.beryl.gui.widgets.Table;
043: import org.beryl.gui.widgets.TextField;
044: import org.beryl.gui.widgets.TreeItem;
045: import org.w3c.dom.Element;
046:
047: public class HIGEditor extends Controller {
048: private Element layoutNode = null;
049: private Dialog dialog = null;
050: private Table rowsTable = null;
051: private Table columnsTable = null;
052: private TableDataModel rowsTableModel = null;
053: private TableDataModel columnsTableModel = null;
054: private MapDataModel dataModel = null;
055: private WidgetUserObject object = null;
056:
057: private class TableEntry extends TableRow {
058: private TableDataModel model;
059: private TableEditor stringEditor = new StringAdapter.StringEditor();
060:
061: public TableEntry(TableDataModel model, int pixels, int weight)
062: throws GUIException {
063: this .model = model;
064: setValue("pixels", String.valueOf(pixels));
065: setValue("weight", String.valueOf(weight));
066: }
067:
068: public Object getValue(String key) {
069: if (key.equals("index")) {
070: return String.valueOf(model.getRowIndex(this ));
071: }
072: return super .getValue(key);
073: }
074:
075: public int getPixels() {
076: return Integer.parseInt((String) getValue("pixels"));
077: }
078:
079: public int getWeight() {
080: return Integer.parseInt((String) getValue("weight"));
081: }
082:
083: public Widget getEditor(Table table, Object value,
084: TableRow row, String key) throws GUIException {
085: MapDataModel dataModel = new MapDataModel();
086: dataModel.setValue("value", value);
087:
088: Panel panel = new Panel(dialog, null);
089: TextField textField = new TextField(panel, null);
090: textField.setProperty("key", "value");
091: textField.addValidator(new IntegerValidator());
092: textField.finalizeConstruction();
093:
094: panel.addChild(textField, null);
095: panel.recursiveSetDataModel(dataModel);
096:
097: return panel;
098: }
099:
100: public boolean isEditable(String key) {
101: return !key.equals("index");
102: }
103: };
104:
105: public HIGEditor(Frame frame, Element layoutNode,
106: WidgetUserObject object) throws GUIException {
107: this .object = object;
108: this .layoutNode = layoutNode;
109:
110: dataModel = new MapDataModel();
111: dataModel.setValue("rowpixels", "0");
112: dataModel.setValue("columnpixels", "0");
113: dataModel.setValue("rowweight", new Integer(0));
114: dataModel.setValue("columnweight", new Integer(0));
115: dataModel.setValue("row", new int[] {});
116: dataModel.setValue("column", new int[] {});
117:
118: rowsTableModel = new TableDataModel();
119: columnsTableModel = new TableDataModel();
120:
121: StringTokenizer mainTokenizer = new StringTokenizer(layoutNode
122: .getAttribute("horiz"), ",");
123: StringTokenizer weightTokenizer = new StringTokenizer(
124: layoutNode.getAttribute("hweights"), ",");
125:
126: while (mainTokenizer.hasMoreTokens()) {
127: int pixels = Integer.parseInt(mainTokenizer.nextToken());
128: int weight = 0;
129:
130: if (weightTokenizer.hasMoreTokens())
131: weight = Integer.parseInt(weightTokenizer.nextToken());
132:
133: columnsTableModel.addRow(new TableEntry(columnsTableModel,
134: pixels, weight));
135: }
136:
137: mainTokenizer = new StringTokenizer(layoutNode
138: .getAttribute("vert"), ",");
139: weightTokenizer = new StringTokenizer(layoutNode
140: .getAttribute("vweights"), ",");
141:
142: while (mainTokenizer.hasMoreTokens()) {
143: int pixels = Integer.parseInt(mainTokenizer.nextToken());
144: int weight = 0;
145:
146: if (weightTokenizer.hasMoreTokens())
147: weight = Integer.parseInt(weightTokenizer.nextToken());
148:
149: rowsTableModel.addRow(new TableEntry(rowsTableModel,
150: pixels, weight));
151: }
152:
153: dialog = constructDialog("HIGEditor", dataModel);
154: ((TextField) dialog.getWidget("ColumnPixels"))
155: .addValidator(new IntegerValidator());
156: ((TextField) dialog.getWidget("RowPixels"))
157: .addValidator(new IntegerValidator());
158:
159: rowsTable = (Table) dialog.getWidget("RowsTable");
160: rowsTable.setTableDataModel(rowsTableModel);
161:
162: columnsTable = (Table) dialog.getWidget("ColumnsTable");
163: columnsTable.setTableDataModel(columnsTableModel);
164: dialog.initDialog(frame);
165: dialog.show();
166: }
167:
168: private void doOK() throws GUIException {
169: try {
170: dialog.recursiveValidate();
171: } catch (Exception e) {
172: return;
173: }
174: String horiz = "", vert = "", hweights = "", vweights = "";
175:
176: for (int i = 0; i < rowsTableModel.getRowCount(); i++) {
177: TableEntry entry = (TableEntry) rowsTableModel
178: .getTableRow(i);
179: vert += String.valueOf(entry.getPixels());
180: vweights += String.valueOf(entry.getWeight());
181:
182: if (rowsTableModel.getRowCount() - 1 != i) {
183: vert += ",";
184: vweights += ",";
185: }
186: }
187:
188: for (int i = 0; i < columnsTableModel.getRowCount(); i++) {
189: TableEntry entry = (TableEntry) columnsTableModel
190: .getTableRow(i);
191: horiz += String.valueOf(entry.getPixels());
192: hweights += String.valueOf(entry.getWeight());
193:
194: if (columnsTableModel.getRowCount() - 1 != i) {
195: horiz += ",";
196: hweights += ",";
197: }
198: }
199:
200: layoutNode.setAttribute("type", "hig");
201: layoutNode.setAttribute("horiz", horiz);
202: layoutNode.setAttribute("vert", vert);
203: layoutNode.setAttribute("hweights", hweights);
204: layoutNode.setAttribute("vweights", vweights);
205: object.widget.setProperty("layout", LayoutFactory.getInstance()
206: .constructLayout(object.widget, layoutNode));
207:
208: for (int i = 0; i < object.treeNode.getChildCount(); i++) {
209: TreeItem item = (TreeItem) object.treeNode.getChild(i);
210: WidgetUserObject childObject = (WidgetUserObject) item
211: .getUserObject();
212: Element anchorNode = XMLUtils.getChild(childObject.element,
213: "anchor");
214:
215: Widget childWidget = childObject.widget;
216: object.widget.removeChildWidget(childWidget);
217: object.widget.addChild(childWidget, WidgetTree
218: .createAnchor(object.widget, anchorNode));
219: }
220:
221: object.widget.revalidate();
222: }
223:
224: public void eventOccured(GUIEvent event) {
225: String name = event.getName();
226:
227: try {
228: if (name.equals("cancel")) {
229: dialog.dispose();
230: } else if (name.equals("ok")) {
231: doOK();
232: dialog.dispose();
233: } else if (name.equals("apply")) {
234: doOK();
235: } else if (name.startsWith("move")) {
236: TableDataModel tableDataModel = null;
237: String key = null;
238: int pos = -1;
239: int[] columns = null;
240: if (name.endsWith("column")) {
241: columns = ((int[]) dataModel.getValue("column"));
242: pos = (columns.length == 0) ? -1 : columns[0];
243: tableDataModel = columnsTableModel;
244: key = "column";
245: columnsTable.stopEditors();
246: } else if (name.endsWith("row")) {
247: columns = ((int[]) dataModel.getValue("row"));
248: pos = (columns.length == 0) ? -1 : columns[0];
249: tableDataModel = rowsTableModel;
250: key = "row";
251: rowsTable.stopEditors();
252: }
253: if (name.indexOf("up") != -1 && pos > 0) {
254: TableRow tableRow = tableDataModel.getTableRow(pos);
255: tableDataModel.removeRow(pos);
256: tableDataModel.insertRow(pos - 1, tableRow);
257: columns[0] = pos - 1;
258: } else if (name.indexOf("down") != -1 && pos >= 0
259: && pos < tableDataModel.getRowCount() - 1) {
260: TableRow tableRow = tableDataModel.getTableRow(pos);
261: tableDataModel.removeRow(pos);
262: tableDataModel.insertRow(pos + 1, tableRow);
263: columns[0] = pos + 1;
264: }
265: dataModel.setValue(key, columns);
266: } else if (name.equals("insert_column_before")
267: || name.equals("insert_column_after")) {
268: try {
269: dialog.recursiveValidate();
270: } catch (Exception e) {
271: return;
272: }
273:
274: int index = -1;
275: int columns[] = ((int[]) dataModel.getValue("column"));
276: int pos = (columns.length == 0) ? -1 : columns[0];
277:
278: if (name.equals("insert_column_before")) {
279: if (pos != -1)
280: index = pos;
281:
282: if (index < 0)
283: index = 0;
284: } else if (name.equals("insert_column_after")) {
285: if (pos != -1)
286: index = pos + 1;
287: else
288: index = columnsTableModel.getRowCount();
289: }
290:
291: TableRow tableRow = new TableEntry(columnsTableModel,
292: Integer.parseInt((String) dataModel
293: .getValue("columnpixels")),
294: ((Integer) dataModel.getValue("columnweight"))
295: .intValue());
296: columnsTableModel.insertRow(index, tableRow);
297: } else if (name.equals("insert_row_before")
298: || name.equals("insert_row_after")) {
299: try {
300: dialog.recursiveValidate();
301: } catch (Exception e) {
302: return;
303: }
304:
305: int index = -1;
306: int rows[] = ((int[]) dataModel.getValue("row"));
307: int pos = (rows.length == 0) ? -1 : rows[0];
308:
309: if (name.equals("insert_row_before")) {
310: if (pos != -1)
311: index = pos;
312:
313: if (index < 0)
314: index = 0;
315: } else if (name.equals("insert_row_after")) {
316: if (pos != -1)
317: index = pos + 1;
318: else
319: index = rowsTableModel.getRowCount();
320: }
321:
322: TableRow tableRow = new TableEntry(rowsTableModel,
323: Integer.parseInt((String) dataModel
324: .getValue("rowpixels")),
325: ((Integer) dataModel.getValue("rowweight"))
326: .intValue());
327: rowsTableModel.insertRow(index, tableRow);
328: } else if (name.equals("delete_row")) {
329: int rows[] = ((int[]) dataModel.getValue("row"));
330: if (rows.length != 0)
331: rowsTableModel.removeRow(rows[0]);
332: } else if (name.equals("delete_column")) {
333: int columns[] = ((int[]) dataModel.getValue("column"));
334: if (columns.length != 0)
335: columnsTableModel.removeRow(columns[0]);
336: }
337: } catch (Exception e) {
338: new MessageDialog(e);
339: }
340: }
341: }
|