001: /*
002: * Created on 07/03/2004
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2004 Igor Regis da Silva Simões
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: */
023:
024: package br.com.gfpshare.builder.menu;
025:
026: import java.awt.Component;
027: import java.awt.event.ActionListener;
028: import java.net.URL;
029: import java.util.ArrayList;
030: import java.util.Enumeration;
031: import java.util.Iterator;
032:
033: import javax.swing.JMenu;
034: import javax.swing.JMenuItem;
035: import javax.swing.JSeparator;
036: import javax.swing.event.MenuEvent;
037: import javax.swing.event.MenuListener;
038: import javax.xml.parsers.DocumentBuilder;
039: import javax.xml.parsers.DocumentBuilderFactory;
040:
041: import org.w3c.dom.Document;
042: import org.w3c.dom.NamedNodeMap;
043: import org.w3c.dom.Node;
044: import org.w3c.dom.NodeList;
045:
046: import br.com.gfpshare.beans.aplicativos.IconeFactory;
047: import br.com.gfpshare.plugin.core.DynamicClassLoader;
048:
049: /**
050: * @author Igor Regis da Silva Simões
051: */
052: public abstract class MenuBuilder extends JMenu {
053: protected MenuBuilderListener listener = new MenuBuilderListener();
054:
055: private ArrayList menuItens = new ArrayList();
056:
057: private DynamicClassLoader classLoader = DynamicClassLoader
058: .getClassLoader();
059:
060: /**
061: * Cria uma nova instância de MenuJanelaFacotry
062: * @param xmlFile Endereço do arquivo xml
063: * @param fileOpen boolean indicando se existe um arquivo aberto
064: * @throws MenuCreationException
065: */
066: @SuppressWarnings("unchecked")
067: public MenuBuilder(final String xmlFile, final boolean fileOpen)
068: throws MenuCreationException {
069: //Por enquando não vale a pena colocar a criação dos menu em Thread separada
070: // (new Thread(){
071: // {
072: // setPriority(Thread.MIN_PRIORITY);
073: // setDaemon(true);
074: // }
075: // public void run()
076: // {
077: try {
078: Enumeration enu = DynamicClassLoader.getClassLoader()
079: .getResources(xmlFile);
080: while (enu.hasMoreElements()) //for each xml file...
081: {
082: DocumentBuilderFactory factory = DocumentBuilderFactory
083: .newInstance();
084: factory.setValidating(true);
085: factory.setNamespaceAware(true);
086:
087: URL url = (URL) enu.nextElement();
088:
089: DocumentBuilder builder = factory.newDocumentBuilder();
090: Document document = builder.parse(url.openStream(), url
091: .toString()); //We load/parse the file
092:
093: NodeList menus = document.getLastChild()
094: .getChildNodes();//get the menus definition inside this file
095:
096: for (int i = 0; i < menus.getLength(); i++)//for each menu entry
097: {
098: Node n = null;
099: if ((n = menus.item(i)).getNodeName().equals(
100: "MenuItem"))//We build a MenuItem
101: {
102: NamedNodeMap atributos = n.getAttributes();
103: Node atributo = null;
104:
105: JMenuItem item = new JMenuItem();
106: item
107: .setText((atributos
108: .getNamedItem("Icone") == null ? " "
109: : "")
110: + MenuBuilderMessages
111: .getMessages()
112: .getString(
113: n
114: .getLastChild()
115: .getNodeValue()
116: .trim()));
117:
118: if ((atributo = atributos
119: .getNamedItem("SecurityName")) != null)
120: item.setEnabled(hasAccess(atributo
121: .getNodeValue()));
122: if ((atributo = atributos
123: .getNamedItem("Action")) != null)
124: item
125: .addActionListener((ActionListener) classLoader
126: .loadClass(
127: atributo
128: .getNodeValue())
129: .getDeclaredMethod(
130: "getAction",
131: new Class[] {})
132: .invoke(null,
133: new Object[] {}));
134: if ((atributo = atributos
135: .getNamedItem("Command")) != null)
136: item.setActionCommand(atributo
137: .getNodeValue().trim());
138: if ((atributo = atributos
139: .getNamedItem("Mnemonic")) != null)
140: item.setMnemonic(MenuBuilderMessages
141: .getMessages().getString(
142: atributo.getNodeValue())
143: .charAt(0));
144: if ((atributo = atributos
145: .getNamedItem("ToolTip")) != null)
146: item.setToolTipText(MenuBuilderMessages
147: .getMessages().getString(
148: atributo.getNodeValue()
149: .trim()));
150: if ((atributo = atributos.getNamedItem("Icone")) != null)
151: item.setIcon(IconeFactory.getIconeFactory()
152: .getImageIcon(
153: atributo.getNodeValue()));
154: if (!fileOpen
155: && (atributo = atributos
156: .getNamedItem("NeedFileOpen")) != null)
157: item.setEnabled(!Boolean.valueOf(
158: atributo.getNodeValue())
159: .booleanValue());
160:
161: MenuBuilder.this .menuItens.add(item);
162: } else if ((n = menus.item(i)).getNodeName()
163: .equals("Separator"))//We build a Separator
164: {
165: MenuBuilder.this .menuItens
166: .add(new JSeparator());
167: } else if ((n = menus.item(i)).getNodeName()
168: .equals("MenuInterno"))//We build a Submenu
169: {
170:
171: NamedNodeMap atributos = n.getAttributes();
172: Node atributo = null;
173: String builderName = null;
174: /*TODO É necessário controle de acesso para menus?
175: if ((atributo = atributos.getNamedItem("SecurityName")) != null)
176: System.out.println(atributo.getNodeValue());
177: */
178:
179: builderName = atributos.getNamedItem("Builder")
180: .getNodeValue();
181: JMenu menu = (JMenu) classLoader.loadClass(
182: builderName).getConstructor(
183: new Class[] { boolean.class })
184: .newInstance(
185: new Object[] { Boolean
186: .valueOf(fileOpen) });
187:
188: menu.setText(" "
189: + MenuBuilderMessages.getMessages()
190: .getString(
191: n.getLastChild()
192: .getNodeValue()
193: .trim()));
194:
195: if ((atributo = atributos
196: .getNamedItem("Mnemonic")) != null)
197: menu.setMnemonic(atributo.getNodeValue()
198: .charAt(0));
199:
200: MenuBuilder.this .menuItens.add(menu);
201: }
202: }
203: }
204: } catch (Exception e) {
205: throw new MenuCreationException(e);
206: }
207:
208: rebuildMenu(false);
209: addMenuListener(listener);
210: }
211:
212: // }).start();
213: // }
214:
215: /**
216: * Indica se o usuário atual possui acesso pára o determinado nivel de segurança
217: * @param securityLevel Nivel de segurança a ser checado o direito de acesso
218: * @return Indica false caso o ususário não possua acesso
219: */
220: public abstract boolean hasAccess(String securityLevel);
221:
222: /**
223: * Este meptodo reconstrói o menu
224: * @param removeAll Indica se devem ser removidos todos os itens
225: */
226: protected void rebuildMenu(boolean removeAll) {
227: if (removeAll)
228: this .removeAll();
229:
230: Iterator menus = this .menuItens.iterator();
231: while (menus.hasNext()) {
232: add((Component) menus.next());
233: }
234: }
235:
236: /**
237: *
238: */
239: private class MenuBuilderListener implements MenuListener {
240: /**
241: * @see javax.swing.event.MenuListener#menuSelected(javax.swing.event.MenuEvent)
242: */
243: public void menuSelected(MenuEvent e) {
244: rebuildMenu(true);
245: }
246:
247: /**
248: * @see javax.swing.event.MenuListener#menuDeselected(javax.swing.event.MenuEvent)
249: */
250: public void menuDeselected(MenuEvent e) {
251: // TODO Auto-generated method stub
252:
253: }
254:
255: /**
256: * @see javax.swing.event.MenuListener#menuCanceled(javax.swing.event.MenuEvent)
257: */
258: public void menuCanceled(MenuEvent e) {
259: // TODO Auto-generated method stub
260:
261: }
262: }
263: }
|