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: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui.builder;
023:
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.FileOutputStream;
027: import java.util.Iterator;
028: import java.util.Properties;
029:
030: import org.beryl.gui.Controller;
031: import org.beryl.gui.DialogUtils;
032: import org.beryl.gui.GUIEvent;
033: import org.beryl.gui.GUIEventListener;
034: import org.beryl.gui.GUIException;
035: import org.beryl.gui.MessageDialog;
036: import org.beryl.gui.View;
037: import org.beryl.gui.Widget;
038: import org.beryl.gui.WidgetFactory;
039: import org.beryl.gui.model.MapDataModel;
040: import org.beryl.gui.model.TableDataModel;
041: import org.beryl.gui.model.TableRow;
042: import org.beryl.gui.widgets.Dialog;
043: import org.beryl.gui.widgets.Frame;
044: import org.beryl.gui.widgets.PopupMenu;
045: import org.beryl.gui.widgets.Table;
046:
047: /**
048: * Internationalization editor
049: */
050: public class InternationalizationEditor extends Controller {
051: private static boolean modified = false;
052: private WidgetTree widgetTree = null;
053: private Properties properties = null;
054: private MapDataModel dataModel = null;
055: private Frame frame = null;
056: private Table propertyTable = null;
057: private TableDataModel propertyModel = null;
058:
059: private class PropertyTableRow extends TableRow {
060: private String key = null;
061:
062: public PropertyTableRow(String key) {
063: this .key = key;
064: }
065:
066: public boolean isEditable(String key) {
067: return key.equals("value");
068: }
069:
070: public Object getValue(String key) {
071: if (key.equals("key"))
072: return this .key;
073: else if (key.equals("value"))
074: return properties.getProperty(this .key);
075: else
076: throw new RuntimeException("Invalid key selected");
077: }
078:
079: public void setValue(View source, String key, Object newValue)
080: throws GUIException {
081: if (key.equals("value")) {
082: properties.setProperty(this .key, (String) newValue);
083: modified = true;
084: } else {
085: throw new RuntimeException("Invalid key selected");
086: }
087: }
088: };
089:
090: public InternationalizationEditor(WidgetTree widgetTree,
091: Properties properties) throws GUIException {
092: this .properties = properties;
093: this .widgetTree = widgetTree;
094: dataModel = new MapDataModel();
095: frame = constructFrame("InternationalizationEditor", dataModel);
096: propertyTable = (Table) frame.getWidget("PropertyTable");
097:
098: propertyModel = new TableDataModel();
099: buildModel();
100: propertyTable.setTableDataModel(propertyModel);
101: frame.show();
102: }
103:
104: private void buildModel() throws GUIException {
105: dataModel.setValue("property.value", new TableRow[] {});
106: propertyModel.clear();
107: for (Iterator i = properties.keySet().iterator(); i.hasNext();)
108: propertyModel
109: .addRow(new PropertyTableRow((String) i.next()));
110: }
111:
112: public boolean isVisible() throws GUIException {
113: return frame.isVisible();
114: }
115:
116: public boolean doAskSaveCancel(Frame frame) throws GUIException {
117: if (modified) {
118: switch (DialogUtils.showYesNoCancelDialog(frame,
119: getString("builder.savei18n.title"),
120: getString("builder.savei18n.label"))) {
121: case DialogUtils.RESULT_YES:
122: doSave();
123: return true;
124: case DialogUtils.RESULT_NO:
125: return true;
126: case DialogUtils.RESULT_CANCEL:
127: return false;
128: }
129: }
130: return true;
131: }
132:
133: private void doSave() throws GUIException {
134: try {
135: File file = DialogUtils.showSaveFileDialog(frame,
136: "properties");
137: if (file != null) {
138: properties
139: .store(new FileOutputStream(file),
140: "Internationalization file created using the Builder");
141: }
142: } catch (Exception e) {
143: throw new GUIException("Error while saving file", e);
144: }
145: }
146:
147: public void eventOccured(GUIEvent event) {
148: String name = event.getName();
149: try {
150: if (name.equals("add")) {
151: final Dialog dialog = (Dialog) WidgetFactory
152: .getInstance().constructWidget(getClass(),
153: "AddInternationalizationDialog",
154: new GUIEventListener() {
155: public void eventOccured(
156: GUIEvent event) {
157: try {
158: Widget source = event
159: .getSource();
160: if (event.getName().equals(
161: "ok")) {
162: String key = (String) source
163: .getDataModel()
164: .getValue("key");
165: String value = (String) source
166: .getDataModel()
167: .getValue(
168: "value");
169:
170: boolean isNew = (properties
171: .getProperty(key) == null);
172: properties.setProperty(
173: key, value);
174: if (isNew)
175: propertyModel
176: .addRow(new PropertyTableRow(
177: key));
178: modified = true;
179: }
180: ((Dialog) source
181: .getParentWidgetByClass(Dialog.class))
182: .dispose();
183: } catch (Exception e) {
184: new MessageDialog(e);
185: }
186: }
187: }, new MapDataModel());
188: dialog.initDialog(frame);
189: dialog.show();
190: } else if (name.equals("popup")) {
191: PopupMenu popup = (PopupMenu) constructWidget("PropertyPopup");
192: popup.popup(event);
193: } else if (name.equals("delete")) {
194: TableRow rows[] = (TableRow[]) dataModel
195: .getValue("property.value");
196: for (int i = 0; i < rows.length; i++) {
197: PropertyTableRow row = (PropertyTableRow) rows[i];
198: propertyModel.removeRow(row);
199: properties.remove(row.getValue("key"));
200: }
201: } else if (name.equals("clear")) {
202: dataModel.setValue("property.value", new TableRow[] {});
203: propertyModel.clear();
204: properties.clear();
205: } else if (name.equals("import")) {
206: File file = DialogUtils.showOpenFileDialog(frame,
207: "properties");
208: if (file != null) {
209: properties.load(new FileInputStream(file));
210: buildModel();
211: }
212: } else if (name.equals("save")) {
213: doSave();
214: } else if (name.equals("refresh")) {
215: widgetTree.refreshInternationalProperties();
216: }
217: } catch (Exception e) {
218: new MessageDialog(e);
219: }
220: }
221: }
|