KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > component > PosButton


1 /*
2  * $Id: PosButton.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.component;
26
27 import java.awt.Component JavaDoc;
28 import java.awt.Container JavaDoc;
29 import java.awt.AWTEvent JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
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 /**
44  *
45  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  * @version $Rev: 5462 $
47  * @since 3.1
48  */

49 public class PosButton {
50
51     public static final String JavaDoc module = PosButton.class.getName();
52
53     protected Map JavaDoc loadedXButtons = new HashMap JavaDoc();
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 JavaDoc[] component) {
68         for (int i = 0; i < component.length; i++) {
69             if (component[i] instanceof XButton) {
70                 XButton button = (XButton) component[i];
71                 String JavaDoc buttonName = button.getName();
72                 String JavaDoc styleName = buttonName == null ? null : (String JavaDoc) 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 JavaDoc) {
82                 Component JavaDoc[] subComponents = ((Container JavaDoc) component[i]).getComponents();
83                 loadButtons(subComponents);
84             }
85         }
86     }
87
88     public boolean isLockable(String JavaDoc 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 JavaDoc i = loadedXButtons.keySet().iterator();
98         while (i.hasNext()) {
99             String JavaDoc buttonName = (String JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc buttonName = ButtonEventConfig.getButtonName(event);
123         final ClassLoader JavaDoc cl = this.getClassLoader(pos);
124
125         if (buttonName != null) {
126             final SwingWorker worker = new SwingWorker() {
127                 public Object JavaDoc 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 JavaDoc getClassLoader(PosScreen pos) {
148         ClassLoader JavaDoc cl = pos.getClassLoader();
149         if (cl == null) {
150             try {
151                 cl = Thread.currentThread().getContextClassLoader();
152             } catch (Throwable JavaDoc 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 JavaDoc t) {
159                     Debug.logError(t, module);
160                 }
161             }
162         }
163         
164         return cl;
165     }
166 }
167
Popular Tags