2
|
1 |
/*
|
|
2 |
* Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
package javax.swing.plaf.synth;
|
|
26 |
|
|
27 |
import java.awt.*;
|
|
28 |
import java.awt.event.*;
|
|
29 |
|
|
30 |
import javax.swing.*;
|
|
31 |
import javax.swing.plaf.*;
|
|
32 |
import javax.swing.plaf.basic.BasicSpinnerUI;
|
|
33 |
|
|
34 |
import java.beans.*;
|
|
35 |
import sun.swing.plaf.synth.SynthUI;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* Synth's SpinnerUI.
|
|
39 |
*
|
|
40 |
* @author Hans Muller
|
|
41 |
* @author Joshua Outwater
|
|
42 |
*/
|
|
43 |
class SynthSpinnerUI extends BasicSpinnerUI implements PropertyChangeListener,
|
|
44 |
SynthUI {
|
|
45 |
private SynthStyle style;
|
2658
|
46 |
/**
|
|
47 |
* A FocusListener implementation which causes the entire spinner to be
|
|
48 |
* repainted whenever the editor component (typically a text field) becomes
|
|
49 |
* focused, or loses focus. This is necessary because since SynthSpinnerUI
|
|
50 |
* is composed of an editor and two buttons, it is necessary that all three
|
|
51 |
* components indicate that they are "focused" so that they can be drawn
|
|
52 |
* appropriately. The repaint is used to ensure that the buttons are drawn
|
|
53 |
* in the new focused or unfocused state, mirroring that of the editor.
|
|
54 |
*/
|
|
55 |
private EditorFocusHandler editorFocusHandler = new EditorFocusHandler();
|
2
|
56 |
|
|
57 |
/**
|
|
58 |
* Returns a new instance of SynthSpinnerUI.
|
|
59 |
*
|
|
60 |
* @param c the JSpinner (not used)
|
|
61 |
* @see ComponentUI#createUI
|
|
62 |
* @return a new SynthSpinnerUI object
|
|
63 |
*/
|
|
64 |
public static ComponentUI createUI(JComponent c) {
|
|
65 |
return new SynthSpinnerUI();
|
|
66 |
}
|
|
67 |
|
2658
|
68 |
@Override
|
2
|
69 |
protected void installListeners() {
|
|
70 |
super.installListeners();
|
|
71 |
spinner.addPropertyChangeListener(this);
|
2658
|
72 |
JComponent editor = spinner.getEditor();
|
|
73 |
if (editor instanceof JSpinner.DefaultEditor) {
|
|
74 |
JTextField tf = ((JSpinner.DefaultEditor)editor).getTextField();
|
|
75 |
if (tf != null) {
|
|
76 |
tf.addFocusListener(editorFocusHandler);
|
|
77 |
}
|
|
78 |
}
|
2
|
79 |
}
|
|
80 |
|
|
81 |
/**
|
|
82 |
* Removes the <code>propertyChangeListener</code> added
|
|
83 |
* by installListeners.
|
|
84 |
* <p>
|
|
85 |
* This method is called by <code>uninstallUI</code>.
|
|
86 |
*
|
|
87 |
* @see #installListeners
|
|
88 |
*/
|
2658
|
89 |
@Override
|
2
|
90 |
protected void uninstallListeners() {
|
|
91 |
super.uninstallListeners();
|
|
92 |
spinner.removePropertyChangeListener(this);
|
2658
|
93 |
JComponent editor = spinner.getEditor();
|
|
94 |
if (editor instanceof JSpinner.DefaultEditor) {
|
|
95 |
JTextField tf = ((JSpinner.DefaultEditor)editor).getTextField();
|
|
96 |
if (tf != null) {
|
|
97 |
tf.removeFocusListener(editorFocusHandler);
|
|
98 |
}
|
|
99 |
}
|
2
|
100 |
}
|
|
101 |
|
|
102 |
/**
|
|
103 |
* Initialize the <code>JSpinner</code> <code>border</code>,
|
|
104 |
* <code>foreground</code>, and <code>background</code>, properties
|
|
105 |
* based on the corresponding "Spinner.*" properties from defaults table.
|
|
106 |
* The <code>JSpinners</code> layout is set to the value returned by
|
|
107 |
* <code>createLayout</code>. This method is called by <code>installUI</code>.
|
|
108 |
*
|
|
109 |
* @see #uninstallDefaults
|
|
110 |
* @see #installUI
|
|
111 |
* @see #createLayout
|
|
112 |
* @see LookAndFeel#installBorder
|
|
113 |
* @see LookAndFeel#installColors
|
|
114 |
*/
|
|
115 |
protected void installDefaults() {
|
|
116 |
LayoutManager layout = spinner.getLayout();
|
|
117 |
|
|
118 |
if (layout == null || layout instanceof UIResource) {
|
|
119 |
spinner.setLayout(createLayout());
|
|
120 |
}
|
|
121 |
updateStyle(spinner);
|
|
122 |
}
|
|
123 |
|
|
124 |
|
|
125 |
private void updateStyle(JSpinner c) {
|
|
126 |
SynthContext context = getContext(c, ENABLED);
|
|
127 |
SynthStyle oldStyle = style;
|
|
128 |
style = SynthLookAndFeel.updateStyle(context, this);
|
|
129 |
if (style != oldStyle) {
|
|
130 |
if (oldStyle != null) {
|
|
131 |
// Only call installKeyboardActions as uninstall is not
|
|
132 |
// public.
|
|
133 |
installKeyboardActions();
|
|
134 |
}
|
|
135 |
}
|
|
136 |
context.dispose();
|
|
137 |
}
|
|
138 |
|
|
139 |
|
|
140 |
/**
|
|
141 |
* Sets the <code>JSpinner's</code> layout manager to null. This
|
|
142 |
* method is called by <code>uninstallUI</code>.
|
|
143 |
*
|
|
144 |
* @see #installDefaults
|
|
145 |
* @see #uninstallUI
|
|
146 |
*/
|
|
147 |
protected void uninstallDefaults() {
|
|
148 |
if (spinner.getLayout() instanceof UIResource) {
|
|
149 |
spinner.setLayout(null);
|
|
150 |
}
|
|
151 |
|
|
152 |
SynthContext context = getContext(spinner, ENABLED);
|
|
153 |
|
|
154 |
style.uninstallDefaults(context);
|
|
155 |
context.dispose();
|
|
156 |
style = null;
|
|
157 |
}
|
|
158 |
|
|
159 |
|
|
160 |
protected LayoutManager createLayout() {
|
|
161 |
return new SpinnerLayout();
|
|
162 |
}
|
|
163 |
|
|
164 |
|
|
165 |
/**
|
|
166 |
* Create a component that will replace the spinner models value
|
|
167 |
* with the object returned by <code>spinner.getPreviousValue</code>.
|
|
168 |
* By default the <code>previousButton</code> is a JButton
|
|
169 |
* who's <code>ActionListener</code> updates it's <code>JSpinner</code>
|
|
170 |
* ancestors model. If a previousButton isn't needed (in a subclass)
|
|
171 |
* then override this method to return null.
|
|
172 |
*
|
|
173 |
* @return a component that will replace the spinners model with the
|
|
174 |
* next value in the sequence, or null
|
|
175 |
* @see #installUI
|
|
176 |
* @see #createNextButton
|
|
177 |
*/
|
|
178 |
protected Component createPreviousButton() {
|
|
179 |
JButton b = new SynthArrowButton(SwingConstants.SOUTH);
|
|
180 |
b.setName("Spinner.previousButton");
|
|
181 |
installPreviousButtonListeners(b);
|
|
182 |
return b;
|
|
183 |
}
|
|
184 |
|
|
185 |
|
|
186 |
/**
|
|
187 |
* Create a component that will replace the spinner models value
|
|
188 |
* with the object returned by <code>spinner.getNextValue</code>.
|
|
189 |
* By default the <code>nextButton</code> is a JButton
|
|
190 |
* who's <code>ActionListener</code> updates it's <code>JSpinner</code>
|
|
191 |
* ancestors model. If a nextButton isn't needed (in a subclass)
|
|
192 |
* then override this method to return null.
|
|
193 |
*
|
|
194 |
* @return a component that will replace the spinners model with the
|
|
195 |
* next value in the sequence, or null
|
|
196 |
* @see #installUI
|
|
197 |
* @see #createPreviousButton
|
|
198 |
*/
|
|
199 |
protected Component createNextButton() {
|
|
200 |
JButton b = new SynthArrowButton(SwingConstants.NORTH);
|
|
201 |
b.setName("Spinner.nextButton");
|
|
202 |
installNextButtonListeners(b);
|
|
203 |
return b;
|
|
204 |
}
|
|
205 |
|
|
206 |
|
|
207 |
/**
|
|
208 |
* This method is called by installUI to get the editor component
|
|
209 |
* of the <code>JSpinner</code>. By default it just returns
|
|
210 |
* <code>JSpinner.getEditor()</code>. Subclasses can override
|
|
211 |
* <code>createEditor</code> to return a component that contains
|
|
212 |
* the spinner's editor or null, if they're going to handle adding
|
|
213 |
* the editor to the <code>JSpinner</code> in an
|
|
214 |
* <code>installUI</code> override.
|
|
215 |
* <p>
|
|
216 |
* Typically this method would be overridden to wrap the editor
|
|
217 |
* with a container with a custom border, since one can't assume
|
|
218 |
* that the editors border can be set directly.
|
|
219 |
* <p>
|
|
220 |
* The <code>replaceEditor</code> method is called when the spinners
|
|
221 |
* editor is changed with <code>JSpinner.setEditor</code>. If you've
|
|
222 |
* overriden this method, then you'll probably want to override
|
|
223 |
* <code>replaceEditor</code> as well.
|
|
224 |
*
|
|
225 |
* @return the JSpinners editor JComponent, spinner.getEditor() by default
|
|
226 |
* @see #installUI
|
|
227 |
* @see #replaceEditor
|
|
228 |
* @see JSpinner#getEditor
|
|
229 |
*/
|
|
230 |
protected JComponent createEditor() {
|
|
231 |
JComponent editor = spinner.getEditor();
|
|
232 |
editor.setName("Spinner.editor");
|
|
233 |
updateEditorAlignment(editor);
|
|
234 |
return editor;
|
|
235 |
}
|
|
236 |
|
|
237 |
|
|
238 |
/**
|
|
239 |
* Called by the <code>PropertyChangeListener</code> when the
|
|
240 |
* <code>JSpinner</code> editor property changes. It's the responsibility
|
|
241 |
* of this method to remove the old editor and add the new one. By
|
|
242 |
* default this operation is just:
|
|
243 |
* <pre>
|
|
244 |
* spinner.remove(oldEditor);
|
|
245 |
* spinner.add(newEditor, "Editor");
|
|
246 |
* </pre>
|
|
247 |
* The implementation of <code>replaceEditor</code> should be coordinated
|
|
248 |
* with the <code>createEditor</code> method.
|
|
249 |
*
|
|
250 |
* @see #createEditor
|
|
251 |
* @see #createPropertyChangeListener
|
|
252 |
*/
|
|
253 |
protected void replaceEditor(JComponent oldEditor, JComponent newEditor) {
|
|
254 |
spinner.remove(oldEditor);
|
|
255 |
spinner.add(newEditor, "Editor");
|
2658
|
256 |
if (oldEditor instanceof JSpinner.DefaultEditor) {
|
|
257 |
JTextField tf = ((JSpinner.DefaultEditor)oldEditor).getTextField();
|
|
258 |
if (tf != null) {
|
|
259 |
tf.removeFocusListener(editorFocusHandler);
|
|
260 |
}
|
|
261 |
}
|
|
262 |
if (newEditor instanceof JSpinner.DefaultEditor) {
|
|
263 |
JTextField tf = ((JSpinner.DefaultEditor)newEditor).getTextField();
|
|
264 |
if (tf != null) {
|
|
265 |
tf.addFocusListener(editorFocusHandler);
|
|
266 |
}
|
|
267 |
}
|
2
|
268 |
}
|
|
269 |
|
|
270 |
private void updateEditorAlignment(JComponent editor) {
|
|
271 |
if (editor instanceof JSpinner.DefaultEditor) {
|
|
272 |
SynthContext context = getContext(spinner);
|
|
273 |
Integer alignment = (Integer)context.getStyle().get(
|
|
274 |
context, "Spinner.editorAlignment");
|
2658
|
275 |
JTextField text = ((JSpinner.DefaultEditor)editor).getTextField();
|
2
|
276 |
if (alignment != null) {
|
|
277 |
text.setHorizontalAlignment(alignment);
|
2658
|
278 |
|
2
|
279 |
}
|
2658
|
280 |
// copy across the sizeVariant property to the editor
|
|
281 |
text.putClientProperty("JComponent.sizeVariant",
|
|
282 |
spinner.getClientProperty("JComponent.sizeVariant"));
|
2
|
283 |
}
|
|
284 |
}
|
|
285 |
|
|
286 |
public SynthContext getContext(JComponent c) {
|
|
287 |
return getContext(c, getComponentState(c));
|
|
288 |
}
|
|
289 |
|
|
290 |
private SynthContext getContext(JComponent c, int state) {
|
|
291 |
return SynthContext.getContext(SynthContext.class, c,
|
|
292 |
SynthLookAndFeel.getRegion(c), style, state);
|
|
293 |
}
|
|
294 |
|
|
295 |
|
|
296 |
private Region getRegion(JComponent c) {
|
|
297 |
return SynthLookAndFeel.getRegion(c);
|
|
298 |
}
|
|
299 |
|
|
300 |
|
|
301 |
private int getComponentState(JComponent c) {
|
|
302 |
return SynthLookAndFeel.getComponentState(c);
|
|
303 |
}
|
|
304 |
|
|
305 |
|
|
306 |
public void update(Graphics g, JComponent c) {
|
|
307 |
SynthContext context = getContext(c);
|
|
308 |
|
|
309 |
SynthLookAndFeel.update(context, g);
|
|
310 |
context.getPainter().paintSpinnerBackground(context,
|
|
311 |
g, 0, 0, c.getWidth(), c.getHeight());
|
|
312 |
paint(context, g);
|
|
313 |
context.dispose();
|
|
314 |
}
|
|
315 |
|
|
316 |
|
|
317 |
public void paint(Graphics g, JComponent c) {
|
|
318 |
SynthContext context = getContext(c);
|
|
319 |
|
|
320 |
paint(context, g);
|
|
321 |
context.dispose();
|
|
322 |
}
|
|
323 |
|
|
324 |
|
|
325 |
protected void paint(SynthContext context, Graphics g) {
|
|
326 |
}
|
|
327 |
|
|
328 |
public void paintBorder(SynthContext context, Graphics g, int x,
|
|
329 |
int y, int w, int h) {
|
|
330 |
context.getPainter().paintSpinnerBorder(context, g, x, y, w, h);
|
|
331 |
}
|
|
332 |
|
|
333 |
/**
|
|
334 |
* A simple layout manager for the editor and the next/previous buttons.
|
|
335 |
* See the SynthSpinnerUI javadoc for more information about exactly
|
|
336 |
* how the components are arranged.
|
|
337 |
*/
|
|
338 |
private static class SpinnerLayout implements LayoutManager, UIResource
|
|
339 |
{
|
|
340 |
private Component nextButton = null;
|
|
341 |
private Component previousButton = null;
|
|
342 |
private Component editor = null;
|
|
343 |
|
|
344 |
public void addLayoutComponent(String name, Component c) {
|
|
345 |
if ("Next".equals(name)) {
|
|
346 |
nextButton = c;
|
|
347 |
}
|
|
348 |
else if ("Previous".equals(name)) {
|
|
349 |
previousButton = c;
|
|
350 |
}
|
|
351 |
else if ("Editor".equals(name)) {
|
|
352 |
editor = c;
|
|
353 |
}
|
|
354 |
}
|
|
355 |
|
|
356 |
public void removeLayoutComponent(Component c) {
|
|
357 |
if (c == nextButton) {
|
|
358 |
nextButton = null;
|
|
359 |
}
|
|
360 |
else if (c == previousButton) {
|
|
361 |
previousButton = null;
|
|
362 |
}
|
|
363 |
else if (c == editor) {
|
|
364 |
editor = null;
|
|
365 |
}
|
|
366 |
}
|
|
367 |
|
|
368 |
private Dimension preferredSize(Component c) {
|
|
369 |
return (c == null) ? new Dimension(0, 0) : c.getPreferredSize();
|
|
370 |
}
|
|
371 |
|
|
372 |
public Dimension preferredLayoutSize(Container parent) {
|
|
373 |
Dimension nextD = preferredSize(nextButton);
|
|
374 |
Dimension previousD = preferredSize(previousButton);
|
|
375 |
Dimension editorD = preferredSize(editor);
|
|
376 |
|
|
377 |
/* Force the editors height to be a multiple of 2
|
|
378 |
*/
|
|
379 |
editorD.height = ((editorD.height + 1) / 2) * 2;
|
|
380 |
|
|
381 |
Dimension size = new Dimension(editorD.width, editorD.height);
|
|
382 |
size.width += Math.max(nextD.width, previousD.width);
|
|
383 |
Insets insets = parent.getInsets();
|
|
384 |
size.width += insets.left + insets.right;
|
|
385 |
size.height += insets.top + insets.bottom;
|
|
386 |
return size;
|
|
387 |
}
|
|
388 |
|
|
389 |
public Dimension minimumLayoutSize(Container parent) {
|
|
390 |
return preferredLayoutSize(parent);
|
|
391 |
}
|
|
392 |
|
|
393 |
private void setBounds(Component c, int x, int y, int width, int height) {
|
|
394 |
if (c != null) {
|
|
395 |
c.setBounds(x, y, width, height);
|
|
396 |
}
|
|
397 |
}
|
|
398 |
|
|
399 |
public void layoutContainer(Container parent) {
|
|
400 |
Insets insets = parent.getInsets();
|
|
401 |
int availWidth = parent.getWidth() - (insets.left + insets.right);
|
|
402 |
int availHeight = parent.getHeight() - (insets.top + insets.bottom);
|
|
403 |
Dimension nextD = preferredSize(nextButton);
|
|
404 |
Dimension previousD = preferredSize(previousButton);
|
|
405 |
int nextHeight = availHeight / 2;
|
|
406 |
int previousHeight = availHeight - nextHeight;
|
|
407 |
int buttonsWidth = Math.max(nextD.width, previousD.width);
|
|
408 |
int editorWidth = availWidth - buttonsWidth;
|
|
409 |
|
|
410 |
/* Deal with the spinners componentOrientation property.
|
|
411 |
*/
|
|
412 |
int editorX, buttonsX;
|
|
413 |
if (parent.getComponentOrientation().isLeftToRight()) {
|
|
414 |
editorX = insets.left;
|
|
415 |
buttonsX = editorX + editorWidth;
|
|
416 |
}
|
|
417 |
else {
|
|
418 |
buttonsX = insets.left;
|
|
419 |
editorX = buttonsX + buttonsWidth;
|
|
420 |
}
|
|
421 |
|
|
422 |
int previousY = insets.top + nextHeight;
|
|
423 |
setBounds(editor, editorX, insets.top, editorWidth, availHeight);
|
|
424 |
setBounds(nextButton, buttonsX, insets.top, buttonsWidth, nextHeight);
|
|
425 |
setBounds(previousButton, buttonsX, previousY, buttonsWidth, previousHeight);
|
|
426 |
}
|
|
427 |
}
|
|
428 |
|
|
429 |
|
|
430 |
public void propertyChange(PropertyChangeEvent e) {
|
|
431 |
String propertyName = e.getPropertyName();
|
|
432 |
JSpinner spinner = (JSpinner)(e.getSource());
|
|
433 |
SpinnerUI spinnerUI = spinner.getUI();
|
|
434 |
|
|
435 |
if (spinnerUI instanceof SynthSpinnerUI) {
|
|
436 |
SynthSpinnerUI ui = (SynthSpinnerUI)spinnerUI;
|
|
437 |
|
|
438 |
if (SynthLookAndFeel.shouldUpdateStyle(e)) {
|
|
439 |
ui.updateStyle(spinner);
|
|
440 |
}
|
|
441 |
}
|
|
442 |
}
|
2658
|
443 |
|
|
444 |
/** Listen to editor text field focus changes and repaint whole spinner */
|
|
445 |
private class EditorFocusHandler implements FocusListener{
|
|
446 |
/** Invoked when a editor text field gains the keyboard focus. */
|
|
447 |
public void focusGained(FocusEvent e) {
|
|
448 |
spinner.repaint();
|
|
449 |
}
|
|
450 |
|
|
451 |
/** Invoked when a editor text field loses the keyboard focus. */
|
|
452 |
public void focusLost(FocusEvent e) {
|
|
453 |
spinner.repaint();
|
|
454 |
}
|
|
455 |
}
|
|
456 |
|
|
457 |
/** Override the arrowbuttons focus handling to follow the text fields focus */
|
|
458 |
private class SpinnerArrowButton extends SynthArrowButton{
|
|
459 |
public SpinnerArrowButton(int direction) {
|
|
460 |
super(direction);
|
|
461 |
}
|
|
462 |
|
|
463 |
@Override
|
|
464 |
public boolean isFocusOwner() {
|
|
465 |
if (spinner == null){
|
|
466 |
return super.isFocusOwner();
|
|
467 |
} else if (spinner.getEditor() instanceof JSpinner.DefaultEditor){
|
|
468 |
return ((JSpinner.DefaultEditor)spinner.getEditor())
|
|
469 |
.getTextField().isFocusOwner();
|
|
470 |
} else if (spinner.getEditor()!= null) {
|
|
471 |
return spinner.getEditor().isFocusOwner();
|
|
472 |
} else {
|
|
473 |
return super.isFocusOwner();
|
|
474 |
}
|
|
475 |
}
|
|
476 |
}
|
2
|
477 |
}
|