View Javadoc

1   /*
2    * PriceSpinnerPanel.java Created @ Jun 22, 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   */
15  package org.gscg.common.gui.components;
16  
17  import info.clearthought.layout.TableLayout;
18  
19  import java.awt.Color;
20  
21  import javax.swing.BoxLayout;
22  import javax.swing.JFormattedTextField;
23  import javax.swing.JPanel;
24  import javax.swing.JSpinner;
25  import javax.swing.SpinnerModel;
26  import javax.swing.SpinnerNumberModel;
27  import javax.swing.SwingConstants;
28  import javax.swing.event.ChangeEvent;
29  import javax.swing.event.ChangeListener;
30  
31  
32  /***
33   * The PriceSpinnerPanel <br>
34   * 
35   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
36   * Delft, the Netherlands. All rights reserved.
37   * 
38   * See for project information <a href="http://www.simulation.tudelft.nl/">
39   * www.simulation.tudelft.nl </a>.
40   * 
41   * The source code and binary code of this software is proprietary information
42   * of Delft University of Technology.
43   * 
44   * @version 1.0 Jun 22, 2004 <br>
45   * @author <a
46   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
47   *         van Houten </a>
48   * @version $Revision: 1.1 $ $Date: 2005/06/16 12:33:53 $
49   * @since 1.0.0 <br>
50   */
51  public class PriceSpinnerPanel extends JPanel implements ChangeListener
52  {
53  	/*** the serial version uid */
54  	private static final long serialVersionUID = 11L;
55  
56  	/*** the name of the spinner */
57  	public static final String PRICE_SPINNER_NAME = "PRICE_SPINNER";
58  
59  	/*** the base price for a product */
60  	private double basePrice = 0;
61  
62  	/*** the chosen amount of a product */
63  	private double amount = 0;
64  
65  	/*** the day spinner */
66  	private JSpinner spinner = null;
67  
68  	/*** the stepsize for the spinner */
69  	private int stepSize = 100;
70  
71  	/***
72  	 * constructs a new PriceSpinnerPanel
73  	 * 
74  	 * @param editable indicates whether the spinner is editable
75  	 * @param initialBasePrice the initial base price for a product
76  	 * @param initialPrice the initial price
77  	 * @param initialAmount the initial amount of the product in the rfq
78  	 * @param stepSize the step size of the spinner panel
79  	 */
80  	public PriceSpinnerPanel(final boolean editable,
81  			final double initialBasePrice, final int initialPrice,
82  			final double initialAmount, final int stepSize)
83  	{
84  		this(editable, initialBasePrice, initialPrice, null, stepSize);
85  		this.amount = initialAmount;
86  	}
87  
88  	/***
89  	 * constructs a new PriceSpinnerPanel
90  	 * 
91  	 * @param editable indicates whether the spinner is editable
92  	 * @param initialBasePrice the initial base price for a product
93  	 * @param initialPrice the initial price
94  	 * @param changeListener the changelistener, may be null
95  	 * @param stepSize the stepSize
96  	 */
97  	public PriceSpinnerPanel(final boolean editable,
98  			final double initialBasePrice, final double initialPrice,
99  			final ChangeListener[] changeListener, final int stepSize)
100 	{
101 		super();
102 		this.basePrice = initialBasePrice;
103 		this.stepSize = stepSize;
104 
105 		double[][] layout = {{250}, {20}};
106 		this.setLayout(new TableLayout(layout));
107 		this.setOpaque(false);
108 
109 		JPanel amountParametersPanel = new JPanel();
110 		amountParametersPanel.setLayout(new BoxLayout(amountParametersPanel,
111 				BoxLayout.X_AXIS));
112 		amountParametersPanel.setOpaque(false);
113 
114 		this.add(amountParametersPanel, "0,0,L,C");
115 		String[] labels = {""};
116 
117 		// Add the day label-spinner pair
118 		SpinnerModel amountModel = new SpinnerNumberModel(initialPrice, // initial
119 				// value
120 				0, // min
121 				Double.MAX_VALUE, // max
122 				this.stepSize); // step
123 
124 		this.spinner = SpinnerUtilities.addLabeledSpinner(
125 				amountParametersPanel, labels[0], amountModel);
126 		this.spinner.setEnabled(editable);
127 		this.spinner.setName(PriceSpinnerPanel.PRICE_SPINNER_NAME);
128 
129 		// add the change listener if not null
130 		if (changeListener != null)
131 		{
132 			for (int i = 0; i < changeListener.length; i++)
133 			{
134 				this.spinner.addChangeListener(changeListener[i]);
135 			}
136 		}
137 
138 		// tweak the spinner's formatted text field.
139 		JFormattedTextField ftf = null;
140 
141 		ftf = SpinnerUtilities.getTextField(this.spinner);
142 		if (ftf != null)
143 		{
144 			ftf.setColumns(6); // specify more width than we need
145 			ftf.setHorizontalAlignment(SwingConstants.RIGHT);
146 			ftf.setEditable(false);
147 			ftf.setBackground(new Color(255, 255, 255));
148 		}
149 	}
150 
151 	/***
152 	 * @return Returns the spinner.
153 	 */
154 	public JSpinner getSpinner()
155 	{
156 		return this.spinner;
157 	}
158 
159 	/***
160 	 * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
161 	 */
162 	public void stateChanged(final ChangeEvent e)
163 	{
164 		if (((JSpinner) e.getSource()).getName().equalsIgnoreCase(
165 				AmountSpinnerPanel.AMOUNT_SPINNER_NAME))
166 		{
167 			// we got a changed amount
168 			this.amount = ((Double) ((JSpinner) e.getSource()).getValue())
169 					.doubleValue();
170 		}
171 		if (((JSpinner) e.getSource()).getName().equalsIgnoreCase(
172 				PriceSpinnerPanel.PRICE_SPINNER_NAME))
173 		{
174 			// we got a changed base price
175 			this.basePrice = ((Double) ((JSpinner) e.getSource()).getValue())
176 					.doubleValue();
177 		}
178 		this.spinner.setValue(new Double(this.amount * this.basePrice));
179 	}
180 }