001 /****************************************************************************** 002 * Copyright (C) MActor Developers. All rights reserved. * 003 * ---------------------------------------------------------------------------* 004 * This file is part of MActor. * 005 * * 006 * MActor is free software; you can redistribute it and/or modify * 007 * it under the terms of the GNU General Public License as published by * 008 * the Free Software Foundation; either version 2 of the License, or * 009 * (at your option) any later version. * 010 * * 011 * MActor is distributed in the hope that it will be useful, * 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 014 * GNU General Public License for more details. * 015 * * 016 * You should have received a copy of the GNU General Public License * 017 * along with MActor; if not, write to the Free Software * 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * 019 ******************************************************************************/ 020 package org.mactor.ui.gui; 021 022 import java.net.URL; 023 import java.util.HashMap; 024 import java.util.Iterator; 025 import java.util.Map; 026 027 import javax.swing.JMenu; 028 import javax.swing.JMenuItem; 029 import javax.swing.JPopupMenu; 030 import javax.swing.JSeparator; 031 032 import org.dom4j.Document; 033 import org.dom4j.Element; 034 import org.dom4j.io.SAXReader; 035 import org.mactor.framework.MactorException; 036 037 public class MenuBuilder { 038 Map<String, Element> menuElements = new HashMap<String, Element>(); 039 ActionsManager actionManager; 040 public MenuBuilder(ActionsManager actionManager) throws MactorException { 041 this.actionManager = actionManager; 042 try { 043 loadMenuDefs("project_menus.xml"); 044 } catch (Exception e) { 045 throw new MactorException(e); 046 } 047 try { 048 loadMenuDefs("project_menus_ex.xml"); 049 System.out.println("project_menus_ex.xml was loaded"); 050 } catch (Exception e) { 051 System.out.println("project_menus_ex.xml was not loaded. Reason:" + e.getMessage()); 052 } 053 } 054 private void loadMenuDefs(String resource) throws Exception { 055 URL u = Thread.currentThread().getContextClassLoader().getResource(resource); 056 if (u == null) 057 throw new RuntimeException("Resource '" + resource + "' was not found"); 058 Document doc = null; 059 doc = new SAXReader().read(Thread.currentThread().getContextClassLoader().getResourceAsStream(resource)); 060 Iterator it = doc.getRootElement().elementIterator("menu"); 061 while (it.hasNext()) { 062 Element e = (Element) it.next(); 063 menuElements.put(e.attributeValue("name"), e); 064 } 065 } 066 private Element getMenuElement(String name) throws MactorException { 067 Element e = menuElements.get(name); 068 if (e == null) 069 throw new MactorException("The menu '" + name + "' does not exist"); 070 return e; 071 } 072 public JPopupMenu buildPopuMenu(String name) throws MactorException { 073 return createPopupMenu(menuElements.get(name)); 074 } 075 public JMenu buildMenu(String name) throws MactorException { 076 return createMenu(menuElements.get(name)); 077 } 078 private JPopupMenu createPopupMenu(Element e) throws MactorException { 079 JPopupMenu m = new JPopupMenu(e.attributeValue("name")); 080 Iterator it = e.elementIterator(); 081 while (it.hasNext()) { 082 Element child = (Element) it.next(); 083 if ("menu".equals(child.getName())) { 084 m.add(createMenu(getMenuElement(child.attributeValue("name")))); 085 } else if ("action".equals(child.getName())) { 086 JMenuItem item = new JMenuItem(actionManager.getAction(child.attributeValue("name"))); 087 m.add(item); 088 } else if ("seperator".equals(child.getName())) { 089 m.add(new JSeparator()); 090 } else { 091 throw new MactorException("Encountered unexpected node '" + child.getName() + "' while building menu"); 092 } 093 } 094 return m; 095 } 096 private JMenu createMenu(Element e) throws MactorException { 097 JMenu m = new JMenu(e.attributeValue("name")); 098 Iterator it = e.elementIterator(); 099 while (it.hasNext()) { 100 Element child = (Element) it.next(); 101 if ("menu".equals(child.getName())) { 102 m.add(createMenu(getMenuElement(child.attributeValue("name")))); 103 } else if ("action".equals(child.getName())) { 104 JMenuItem item = new JMenuItem(actionManager.getAction(child.attributeValue("name"))); 105 m.add(item); 106 } else if ("seperator".equals(child.getName())) { 107 m.add(new JSeparator()); 108 } else { 109 throw new MactorException("Encountered unexpected node '" + child.getName() + "' while building menu"); 110 } 111 } 112 return m; 113 } 114 }