1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.gscg.gameleader.dialogs.actor.components;
15
16 import info.clearthought.layout.TableLayout;
17 import info.clearthought.layout.TableLayoutConstants;
18
19 import java.awt.Color;
20 import java.rmi.RemoteException;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Map;
24
25 import javax.swing.JButton;
26 import javax.swing.JLabel;
27 import javax.swing.JPanel;
28 import javax.swing.JSpinner;
29 import javax.swing.SwingConstants;
30
31 import nl.tudelft.simulation.event.EventInterface;
32 import nl.tudelft.simulation.event.EventListenerInterface;
33 import nl.tudelft.simulation.logger.Logger;
34 import nl.tudelft.simulation.supplychain.actor.Trader;
35 import nl.tudelft.simulation.supplychain.product.Product;
36 import nl.tudelft.simulation.supplychain.stock.StockInterface;
37 import nl.tudelft.simulation.supplychain.stock.StockUpdateData;
38
39 import org.gscg.common.gui.components.AmountSpinnerPanel;
40 import org.gscg.common.gui.exceptions.ReceivedUnknownEventException;
41 import org.gscg.gameleader.dialogs.actor.actions.UpdateStockAction;
42 import org.gscg.gameleader.dialogs.components.AbstractPanel;
43
44 /***
45 * The StockPanel is used to show information about the stock of a trader, and
46 * to enable a game leader to change the amount of the products.
47 * <p>
48 *
49 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
50 * Delft, the Netherlands. All rights reserved.
51 *
52 * See for project information <a href="http://www.simulation.tudelft.nl/">
53 * www.simulation.tudelft.nl </a>.
54 *
55 * The source code and binary code of this software is proprietary information
56 * of Delft University of Technology.
57 *
58 * @author <a
59 * href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
60 * van Houten </a>
61 * @version $Revision: 1.1 $ $Date: 2005/06/16 12:33:58 $
62 * @since 1.0.3
63 */
64 public class StockPanel extends AbstractPanel implements EventListenerInterface
65 {
66 /*** the serial version uid */
67 private static final long serialVersionUID = 11L;
68
69 /*** the actor */
70 private Trader trader = null;
71
72 /*** the map with amount spinner panels and products */
73 private Map spinners = null;
74
75 /*** the map with amount labels and products */
76 private Map labels = null;
77
78 /***
79 * constructs a new StockPanel
80 *
81 * @param actor the actor
82 */
83 public StockPanel(final Object actor)
84 {
85 super("stock");
86 this.spinners = new HashMap();
87 this.labels = new HashMap();
88
89 try
90 {
91 this.trader = (Trader) actor;
92 } catch (Exception exception)
93 {
94
95 exception = null;
96 }
97 if (this.trader != null)
98 {
99 this.initialize();
100 } else
101 {
102 Logger.severe(this, "<init>",
103 "Could not extract actor from received object of class: "
104 + actor.getClass());
105 }
106 }
107
108 /***
109 * @see org.gscg.gameleader.dialogs.components.AbstractPanel#initialize()
110 */
111 protected void initialize()
112 {
113 double[][] panelLayout = {{TableLayoutConstants.FILL},
114 {TableLayoutConstants.PREFERRED}};
115 TableLayout panelTableLayout = new TableLayout(panelLayout);
116 this.setLayout(panelTableLayout);
117
118 StockInterface stock = this.trader.getStock();
119 try
120 {
121 stock.addListener(this, StockInterface.STOCK_CHANGE_EVENT);
122 } catch (RemoteException remoteException)
123 {
124 Logger.severe(this, "initialize", remoteException);
125 }
126
127 for (Iterator it = stock.iterator(); it.hasNext();)
128 {
129 JPanel panel = new JPanel();
130 panel.setBackground(Color.WHITE);
131 Product product = (Product) it.next();
132
133 double[][] layout = {{140, 60, 100, 80},
134 {TableLayoutConstants.FILL}};
135 panel.setLayout(new TableLayout(layout));
136
137 JLabel nameLabel = super.getFormattedLabel("actual amount for "
138 + product.getName());
139 panel.add(nameLabel, "0,0,L,C");
140
141 JLabel amountLabel = super.getFormattedLabel(""
142 + stock.getActualAmount(product));
143 amountLabel.setHorizontalAlignment(SwingConstants.RIGHT);
144 panel.add(amountLabel, "1,0,L,C");
145 this.labels.put(product.getName(), amountLabel);
146
147
148 AmountSpinnerPanel spinner = new AmountSpinnerPanel(true, 0, 1000,
149 1, 0);
150 panel.add(spinner, "2,0,R,C");
151
152
153 this.spinners.put(product, spinner);
154
155
156 panel.add(new JButton(
157 new UpdateStockAction(this, "update", product)), "3,0,L,C");
158
159 this.add(panel, "0," + (panelTableLayout.getNumRow() - 1) + ",L,C");
160
161 if (it.hasNext())
162 {
163 panelTableLayout.insertRow(panelTableLayout.getNumRow(), 20);
164 }
165 }
166 }
167
168 /***
169 * Method updateStock updates the stock of a product.
170 *
171 * @param product the product
172 */
173 public void updateStock(final Product product)
174 {
175 JSpinner spinner = ((AmountSpinnerPanel) this.spinners.get(product))
176 .getSpinner();
177 double amount = ((Double) spinner.getValue()).doubleValue();
178
179 if (amount > 0)
180 {
181 this.trader.getStock().addStock(product, amount,
182 (product.getUnitMarketPrice() * amount));
183 } else if (amount < 0)
184 {
185 this.trader.getStock().getStock(product, amount);
186 }
187
188
189 spinner.setValue(new Double(0.0));
190 }
191
192 /***
193 * @see nl.tudelft.simulation.event.EventListenerInterface#notify(nl.tudelft.simulation.event.EventInterface)
194 */
195 public void notify(final EventInterface event) throws RemoteException
196 {
197 if (event.getType().equals(StockInterface.STOCK_CHANGE_EVENT))
198 {
199 StockUpdateData data = (StockUpdateData) event.getContent();
200 JLabel label = (JLabel) this.labels.get(data.getProductName());
201 label.setText("" + data.getActualAmount());
202 label.repaint();
203 return;
204 }
205 new ReceivedUnknownEventException(this, "notify", event.getType());
206 }
207 }