KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > config > ButtonEventConfig


1 /*
2  * $Id: ButtonEventConfig.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.pos.config;
26
27 import java.lang.reflect.Method JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.awt.AWTEvent JavaDoc;
33
34 import net.xoetrope.swing.XButton;
35
36 import org.ofbiz.base.config.GenericConfigException;
37 import org.ofbiz.base.config.ResourceLoader;
38 import org.ofbiz.base.util.GeneralException;
39 import org.ofbiz.base.util.UtilValidate;
40 import org.ofbiz.base.util.UtilXml;
41 import org.ofbiz.base.util.Debug;
42 import org.ofbiz.base.util.cache.UtilCache;
43 import org.ofbiz.pos.screen.PosScreen;
44
45 import org.w3c.dom.Element JavaDoc;
46
47 /**
48  *
49  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
50  * @version $Rev: 5462 $
51  * @since 3.1
52  */

53 public class ButtonEventConfig implements java.io.Serializable JavaDoc {
54
55     public static final String JavaDoc module = ButtonEventConfig.class.getName();
56     public static final String JavaDoc BUTTON_EVENT_CONFIG = "buttonevents.xml";
57     private static transient UtilCache buttonConfig = new UtilCache("pos.ButtonEvent", 0, 0, 0, false, true);
58
59     protected String JavaDoc buttonName = null;
60     protected String JavaDoc className = null;
61     protected String JavaDoc method = null;
62     protected int keyCode = -1;
63     protected boolean disableLock = false;
64
65     public static void loadButtonConfig() throws GenericConfigException {
66         Element JavaDoc root = ResourceLoader.getXmlRootElement(ButtonEventConfig.BUTTON_EVENT_CONFIG);
67         List JavaDoc buttonEvents = UtilXml.childElementList(root, "event");
68         if (!UtilValidate.isEmpty(buttonEvents)) {
69             Iterator JavaDoc i = buttonEvents.iterator();
70             while (i.hasNext()) {
71                 Element JavaDoc e = (Element JavaDoc) i.next();
72                 ButtonEventConfig bef = new ButtonEventConfig(e);
73                 buttonConfig.put(bef.getName(), bef);
74             }
75         }
76     }
77
78     public static boolean isLockable(String JavaDoc buttonName) {
79         ButtonEventConfig bef = (ButtonEventConfig) buttonConfig.get(buttonName);
80         if (bef == null) {
81             return true;
82         }
83         return bef.isLockable();
84     }
85
86     public static void invokeButtonEvents(List JavaDoc buttonNames, PosScreen pos) throws ButtonEventNotFound, ButtonEventException {
87         if (buttonNames != null) {
88             Iterator JavaDoc i = buttonNames.iterator();
89             while (i.hasNext()) {
90                 invokeButtonEvent(((String JavaDoc) i.next()), pos, null);
91             }
92         }
93     }
94
95     public static void invokeButtonEvent(String JavaDoc buttonName, PosScreen pos, AWTEvent JavaDoc event) throws ButtonEventNotFound, ButtonEventException {
96         // load / re-load the button configs
97
if (buttonConfig.size() == 0) {
98             synchronized(ButtonEventConfig.class) {
99                 try {
100                     loadButtonConfig();
101                 } catch (GenericConfigException e) {
102                     Debug.logError(e, module);
103                 }
104             }
105         }
106
107         // check for sku mapping buttons
108
if (buttonName.startsWith("SKU.")) {
109             buttonName = "menuSku";
110         }
111
112         // invoke the button event
113
ButtonEventConfig bef = (ButtonEventConfig) buttonConfig.get(buttonName);
114         if (bef == null) {
115             throw new ButtonEventNotFound("No button definition found for button - " + buttonName);
116         }
117         bef.invoke(pos, event);
118     }
119
120     public static List JavaDoc findButtonKeyAssign(int keyCode) {
121         List JavaDoc buttonAssignments = new ArrayList JavaDoc();
122         Iterator JavaDoc i = buttonConfig.values().iterator();
123         while (i.hasNext()) {
124             ButtonEventConfig bef = (ButtonEventConfig) i.next();
125             if (bef.getKeyCode() == keyCode) {
126                 buttonAssignments.add(bef.getName());
127             }
128         }
129
130         return buttonAssignments;
131     }
132
133     public static String JavaDoc getButtonName(AWTEvent JavaDoc event) {
134         if (event == null) {
135             throw new IllegalArgumentException JavaDoc("AWTEvent parameter cannot be null");
136         }
137         Object JavaDoc source = event.getSource();
138         if (source instanceof XButton) {
139             XButton button = (XButton) source;
140             return button.getName();
141         } else {
142             return null;
143         }
144     }
145
146     protected ButtonEventConfig() {
147     }
148
149     protected ButtonEventConfig(Element JavaDoc element) {
150         this.buttonName = element.getAttribute("button-name");
151         this.className = element.getAttribute("class-name");
152         this.method = element.getAttribute("method-name");
153         String JavaDoc keyCodeStr = element.getAttribute("key-code");
154         if (UtilValidate.isNotEmpty(keyCodeStr)) {
155             try {
156                 this.keyCode = Integer.parseInt(keyCodeStr);
157             } catch (Throwable JavaDoc t) {
158                 Debug.logWarning("Key code definition for button [" + buttonName + "] is not a valid Integer", module);
159             }
160         }
161         this.disableLock = "true".equals(element.getAttribute("disable-lock"));
162     }
163
164     public String JavaDoc getName() {
165         return this.buttonName;
166     }
167
168     public int getKeyCode() {
169         return this.keyCode;
170     }
171
172     public boolean isLockable() {
173         return !disableLock;
174     }
175
176     public void invoke(PosScreen pos, AWTEvent JavaDoc event) throws ButtonEventNotFound, ButtonEventException {
177         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
178         if (cl == null) {
179             Debug.log("Unable to obtain the context classloader", module);
180             cl = this.getClass().getClassLoader();
181         }
182
183         // two variations are available
184
Class JavaDoc[] paramTypes1 = new Class JavaDoc[] { PosScreen.class, AWTEvent JavaDoc.class };
185         Class JavaDoc[] paramTypes2 = new Class JavaDoc[] { PosScreen.class };
186         Object JavaDoc[] params1 = new Object JavaDoc[] { pos, event };
187         Object JavaDoc[] params2 = new Object JavaDoc[] { pos };
188
189         // load the class
190
Class JavaDoc buttonClass = null;
191         try {
192             buttonClass = cl.loadClass(this.className);
193         } catch (ClassNotFoundException JavaDoc e) {
194             throw new ButtonEventNotFound(e);
195         }
196
197         // load the method
198
Method JavaDoc buttonMethod = null;
199         int methodType = 0;
200         try {
201             buttonMethod = buttonClass.getMethod(this.method, paramTypes1);
202             methodType = 1;
203         } catch (NoSuchMethodException JavaDoc e) {
204             try {
205                 buttonMethod = buttonClass.getMethod(this.method, paramTypes2);
206                 methodType = 2;
207             } catch (NoSuchMethodException JavaDoc e2) {
208                 Debug.logError(e2, "No method found with matching signatures - " + this.buttonName, module);
209                 throw new ButtonEventNotFound(e);
210             }
211         }
212
213         // invoke the method
214
try {
215             switch (methodType) {
216                 case 1:
217                     buttonMethod.invoke(null, params1);
218                     break;
219                 case 2:
220                     buttonMethod.invoke(null, params2);
221                     break;
222                 default:
223                     throw new ButtonEventNotFound("No class method found for button - " + this.buttonName);
224             }
225         } catch (IllegalAccessException JavaDoc e) {
226             throw new ButtonEventException(e);
227         } catch (InvocationTargetException JavaDoc e) {
228             throw new ButtonEventException(e);
229         } catch (Throwable JavaDoc t) {
230             throw new ButtonEventException(t);
231         }
232     }
233
234     public static class ButtonEventNotFound extends GeneralException {
235         public ButtonEventNotFound() {
236             super();
237         }
238
239         public ButtonEventNotFound(String JavaDoc str) {
240             super(str);
241         }
242
243         public ButtonEventNotFound(String JavaDoc str, Throwable JavaDoc nested) {
244             super(str, nested);
245         }
246
247         public ButtonEventNotFound(Throwable JavaDoc nested) {
248             super(nested);
249         }
250     }
251
252     public static class ButtonEventException extends GeneralException {
253         public ButtonEventException() {
254             super();
255         }
256
257         public ButtonEventException(String JavaDoc str) {
258             super(str);
259         }
260
261         public ButtonEventException(String JavaDoc str, Throwable JavaDoc nested) {
262             super(str, nested);
263         }
264
265         public ButtonEventException(Throwable JavaDoc nested) {
266             super(nested);
267         }
268     }
269 }
270
Popular Tags