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.util.Locale;
021:
022: import net.xoetrope.swing.XTable;
023: import net.xoetrope.swing.XScrollPane;
024: import net.xoetrope.xui.data.XModel;
025:
026: import org.ofbiz.pos.PosTransaction;
027: import org.ofbiz.pos.screen.PosScreen;
028: import org.ofbiz.base.util.UtilProperties;
029:
030: public class Journal {
031:
032: public static final String module = Journal.class.getName();
033:
034: private static String[] field = { "sku", "desc", "qty", "price",
035: "index" };
036: private static String[] name = { "SKU", "ITEM", "QTY", "AMT", "" };
037: private static int[] width = { 100, 170, 60, 80, 0 };
038: private Locale defaultLocale = Locale.getDefault();
039:
040: protected XScrollPane jpanel = null;
041: protected XTable jtable = null;
042: protected String style = null;
043:
044: public Journal(PosScreen page) {
045: //The vertical bar is always visible to allow access to horizontal bar without shrink the journal panel
046: this .jpanel = (XScrollPane) page.findComponent("journal_panel");
047: this .jpanel.setVisible(false);
048:
049: this .jtable = (XTable) page.findComponent("jtable");
050:
051: // set the table as selectable
052: jtable.setInteractiveTable(true);
053: jtable.setFocusable(false);
054:
055: // set the styles
056: jtable.setBorderStyle("journalBorder");
057: jtable.setHeaderStyle("journalHeader");
058: jtable.setStyle("journalData");
059: jtable.setSelectedStyle("journalSelected");
060:
061: // initialize the journal table header
062: XModel jmodel = createModel();
063: if (jmodel != null) {
064: this .appendEmpty(jmodel);
065: jtable.setModel(jmodel);
066:
067: for (int i = 0; i < width.length; i++) {
068: jtable.setColWidth(i, width[i]);
069: }
070: }
071: jtable.setSelectedRow(0);
072: }
073:
074: public String getSelectedSku() {
075: XModel jmodel = (XModel) XModel.getInstance().get(
076: "journal/items");
077: XModel model = jmodel.get(jtable.getSelectedRow() + 1);
078: return model.getValueAsString("sku");
079: }
080:
081: public String getSelectedIdx() {
082: XModel jmodel = (XModel) XModel.getInstance().get(
083: "journal/items");
084: XModel model = jmodel.get(jtable.getSelectedRow() + 1);
085: return model.getValueAsString("index");
086: }
087:
088: public void selectNext() {
089: jtable.next();
090: }
091:
092: public void selectPrevious() {
093: jtable.prev();
094: }
095:
096: public void focus() {
097: if (jtable.isEnabled()) {
098: jtable.requestFocus();
099: }
100: }
101:
102: public void setLock(boolean lock) {
103: jtable.setInteractiveTable(!lock);
104: jtable.setFocusable(!lock);
105: jtable.setVisible(!lock);
106: jtable.setEnabled(!lock);
107: if (!lock) {
108: this .jpanel.setVisible(true);
109: }
110: }
111:
112: public void refresh(PosScreen pos) {
113: if (!jtable.isEnabled()) {
114: // no point in refreshing when we are locked;
115: // we will auto-refresh when unlocked
116: return;
117: }
118:
119: PosTransaction tx = PosTransaction.getCurrentTx(pos
120: .getSession());
121: XModel jmodel = this .createModel();
122: if (tx != null && !tx.isEmpty()) {
123: tx.appendItemDataModel(jmodel);
124: this .appendEmpty(jmodel);
125: tx.appendTotalDataModel(jmodel);
126: if (tx.selectedPayments() > 0) {
127: this .appendEmpty(jmodel);
128: tx.appendPaymentDataModel(jmodel);
129: }
130: if (pos.getInput().isFunctionSet("PAID")) {
131: tx.appendChangeDataModel(jmodel);
132: }
133: } else {
134: this .appendEmpty(jmodel);
135: }
136:
137: // make sure we are at the last item in the journal
138: jtable.setSelectedRow(0);
139:
140: try {
141: jtable.repaint();
142: } catch (ArrayIndexOutOfBoundsException e) {
143: // bug in XUI causes this; ignore for now
144: // it has been reported and will be fixed soon
145: }
146: }
147:
148: private XModel createModel() {
149: XModel jmodel = (XModel) XModel.getInstance().get(
150: "journal/items");
151:
152: // clear the list
153: jmodel.clear();
154:
155: if (field.length == 0) {
156: return null;
157: }
158:
159: // create the header
160: XModel headerNode = appendNode(jmodel, "th", "", "");
161: for (int i = 0; i < field.length; i++) {
162: appendNode(headerNode, "td", field[i], UtilProperties
163: .getMessage("pos", name[i], defaultLocale));
164: }
165:
166: return jmodel;
167: }
168:
169: private void appendEmpty(XModel jmodel) {
170: XModel headerNode = appendNode(jmodel, "tr", "", "");
171: for (int i = 0; i < field.length; i++) {
172: appendNode(headerNode, "td", field[i], "");
173: }
174: }
175:
176: public static XModel appendNode(XModel node, String tag,
177: String name, String value) {
178: XModel newNode = (XModel) node.append(name);
179: newNode.setTagName(tag);
180: if (value != null) {
181: newNode.set(value);
182: }
183: return newNode;
184: }
185: }
|