1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.gscg.common.gui.components;
15
16 import java.awt.Container;
17
18 import javax.swing.JComponent;
19 import javax.swing.JFormattedTextField;
20 import javax.swing.JLabel;
21 import javax.swing.JSpinner;
22 import javax.swing.SpinnerModel;
23
24 import org.gscg.common.gui.components.interactivespinner.DefaultEditor;
25 import org.gscg.common.gui.components.interactivespinner.JInteractiveSpinner;
26
27 import nl.tudelft.simulation.logger.Logger;
28
29 /***
30 * <br>
31 *
32 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
33 * Delft, the Netherlands. All rights reserved.
34 *
35 * See for project information <a href="http://www.simulation.tudelft.nl/">
36 * www.simulation.tudelft.nl </a>.
37 *
38 * The source code and binary code of this software is proprietary information
39 * of Delft University of Technology.
40 *
41 * @version 1.0 Jun 22, 2004 <br>
42 * @author <a
43 * href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
44 * van Houten </a>
45 * @version $Revision: 1.1 $ $Date: 2005/06/16 12:33:53 $
46 * @since 1.0.0 <br>
47 */
48 public final class SpinnerUtilities
49 {
50 /***
51 * constructs a new SpinnerUtilities
52 */
53 private SpinnerUtilities()
54 {
55
56 }
57
58 /***
59 * Method addLabeledSpinner.
60 *
61 * @param container the container to add the spinner to
62 * @param labelCaption the caption of the label of the spinner
63 * @param model the model for the spinner
64 * @return returns a spinner
65 */
66 public static JSpinner addLabeledSpinner(final Container container,
67 final String labelCaption, final SpinnerModel model)
68 {
69 JLabel label = new JLabel(labelCaption);
70 container.add(label);
71
72 JSpinner spinner = new JSpinner(model);
73 label.setLabelFor(spinner);
74 container.add(spinner);
75
76 return spinner;
77 }
78
79 /***
80 * Method addLabeledSpinner.
81 *
82 * @param container the container to add the spinner to
83 * @param labelCaption the caption of the label of the spinner
84 * @param model the model for the spinner
85 * @return returns a spinner
86 */
87 public static JInteractiveSpinner addLabeledInteractiveSpinner(
88 final Container container, final String labelCaption,
89 final SpinnerModel model)
90 {
91 JLabel label = new JLabel(labelCaption);
92 container.add(label);
93
94 JInteractiveSpinner spinner = new JInteractiveSpinner(model);
95 label.setLabelFor(spinner);
96 container.add(spinner);
97
98 return spinner;
99 }
100
101 /***
102 * Method getMonthStrings.
103 *
104 * @return returns a string[] with the numbers of each month
105 */
106 public static String[] getMonthStrings()
107 {
108 return new String[]{"01", "02", "03", "04", "05", "06", "07", "08",
109 "09", "10", "11", "12"};
110 }
111
112 /***
113 * Return the formatted text field used by the editor, or null if the editor
114 * doesn't descend from JSpinner.DefaultEditor or DefaultEditor..
115 *
116 * @param spinner the spinner
117 * @return returns a formatted textfield for the spinner
118 */
119 public static JFormattedTextField getTextField(final JSpinner spinner)
120 {
121 JComponent editor = spinner.getEditor();
122 if (editor instanceof JSpinner.DefaultEditor)
123 {
124 return ((JSpinner.DefaultEditor) editor).getTextField();
125 }
126 Logger.severe("SpinnerUtilities",
127 "getTextField(final JSpinner spinner)",
128 "Unexpected editor type: " + spinner.getEditor().getClass()
129 + " isn't a descendant of DefaultEditor");
130 return null;
131 }
132
133 /***
134 * Return the formatted text field used by the editor, or null if the editor
135 * doesn't descend from JSpinner.DefaultEditor or DefaultEditor..
136 *
137 * @param spinner the spinner
138 * @return returns a formatted textfield for the spinner
139 */
140 public static org.gscg.common.gui.components.interactivespinner.JFormattedTextField getTextField(
141 final JInteractiveSpinner spinner)
142 {
143 JComponent editor = spinner.getEditor();
144 if (editor instanceof DefaultEditor)
145 {
146 return ((DefaultEditor) editor).getTextField();
147 }
148 Logger.severe("SpinnerUtilities",
149 "getTextField(final JSpinner spinner)",
150 "Unexpected editor type: " + spinner.getEditor().getClass()
151 + " isn't a descendant of DefaultEditor");
152 return null;
153 }
154 }