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.configuration;
019:
020: import java.awt.BorderLayout;
021: import java.io.IOException;
022: import java.io.StringReader;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import javax.xml.parsers.ParserConfigurationException;
027:
028: import org.xml.sax.InputSource;
029: import org.xml.sax.SAXException;
030: import org.xml.sax.helpers.AttributesImpl;
031:
032: import de.finix.contelligent.client.base.ComponentFactory;
033: import de.finix.contelligent.client.base.resource.ContelligentTextResource;
034: import de.finix.contelligent.client.event.ContelligentEvent;
035: import de.finix.contelligent.client.event.ResourceModifiedEvent;
036: import de.finix.contelligent.client.gui.AbstractComponentResourceEditor;
037: import de.finix.contelligent.client.gui.GlobalGUIMapping;
038: import de.finix.contelligent.client.gui.module.MappingPane;
039: import de.finix.contelligent.client.util.xml.DumpImportHandler;
040: import de.zeigermann.xml.XMLEncode;
041: import de.zeigermann.xml.XMLStringWriter;
042: import de.zeigermann.xml.XMLWriter;
043: import de.zeigermann.xml.simpleImporter.DefaultSimpleImportHandler;
044: import de.zeigermann.xml.simpleImporter.SimpleImporter;
045: import de.zeigermann.xml.simpleImporter.SimplePath;
046:
047: public class GUIMappingEditor extends AbstractComponentResourceEditor {
048: private static Logger logger = Logger
049: .getLogger(GUIMappingEditor.class.getName());
050:
051: private MappingPane mappingPane = new MappingPane(null, null, null,
052: null);
053:
054: public void init() {
055: setResourceComponent(this );
056: add(mappingPane, BorderLayout.CENTER);
057: update();
058: }
059:
060: public void setEditable(boolean editable) {
061: super .setEditable(editable);
062: if (mappingPane != null)
063: mappingPane.setEditable(editable);
064: }
065:
066: protected void displayResource() {
067: // Parse resource and update editor
068: ContelligentTextResource resource = (ContelligentTextResource) getGUI()
069: .getResource();
070: if (resource != null) {
071: SimpleImporter importer = new SimpleImporter();
072: importer
073: .setIncludeLeadingCDataIntoStartElementCallback(true);
074: importer
075: .addSimpleImportHandler(new GUIModificationHandler());
076: importer.addSimpleImportHandler(new DumpImportHandler(
077: Level.FINEST, false));
078: try {
079: importer.parse(new InputSource(new StringReader(
080: resource.getText())));
081: } catch (ParserConfigurationException e) {
082: } catch (SAXException e) {
083: } catch (IOException e) {
084: }
085: }
086: }
087:
088: protected void updateResource() {
089: ContelligentTextResource resource = (ContelligentTextResource) getGUI()
090: .getResource();
091: if (resource == null || getGUI().isResourceInherited()) {
092: resource = new ContelligentTextResource(getGUI()
093: .getResourceCategoryMap(),
094: getGUIModificationAsXML());
095: resource.setModified(true);
096: getGUI().setResource(resource);
097: } else {
098: resource.setText("");
099: }
100: if (resource.isModified()) {
101: getComponent().setResourceModified(true);
102: }
103: ComponentFactory.getInstance().fireResourceModifyEvent(
104: new ResourceModifiedEvent(this , getComponent()
105: .getPath(), getGUI().getResourceIdentifier(),
106: resource, getGUI().getResourceMode()));
107: }
108:
109: protected void updateComponent() {
110: updateResource();
111: ContelligentTextResource resource = (ContelligentTextResource) getGUI()
112: .getResource();
113: if (resource != null && !getGUI().isResourceInherited()) {
114: resource.setText(getGUIModificationAsXML());
115: if (resource.isModified()) {
116: getComponent().setResourceModified(true);
117: }
118: }
119: }
120:
121: protected void componentChanged(ContelligentEvent event) {
122: update();
123: }
124:
125: protected void childComponentAdded(ContelligentEvent event) {
126: }
127:
128: protected void childComponentRemoved(ContelligentEvent event) {
129: }
130:
131: protected void childComponentChanged(ContelligentEvent event) {
132: }
133:
134: protected void descendentComponentChanged(ContelligentEvent event) {
135: }
136:
137: private String getGUIModificationAsXML() {
138: XMLStringWriter configurationWriter = XMLStringWriter.create();
139: configurationWriter.writeElementWithPCData(XMLWriter
140: .createStartTag("gui-configuration", new String[] {
141: "type", "command", "gui" }, new String[] {
142: mappingPane.getType(),
143: mappingPane.getCommand(),
144: ":" + mappingPane.getGUIImpl() }), XMLEncode
145: .xmlEncodeTextAsCDATABlock(mappingPane
146: .getConfiguration()), XMLWriter
147: .createEndTag("gui-configuration"));
148: return configurationWriter.toString();
149: }
150:
151: private final class GUIModificationHandler extends
152: DefaultSimpleImportHandler {
153: public void startElement(SimplePath path, String name,
154: AttributesImpl attrs, String leadingCData) {
155: try {
156: if (path.matches(GlobalGUIMapping.DOMAIN)) {
157: mappingPane.setType(attrs.getValue("type"));
158: mappingPane.setCommand(attrs.getValue("command"));
159: mappingPane.setConfiguration(XMLEncode
160: .xmlDecodeTextToCDATA(leadingCData));
161: mappingPane.setGUI(attrs.getValue("gui"));
162: }
163: } catch (Throwable t) {
164: logger
165: .log(
166: Level.SEVERE,
167: "Caught exception while gui-configuration import",
168: t);
169: }
170: }
171: }
172: }
|