KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > toolbar > ToolBarXMLDecoder


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.gui.toolbar;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.logging.Logger JavaDoc;
24
25 import org.columba.api.gui.frame.IFrameMediator;
26 import org.columba.api.plugin.IExtension;
27 import org.columba.api.plugin.IExtensionHandler;
28 import org.columba.api.plugin.IExtensionHandlerKeys;
29 import org.columba.api.plugin.PluginException;
30 import org.columba.api.plugin.PluginHandlerNotFoundException;
31 import org.columba.core.gui.action.AbstractColumbaAction;
32 import org.columba.core.logging.Logging;
33 import org.columba.core.plugin.PluginManager;
34 import org.jdom.Document;
35 import org.jdom.Element;
36 import org.jdom.JDOMException;
37 import org.jdom.input.SAXBuilder;
38
39 /**
40  * Create a toolbar from an xml file.
41  *
42  * @author fdietz
43  *
44  */

45 public class ToolBarXMLDecoder {
46
47     private static final Logger JavaDoc LOG = Logger
48             .getLogger("org.columba.core.gui.menu");
49
50     private IExtensionHandler pluginHandler;
51
52     private IFrameMediator mediator;
53
54     public ToolBarXMLDecoder(IFrameMediator mediator) {
55         super();
56
57         this.mediator = mediator;
58
59         try {
60             pluginHandler = PluginManager
61                     .getInstance().getExtensionHandler(IExtensionHandlerKeys.ORG_COLUMBA_CORE_ACTION);
62         } catch (PluginHandlerNotFoundException e) {
63             e.printStackTrace();
64         }
65     }
66
67     public ExtendableToolBar createToolBar(InputStream JavaDoc is) {
68         ExtendableToolBar toolBar = new ExtendableToolBar();
69
70         extendToolBar(toolBar, is);
71
72         return toolBar;
73     }
74
75     public void extendToolBar(ExtendableToolBar toolBar, InputStream JavaDoc is) {
76
77         Document doc = retrieveDocument(is);
78
79         Element toolBarElement = doc.getRootElement();
80         if (toolBarElement.getName().equals("toolbar") == false) {
81             LOG.severe("root element <toolbar> expected, but was "+toolBarElement.getName());
82             return;
83         }
84
85         Iterator JavaDoc it = toolBarElement.getChildren().listIterator();
86         while (it.hasNext()) {
87             Element menuElement = (Element) it.next();
88             if (menuElement.getName().equals("button")) {
89
90                 String JavaDoc actionId = menuElement.getAttributeValue("id");
91                 // deprecated config-file support
92
if (actionId == null)
93                     actionId = menuElement.getAttributeValue("action");
94
95                 // deprecated config-file support
96
// -> skip creation of "Cancel" button
97
if (actionId.equals("Cancel"))
98                     continue;
99
100                 AbstractColumbaAction action = getAction(actionId, mediator);
101                 if ( action == null ) continue;
102                 
103                 toolBar.add(action);
104
105             } else if (menuElement.getName().equals("separator")) {
106                 toolBar.addSeparator();
107             } else
108                 LOG
109                         .severe("unkown element tag <" + menuElement.getName()
110                                 + ">");
111         }
112
113     }
114
115     private AbstractColumbaAction getAction(String JavaDoc id,
116             IFrameMediator frameMediator) {
117         if (id == null)
118             throw new IllegalArgumentException JavaDoc("id == null");
119         if (frameMediator == null)
120             throw new IllegalArgumentException JavaDoc("frameMediator == null");
121
122         IExtension extension = pluginHandler.getExtension(id);
123
124         AbstractColumbaAction a = null;
125
126         try {
127             if (extension != null)
128                 a = (AbstractColumbaAction) extension
129                         .instanciateExtension(new Object JavaDoc[] { frameMediator });
130         } catch (PluginException e) {
131             LOG.severe(e.getMessage());
132             if (Logging.DEBUG)
133                 e.printStackTrace();
134
135         }
136
137         return a;
138
139     }
140
141     /**
142      * @param xmlResource
143      * @return
144      */

145     private Document retrieveDocument(InputStream JavaDoc is) {
146         SAXBuilder builder = new SAXBuilder();
147         builder.setIgnoringElementContentWhitespace(true);
148         Document doc = null;
149         try {
150             doc = builder.build(is);
151         } catch (JDOMException e) {
152             LOG.severe(e.getMessage());
153             e.printStackTrace();
154         } catch (IOException JavaDoc e) {
155             LOG.severe(e.getMessage());
156             e.printStackTrace();
157         }
158         return doc;
159     }
160 }
161
Popular Tags