1 25 package org.ofbiz.pos.component; 26 27 import java.awt.Component ; 28 import java.awt.Container ; 29 import java.awt.AWTEvent ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 34 import net.xoetrope.swing.XButton; 35 import net.xoetrope.xui.helper.SwingWorker; 36 37 import org.ofbiz.base.config.GenericConfigException; 38 import org.ofbiz.base.util.Debug; 39 import org.ofbiz.base.util.UtilValidate; 40 import org.ofbiz.pos.config.ButtonEventConfig; 41 import org.ofbiz.pos.screen.PosScreen; 42 43 49 public class PosButton { 50 51 public static final String module = PosButton.class.getName(); 52 53 protected Map loadedXButtons = new HashMap (); 54 protected PosScreen pos = null; 55 56 public PosButton(PosScreen pos) { 57 this.pos = pos; 58 this.loadButtons(pos.getComponents()); 59 60 try { 61 ButtonEventConfig.loadButtonConfig(); 62 } catch (GenericConfigException e) { 63 Debug.logError(e, module); 64 } 65 } 66 67 private void loadButtons(Component [] component) { 68 for (int i = 0; i < component.length; i++) { 69 if (component[i] instanceof XButton) { 70 XButton button = (XButton) component[i]; 71 String buttonName = button.getName(); 72 String styleName = buttonName == null ? null : (String ) pos.getAttribute("style", buttonName); 73 PosButtonWrapper wrapper = new PosButtonWrapper(button, styleName); 74 if (UtilValidate.isEmpty(buttonName)) { 75 wrapper.setEnabled(false); 76 } else { 77 pos.addActionHandler(button, PosScreen.BUTTON_ACTION_METHOD); 78 loadedXButtons.put(button.getName(), wrapper); 79 } 80 } 81 if (component[i] instanceof Container ) { 82 Component [] subComponents = ((Container ) component[i]).getComponents(); 83 loadButtons(subComponents); 84 } 85 } 86 } 87 88 public boolean isLockable(String name) { 89 if (!loadedXButtons.containsKey(name)) { 90 return false; 91 } 92 93 return ButtonEventConfig.isLockable(name); 94 } 95 96 public void setLock(boolean lock) { 97 Iterator i = loadedXButtons.keySet().iterator(); 98 while (i.hasNext()) { 99 String buttonName = (String ) i.next(); 100 if (this.isLockable(buttonName) && lock) { 101 this.setLock(buttonName, lock); 102 } else { 103 this.setLock(buttonName, false); 104 } 105 } 106 } 107 108 public void setLock(String buttonName, boolean lock) { 109 PosButtonWrapper button = (PosButtonWrapper) loadedXButtons.get(buttonName); 110 button.setEnabled(!lock); 111 } 112 113 public void buttonPressed(final PosScreen pos, final AWTEvent event) { 114 if (pos == null) { 115 Debug.logWarning("Received a null PosScreen object in buttonPressed event", module); 116 return; 117 } 118 if (event == null) { 119 Debug.logWarning("Received a null AWTEvent object in buttonPressed event", module); 120 return; 121 } 122 final String buttonName = ButtonEventConfig.getButtonName(event); 123 final ClassLoader cl = this.getClassLoader(pos); 124 125 if (buttonName != null) { 126 final SwingWorker worker = new SwingWorker() { 127 public Object construct() { 128 if (cl != null) { 129 Thread.currentThread().setContextClassLoader(cl); 130 } 131 try { 132 ButtonEventConfig.invokeButtonEvent(buttonName, pos, event); 133 } catch (ButtonEventConfig.ButtonEventNotFound e) { 134 Debug.logWarning(e, "Button not found - " + buttonName, module); 135 } catch (ButtonEventConfig.ButtonEventException e) { 136 Debug.logError(e, "Button invocation exception - " + buttonName, module); 137 } 138 return null; 139 } 140 }; 141 worker.start(); 142 } else { 143 Debug.logWarning("No button name found for buttonPressed event", module); 144 } 145 } 146 147 private ClassLoader getClassLoader(PosScreen pos) { 148 ClassLoader cl = pos.getClassLoader(); 149 if (cl == null) { 150 try { 151 cl = Thread.currentThread().getContextClassLoader(); 152 } catch (Throwable t) { 153 } 154 if (cl == null) { 155 Debug.log("No context classloader available; using class classloader", module); 156 try { 157 cl = this.getClass().getClassLoader(); 158 } catch (Throwable t) { 159 Debug.logError(t, module); 160 } 161 } 162 } 163 164 return cl; 165 } 166 } 167 | Popular Tags |