View Javadoc

1   /*
2    * AmountSpinnerPanel.java Created @ Jun 22, 2004
3    * 
4    * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands. All rights reserved.
6    * 
7    * See for project information <a href="http://www.simulation.tudelft.nl/">
8    * www.simulation.tudelft.nl </a>.
9    * 
10   * The source code and binary code of this software is proprietary information
11   * of Delft University of Technology.
12   */
13  package org.gscg.common.gui.components;
14  
15  import info.clearthought.layout.TableLayout;
16  
17  import java.awt.Color;
18  import java.awt.event.MouseEvent;
19  import java.awt.event.MouseListener;
20  
21  import javax.swing.BoxLayout;
22  import javax.swing.JButton;
23  import javax.swing.JFormattedTextField;
24  import javax.swing.JPanel;
25  import javax.swing.JSpinner;
26  import javax.swing.SpinnerModel;
27  import javax.swing.SpinnerNumberModel;
28  import javax.swing.SwingConstants;
29  
30  import nl.tudelft.simulation.logger.Logger;
31  
32  
33  /***
34   * The AmountSpinnerModel is a spinner model to indicate the amount for a
35   * product. <br>
36   * 
37   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
38   * Delft, the Netherlands. All rights reserved.
39   * 
40   * See for project information <a href="http://www.simulation.tudelft.nl/">
41   * www.simulation.tudelft.nl </a>.
42   * 
43   * The source code and binary code of this software is proprietary information
44   * of Delft University of Technology.
45   * 
46   * @author <a
47   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
48   *         van Houten </a>* *
49   * @version $Revision: 1.1 $ $Date: 2005/06/16 12:33:54 $
50   * @since 1.0.0 <br>
51   */
52  public class EstimatedTransportTimePanel extends JPanel implements
53  		MouseListener
54  {
55  	/*** the serial version uid */
56  	private static final long serialVersionUID = 11L;
57  
58  	/*** the name of the spinner */
59  	public static final String ESTIMATED_TRANSPORT_TIME_SPINNER = "ESTIMATED_TRANSPORT_TIME_SPINNER";
60  
61  	/*** the day spinner */
62  	private JSpinner spinner = null;
63  
64  	/*** the minimum */
65  	private double minimum = 1.0;
66  
67  	/*** the maximum */
68  	private double maximum = 10000;
69  
70  	/*** the step size */
71  	private int stepSize = 1;
72  
73  	/*** the initial value */
74  	private int initialvalue = 0;
75  
76  	/*** indicates whether the spinner is being changed */
77  	private boolean pressed = false;
78  
79  	/*** the thread for increasing the stepsize */
80  	private IncreaseStepSizeThread increaseThread = null;
81  
82  	/*** the formatted text field */
83  	private JFormattedTextField ftf = null;
84  
85  	/***
86  	 * constructs a new AmountSpinnerPanel, initializes the spinner with a
87  	 * minimum of 1, a maximum of 1000 and a stepsize of 1
88  	 * 
89  	 * @param initialvalue the initial value of the spinner
90  	 */
91  	public EstimatedTransportTimePanel(final int initialvalue)
92  	{
93  		this(1.0, 1000.0, 1, initialvalue);
94  	}
95  
96  	/***
97  	 * constructs a new AmountSpinnerPanel
98  	 * 
99  	 * @param minimum the minimum value
100 	 * @param maximum the maximum value
101 	 * @param stepsize the step size
102 	 * @param initialvalue the initial value of the spinner
103 	 */
104 	public EstimatedTransportTimePanel(final double minimum,
105 			final double maximum, final int stepsize, final int initialvalue)
106 	{
107 		super();
108 
109 		try
110 		{
111 			this.minimum = minimum;
112 			this.maximum = maximum;
113 			this.stepSize = stepsize;
114 			this.initialvalue = initialvalue;
115 
116 			double[][] layout = {{250}, {20}};
117 			this.setLayout(new TableLayout(layout));
118 			this.setOpaque(false);
119 
120 			JPanel amountParametersPanel = new JPanel();
121 			amountParametersPanel.setLayout(new BoxLayout(
122 					amountParametersPanel, BoxLayout.X_AXIS));
123 			amountParametersPanel.setOpaque(false);
124 
125 			this.add(amountParametersPanel, "0,0,L,C");
126 			String[] labels = {"Day(s):"};
127 
128 			// add the abel-spinner pair
129 			SpinnerModel amountModel = new SpinnerNumberModel(
130 					this.initialvalue, // initial
131 					// value
132 					this.minimum, // min
133 					this.maximum, // max
134 					this.stepSize); // step
135 
136 			this.spinner = SpinnerUtilities.addLabeledSpinner(
137 					amountParametersPanel, labels[0], amountModel);
138 			this.spinner.setEnabled(false);
139 			this.spinner
140 					.setName(EstimatedTransportTimePanel.ESTIMATED_TRANSPORT_TIME_SPINNER);
141 
142 			// we implement a mouse listener to play with increased stepsize as
143 			// the spinner has been pressed
144 			Object[] keys = this.spinner.getComponents();
145 			for (int i = 0; i < keys.length; i++)
146 			{
147 				Object obj = this.spinner.getComponents()[i];
148 				if (JButton.class.isAssignableFrom(obj.getClass()))
149 				{
150 					((JButton) obj).addMouseListener(this);
151 				}
152 			}
153 
154 			// tweak the spinner's formatted text field.
155 			this.ftf = null;
156 
157 			this.ftf = SpinnerUtilities.getTextField(this.spinner);
158 			if (this.ftf != null)
159 			{
160 				this.ftf.setColumns(5); // specify more width than we need
161 				this.ftf.setHorizontalAlignment(SwingConstants.RIGHT);
162 				this.ftf.setEditable(false);
163 				this.ftf.setBackground(new Color(255, 255, 255));
164 			}
165 		} catch (Exception exception)
166 		{
167 			exception.printStackTrace();
168 		}
169 	}
170 
171 	/***
172 	 * @return Returns the spinner.
173 	 */
174 	public JSpinner getSpinner()
175 	{
176 		return this.spinner;
177 	}
178 
179 	/***
180 	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
181 	 */
182 	public void mouseClicked(final MouseEvent e)
183 	{
184 		// nothing to do
185 	}
186 
187 	/***
188 	 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
189 	 */
190 	public void mouseEntered(final MouseEvent e)
191 	{
192 		// nothing to do
193 	}
194 
195 	/***
196 	 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
197 	 */
198 	public void mouseExited(final MouseEvent e)
199 	{
200 		// nothing to do
201 	}
202 
203 	/***
204 	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
205 	 */
206 	public void mousePressed(final MouseEvent e)
207 	{
208 		this.pressed = true;
209 		this.increaseThread = new IncreaseStepSizeThread(this);
210 		this.increaseThread.start();
211 	}
212 
213 	/***
214 	 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
215 	 */
216 	public void mouseReleased(final MouseEvent e)
217 	{
218 		this.pressed = false;
219 		try
220 		{
221 			this.increaseThread = null;
222 			// reset the spinner model
223 			SpinnerModel model = new SpinnerNumberModel(((Double) this.spinner
224 					.getValue()).doubleValue(), // initial
225 					// value
226 					this.minimum, // min
227 					this.maximum, // max
228 					this.stepSize); // step
229 			this.spinner.setModel(model);
230 		} catch (Exception exception)
231 		{
232 			Logger.severe(this, "mouseReleased", exception);
233 		}
234 	}
235 
236 	/***
237 	 * A thread used to increase the stepsize while the mouse button (either
238 	 * increase or decrease) is being pressed.
239 	 * <p>
240 	 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628
241 	 * BX Delft, the Netherlands. All rights reserved.
242 	 * 
243 	 * See for project information <a href="http://www.simulation.tudelft.nl/">
244 	 * www.simulation.tudelft.nl </a>.
245 	 * 
246 	 * The source code and binary code of this software is proprietary
247 	 * information of Delft University of Technology.
248 	 * 
249 	 * @author <a
250 	 *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
251 	 *         van Houten </a>
252 	 * @version $Revision: 1.1 $ $Date: 2005/06/16 12:33:54 $
253 	 * @since 1.0.6
254 	 */
255 	private class IncreaseStepSizeThread extends Thread
256 	{
257 		/*** the owner */
258 		private EstimatedTransportTimePanel owner = null;
259 
260 		/***
261 		 * constructs a new increaseStepSizeThread
262 		 * 
263 		 * @param owner the owner
264 		 */
265 		public IncreaseStepSizeThread(final EstimatedTransportTimePanel owner)
266 		{
267 			this.owner = owner;
268 		}
269 
270 		/***
271 		 * @see java.lang.Runnable#run()
272 		 */
273 		public void run()
274 		{
275 			super.run();
276 
277 			// if only cicked once, we must sleep a little
278 			// while and than check whether we should run
279 			try
280 			{
281 				Thread.sleep(500);
282 				int count = 0;
283 				while (this.owner.pressed)
284 				{
285 					count++;
286 					double stepSize = this.owner.stepSize * count;
287 					if (stepSize > (this.owner.maximum - ((Double) this.owner.spinner
288 							.getValue()).doubleValue()))
289 					{
290 						stepSize = this.owner.maximum
291 								- ((Double) this.owner.spinner.getValue())
292 										.doubleValue();
293 					}
294 					if (stepSize > (((Double) this.owner.spinner.getValue())
295 							.doubleValue() - this.owner.minimum))
296 					{
297 						stepSize = ((Double) this.owner.spinner.getValue())
298 								.doubleValue()
299 								- this.owner.minimum;
300 					}
301 					SpinnerModel model = new SpinnerNumberModel(
302 							((Double) this.owner.spinner.getValue())
303 									.doubleValue(),
304 							// current value
305 							this.owner.minimum, // min
306 							this.owner.maximum, // max
307 							stepSize); // step
308 					this.owner.spinner.setModel(model);
309 
310 					// we sleep before increasing the time step
311 					Thread.sleep(100);
312 				}
313 			} catch (Exception exception)
314 			{
315 				Logger.severe(this, "run", exception);
316 			}
317 		}
318 	}
319 }