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;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.logging.Level;
023: import java.util.logging.Logger;
024:
025: import org.xml.sax.helpers.AttributesImpl;
026:
027: import de.finix.contelligent.client.base.ConfigurationManager;
028: import de.finix.contelligent.client.base.Session;
029: import de.finix.contelligent.client.base.SessionListener;
030: import de.finix.contelligent.client.event.ConfigurationEvent;
031: import de.finix.contelligent.client.event.ConfigurationEventListener;
032: import de.finix.contelligent.client.remote.ActionResult;
033: import de.finix.contelligent.client.remote.Actions;
034: import de.finix.contelligent.client.remote.RemoteActionException;
035: import de.zeigermann.xml.XMLEncode;
036: import de.zeigermann.xml.simpleImporter.DefaultSimpleImportHandler;
037: import de.zeigermann.xml.simpleImporter.SimplePath;
038:
039: public class GlobalGUIMapping implements ConfigurationEventListener,
040: SessionListener {
041: public final static String DOMAIN = "gui-configuration";
042:
043: private static Logger logger = Logger
044: .getLogger(GlobalGUIMapping.class.getName());
045:
046: private static GlobalGUIMapping globalGUIMapping = null;
047:
048: private Map<String, Object> mappings = new HashMap<String, Object>();
049:
050: private GlobalGUIMapping() {
051: ConfigurationManager.getInstance()
052: .addConfigurationEventListener(this , false);
053: }
054:
055: public static GlobalGUIMapping getInstance() {
056: if (globalGUIMapping == null) {
057: globalGUIMapping = new GlobalGUIMapping();
058: try {
059: globalGUIMapping.load();
060: } catch (RemoteActionException rae) {
061: logger.log(Level.SEVERE,
062: "Could not load global gui mapping", rae);
063: }
064: Session.getInstance().addSessionListener(globalGUIMapping);
065: }
066: return globalGUIMapping;
067: }
068:
069: public void sessionChanged(Session session) {
070: try {
071: load();
072: } catch (RemoteActionException rae) {
073: logger.log(Level.SEVERE,
074: "Could not update global gui mapping", rae);
075: }
076: }
077:
078: public synchronized void load() throws RemoteActionException {
079: mappings = new HashMap<String, Object>();
080: GUIModificationHandler handler = new GUIModificationHandler();
081: ActionResult actionResult = Actions.loadConfiguration(DOMAIN,
082: handler);
083: actionResult.showErrors();
084: }
085:
086: public synchronized Map<String, Object> getGuiModifications() {
087: return mappings;
088: }
089:
090: public void onConfigurationAdded(ConfigurationEvent event) {
091: }
092:
093: public void onConfigurationRemoved(ConfigurationEvent event) {
094: }
095:
096: public void onConfigurationChanged(ConfigurationEvent event) {
097: if (event.getDomain().equals(DOMAIN)) {
098: try {
099: load();
100: } catch (RemoteActionException rae) {
101: logger.log(Level.SEVERE,
102: "Could not load global gui mapping", rae);
103: }
104: }
105: }
106:
107: private final class GUIModificationHandler extends
108: DefaultSimpleImportHandler {
109: private String currentType;
110:
111: private GUIModification currentModification;
112:
113: public void startElement(SimplePath path, String name,
114: AttributesImpl attrs, String leadingCData) {
115: try {
116: if (path.matches(DOMAIN)) {
117: currentType = attrs.getValue("type");
118: String commandAttribute = attrs.getValue("command");
119: String gui = attrs.getValue("gui");
120: String impl;
121: if (!gui.startsWith(":")) {
122: impl = GUIRegistry.getInstance().getImpl(gui);
123: } else {
124: impl = gui.substring(1);
125: }
126: int command = GUIModification
127: .parseCommand(commandAttribute);
128: String guiConfiguration = XMLEncode
129: .xmlDecodeTextToCDATA(leadingCData);
130: currentModification = new GUIModification(command,
131: new GUIDescription(impl, guiConfiguration));
132: }
133: } catch (Throwable t) {
134: logger
135: .log(
136: Level.SEVERE,
137: "Caught exception while gui-configuration import",
138: t);
139: }
140: }
141:
142: public void endElement(SimplePath path, String name) {
143: if (path.matches(DOMAIN) && currentModification != null
144: && currentType != null) {
145: DefaultGUIMapping.addGUIMapping(mappings, currentType,
146: currentModification);
147: }
148: }
149: }
150: }
|