View Javadoc

1   /*
2    * TransportationCostsSpinnerPanel.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 TransportationCostsSpinnerPanel <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 TransportationCostsSpinnerPanel extends JPanel implements
52  		ChangeListener
53  {
54  	/*** the serial version uid */
55  	private static final long serialVersionUID = 11L;
56  
57  	/*** the name of the spinner */
58  	public static final String TRANSPORTATION_SPINNER_NAME = "TRANSPORTATION_COSTS_SPINNER";
59  
60  	/*** the fixed costs */
61  	private double fixedCosts = 0;
62  
63  	/*** the transportation costs per unit of a product */
64  	private double costsPerUnit = 0;
65  
66  	/*** the chosen amount of a product */
67  	private double amount = 0;
68  
69  	/*** the day spinner */
70  	private JSpinner spinner = null;
71  
72  	/*** the stepsize for the spinner */
73  	private int stepSize = 100;
74  
75  	/***
76  	 * constructs a new PriceSpinnerPanel
77  	 * 
78  	 * @param editable indicates whether the spinner is editable
79  	 * @param fixedCosts the fixed costs
80  	 * @param unitTransportCosts the transportation costs per unit *
81  	 * @param initialAmount the initial amount of the product in the rfq
82  	 * @param changeListener the changelistener, may be null
83  	 * @param stepSize the stepSize
84  	 */
85  	public TransportationCostsSpinnerPanel(final boolean editable,
86  			final double fixedCosts, final double unitTransportCosts,
87  			final double initialAmount, final ChangeListener changeListener,
88  			final int stepSize)
89  	{
90  		super();
91  		this.fixedCosts = fixedCosts;
92  		this.costsPerUnit = unitTransportCosts;
93  		this.stepSize = stepSize;
94  		this.amount = initialAmount;
95  
96  		double[][] layout = {{250}, {20}};
97  		this.setLayout(new TableLayout(layout));
98  		this.setOpaque(false);
99  
100 		JPanel amountParametersPanel = new JPanel();
101 		amountParametersPanel.setLayout(new BoxLayout(amountParametersPanel,
102 				BoxLayout.X_AXIS));
103 		amountParametersPanel.setOpaque(false);
104 
105 		this.add(amountParametersPanel, "0,0,L,C");
106 		String[] labels = {""};
107 
108 		// Add the day label-spinner pair
109 		SpinnerModel amountModel = new SpinnerNumberModel(Math
110 				.round(this.fixedCosts + (this.amount * this.costsPerUnit)), // initial
111 				// value
112 				0, // min
113 				Double.MAX_VALUE, // max
114 				this.stepSize); // step
115 
116 		this.spinner = SpinnerUtilities.addLabeledSpinner(
117 				amountParametersPanel, labels[0], amountModel);
118 		this.spinner.setEnabled(editable);
119 		this.spinner
120 				.setName(TransportationCostsSpinnerPanel.TRANSPORTATION_SPINNER_NAME);
121 
122 		// add the change listener if not null
123 		if (changeListener != null)
124 		{
125 			this.spinner.addChangeListener(changeListener);
126 		}
127 
128 		// tweak the spinner's formatted text field.
129 		JFormattedTextField ftf = null;
130 
131 		ftf = SpinnerUtilities.getTextField(this.spinner);
132 		if (ftf != null)
133 		{
134 			ftf.setColumns(6); // specify more width than we need
135 			ftf.setHorizontalAlignment(SwingConstants.RIGHT);
136 			ftf.setEditable(false);
137 			ftf.setBackground(new Color(255, 255, 255));
138 		}
139 	}
140 
141 	/***
142 	 * @return Returns the spinner.
143 	 */
144 	public JSpinner getSpinner()
145 	{
146 		return this.spinner;
147 	}
148 
149 	/***
150 	 * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
151 	 */
152 	public void stateChanged(final ChangeEvent e)
153 	{
154 		if (((JSpinner) e.getSource()).getName().equalsIgnoreCase(
155 				AmountSpinnerPanel.AMOUNT_SPINNER_NAME))
156 		{
157 			// we got a changed amount
158 			this.amount = ((Double) ((JSpinner) e.getSource()).getValue())
159 					.doubleValue();
160 		}
161 		this.spinner.setValue(new Double(Math.round(this.fixedCosts
162 				+ (this.amount * this.costsPerUnit))));
163 	}
164 }