001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package wingscms;
014:
015: import org.wings.CmsFrame;
016: import org.wingx.XTable;
017: import org.wingx.table.EditableTableCellRenderer;
018: import org.wings.*;
019: import org.wings.table.STableCellRenderer;
020:
021: import javax.swing.table.DefaultTableModel;
022: import javax.swing.event.ListSelectionListener;
023: import javax.swing.event.ListSelectionEvent;
024: import java.util.LinkedList;
025: import java.awt.event.ActionListener;
026: import java.awt.event.ActionEvent;
027: import java.awt.*;
028:
029: /**
030: * <code>JoomlaExample<code>.
031: * <p/>
032: * User: raedler
033: * Date: 08.08.2007
034: * Time: 09:41:33
035: *
036: * @author raedler
037: * @version $Id
038: */
039: public class JoomlaExample {
040:
041: private CmsFrame rootFrame = new CmsFrame();
042:
043: public JoomlaExample() {
044: // Shoppingcart contents (Key = product index in PLCONTENT, Value = amount)
045: //final HashMap<Integer, Integer> SCCONTENT = new HashMap<Integer, Integer>();
046: // SessionManager.getSession().setProperty("shoppingcart", SCCONTENT);
047: final LinkedList<ShoppingCartItem> SCLIST = new LinkedList<ShoppingCartItem>();
048:
049: // Pricelist panel contains the pricelist table,
050: // an addtoshoppingcart Button and a message label.
051: final SPanel ppricelist = new SPanel(new SFlowDownLayout());
052:
053: // Result label
054: final SLabel message = new SLabel("Test Message");
055:
056: // Products
057: final LinkedList<Product> PRODUCTS = new LinkedList<Product>();
058: PRODUCTS.add(new Product(1001, "Rotes Auto", 50000d));
059: PRODUCTS.add(new Product(1002, "Blaues Auto", 75000d));
060: PRODUCTS.add(new Product(1003, "Schwarzes Auto", 100000d));
061:
062: // Productlist table
063: final String[] PLCOLNAMES = { "Art. Nr.", "Artikel",
064: "Stückpreis (in €)" };
065: final XTable productlist = new XTable(new DefaultTableModel() {
066: public Object getValueAt(int row, int col) {
067: Product product = PRODUCTS.get(row);
068: switch (col) {
069: case 0:
070: return product.getItemnumber();
071: case 1:
072: return product.getDescription();
073: case 2:
074: return product.getPrice();
075: default:
076: return null;
077: }
078: }
079:
080: @Override
081: public Class<?> getColumnClass(int columnIndex) {
082: switch (columnIndex) {
083: case 0:
084: return Integer.class;
085: default:
086: return String.class;
087: }
088: }
089:
090: public int getRowCount() {
091: return PRODUCTS.size();
092: }
093:
094: public int getColumnCount() {
095: return PLCOLNAMES.length;
096: }
097:
098: public String getColumnName(int i) {
099: return PLCOLNAMES[i];
100: }
101: });
102: productlist.addSelectionListener(new ListSelectionListener() {
103: public void valueChanged(ListSelectionEvent e) {
104: if (productlist.getSelectedRow() != -1) {
105: message.setText("");
106: ppricelist.reload();
107: }
108: }
109: });
110:
111: JoomlaTableCellRenderer renderer = new JoomlaTableCellRenderer();
112: renderer.addActionListener(new ActionListener() {
113:
114: public void actionPerformed(ActionEvent e) {
115: System.out.println("JoomlaExample.actionPerformed");
116:
117: int row = productlist.getSelectedRow();
118: Object value = productlist.getValueAt(row, 1);
119:
120: message.setText("Product: " + value
121: + " has been selected.");
122: message.reload();
123: }
124: });
125:
126: productlist.setDefaultRenderer(Integer.class, renderer);
127: productlist.setEditable(false);
128:
129: // Shoppingcart table
130: final String[] SCCOLNAMES = { "Art. Nr.", "Artikel",
131: "Stückpreis (in €)", "Anzahl",
132: "Gesamtpreis (in €)" };
133: final STable shoppingcart = new STable(new DefaultTableModel() {
134: public Object getValueAt(int row, int col) {
135: ShoppingCartItem item = SCLIST.get(row);
136: switch (col) {
137: case 0:
138: return item.getProduct().getItemnumber();
139: case 1:
140: return item.getProduct().getDescription();
141: case 2:
142: return item.getProduct().getPrice();
143: case 3:
144: return item.getAmount();
145: case 4:
146: return item.getAllRoundPrice();
147: default:
148: return null;
149: }
150: }
151:
152: public int getRowCount() {
153: return SCLIST.size();
154: }
155:
156: public int getColumnCount() {
157: return SCCOLNAMES.length;
158: }
159:
160: public String getColumnName(int i) {
161: return SCCOLNAMES[i];
162: }
163: });
164: shoppingcart.setEditable(false);
165:
166: // Addtoshoppingcart button
167: SButton addtoshoppingcart = new SButton("In den Warenkorb");
168: addtoshoppingcart.addActionListener(new ActionListener() {
169: public void actionPerformed(ActionEvent ae) {
170: if (productlist.getSelectedRow() != -1) {
171: Product selection = PRODUCTS.get(productlist
172: .getSelectedRow());
173:
174: // If the selected item is already in the shoppingcart just increase the amount
175: boolean isinlist = false;
176: for (ShoppingCartItem item : SCLIST) {
177: if (item.getProduct().equals(selection)) {
178: item.setAmount(item.getAmount() + 1);
179: isinlist = true;
180: break;
181: }
182: }
183: if (!isinlist) {
184: SCLIST.add(new ShoppingCartItem(selection));
185: shoppingcart.reload();
186: }
187:
188: message.setForeground(Color.BLACK);
189: message.setText("Das Produkt \""
190: + selection.getDescription()
191: + "\" wurde zum Warenkorb hinzugefügt.");
192: productlist.clearSelection();
193: ppricelist.reload();
194: } else {
195: message.setForeground(Color.RED);
196: message
197: .setText("Fehler: Kein Produkt ausgewählt.");
198: }
199: }
200: });
201:
202: // Pricelist panel
203: ppricelist.add(productlist);
204: ppricelist.add(addtoshoppingcart);
205:
206: rootFrame.add(productlist, "PRODUCTLIST");
207: rootFrame.add(addtoshoppingcart, "ADDTOSHOPPINGCART");
208: rootFrame.add(ppricelist, "PRICELIST");
209: rootFrame.add(shoppingcart, "SHOPPINGCART");
210: rootFrame.add(message, "MESSAGE");
211:
212: rootFrame.getContentPane()
213: .setPreferredSize(SDimension.FULLAREA);
214: rootFrame.getContentPane().setVerticalAlignment(
215: SConstants.TOP_ALIGN);
216: rootFrame.setVisible(true);
217: }
218:
219: class JoomlaTableCellRenderer extends SButton implements
220: STableCellRenderer, EditableTableCellRenderer {
221:
222: public Object getValue() {
223: return getText();
224: }
225:
226: public LowLevelEventListener getLowLevelEventListener(
227: STable table, int row, int column) {
228: return this ;
229: }
230:
231: public SComponent getTableCellRendererComponent(
232: final STable table, final Object value,
233: boolean isSelected, final int row, final int column) {
234: setText(String.valueOf(value));
235:
236: return this;
237: }
238: }
239: }
|