001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.gui.composed;
019:
020: import java.awt.BorderLayout;
021: import java.awt.event.ActionEvent;
022: import java.io.IOException;
023: import java.io.StringWriter;
024: import java.io.Writer;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028: import java.util.logging.Level;
029: import java.util.logging.Logger;
030:
031: import javax.swing.AbstractAction;
032: import javax.swing.Box;
033: import javax.swing.JButton;
034: import javax.swing.JCheckBox;
035: import javax.swing.JComponent;
036: import javax.swing.JLabel;
037: import javax.swing.JPanel;
038: import javax.swing.JScrollPane;
039: import javax.swing.JTextField;
040: import javax.swing.JToolBar;
041: import javax.swing.border.TitledBorder;
042:
043: import de.finix.contelligent.client.base.Description;
044: import de.finix.contelligent.client.gui.GUIConfigurationEditor;
045: import de.finix.contelligent.client.gui.GUIRegistry;
046: import de.finix.contelligent.client.i18n.Resources;
047: import de.finix.contelligent.client.util.ButtonComposer;
048: import de.finix.contelligent.client.util.TableLayout;
049: import de.finix.contelligent.client.util.list.NameValueComboBox;
050: import de.finix.contelligent.client.util.list.NameValueListModel;
051: import de.finix.contelligent.client.wizard.DescriptionPane;
052: import de.zeigermann.xml.XMLWriter;
053:
054: public class FormGUIConfigurationEditor implements
055: GUIConfigurationEditor {
056: private final static Logger logger = Logger
057: .getLogger(FormGUIConfigurationEditor.class.getName());
058:
059: private final static int GAP = 5;
060:
061: private JCheckBox hideUndefined = new JCheckBox();
062:
063: private JTextField[] elementPath;
064:
065: private JCheckBox[] elementHide;
066:
067: private NameValueComboBox[] elementGUI;
068:
069: private DescriptionPane[] elementDescription;
070:
071: private List elements, elementOrder;
072:
073: private int mode;
074:
075: private JPanel elementsPanel = new JPanel(), editor = new JPanel(
076: new BorderLayout());
077:
078: private JScrollPane elementScroller = new JScrollPane(elementsPanel);
079:
080: public void init() {
081: elementsPanel.setOpaque(false);
082: editor.setOpaque(false);
083: JPanel generalConfiguration = new JPanel(new TableLayout(
084: new double[][] { { 0.4, TableLayout.FILL },
085: { 28, TableLayout.PREFERRED } }));
086: generalConfiguration.setOpaque(false);
087: generalConfiguration.setBorder(new TitledBorder(Resources
088: .getLocalString("gui_configuration_general")));
089: JToolBar toolbar = new JToolBar(JToolBar.HORIZONTAL);
090: toolbar.setFloatable(false);
091: toolbar.setRollover(true);
092: toolbar.add(ButtonComposer.createButton(
093: new AddElementAction(0), false, null, true, true));
094: toolbar.add(Box.createHorizontalGlue());
095: toolbar.add(Box.createVerticalStrut(28));
096: generalConfiguration.add(toolbar, "0,0,1,0");
097: generalConfiguration.add(new JLabel(Resources
098: .getLocalString("gui_configuration_hide")), "0,1");
099: hideUndefined
100: .setSelected(mode == FormGUIConfiguration.HIDE_UNCONFIGURED_ELEMENTS);
101: generalConfiguration.add(hideUndefined, "1,1");
102: editor.add(generalConfiguration, BorderLayout.NORTH);
103: editor.add(elementScroller, BorderLayout.CENTER);
104: compose();
105: }
106:
107: private void updateModel() {
108: setConfiguration(getConfiguration());
109: }
110:
111: private void compose() {
112: elementPath = new JTextField[elements.size()];
113: elementHide = new JCheckBox[elements.size()];
114: elementGUI = new NameValueComboBox[elements.size()];
115: elementDescription = new DescriptionPane[elements.size()];
116:
117: double[] rows = new double[elements.size()];
118: for (int i = 0; i < elements.size(); i++) {
119: rows[i] = TableLayout.MINIMUM;
120: }
121: elementsPanel.removeAll();
122: elementsPanel.setLayout(new TableLayout(new double[][] {
123: { TableLayout.FILL }, rows }));
124: for (int i = 0; i < elements.size(); i++) {
125: FormGUIConfiguration.ElementConfiguration element = (FormGUIConfiguration.ElementConfiguration) elements
126: .get(i);
127: JPanel elementPanel = new JPanel(new TableLayout(
128: new double[][] { { 0.5, 0.5 },
129: { 28, TableLayout.MINIMUM } }));
130: elementPanel.setOpaque(false);
131: JToolBar toolbar = new JToolBar(JToolBar.HORIZONTAL);
132: toolbar.setFloatable(false);
133: toolbar.setRollover(true);
134: toolbar.add(ButtonComposer.createButton(
135: new AddElementAction(i + 1), false, null, true,
136: true));
137: toolbar.add(ButtonComposer
138: .createButton(new RemoveElementAction(i), false,
139: null, true, true));
140: if (i < elements.size() - 1)
141: toolbar
142: .add(ButtonComposer.createButton(
143: new MoveDownAction(i), false, null,
144: true, true));
145: if (i > 0)
146: toolbar.add(ButtonComposer.createButton(
147: new MoveUpAction(i), false, null, true, true));
148: toolbar.add(Box.createHorizontalGlue());
149: toolbar.add(Box.createVerticalStrut(28));
150: elementPanel.add(toolbar, "0,0,1,0");
151: JPanel generalPanel = new JPanel(new TableLayout(
152: new double[][] {
153: { TableLayout.PREFERRED, GAP,
154: TableLayout.FILL,
155: TableLayout.MINIMUM },
156: { TableLayout.PREFERRED,
157: TableLayout.PREFERRED,
158: TableLayout.PREFERRED } }));
159: generalPanel.setOpaque(false);
160: generalPanel.setBorder(new TitledBorder(Resources
161: .getLocalString("element_configuration_general")));
162: elementPath[i] = new JTextField(element.getPath());
163: generalPanel.add(new JLabel(Resources
164: .getLocalString("element_path")), "0,0");
165: generalPanel.add(elementPath[i], "2,0,3,0");
166: generalPanel.add(new JLabel(Resources
167: .getLocalString("element_hide")), "0,1");
168: elementHide[i] = new JCheckBox();
169: elementHide[i].setSelected(element.isHidden());
170: generalPanel.add(elementHide[i], "2,1");
171: generalPanel.add(new JLabel(Resources
172: .getLocalString("element_gui")), "0,2");
173: elementGUI[i] = new NameValueComboBox(NameValueListModel
174: .buildNameValuePairs(GUIRegistry.getInstance()
175: .getImpls().toArray(),
176: (String[]) GUIRegistry.getInstance()
177: .getSymbolicNames().toArray(
178: new String[0])));
179: if (element.getGui() != null) {
180: elementGUI[i].setSelectedItemByValue(element.getGui());
181: }
182: generalPanel.add(elementGUI[i], "2,2");
183: generalPanel.add(new JButton("Configure"), "3,2");
184: elementPanel.add(generalPanel, "0,1");
185: elementDescription[i] = new DescriptionPane(element
186: .getDescriptions());
187: elementDescription[i]
188: .setBorder(new TitledBorder(
189: Resources
190: .getLocalString("element_configuration_description")));
191: elementPanel.add(elementDescription[i], "1,1");
192: elementsPanel.add(elementPanel, "0," + i);
193: }
194: elementScroller.revalidate();
195: elementScroller.repaint();
196: }
197:
198: public void setConfiguration(String configuration) {
199: FormGUIConfiguration guiConfiguration = new FormGUIConfiguration(
200: configuration);
201: this .elements = guiConfiguration.getElements();
202: this .elementOrder = guiConfiguration.getElementOrder();
203: this .mode = guiConfiguration.getMode();
204: }
205:
206: public String getConfiguration() {
207: Writer configWriter = new StringWriter(256);
208: XMLWriter writer = new XMLWriter(configWriter);
209: writer.setPrettyPrintMode(true);
210: try {
211: writer.write(XMLWriter.createStartTag("configuration"));
212: writer.write(XMLWriter.createStartTag("order", "mode",
213: hideUndefined.isSelected() ? "hide" : "show"));
214: for (int i = 0; i < elementPath.length; i++) {
215: writer.write(elementPath[i].getText());
216: if (i < elementPath.length - 1)
217: writer.write(',');
218: }
219: writer.write(XMLWriter.createEndTag("order"));
220: for (int i = 0; i < elementPath.length; i++) {
221: writer.write(XMLWriter.createStartTag("element",
222: new String[] { "path", "hidden", "gui" },
223: new String[] {
224: elementPath[i].getText(),
225: Boolean.valueOf(
226: elementHide[i].isSelected())
227: .toString(),
228: (String) elementGUI[i]
229: .getSelectedItemValue() }));
230: Map descriptions = elementDescription[i]
231: .getDescriptions();
232: for (Iterator k = descriptions.values().iterator(); k
233: .hasNext();) {
234: Description description = ((Description) k.next());
235: description.writeToXML(writer);
236: }
237: writer.write(XMLWriter.createEndTag("element"));
238: }
239: writer.write(XMLWriter.createEndTag("configuration"));
240: writer.flush();
241: String configString = configWriter.toString();
242: return configString;
243: } catch (IOException e) {
244: logger.log(Level.SEVERE, "Could not write configuration!",
245: e);
246: return null;
247: }
248: }
249:
250: public JComponent getEditorComponent() {
251: return editor;
252: }
253:
254: private final class AddElementAction extends AbstractAction {
255: private int index;
256:
257: public AddElementAction(int index) {
258: super ("add_element_action", Resources.addElementIcon);
259: this .index = index;
260: }
261:
262: public void actionPerformed(ActionEvent e) {
263: updateModel();
264: FormGUIConfiguration.ElementConfiguration element = new FormGUIConfiguration.ElementConfiguration(
265: "");
266: elements.add(index, element);
267: compose();
268: }
269: }
270:
271: private final class RemoveElementAction extends AbstractAction {
272: private int index;
273:
274: public RemoveElementAction(int index) {
275: super ("remove_element_action", Resources.removeElementIcon);
276: this .index = index;
277: }
278:
279: public void actionPerformed(ActionEvent e) {
280: updateModel();
281: elements.remove(index);
282: compose();
283: }
284: }
285:
286: private final class MoveUpAction extends AbstractAction {
287: private int index;
288:
289: public MoveUpAction(int index) {
290: super ("move_up_action", Resources.moveUpIcon);
291: this .index = index;
292: }
293:
294: public void actionPerformed(ActionEvent e) {
295: updateModel();
296: Object element = elements.remove(index);
297: elements.add(index - 1, element);
298: compose();
299: }
300: }
301:
302: private final class MoveDownAction extends AbstractAction {
303: private int index;
304:
305: public MoveDownAction(int index) {
306: super ("move_down_action", Resources.moveDownIcon);
307: this .index = index;
308: }
309:
310: public void actionPerformed(ActionEvent e) {
311: updateModel();
312: Object element = elements.remove(index);
313: elements.add(index + 1, element);
314: compose();
315: }
316: }
317: }
|