001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.core.gui.toolbar;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.Iterator;
023: import java.util.logging.Logger;
024:
025: import org.columba.api.gui.frame.IFrameMediator;
026: import org.columba.api.plugin.IExtension;
027: import org.columba.api.plugin.IExtensionHandler;
028: import org.columba.api.plugin.IExtensionHandlerKeys;
029: import org.columba.api.plugin.PluginException;
030: import org.columba.api.plugin.PluginHandlerNotFoundException;
031: import org.columba.core.gui.action.AbstractColumbaAction;
032: import org.columba.core.logging.Logging;
033: import org.columba.core.plugin.PluginManager;
034: import org.jdom.Document;
035: import org.jdom.Element;
036: import org.jdom.JDOMException;
037: import org.jdom.input.SAXBuilder;
038:
039: /**
040: * Create a toolbar from an xml file.
041: *
042: * @author fdietz
043: *
044: */
045: public class ToolBarXMLDecoder {
046:
047: private static final Logger LOG = Logger
048: .getLogger("org.columba.core.gui.menu");
049:
050: private IExtensionHandler pluginHandler;
051:
052: private IFrameMediator mediator;
053:
054: public ToolBarXMLDecoder(IFrameMediator mediator) {
055: super ();
056:
057: this .mediator = mediator;
058:
059: try {
060: pluginHandler = PluginManager
061: .getInstance()
062: .getExtensionHandler(
063: IExtensionHandlerKeys.ORG_COLUMBA_CORE_ACTION);
064: } catch (PluginHandlerNotFoundException e) {
065: e.printStackTrace();
066: }
067: }
068:
069: public ExtendableToolBar createToolBar(InputStream is) {
070: ExtendableToolBar toolBar = new ExtendableToolBar();
071:
072: extendToolBar(toolBar, is);
073:
074: return toolBar;
075: }
076:
077: public void extendToolBar(ExtendableToolBar toolBar, InputStream is) {
078:
079: Document doc = retrieveDocument(is);
080:
081: Element toolBarElement = doc.getRootElement();
082: if (toolBarElement.getName().equals("toolbar") == false) {
083: LOG.severe("root element <toolbar> expected, but was "
084: + toolBarElement.getName());
085: return;
086: }
087:
088: Iterator it = toolBarElement.getChildren().listIterator();
089: while (it.hasNext()) {
090: Element menuElement = (Element) it.next();
091: if (menuElement.getName().equals("button")) {
092:
093: String actionId = menuElement.getAttributeValue("id");
094: // deprecated config-file support
095: if (actionId == null)
096: actionId = menuElement.getAttributeValue("action");
097:
098: // deprecated config-file support
099: // -> skip creation of "Cancel" button
100: if (actionId.equals("Cancel"))
101: continue;
102:
103: AbstractColumbaAction action = getAction(actionId,
104: mediator);
105: if (action == null)
106: continue;
107:
108: toolBar.add(action);
109:
110: } else if (menuElement.getName().equals("separator")) {
111: toolBar.addSeparator();
112: } else
113: LOG.severe("unkown element tag <"
114: + menuElement.getName() + ">");
115: }
116:
117: }
118:
119: private AbstractColumbaAction getAction(String id,
120: IFrameMediator frameMediator) {
121: if (id == null)
122: throw new IllegalArgumentException("id == null");
123: if (frameMediator == null)
124: throw new IllegalArgumentException("frameMediator == null");
125:
126: IExtension extension = pluginHandler.getExtension(id);
127:
128: AbstractColumbaAction a = null;
129:
130: try {
131: if (extension != null)
132: a = (AbstractColumbaAction) extension
133: .instanciateExtension(new Object[] { frameMediator });
134: } catch (PluginException e) {
135: LOG.severe(e.getMessage());
136: if (Logging.DEBUG)
137: e.printStackTrace();
138:
139: }
140:
141: return a;
142:
143: }
144:
145: /**
146: * @param xmlResource
147: * @return
148: */
149: private Document retrieveDocument(InputStream is) {
150: SAXBuilder builder = new SAXBuilder();
151: builder.setIgnoringElementContentWhitespace(true);
152: Document doc = null;
153: try {
154: doc = builder.build(is);
155: } catch (JDOMException e) {
156: LOG.severe(e.getMessage());
157: e.printStackTrace();
158: } catch (IOException e) {
159: LOG.severe(e.getMessage());
160: e.printStackTrace();
161: }
162: return doc;
163: }
164: }
|