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.igor.builder.toolbar;
025:
026: import java.awt.Dimension;
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.JButton;
034: import javax.swing.JComponent;
035: import javax.swing.JSeparator;
036: import javax.swing.JToolBar;
037: import javax.swing.SwingConstants;
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.igor.beans.aplicativos.IconeFactory;
047: import br.com.igor.plugin.core.DynamicClassLoader;
048:
049: /**
050: * @author Igor Regis da Silva Simões
051: */
052: public abstract class ToolBarBuilder extends JToolBar {
053:
054: private ArrayList botoes = new ArrayList();
055:
056: private DynamicClassLoader classLoader = DynamicClassLoader
057: .getClassLoader();
058:
059: /**
060: * Cria uma nova instância de MenuJanelaFacotry
061: * @param xmlFile Endereço do arquivo xml
062: * @param fileOpen boolean indicando se existe um arquivo aberto
063: * @throws ToolBarCreationException
064: */
065: public ToolBarBuilder(String xmlFile, boolean fileOpen)
066: throws ToolBarCreationException {
067: setRollover(true);
068: setMaximumSize(new Dimension(300, 30));
069: setMinimumSize(new Dimension(158, 30));
070: setPreferredSize(new Dimension(158, 30));
071:
072: String exceptionMessage = "reading xml" + xmlFile;
073: try {
074: Enumeration enu = DynamicClassLoader.getClassLoader()
075: .getResources(xmlFile);
076: while (enu.hasMoreElements()) //for each xml file...
077: {
078: DocumentBuilderFactory factory = DocumentBuilderFactory
079: .newInstance();
080: factory.setValidating(true);
081: factory.setNamespaceAware(true);
082:
083: URL url = (URL) enu.nextElement();
084:
085: DocumentBuilder builder = factory.newDocumentBuilder();
086: Document document = builder.parse(url.openStream(), url
087: .toString()); //We load/parse the file
088:
089: NodeList menus = document.getLastChild()
090: .getChildNodes();
091:
092: for (int i = 0; i < menus.getLength(); i++) {
093: Node n = null;
094: if ((n = menus.item(i)).getNodeName().equals(
095: "Button")) {
096: NamedNodeMap atributos = n.getAttributes();
097: Node atributo = null;
098:
099: JButton botao = new JButton();
100:
101: if ((atributo = atributos
102: .getNamedItem("SecurityName")) != null) {
103: exceptionMessage = "setting security params: "
104: + atributo.getNodeValue();
105: botao.setEnabled(hasAccess(atributo
106: .getNodeValue()));
107: }
108: if ((atributo = atributos
109: .getNamedItem("Action")) != null) {
110: exceptionMessage = "setting listener "
111: + atributo.getNodeValue();
112: botao
113: .addActionListener((ActionListener) classLoader
114: .loadClass(
115: atributo
116: .getNodeValue())
117: .getDeclaredMethod(
118: "getAction",
119: new Class[] {})
120: .invoke(null,
121: new Object[] {}));
122: }
123: if ((atributo = atributos
124: .getNamedItem("ToolTip")) != null) {
125: exceptionMessage = "setting toolTip "
126: + atributo.getNodeValue();
127: botao.setToolTipText(ToolBarBuilderMessages
128: .getMessages().getString(
129: atributo.getNodeValue()
130: .trim()));
131: }
132: if ((atributo = atributos.getNamedItem("Icone")) != null) {
133: exceptionMessage = "setting icon "
134: + atributo.getNodeValue();
135: botao.setIcon(IconeFactory
136: .getIconeFactory().getImageIcon(
137: atributo.getNodeValue()));
138: }
139: if (!fileOpen
140: && (atributo = atributos
141: .getNamedItem("NeedFileOpen")) != null) {
142: exceptionMessage = "setting needFileOpen "
143: + atributo.getNodeValue();
144: botao.setEnabled(!Boolean.valueOf(
145: atributo.getNodeValue())
146: .booleanValue());
147: }
148:
149: ToolBarBuilder.this .botoes.add(botao);
150: } else if ((n = menus.item(i)).getNodeName()
151: .equals("Separator")) {
152: JSeparator item = new JSeparator();
153: item.setOrientation(SwingConstants.VERTICAL);
154: item.setMaximumSize(new Dimension(2, 25));
155: ToolBarBuilder.this .botoes.add(item);
156: }
157: }
158: }
159: } catch (Exception e) {
160: throw new ToolBarCreationException(exceptionMessage, e);
161: }
162: rebuildTooBar();
163: }
164:
165: /**
166: *
167: */
168: public void rebuildTooBar() {
169: this .removeAll();
170:
171: Iterator menus = this .botoes.iterator();
172: while (menus.hasNext()) {
173: add((JComponent) menus.next());
174: }
175: }
176:
177: /**
178: * Indica se o usuário atual possui acesso pára o determinado nivel de segurança
179: * @param securityLevel Nivel de segurança a ser checado o direito de acesso
180: * @return Indica false caso o ususário não possua acesso
181: */
182: public abstract boolean hasAccess(String securityLevel);
183:
184: }
|