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.io.IOException;
021: import java.io.StringReader;
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Locale;
026: import java.util.Map;
027: import java.util.StringTokenizer;
028: import java.util.logging.Level;
029: import java.util.logging.Logger;
030:
031: import javax.swing.ImageIcon;
032: import javax.xml.parsers.ParserConfigurationException;
033:
034: import org.xml.sax.InputSource;
035: import org.xml.sax.SAXException;
036: import org.xml.sax.SAXParseException;
037: import org.xml.sax.helpers.AttributesImpl;
038:
039: import de.finix.contelligent.client.base.ContelligentConstants;
040: import de.finix.contelligent.client.base.Description;
041: import de.finix.contelligent.client.modules.preferences.PreferencesModule;
042: import de.finix.contelligent.client.util.xml.DumpImportHandler;
043: import de.zeigermann.xml.simpleImporter.ConversionHelpers;
044: import de.zeigermann.xml.simpleImporter.DefaultSimpleImportHandler;
045: import de.zeigermann.xml.simpleImporter.SimpleImporter;
046: import de.zeigermann.xml.simpleImporter.SimplePath;
047:
048: public class FormGUIConfiguration {
049: private final static Logger logger = Logger
050: .getLogger(FormGUIConfiguration.class.getName());
051:
052: public final static int SHOW_UNCONFIGURED_ELEMENTS = 0;
053:
054: public final static int HIDE_UNCONFIGURED_ELEMENTS = 1;
055:
056: private List<FormGUIConfiguration.ElementConfiguration> elements = new ArrayList<FormGUIConfiguration.ElementConfiguration>();
057:
058: private List<String> elementOrder = new ArrayList<String>();
059:
060: private int mode = FormGUIConfiguration.SHOW_UNCONFIGURED_ELEMENTS;
061:
062: public FormGUIConfiguration(String configuration) {
063: logger.log(Level.FINE, "Form editor configuration is: "
064: + configuration);
065: if (configuration != null && configuration.trim().length() > 0) {
066: SimpleImporter importer = new SimpleImporter();
067: importer
068: .setIncludeLeadingCDataIntoStartElementCallback(true);
069: ImportHandler importHandler = new ImportHandler();
070: importer.addSimpleImportHandler(importHandler);
071: // let's see what's going on (dump both on the fly and in buffer)
072: DumpImportHandler dumpHandler = new DumpImportHandler(
073: Level.FINEST, true);
074: importer.addSimpleImportHandler(dumpHandler);
075: try {
076: importer.parse(new InputSource(new StringReader(
077: ContelligentConstants.XML_HEADER
078: + configuration)));
079: } catch (ParserConfigurationException pce) {
080: // Parser with specified options can't be built
081: logger.log(Level.SEVERE,
082: "Could not parse form editor configuration!",
083: pce);
084: } catch (SAXParseException spe) {
085: logger.log(Level.SEVERE,
086: "Could not parse form editor configuration!",
087: spe);
088: } catch (SAXException sxe) {
089: logger.log(Level.SEVERE,
090: "Could not parse form editor configuration!",
091: sxe);
092: } catch (IOException ioe) {
093: logger.log(Level.SEVERE,
094: "Could not parse form editor configuration!",
095: ioe);
096: }
097: }
098: }
099:
100: public List getElements() {
101: return elements;
102: }
103:
104: public List<String> getElementOrder() {
105: return elementOrder;
106: }
107:
108: public int getMode() {
109: return mode;
110: }
111:
112: final static class ElementConfiguration {
113: private boolean hidden;
114:
115: private String path;
116:
117: private Map descriptionMap = new HashMap();
118:
119: private String gui, guiConfiguration;
120:
121: private ImageIcon icon;
122:
123: public ElementConfiguration(String identifier) {
124: this .path = identifier;
125: }
126:
127: public String getPath() {
128: return path;
129: }
130:
131: public boolean isHidden() {
132: return hidden;
133: }
134:
135: public void setHidden(boolean hidden) {
136: this .hidden = hidden;
137: }
138:
139: public String getGui() {
140: return gui;
141: }
142:
143: public void setGui(String gui) {
144: this .gui = gui;
145: }
146:
147: public String getGuiConfiguration() {
148: return guiConfiguration;
149: }
150:
151: public void setGuiConfiguration(String guiConfiguration) {
152: this .guiConfiguration = guiConfiguration;
153: }
154:
155: public ImageIcon getIcon() {
156: return icon;
157: }
158:
159: public void setIcon(ImageIcon icon) {
160: this .icon = icon;
161: }
162:
163: public void addDescription(String lang, String title,
164: String text, String helpURL) {
165: descriptionMap.put(lang, new Description(lang, title, text,
166: helpURL));
167: }
168:
169: public Description getDescription(Locale locale) {
170: return getDescription(locale.getLanguage());
171: }
172:
173: public Description getDescription(String lang) {
174: if (descriptionMap.containsKey(lang)) {
175: return (Description) descriptionMap.get(lang);
176: } else if (descriptionMap.containsKey(PreferencesModule
177: .getDefaultLanguage())) {
178: return (Description) descriptionMap
179: .get(PreferencesModule.getDefaultLanguage());
180: }
181: return null;
182: }
183:
184: public Map getDescriptions() {
185: return descriptionMap;
186: }
187: }
188:
189: private final class ImportHandler extends
190: DefaultSimpleImportHandler {
191: private ElementConfiguration currentElement;
192:
193: public void startElement(SimplePath path, String name,
194: AttributesImpl attributes, String leadingCData) {
195: if (path.matches("order")) {
196: if (attributes.getValue("mode").equals("show")) {
197: mode = FormGUIConfiguration.SHOW_UNCONFIGURED_ELEMENTS;
198: } else {
199: mode = FormGUIConfiguration.HIDE_UNCONFIGURED_ELEMENTS;
200: }
201: String orderString = leadingCData;
202: if (orderString != null) {
203: StringTokenizer tokenizer = new StringTokenizer(
204: orderString, ",");
205: for (int i = 0; tokenizer.hasMoreTokens();) {
206: String childName = tokenizer.nextToken();
207: elementOrder.add(childName);
208: }
209: }
210: }
211: if (path.matches("element")) {
212: String id = attributes.getValue("path");
213: FormGUIConfiguration.ElementConfiguration element = new FormGUIConfiguration.ElementConfiguration(
214: id);
215: elements.add(element);
216: element.setHidden(ConversionHelpers.getBoolean(
217: attributes.getValue("hidden"), false));
218: element.setGui(attributes.getValue("gui"));
219: currentElement = element;
220: }
221: if (path.matches("element/configuration")) {
222: currentElement.setGuiConfiguration(leadingCData);
223: }
224: if (path.matches("element/description")) {
225: String language = attributes.getValue("lang");
226: String title = attributes.getValue("title");
227: String text = leadingCData;
228: if (text == null)
229: text = "";
230: String helpURL = attributes.getValue("help");
231: currentElement.addDescription(language, title, text,
232: helpURL);
233: }
234: }
235: }
236: }
|