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: package br.com.gfpshare.builder.menu;
024:
025: import java.net.URL;
026: import java.util.ArrayList;
027: import java.util.Enumeration;
028: import java.util.Iterator;
029:
030: import javax.swing.JMenu;
031: import javax.xml.parsers.DocumentBuilder;
032: import javax.xml.parsers.DocumentBuilderFactory;
033:
034: import org.w3c.dom.Document;
035: import org.w3c.dom.NamedNodeMap;
036: import org.w3c.dom.Node;
037: import org.w3c.dom.NodeList;
038:
039: import br.com.gfpshare.beans.IMenuBar;
040: import br.com.gfpshare.plugin.core.DynamicClassLoader;
041:
042: /**
043: * @author Igor Regis da Silva Simões
044: */
045: public abstract class MenuBarBuilder extends IMenuBar {
046: private ArrayList<JMenu> menus = new ArrayList<JMenu>();
047:
048: private DynamicClassLoader classLoader = DynamicClassLoader
049: .getClassLoader();
050:
051: /**
052: * Cria uma nova instância de MenuJanelaFacotry
053: * @param xmlFile
054: * @param fileOpen boolean indicando se existe um arquivo aberto
055: * @throws MenuCreationException
056: */
057: public MenuBarBuilder(String xmlFile, boolean fileOpen)
058: throws MenuCreationException {
059: try {
060: Enumeration enu = DynamicClassLoader.getClassLoader()
061: .getResources(xmlFile);
062: while (enu.hasMoreElements()) //for each xml file...
063: {
064: DocumentBuilderFactory factory = DocumentBuilderFactory
065: .newInstance();
066: factory.setValidating(true);
067: factory.setNamespaceAware(true);
068:
069: URL url = (URL) enu.nextElement();
070:
071: DocumentBuilder builder = factory.newDocumentBuilder();
072: Document document = builder.parse(url.openStream(), url
073: .toString()); //We load/parse the file
074:
075: NodeList menus = document.getLastChild()
076: .getChildNodes();
077:
078: for (int i = 0; i < menus.getLength(); i++) {
079: Node n = null;
080: if ((n = menus.item(i)).getNodeName()
081: .equals("Menu")) {
082:
083: NamedNodeMap atributos = n.getAttributes();
084: Node atributo = null;
085: String builderName = null;
086: /*TODO É necessário controle de acesso para menus?
087: if ((atributo = atributos.getNamedItem("SecurityName")) != null)
088: System.out.println(atributo.getNodeValue());
089: */
090:
091: builderName = atributos.getNamedItem("Builder")
092: .getNodeValue();
093: JMenu menu = (JMenu) classLoader.loadClass(
094: builderName).getConstructor(
095: new Class[] { boolean.class })
096: .newInstance(
097: new Object[] { Boolean
098: .valueOf(fileOpen) });
099:
100: menu.setText(MenuBuilderMessages.getMessages()
101: .getString(
102: n.getLastChild().getNodeValue()
103: .trim()));
104:
105: if ((atributo = atributos
106: .getNamedItem("Mnemonic")) != null)
107: menu.setMnemonic(atributo.getNodeValue()
108: .charAt(0));
109:
110: MenuBarBuilder.this .menus.add(menu);
111: }
112: }
113: }
114: } catch (Exception e) {
115: throw new MenuCreationException(e);
116: }
117: rebuildMenu();
118: }
119:
120: /**
121: * Indica se o usuário atual possui acesso pára o determinado nivel de segurança
122: * @param securityLevel Nivel de segurança a ser checado o direito de acesso
123: * @return Indica false caso o ususário não possua acesso
124: */
125: public abstract boolean hasAccess(String securityLevel);
126:
127: /**
128: *
129: */
130: public void rebuildMenu() {
131: this .removeAll();
132:
133: Iterator menus = this .menus.iterator();
134: while (menus.hasNext()) {
135: add((JMenu) menus.next());
136: }
137: }
138: }
|