1 25 package org.ofbiz.pos.config; 26 27 import java.lang.reflect.Method ; 28 import java.lang.reflect.InvocationTargetException ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.ArrayList ; 32 import java.awt.AWTEvent ; 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 ; 46 47 53 public class ButtonEventConfig implements java.io.Serializable { 54 55 public static final String module = ButtonEventConfig.class.getName(); 56 public static final String BUTTON_EVENT_CONFIG = "buttonevents.xml"; 57 private static transient UtilCache buttonConfig = new UtilCache("pos.ButtonEvent", 0, 0, 0, false, true); 58 59 protected String buttonName = null; 60 protected String className = null; 61 protected String method = null; 62 protected int keyCode = -1; 63 protected boolean disableLock = false; 64 65 public static void loadButtonConfig() throws GenericConfigException { 66 Element root = ResourceLoader.getXmlRootElement(ButtonEventConfig.BUTTON_EVENT_CONFIG); 67 List buttonEvents = UtilXml.childElementList(root, "event"); 68 if (!UtilValidate.isEmpty(buttonEvents)) { 69 Iterator i = buttonEvents.iterator(); 70 while (i.hasNext()) { 71 Element e = (Element ) i.next(); 72 ButtonEventConfig bef = new ButtonEventConfig(e); 73 buttonConfig.put(bef.getName(), bef); 74 } 75 } 76 } 77 78 public static boolean isLockable(String 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 buttonNames, PosScreen pos) throws ButtonEventNotFound, ButtonEventException { 87 if (buttonNames != null) { 88 Iterator i = buttonNames.iterator(); 89 while (i.hasNext()) { 90 invokeButtonEvent(((String ) i.next()), pos, null); 91 } 92 } 93 } 94 95 public static void invokeButtonEvent(String buttonName, PosScreen pos, AWTEvent event) throws ButtonEventNotFound, ButtonEventException { 96 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 if (buttonName.startsWith("SKU.")) { 109 buttonName = "menuSku"; 110 } 111 112 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 findButtonKeyAssign(int keyCode) { 121 List buttonAssignments = new ArrayList (); 122 Iterator 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 getButtonName(AWTEvent event) { 134 if (event == null) { 135 throw new IllegalArgumentException ("AWTEvent parameter cannot be null"); 136 } 137 Object 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 element) { 150 this.buttonName = element.getAttribute("button-name"); 151 this.className = element.getAttribute("class-name"); 152 this.method = element.getAttribute("method-name"); 153 String keyCodeStr = element.getAttribute("key-code"); 154 if (UtilValidate.isNotEmpty(keyCodeStr)) { 155 try { 156 this.keyCode = Integer.parseInt(keyCodeStr); 157 } catch (Throwable 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 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 event) throws ButtonEventNotFound, ButtonEventException { 177 ClassLoader 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 Class [] paramTypes1 = new Class [] { PosScreen.class, AWTEvent .class }; 185 Class [] paramTypes2 = new Class [] { PosScreen.class }; 186 Object [] params1 = new Object [] { pos, event }; 187 Object [] params2 = new Object [] { pos }; 188 189 Class buttonClass = null; 191 try { 192 buttonClass = cl.loadClass(this.className); 193 } catch (ClassNotFoundException e) { 194 throw new ButtonEventNotFound(e); 195 } 196 197 Method buttonMethod = null; 199 int methodType = 0; 200 try { 201 buttonMethod = buttonClass.getMethod(this.method, paramTypes1); 202 methodType = 1; 203 } catch (NoSuchMethodException e) { 204 try { 205 buttonMethod = buttonClass.getMethod(this.method, paramTypes2); 206 methodType = 2; 207 } catch (NoSuchMethodException e2) { 208 Debug.logError(e2, "No method found with matching signatures - " + this.buttonName, module); 209 throw new ButtonEventNotFound(e); 210 } 211 } 212 213 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 e) { 226 throw new ButtonEventException(e); 227 } catch (InvocationTargetException e) { 228 throw new ButtonEventException(e); 229 } catch (Throwable 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 str) { 240 super(str); 241 } 242 243 public ButtonEventNotFound(String str, Throwable nested) { 244 super(str, nested); 245 } 246 247 public ButtonEventNotFound(Throwable 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 str) { 258 super(str); 259 } 260 261 public ButtonEventException(String str, Throwable nested) { 262 super(str, nested); 263 } 264 265 public ButtonEventException(Throwable nested) { 266 super(nested); 267 } 268 } 269 } 270 | Popular Tags |