001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.pos.component;
019:
020: import java.awt.Component;
021: import java.awt.Container;
022: import java.awt.AWTEvent;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import net.xoetrope.swing.XButton;
028: import net.xoetrope.xui.helper.SwingWorker;
029:
030: import org.ofbiz.base.config.GenericConfigException;
031: import org.ofbiz.base.util.Debug;
032: import org.ofbiz.base.util.UtilValidate;
033: import org.ofbiz.pos.config.ButtonEventConfig;
034: import org.ofbiz.pos.screen.PosScreen;
035:
036: public class PosButton {
037:
038: public static final String module = PosButton.class.getName();
039:
040: protected Map loadedXButtons = new HashMap();
041: protected PosScreen pos = null;
042:
043: public PosButton(PosScreen pos) {
044: this .pos = pos;
045: this .loadButtons(pos.getComponents());
046:
047: try {
048: ButtonEventConfig.loadButtonConfig();
049: } catch (GenericConfigException e) {
050: Debug.logError(e, module);
051: }
052: }
053:
054: private void loadButtons(Component[] component) {
055: for (int i = 0; i < component.length; i++) {
056: if (component[i] instanceof XButton) {
057: XButton button = (XButton) component[i];
058: String buttonName = button.getName();
059: String styleName = buttonName == null ? null
060: : (String) pos
061: .getAttribute("style", buttonName);
062: PosButtonWrapper wrapper = new PosButtonWrapper(button,
063: styleName);
064: if (UtilValidate.isEmpty(buttonName)) {
065: wrapper.setEnabled(false);
066: } else {
067: pos.addActionHandler(button,
068: PosScreen.BUTTON_ACTION_METHOD);
069: loadedXButtons.put(button.getName(), wrapper);
070: }
071: }
072: if (component[i] instanceof Container) {
073: Component[] subComponents = ((Container) component[i])
074: .getComponents();
075: loadButtons(subComponents);
076: }
077: }
078: }
079:
080: public boolean isLockable(String name) {
081: if (!loadedXButtons.containsKey(name)) {
082: return false;
083: }
084:
085: return ButtonEventConfig.isLockable(name);
086: }
087:
088: public void setLock(boolean lock) {
089: Iterator i = loadedXButtons.keySet().iterator();
090: while (i.hasNext()) {
091: String buttonName = (String) i.next();
092: if (this .isLockable(buttonName) && lock) {
093: this .setLock(buttonName, lock);
094: } else {
095: this .setLock(buttonName, false);
096: }
097: }
098: }
099:
100: public void setLock(String buttonName, boolean lock) {
101: PosButtonWrapper button = (PosButtonWrapper) loadedXButtons
102: .get(buttonName);
103: button.setEnabled(!lock);
104: }
105:
106: public void buttonPressed(final PosScreen pos, final AWTEvent event) {
107: if (pos == null) {
108: Debug
109: .logWarning(
110: "Received a null PosScreen object in buttonPressed event",
111: module);
112: return;
113: }
114: if (event == null) {
115: Debug
116: .logWarning(
117: "Received a null AWTEvent object in buttonPressed event",
118: module);
119: return;
120: }
121: final String buttonName = ButtonEventConfig
122: .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()
130: .setContextClassLoader(cl);
131: }
132: try {
133: ButtonEventConfig.invokeButtonEvent(buttonName,
134: pos, event);
135: } catch (ButtonEventConfig.ButtonEventNotFound e) {
136: Debug.logWarning(e, "Button not found - "
137: + buttonName, module);
138: } catch (ButtonEventConfig.ButtonEventException e) {
139: Debug.logError(e,
140: "Button invocation exception - "
141: + buttonName, module);
142: }
143: return null;
144: }
145: };
146: worker.start();
147: } else {
148: Debug.logWarning(
149: "No button name found for buttonPressed event",
150: module);
151: }
152: }
153:
154: private ClassLoader getClassLoader(PosScreen pos) {
155: ClassLoader cl = pos.getClassLoader();
156: if (cl == null) {
157: try {
158: cl = Thread.currentThread().getContextClassLoader();
159: } catch (Throwable t) {
160: }
161: if (cl == null) {
162: Debug
163: .log(
164: "No context classloader available; using class classloader",
165: module);
166: try {
167: cl = this .getClass().getClassLoader();
168: } catch (Throwable t) {
169: Debug.logError(t, module);
170: }
171: }
172: }
173:
174: return cl;
175: }
176: }
|