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.screen;
019:
020: import java.util.Hashtable;
021: import java.util.Iterator;
022: import java.util.Locale;
023: import java.util.Map;
024:
025: import javax.swing.DefaultListModel;
026: import javax.swing.ListSelectionModel;
027:
028: import net.xoetrope.swing.XButton;
029: import net.xoetrope.swing.XDialog;
030: import net.xoetrope.swing.XList;
031: import net.xoetrope.xui.XPage;
032:
033: import org.ofbiz.base.util.Debug;
034: import org.ofbiz.base.util.UtilProperties;
035: import org.ofbiz.pos.PosTransaction;
036:
037: public class LoadSale extends XPage {
038:
039: /**
040: * To load a sale from a shopping list. 2 modes : add to or replace the current sale. Also a button to delete a sale (aka shopping list)
041: */
042: public static final String module = LoadSale.class.getName();
043: protected static PosScreen m_pos = null;
044: protected XDialog m_dialog = null;
045: static protected Hashtable m_saleMap = new Hashtable();
046: protected XList m_salesList = null;
047: protected XButton m_cancel = null;
048: protected XButton m_add = null;
049: protected XButton m_replace = null;
050: protected XButton m_delete = null;
051: protected XButton m_replaceAndDelete = null;
052: protected DefaultListModel m_listModel = null;
053: protected static PosTransaction m_trans = null;
054:
055: //TODO : make getter and setter for members (ie m_*) if needed (extern calls). For that in Eclipse use Source/Generate Getters and setters
056:
057: public LoadSale(Hashtable saleMap, PosTransaction trans,
058: PosScreen page) {
059: m_saleMap.putAll(saleMap);
060: m_trans = trans;
061: m_pos = page;
062: }
063:
064: public void openDlg() {
065: m_dialog = (XDialog) pageMgr.loadPage(m_pos.getScreenLocation()
066: + "/dialog/loadsale");
067: m_dialog.setCaption(UtilProperties.getMessage("pos",
068: "LoadASale", Locale.getDefault()));
069: m_salesList = (XList) m_dialog.findComponent("salesList");
070: addMouseHandler(m_salesList, "saleDoubleClick");
071:
072: m_cancel = (XButton) m_dialog.findComponent("BtnCancel");
073: m_add = (XButton) m_dialog.findComponent("BtnAdd");
074: m_replace = (XButton) m_dialog.findComponent("BtnReplace");
075: m_delete = (XButton) m_dialog.findComponent("BtnDelete");
076: m_replaceAndDelete = (XButton) m_dialog
077: .findComponent("BtnReplaceAndDelete");
078: addMouseHandler(m_cancel, "cancel");
079: addMouseHandler(m_add, "addSale");
080: addMouseHandler(m_replace, "replaceSale");
081: addMouseHandler(m_delete, "deleteShoppingList");
082: addMouseHandler(m_replaceAndDelete,
083: "replaceSaleAndDeleteShoppingList");
084:
085: m_listModel = new DefaultListModel();
086: for (Iterator i = m_saleMap.entrySet().iterator(); i.hasNext();) {
087: Object o = i.next();
088: Map.Entry entry = (Map.Entry) o;
089: String val = entry.getValue().toString();
090: m_listModel.addElement(val);
091: }
092: m_salesList.setModel(m_listModel);
093: m_salesList.setVisibleRowCount(-1);
094: m_salesList.ensureIndexIsVisible(m_salesList.getItemCount());
095: m_salesList
096: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
097: m_salesList.setToolTipText(UtilProperties.getMessage("pos",
098: "LoadSaleListDblClickTip", Locale.getDefault()));
099:
100: m_dialog.pack();
101: m_salesList.requestFocusInWindow();
102: m_dialog.showDialog(this );
103: }
104:
105: public synchronized void saleDoubleClick() {
106: if (wasMouseDoubleClicked()) {
107: String sale = selectedSale();
108: if (null != sale) {
109: replaceSaleAndDeleteShoppingList_();
110: }
111: }
112: }
113:
114: public synchronized void cancel() {
115: if (wasMouseClicked()) {
116: closeDlg();
117: }
118: }
119:
120: public synchronized void addSale() {
121: if (wasMouseClicked()) {
122: addSale_();
123: }
124: }
125:
126: private synchronized void addSale_() {
127: String sale = selectedSale();
128: if (null != sale) {
129: addListToCart(sale, true);
130: }
131: }
132:
133: public synchronized void replaceSale() {
134: if (wasMouseClicked()) {
135: replaceSale_();
136: }
137: }
138:
139: private synchronized void replaceSale_() {
140: String sale = selectedSale();
141: if (null != sale) {
142: addListToCart(sale, false);
143: }
144: }
145:
146: public synchronized void deleteShoppingList() {
147: if (wasMouseClicked()) {
148: deleteShoppingList_();
149: }
150: }
151:
152: private synchronized void deleteShoppingList_() {
153: String sale = (String) m_salesList.getSelectedValue();
154: if (null != sale) {
155: String shoppingListId = selectedSale();
156: final ClassLoader cl = this .getClassLoader(m_pos);
157: Thread.currentThread().setContextClassLoader(cl);
158: if (m_trans.clearList(shoppingListId, m_pos)) {
159: int index = m_salesList.getSelectedIndex();
160: m_saleMap.remove(shoppingListId);
161: m_listModel = new DefaultListModel();
162: for (Iterator i = m_saleMap.entrySet().iterator(); i
163: .hasNext();) {
164: Object o = i.next();
165: Map.Entry entry = (Map.Entry) o;
166: String val = entry.getValue().toString();
167: m_listModel.addElement(val);
168: }
169: m_salesList.setModel(m_listModel);
170: int size = m_listModel.getSize();
171: if (size == 0) { //Nobody's left, nothing to do here
172: closeDlg();
173: } else { //Select an index.
174: if (index == size) {
175: //removed item in last position
176: index--;
177: }
178: }
179: m_salesList.setSelectedIndex(index);
180: m_salesList.ensureIndexIsVisible(index);
181: m_salesList.repaint();
182: repaint();
183: }
184: }
185: }
186:
187: public synchronized void replaceSaleAndDeleteShoppingList() {
188: if (wasMouseClicked()) {
189: replaceSaleAndDeleteShoppingList_();
190: }
191: }
192:
193: public synchronized void replaceSaleAndDeleteShoppingList_() {
194: replaceSale_();
195: deleteShoppingList_();
196: }
197:
198: private String selectedSale() {
199: String saleSelected = null;
200: if (null != m_salesList.getSelectedValue()) {
201: String sale = (String) m_salesList.getSelectedValue();
202: Iterator i = m_saleMap.entrySet().iterator();
203: while (i.hasNext()) {
204: Object o = i.next();
205: Map.Entry entry = (Map.Entry) o;
206: String val = entry.getValue().toString();
207: if (val.equals(sale)) {
208: saleSelected = entry.getKey().toString();
209: }
210: }
211: }
212: return saleSelected;
213: }
214:
215: private void addListToCart(String sale, boolean addToCart) {
216: final ClassLoader cl = this .getClassLoader(m_pos);
217: Thread.currentThread().setContextClassLoader(cl);
218: if (!m_trans.addListToCart(sale, m_pos, addToCart)) {
219: Debug.logError(
220: "Error while loading cart from shopping list : "
221: + sale, module);
222: } else {
223: m_trans.calcTax();
224: m_pos.refresh();
225: }
226: closeDlg();
227: }
228:
229: private ClassLoader getClassLoader(PosScreen pos) {
230: ClassLoader cl = pos.getClassLoader();
231: if (cl == null) {
232: try {
233: cl = Thread.currentThread().getContextClassLoader();
234: } catch (Throwable t) {
235: }
236: if (cl == null) {
237: Debug
238: .log(
239: "No context classloader available; using class classloader",
240: module);
241: try {
242: cl = this .getClass().getClassLoader();
243: } catch (Throwable t) {
244: Debug.logError(t, module);
245: }
246: }
247: }
248: return cl;
249: }
250:
251: private void closeDlg() {
252: m_dialog.closeDlg();
253: }
254: }
|