001: /*
002: * Created on 21/11/2004
003: *
004: * ============================================================================
005: * GNU Lesser General Public License
006: * ============================================================================
007: *
008: * Swing Components - visit http://sf.net/projects/gfd
009: *
010: * Copyright (C) 2004 Igor Regis da Silva Simões
011: *
012: * This library is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU Lesser General Public
014: * License as published by the Free Software Foundation; either
015: * version 2.1 of the License, or (at your option) any later version.
016: *
017: * This library is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
020: * Lesser General Public License for more details.
021: *
022: * You should have received a copy of the GNU Lesser General Public
023: * License along with this library; if not, write to the Free Software
024: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
025: */
026: package br.com.igor.beans.aplicativos;
027:
028: import java.awt.event.ActionListener;
029: import java.io.File;
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: import javax.xml.parsers.DocumentBuilder;
034: import javax.xml.parsers.DocumentBuilderFactory;
035:
036: import org.w3c.dom.Document;
037: import org.w3c.dom.NamedNodeMap;
038: import org.w3c.dom.Node;
039: import org.w3c.dom.NodeList;
040:
041: import br.com.igor.plugin.core.DynamicClassLoader;
042:
043: /**
044: * @author Igor Regis da Silva Simoes
045: */
046: public abstract class ShortCutsRegister {
047:
048: private Map modificadores = new HashMap();
049:
050: private Map needFileOpen = new HashMap();
051:
052: /**
053: * @param xmlFile
054: *
055: */
056: public ShortCutsRegister(String xmlFile) {
057: super ();
058: loadShorCuts(xmlFile);
059: }
060:
061: private void loadShorCuts(String xmlFile) {
062: try {
063: DocumentBuilderFactory factory = DocumentBuilderFactory
064: .newInstance();
065: factory.setValidating(true);
066: factory.setNamespaceAware(true);
067: DocumentBuilder builder = factory.newDocumentBuilder();
068: Document document = builder.parse(new File(xmlFile));
069:
070: NodeList menus = document.getLastChild().getChildNodes();
071:
072: for (int i = 0; i < menus.getLength(); i++) {
073: Node n = null;
074: if ((n = menus.item(i)).getNodeName()
075: .equals("ShortCut")) {
076: NamedNodeMap atributos = n.getAttributes();
077: Node atributo = null;
078:
079: if ((atributo = atributos
080: .getNamedItem("SecurityName")) != null)
081: if (!hasAccess(atributo.getNodeValue()))
082: continue;
083:
084: String action = null;
085: String mask = null;
086: String actioKey = null;
087:
088: if ((atributo = atributos.getNamedItem("Mask")) != null) {
089: mask = atributo.getNodeValue();
090: if (modificadores.get(mask) == null) {
091: modificadores.put(mask, new HashMap());
092: }
093: }
094:
095: action = atributos.getNamedItem("Action")
096: .getNodeValue();
097: actioKey = atributos.getNamedItem("ActionKey")
098: .getNodeValue();
099:
100: ((Map) modificadores.get(mask)).put(actioKey,
101: DynamicClassLoader.getClassLoader()
102: .loadClass(action)
103: .getDeclaredMethod("getAction",
104: new Class[] {}).invoke(
105: null, new Object[] {}));
106:
107: if (Boolean.valueOf(
108: atributos.getNamedItem("NeedFileOpen")
109: .getNodeValue()).booleanValue())
110: needFileOpen.put(
111: ((Map) modificadores.get(mask))
112: .get(actioKey), "");
113: }
114: }
115: } catch (Exception e) {
116: //TODO Excecao
117: e.printStackTrace();
118: }
119: }
120:
121: /**
122: * Retorna o ActionListener que ira responder ao evento passado como parâmetro
123: * @param mask Mascara que indica uma tecla como precionada (Ex.: Ctrl)
124: * @param keys Indica a tecla que foi precionada
125: * @return ActionListener que ira responder a este evento
126: */
127: public ActionListener getAction(int mask, int keys) {
128: return (ActionListener) ((Map) modificadores.get("" + mask))
129: .get("" + keys);
130: }
131:
132: /**
133: * Indica se o usuário atual possui acesso pára o determinado nivel de segurança
134: * @param securityLevel Nivel de segurança a ser checado o direito de acesso
135: * @return Indica false caso o ususário não possua acesso
136: */
137: public abstract boolean hasAccess(String securityLevel);
138: }
|