KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: Journal.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 javax.swing.JScrollPane JavaDoc;
28 import java.util.Locale JavaDoc;
29
30 import net.xoetrope.swing.XTable;
31 import net.xoetrope.swing.XPanel;
32 import net.xoetrope.xui.data.XModel;
33
34 import org.ofbiz.pos.PosTransaction;
35 import org.ofbiz.pos.screen.PosScreen;
36 import org.ofbiz.base.util.UtilProperties;
37
38 /**
39  *
40  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
41  * @version $Rev: 5462 $
42  * @since 3.1
43  */

44 public class Journal {
45
46     public static final String JavaDoc module = Journal.class.getName();
47
48     private static String JavaDoc[] field = { "sku", "desc", "qty", "price", "index" };
49     private static String JavaDoc[] name = { "SKU", "ITEM", "QTY", "AMT", "" };
50     private static int[] width = { 100, 170, 60, 80, 0};
51     private Locale JavaDoc defaultLocale = Locale.getDefault();
52
53     protected XPanel jpanel = null;
54     protected XTable jtable = null;
55     protected String JavaDoc style = null;
56
57     public Journal(PosScreen page) {
58         this.jtable = (XTable) page.findComponent("jtable");
59         this.jpanel = (XPanel) page.findComponent("journal_panel");
60         this.jpanel.setVisible(false);
61
62         // set the table as selectable
63
jtable.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
64         jtable.setInteractiveTable(true);
65         jtable.setFocusable(false);
66
67         // set the styles
68
jtable.setBorderStyle("journalBorder");
69         jtable.setHeaderStyle("journalHeader");
70         jtable.setStyle("journalData");
71         jtable.setSelectedStyle("journalSelected");
72
73         // initialize the journal table header
74
XModel jmodel = createModel();
75         if (jmodel != null) {
76             this.appendEmpty(jmodel);
77             jtable.setModel(jmodel);
78
79             for (int i = 0; i < width.length; i++) {
80                 jtable.setColWidth(i, width[i]);
81             }
82         }
83         jtable.setSelectedRow(0);
84     }
85
86     public String JavaDoc getSelectedSku() {
87         XModel jmodel = (XModel) XModel.getInstance().get("journal/items");
88         XModel model = jmodel.get(jtable.getSelectedRow() + 1);
89         return model.getValueAsString("sku");
90     }
91
92     public String JavaDoc getSelectedIdx() {
93         XModel jmodel = (XModel) XModel.getInstance().get("journal/items");
94         XModel model = jmodel.get(jtable.getSelectedRow() + 1);
95         return model.getValueAsString("index");
96     }
97
98     public void selectNext() {
99         jtable.next();
100     }
101
102     public void selectPrevious() {
103         jtable.prev();
104     }
105
106     public void focus() {
107         if (jtable.isEnabled()) {
108             jtable.requestFocus();
109         }
110     }
111
112     public void setLock(boolean lock) {
113         jtable.setInteractiveTable(!lock);
114         jtable.setFocusable(!lock);
115         jtable.setVisible(!lock);
116         jtable.setEnabled(!lock);
117         if (!lock) {
118             this.jpanel.setVisible(true);
119         }
120     }
121
122     public void refresh(PosScreen pos) {
123         if (!jtable.isEnabled()) {
124             // no point in refreshing when we are locked;
125
// we will auto-refresh when unlocked
126
return;
127         }
128
129         PosTransaction tx = PosTransaction.getCurrentTx(pos.getSession());
130         XModel jmodel = this.createModel();
131         if (tx != null && !tx.isEmpty()) {
132             tx.appendItemDataModel(jmodel);
133             this.appendEmpty(jmodel);
134             tx.appendTotalDataModel(jmodel);
135             if (tx.selectedPayments() > 0) {
136                 this.appendEmpty(jmodel);
137                 tx.appendPaymentDataModel(jmodel);
138             }
139             if (pos.getInput().isFunctionSet("PAID")) {
140                 tx.appendChangeDataModel(jmodel);
141             }
142         } else {
143             this.appendEmpty(jmodel);
144         }
145
146         // make sure we are at the last item in the journal
147
jtable.setSelectedRow(0);
148
149         try {
150             jtable.repaint();
151         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
152             // bug in XUI causes this; ignore for now
153
// it has been reported and will be fixed soon
154
}
155     }
156
157     private XModel createModel() {
158         XModel jmodel = (XModel) XModel.getInstance().get("journal/items");
159
160         // clear the list
161
jmodel.clear();
162
163         if (field.length == 0) {
164             return null;
165         }
166
167         // create the header
168
XModel headerNode = appendNode(jmodel, "th", "", "");
169         for (int i = 0 ; i < field.length; i++) {
170             appendNode(headerNode, "td", field[i],UtilProperties.getMessage("pos",name[i],defaultLocale));
171         }
172
173         return jmodel;
174     }
175
176     private void appendEmpty(XModel jmodel) {
177         XModel headerNode = appendNode(jmodel, "tr", "", "");
178         for (int i = 0 ; i < field.length; i++) {
179             appendNode(headerNode, "td", field[i], "");
180         }
181     }
182
183     public static XModel appendNode(XModel node, String JavaDoc tag, String JavaDoc name, String JavaDoc value) {
184         XModel newNode = (XModel) node.append(name);
185         newNode.setTagName(tag);
186         if (value != null) {
187             newNode.set(value);
188         }
189         return newNode;
190     }
191 }
Popular Tags