2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011 - DIGITEO - Vincent Couvert
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 package org.scilab.modules.graphic_objects.utils;
15 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CALLBACKTYPE__;
16 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CALLBACK__;
17 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_HIDDEN__;
18 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_ACCELERATOR__;
19 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_ENABLE__;
20 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_ICON__;
21 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_LABEL__;
22 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_MNEMONIC__;
23 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_SEPARATOR__;
24 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UIMENU__;
27 import java.io.IOException;
28 import java.lang.reflect.InvocationHandler;
29 import java.lang.reflect.InvocationTargetException;
30 import java.lang.reflect.Method;
31 import java.lang.reflect.Proxy;
32 import java.util.Collection;
33 import java.util.TreeSet;
35 import javax.xml.parsers.ParserConfigurationException;
37 import org.scilab.modules.commons.ScilabConstants;
38 import org.scilab.modules.commons.xml.ScilabDocumentBuilderFactory;
39 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
40 import org.scilab.modules.graphic_objects.graphicObject.CallBack;
41 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject.Type;
42 import org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties;
43 import org.scilab.modules.gui.SwingView;
44 import org.scilab.modules.localization.Messages;
45 import org.w3c.dom.Document;
46 import org.w3c.dom.NamedNodeMap;
47 import org.w3c.dom.Node;
48 import org.w3c.dom.NodeList;
49 import org.xml.sax.SAXException;
53 * Create a menuBar from an XML file
54 * @author Vincent COUVERT
56 public final class MenuBarBuilder {
58 private static final String MAINMENUBARXMLFILE = ScilabConstants.SCI + "/modules/gui/etc/main_menubar.xml";
59 private static final String GRAPHICSMENUBARXMLFILE = ScilabConstants.SCI + "/modules/gui/etc/graphics_menubar.xml";
61 private static final String FILE_NOT_FOUND = "Could not find file: ";
63 private static final String CANNOT_CREATE_MENUBAR = "Cannot create MenuBar.\n"
64 + "Check if file *_menubar.xml is available and valid.";
66 private static boolean isParentValid = true;;
71 private MenuBarBuilder() {
72 throw new UnsupportedOperationException();
76 * Create a proxy to access a XML file
77 * @param resultClass class for the return value
78 * @param fileToLoad XML file to load
79 * @return a proxy used to read the XML file
80 * @throws SAXException can be thrown when an error occurs while reading the file
81 * @throws IOException can be thrown when an error occurs while accessing the file
82 * @throws ParserConfigurationException can be thrown when an error occurs while parsing the file
84 public static Object buildMenuBar(Class[] resultClass, String fileToLoad) throws SAXException, IOException, ParserConfigurationException {
86 InvocationHandler invocationHandler = new MenuBarConfigurationHandler(fileToLoad);
88 return Proxy.newProxyInstance(invocationHandler.getClass().getClassLoader(), resultClass, invocationHandler);
92 * Create console menubar from data in a XML file
93 * @param consoleId the console
95 public static void buildConsoleMenuBar(String consoleId) {
96 buildMenuBar(MAINMENUBARXMLFILE, consoleId);
100 * Create graphic figure menubar from data in a XML file
101 * @param figureId the figure
103 public static void buildFigureMenuBar(String figureId) {
104 if (!SwingView.isHeadless()) {
105 MenuBarBuilder.isParentValid = false;
106 buildMenuBar(GRAPHICSMENUBARXMLFILE, figureId);
111 * Create children used in the menubar from data in a XML file
112 * @param fileToLoad XML file to load
113 * @param parentId the menubar parent
115 public static void buildMenuBar(String fileToLoad, String parentId) {
118 MenuBarConfiguration menuBarConfig =
119 (MenuBarConfiguration) buildMenuBar(new Class[] {MenuBarConfiguration.class}, fileToLoad);
120 menuBarConfig.addMenus(parentId);
121 } catch (IllegalArgumentException e) {
122 System.err.println(CANNOT_CREATE_MENUBAR);
123 System.err.println(FILE_NOT_FOUND + e.getLocalizedMessage());
124 } catch (SAXException e) {
125 System.err.println(CANNOT_CREATE_MENUBAR);
126 System.err.println(FILE_NOT_FOUND + e.getLocalizedMessage());
127 } catch (IOException e) {
128 System.err.println(CANNOT_CREATE_MENUBAR);
129 System.err.println(FILE_NOT_FOUND + e.getLocalizedMessage());
130 } catch (ParserConfigurationException e) {
131 System.err.println(CANNOT_CREATE_MENUBAR);
132 System.err.println(FILE_NOT_FOUND + e.getLocalizedMessage());
137 * Class used to read the XMl file
139 private static class MenuBarConfigurationHandler implements InvocationHandler {
140 protected static final String LABEL = "label";
141 protected static final String MENU = "menu";
142 protected static final String MNEMONIC = "mnemonic";
143 protected static final String SUBMENU = "submenu";
144 protected static final String SEPARATOR = "separator";
145 protected static final String ENABLED = "enabled";
146 protected static final String ACCELERATOR = "accelerator";
147 protected static final String CALLBACK = "callback";
148 protected static final String TYPE = "type";
149 protected static final String INSTRUCTION = "instruction";
150 protected static final String TRUE = "true";
151 protected static final String ICON = "icon";
153 private Document dom;
154 private Collection<String> internalMethodNames;
159 * @param xmlFile XML file to load
160 * @throws SAXException can be thrown when an error occurs while reading the file
161 * @throws IOException can be thrown when an error occurs while accessing the file
162 * @throws ParserConfigurationException can be thrown when an error occurs while parsing the file
164 public MenuBarConfigurationHandler(String xmlFile) throws SAXException, IOException, ParserConfigurationException {
166 if (!new File(xmlFile).exists()) {
167 throw new java.io.IOException();
170 // Build dictionary for internal method
171 internalMethodNames = new TreeSet<String>();
172 Method[] internalMethodes = this.getClass().getMethods();
173 for (Method method : internalMethodes) {
174 internalMethodNames.add(method.getName());
177 // Build xml document for request
178 dom = ScilabDocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(xmlFile));
182 * Invoke a proxy to get data
183 * @param proxy the proxy to call
184 * @param method the method to call
185 * @param args the arguments for the method
186 * @return the object read
187 * @throws IllegalAccessException thrown when the method called is inaccessible
188 * @throws InvocationTargetException thorwn when the method called threw an exception
189 * @throws NoSuchMethodException thrown when invoking a non-existing method
190 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
192 public Object invoke(Object proxy, Method method, Object[] args)
193 throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
194 if (internalMethodNames.contains(method.getName())) {
195 return getClass().getMethod(method.getName(), method.getParameterTypes()).invoke(this, args);
202 * Add menus to a menubar using Scilab MVC
203 * @param parentId the tab ID to which the menus will be added to
204 * @see org.scilab.modules.MenuBarConfiguration.utils.MenuBarConfiguration#addMenus(org.scilab.modules.gui.menubar.MenuBar)
206 public void addMenus(String parentId) {
208 for (String childId : (String []) GraphicController.getController().getProperty(parentId, GraphicObjectProperties.__GO_CHILDREN__)) {
209 if (GraphicController.getController().getProperty(childId, GraphicObjectProperties.__GO_TYPE__).equals(__GO_UIMENU__))
210 GraphicController.getController().removeRelationShipAndDelete(childId);
213 NodeList menus = dom.getElementsByTagName(MENU);
215 for (int i = 0; i < menus.getLength(); i++) {
217 String menuId = null;
219 menuId = GraphicController.getController().askObject(Type.UIMENU);
221 menuId = GraphicController.getController().askObject(Type.UIMENUMODEL);
224 // The menu is not visible in Scilab view by default
225 GraphicController.getController().setProperty(menuId, __GO_HIDDEN__, true);
228 String menuLabel = Messages.gettext(menus.item(i).getAttributes().getNamedItem(LABEL).getNodeValue());
229 GraphicController.getController().setProperty(menuId, __GO_UI_LABEL__, menuLabel);
231 // Set the mnemonic if given
232 if (menus.item(i).getAttributes().getNamedItem(MNEMONIC) != null) {
233 String mnemonicString = menus.item(i).getAttributes().getNamedItem(MNEMONIC).getNodeValue();
234 GraphicController.getController().setProperty(menuId, __GO_UI_MNEMONIC__, mnemonicString);
237 // Set the icon if given
238 if (menus.item(i).getAttributes().getNamedItem(ICON) != null) {
239 String iconName = menus.item(i).getNodeValue();
240 GraphicController.getController().setProperty(menuId, __GO_UI_ICON__, iconName);
243 // Set the enable status if given
244 if (menus.item(i).getAttributes().getNamedItem(ENABLED) != null) {
245 boolean enabled = menus.item(i).getAttributes().getNamedItem(ENABLED).getNodeValue().equals(TRUE);
246 GraphicController.getController().setProperty(menuId, __GO_UI_ENABLE__, enabled);
248 // Set the menu parent
249 GraphicController.getController().setGraphicObjectRelationship(parentId, menuId);
250 addSubMenus(menuId, i);
255 * Read submenus data in the XML file and create them using Scilab MVC
256 * @param parentMenuId the parent menu UID for submenus
257 * @param index the index of the parent in menu list
259 public void addSubMenus(String parentMenuId, int index) {
260 Node submenu = dom.getElementsByTagName(MENU).item(index).getFirstChild();
262 boolean separator = false;
264 while (submenu != null) {
265 if (submenu.getNodeName() == SEPARATOR) {
268 } else if (submenu.getNodeName() == SUBMENU) {
270 String menuId = null;
272 menuId = GraphicController.getController().askObject(Type.UIMENU);
274 menuId = GraphicController.getController().askObject(Type.UIMENUMODEL);
277 // The menu is not visible in Scilab view by default
278 GraphicController.getController().setProperty(menuId, __GO_HIDDEN__, true);
280 // Set the menu parent
281 GraphicController.getController().setGraphicObjectRelationship(parentMenuId, menuId);
283 // First we have to read its attributes
284 NamedNodeMap attributes = submenu.getAttributes();
286 for (int i = 0; i < attributes.getLength(); i++) {
287 if (attributes.item(i).getNodeName() == LABEL) {
289 String menuLabel = Messages.gettext(attributes.item(i).getNodeValue());
290 GraphicController.getController().setProperty(menuId, __GO_UI_LABEL__, menuLabel);
291 } else if (attributes.item(i).getNodeName() == MNEMONIC) {
293 String mnemonicString = attributes.item(i).getNodeValue();
294 GraphicController.getController().setProperty(menuId, __GO_UI_MNEMONIC__, mnemonicString);
295 } else if (attributes.item(i).getNodeName() == ENABLED) {
296 // Set the enable status
297 boolean enabled = attributes.item(i).getNodeValue().equals(TRUE);
298 GraphicController.getController().setProperty(menuId, __GO_UI_ENABLE__, enabled);
299 } else if (attributes.item(i).getNodeName() == ICON) {
301 String iconName = attributes.item(i).getNodeValue();
302 GraphicController.getController().setProperty(menuId, __GO_UI_ICON__, iconName);
303 } else if (attributes.item(i).getNodeName() == ACCELERATOR) {
304 // Set the accelerator
305 String acceleratorString = attributes.item(i).getNodeValue();
306 GraphicController.getController().setProperty(menuId, __GO_UI_ACCELERATOR__, acceleratorString);
310 // Then we get its callback (if exists)
311 Node callback = submenu.getFirstChild();
312 while (callback != null) {
313 if (callback.getNodeName() == CALLBACK) {
314 NamedNodeMap cbAttributes = callback.getAttributes();
315 String command = null;
316 int commandType = CallBack.UNTYPED;
317 for (int j = 0; j < cbAttributes.getLength(); j++) {
318 if (cbAttributes.item(j).getNodeName() == INSTRUCTION) {
319 command = cbAttributes.item(j).getNodeValue();
320 } else if (cbAttributes.item(j).getNodeName() == TYPE) {
321 commandType = Integer.parseInt(cbAttributes.item(j).getNodeValue());
324 if (command != null && commandType != CallBack.UNTYPED) {
325 GraphicController.getController().setProperty(menuId, __GO_CALLBACK__, command);
326 GraphicController.getController().setProperty(menuId, __GO_CALLBACKTYPE__, commandType);
328 } else if (callback.getNodeName() == SUBMENU) {
329 addSubMenuItem(menuId, callback);
332 callback = callback.getNextSibling();
336 GraphicController.getController().setProperty(menuId, __GO_UI_SEPARATOR__, true);
343 submenu = submenu.getNextSibling();
348 * Add submenu for menu
349 * @param parentMenuItemId object with this id will become a menu with subMenuItems
350 * @param node to get attributs of the menu
352 public void addSubMenuItem(String parentMenuItemId, Node node) {
354 NamedNodeMap attributes = node.getAttributes();
357 String subMenuItemId = null;
359 subMenuItemId = GraphicController.getController().askObject(Type.UIMENU);
361 subMenuItemId = GraphicController.getController().askObject(Type.UIMENUMODEL);
364 // The menu is not visible in Scilab view by default
365 GraphicController.getController().setProperty(subMenuItemId, __GO_HIDDEN__, true);
367 // Set the menu parent
368 GraphicController.getController().setGraphicObjectRelationship(parentMenuItemId, subMenuItemId);
370 for (int i = 0; i < attributes.getLength(); i++) {
371 if (attributes.item(i).getNodeName() == LABEL) {
373 String menuLabel = Messages.gettext(attributes.item(i).getNodeValue());
374 GraphicController.getController().setProperty(subMenuItemId, __GO_UI_LABEL__, menuLabel);
375 } else if (attributes.item(i).getNodeName() == MNEMONIC) {
377 String mnemonicString = attributes.item(i).getNodeValue();
378 GraphicController.getController().setProperty(subMenuItemId, __GO_UI_MNEMONIC__, mnemonicString);
379 } else if (attributes.item(i).getNodeName() == ICON) {
380 String iconName = attributes.item(i).getNodeValue();
381 GraphicController.getController().setProperty(subMenuItemId, __GO_UI_ICON__, iconName);
382 } else if (attributes.item(i).getNodeName() == ENABLED) {
383 // Set the enable status
384 boolean enabled = attributes.item(i).getNodeValue().equals(TRUE);
385 GraphicController.getController().setProperty(subMenuItemId, __GO_UI_ENABLE__, enabled);
389 // Then we get its callback (if exists)
390 Node callback = node.getFirstChild();
391 while (callback != null) {
392 if (callback.getNodeName() == CALLBACK) {
393 NamedNodeMap cbAttributes = callback.getAttributes();
394 String command = null;
395 int commandType = CallBack.UNTYPED;
396 for (int j = 0; j < cbAttributes.getLength(); j++) {
397 if (cbAttributes.item(j).getNodeName() == INSTRUCTION) {
398 command = cbAttributes.item(j).getNodeValue();
399 } else if (cbAttributes.item(j).getNodeName() == TYPE) {
400 commandType = Integer.parseInt(cbAttributes.item(j).getNodeValue());
403 if (command != null && commandType != CallBack.UNTYPED) {
404 GraphicController.getController().setProperty(subMenuItemId, __GO_CALLBACK__, command);
405 GraphicController.getController().setProperty(subMenuItemId, __GO_CALLBACKTYPE__, commandType);
407 } else if (callback.getNodeName() == SUBMENU) {
408 addSubMenuItem(subMenuItemId, callback);
411 callback = callback.getNextSibling();