View Javadoc

1   /*
2    * @(#) BankAccountPanel.java Oct 28, 2004
3    * 
4    * 
5    * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
6    * Delft, the Netherlands. All rights reserved.
7    * 
8    * See for project information <a href="http://www.simulation.tudelft.nl/">
9    * www.simulation.tudelft.nl </a>.
10   * 
11   * The source code and binary code of this software is proprietary information
12   * of Delft University of Technology.
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  
22  import javax.swing.JButton;
23  import javax.swing.JLabel;
24  import javax.swing.JPanel;
25  import javax.swing.JSpinner;
26  import javax.swing.SwingConstants;
27  import javax.swing.event.ChangeEvent;
28  import javax.swing.event.ChangeListener;
29  
30  import nl.tudelft.simulation.event.EventInterface;
31  import nl.tudelft.simulation.event.EventListenerInterface;
32  import nl.tudelft.simulation.event.TimedEvent;
33  import nl.tudelft.simulation.logger.Logger;
34  import nl.tudelft.simulation.supplychain.actor.SupplyChainActor;
35  import nl.tudelft.simulation.supplychain.banking.BankAccount;
36  
37  import org.gscg.common.gui.components.PriceSpinnerPanel;
38  import org.gscg.common.gui.exceptions.ReceivedUnknownEventException;
39  import org.gscg.gameleader.dialogs.actor.actions.UpdateBalanceAction;
40  import org.gscg.gameleader.dialogs.components.AbstractPanel;
41  
42  /***
43   * The BankAccountPanel is used to show information about the bank account of a
44   * supply chain actor.
45   * <p>
46   * 
47   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
48   * Delft, the Netherlands. All rights reserved.
49   * 
50   * See for project information <a href="http://www.simulation.tudelft.nl/">
51   * www.simulation.tudelft.nl </a>.
52   * 
53   * The source code and binary code of this software is proprietary information
54   * of Delft University of Technology.
55   * 
56   * @author <a
57   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
58   *         van Houten </a>
59   * @version $Revision: 1.1 $ $Date: 2005/06/16 12:33:58 $
60   * @since 1.0.3
61   */
62  public class BankAccountPanel extends AbstractPanel implements
63  		EventListenerInterface, ChangeListener
64  {
65  	/*** the serial version uid */
66  	private static final long serialVersionUID = 11L;
67  
68  	/*** the supply chain actor */
69  	private SupplyChainActor supplyChainActor = null;
70  
71  	/*** the balance label */
72  	private JLabel balanceLabel = null;
73  
74  	/*** the amount to add to the balance */
75  	private double addToBalance = 0.0;
76  
77  	/*** the price spinner panel */
78  	private PriceSpinnerPanel priceSpinnerPanel = null;
79  
80  	/***
81  	 * constructs a new BankAccountPanel
82  	 * 
83  	 * @param actor the actor
84  	 */
85  	public BankAccountPanel(final Object actor)
86  	{
87  		super("finance");
88  		try
89  		{
90  			this.supplyChainActor = (SupplyChainActor) actor;
91  		} catch (Exception exception)
92  		{
93  			// ignore, the Logger statement below will notify us
94  			exception = null;
95  		}
96  		if (this.supplyChainActor != null)
97  		{
98  			this.initialize();
99  		} else
100 		{
101 			Logger.severe(this, "<init>",
102 					"Could not extract actor from received object of class: "
103 							+ actor.getClass());
104 		}
105 	}
106 
107 	/***
108 	 * @see org.gscg.gameleader.dialogs.components.AbstractPanel#initialize()
109 	 */
110 	protected void initialize()
111 	{
112 		JPanel balancePanel = new JPanel();
113 		balancePanel.setBackground(Color.WHITE);
114 
115 		double[][] balanceLayout = {
116 				{AbstractPanel.PREFERRED_WIDTH / 2,
117 						AbstractPanel.PREFERRED_WIDTH / 2}, {20}};
118 		balancePanel.setLayout(new TableLayout(balanceLayout));
119 
120 		JLabel label = new JLabel("balance");
121 		balancePanel.add(label, "0,0,L,C");
122 
123 		this.balanceLabel = new JLabel(""
124 				+ this.supplyChainActor.getBankAccount().getBalance());
125 		this.balanceLabel.setHorizontalAlignment(SwingConstants.RIGHT);
126 		balancePanel.add(this.balanceLabel, "1,0,R,C");
127 
128 		// add listener to listen to changes in the bankaccount
129 		this.supplyChainActor.getBankAccount().addListener(this,
130 				BankAccount.BANK_ACCOUNT_CHANGED_EVENT);
131 
132 		// setup the panel
133 		double[][] panelLayout = {{TableLayoutConstants.FILL},
134 				{20, TableLayoutConstants.PREFERRED}};
135 		super.setLayout(new TableLayout(panelLayout));
136 
137 		// add the components
138 		super.add(balancePanel, "0,0,L,C");
139 		super.add(this.getChangeBalancePanel(), "0,1,L,C");
140 
141 		// we need this to make sure the description pane
142 		// is visible at the first time a dialog is opened
143 		super.validate();
144 		super.doLayout();
145 	}
146 
147 	/***
148 	 * @see nl.tudelft.simulation.event.EventListenerInterface#notify(nl.tudelft.simulation.event.EventInterface)
149 	 */
150 	public void notify(final EventInterface event) throws RemoteException
151 	{
152 		if (event.getType().equals(BankAccount.BANK_ACCOUNT_CHANGED_EVENT))
153 		{
154 			TimedEvent timedEvent = (TimedEvent) event;
155 			this.balanceLabel.setText("");
156 			this.balanceLabel.setText(""
157 					+ ((Double) timedEvent.getContent()).doubleValue());
158 			this.balanceLabel.repaint();
159 
160 			return;
161 		}
162 		new ReceivedUnknownEventException(this, "notify", event.getType());
163 	}
164 
165 	// private methods
166 
167 	/***
168 	 * @return returns the change balance panel
169 	 */
170 	private JPanel getChangeBalancePanel()
171 	{
172 		JPanel panel = new JPanel();
173 		panel.setBackground(Color.WHITE);
174 		double[][] layout = {{TableLayoutConstants.PREFERRED, 150, 100}, {20}};
175 		panel.setLayout(new TableLayout(layout));
176 
177 		JLabel label = super.getFormattedLabel("add to balance");
178 		this.priceSpinnerPanel = new PriceSpinnerPanel(true, 0, 0,
179 				new ChangeListener[]{this}, 10000);
180 
181 		// add the components
182 		panel.add(label, "0,0,L,C");
183 		panel.add(this.priceSpinnerPanel, "1,0,R,C");
184 		panel.add(new JButton(new UpdateBalanceAction(this, "update")),
185 				"2,0,L,C");
186 
187 		return panel;
188 	}
189 
190 	/***
191 	 * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
192 	 */
193 	public void stateChanged(final ChangeEvent e)
194 	{
195 		if (((JSpinner) e.getSource()).getName().equalsIgnoreCase(
196 				PriceSpinnerPanel.PRICE_SPINNER_NAME))
197 		{
198 			double balance = ((Double) ((JSpinner) e.getSource()).getValue())
199 					.doubleValue();
200 			this.addToBalance = balance;
201 		}
202 	}
203 
204 	/***
205 	 * Method updateBalance() updates the balance of the actor. This method is
206 	 * invoked by the update balance action.
207 	 * 
208 	 * @see UpdateBalanceAction
209 	 */
210 	public void updateBalance()
211 	{
212 		if (this.addToBalance > 0)
213 		{
214 			this.supplyChainActor.getBankAccount().addToBalance(
215 					this.addToBalance);
216 		} else if (this.addToBalance < 0)
217 		{
218 			this.supplyChainActor.getBankAccount().withdrawFromBalance(
219 					this.addToBalance);
220 		}
221 
222 		this.addToBalance = 0.0;
223 		this.priceSpinnerPanel.getSpinner().setValue(new Double(0.0));
224 	}
225 }