1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52:
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60:
61:
68: public class BasicSpinnerUI extends SpinnerUI
69: {
70:
78: public static ComponentUI createUI(JComponent c)
79: {
80: return new BasicSpinnerUI();
81: }
82:
83:
91: protected JComponent createEditor()
92: {
93: return spinner.getEditor();
94: }
95:
96:
105: protected LayoutManager createLayout()
106: {
107: return new DefaultLayoutManager();
108: }
109:
110:
115: protected Component createNextButton()
116: {
117: JButton button = new BasicArrowButton(BasicArrowButton.NORTH);
118: return button;
119: }
120:
121:
126: protected Component createPreviousButton()
127: {
128: JButton button = new BasicArrowButton(BasicArrowButton.SOUTH);
129: return button;
130: }
131:
132:
142: protected PropertyChangeListener createPropertyChangeListener()
143: {
144: return new PropertyChangeListener()
145: {
146: public void propertyChange(PropertyChangeEvent event)
147: {
148:
149:
150: if ("editor".equals(event.getPropertyName()))
151: BasicSpinnerUI.this.replaceEditor((JComponent) event.getOldValue(),
152: (JComponent) event.getNewValue());
153:
154: }
155: };
156: }
157:
158:
167: protected void installDefaults()
168: {
169: LookAndFeel.installColorsAndFont(spinner, "Spinner.background",
170: "Spinner.foreground", "Spinner.font");
171: LookAndFeel.installBorder(spinner, "Spinner.border");
172: JComponent e = spinner.getEditor();
173: if (e instanceof JSpinner.DefaultEditor)
174: {
175: JSpinner.DefaultEditor de = (JSpinner.DefaultEditor) e;
176: de.getTextField().setBorder(null);
177: }
178: spinner.setLayout(createLayout());
179: spinner.setOpaque(true);
180: }
181:
182:
190: protected void installListeners()
191: {
192: spinner.addPropertyChangeListener(listener);
193: }
194:
195:
198: protected void installNextButtonListeners(Component c)
199: {
200: c.addMouseListener(new MouseAdapter()
201: {
202: public void mousePressed(MouseEvent evt)
203: {
204: if (! spinner.isEnabled())
205: return;
206: increment();
207: timer.setInitialDelay(500);
208: timer.start();
209: }
210:
211: public void mouseReleased(MouseEvent evt)
212: {
213: timer.stop();
214: }
215:
216: void increment()
217: {
218: Object next = BasicSpinnerUI.this.spinner.getNextValue();
219: if (next != null)
220: BasicSpinnerUI.this.spinner.getModel().setValue(next);
221: }
222:
223: volatile boolean mouseDown;
224: Timer timer = new Timer(50,
225: new ActionListener()
226: {
227: public void actionPerformed(ActionEvent event)
228: {
229: increment();
230: }
231: });
232: });
233: }
234:
235:
238: protected void installPreviousButtonListeners(Component c)
239: {
240: c.addMouseListener(new MouseAdapter()
241: {
242: public void mousePressed(MouseEvent evt)
243: {
244: if (! spinner.isEnabled())
245: return;
246: decrement();
247: timer.setInitialDelay(500);
248: timer.start();
249: }
250:
251: public void mouseReleased(MouseEvent evt)
252: {
253: timer.stop();
254: }
255:
256: void decrement()
257: {
258: Object prev = BasicSpinnerUI.this.spinner.getPreviousValue();
259: if (prev != null)
260: BasicSpinnerUI.this.spinner.getModel().setValue(prev);
261: }
262:
263: volatile boolean mouseDown;
264: Timer timer = new Timer(50,
265: new ActionListener()
266: {
267: public void actionPerformed(ActionEvent event)
268: {
269: decrement();
270: }
271: });
272: });
273: }
274:
275:
288: public void installUI(JComponent c)
289: {
290: super.installUI(c);
291:
292: spinner = (JSpinner) c;
293:
294: installDefaults();
295: installListeners();
296:
297: Component next = createNextButton();
298: Component previous = createPreviousButton();
299:
300: installNextButtonListeners(next);
301: installPreviousButtonListeners(previous);
302:
303: c.add(createEditor(), "Editor");
304: c.add(next, "Next");
305: c.add(previous, "Previous");
306: }
307:
308:
314: protected void replaceEditor(JComponent oldEditor, JComponent newEditor)
315: {
316: spinner.remove(oldEditor);
317: spinner.add(newEditor);
318: }
319:
320:
324: protected void uninstallDefaults()
325: {
326: spinner.setLayout(null);
327: }
328:
329:
333: protected void uninstallListeners()
334: {
335: spinner.removePropertyChangeListener(listener);
336: }
337:
338:
345: public void uninstallUI(JComponent c)
346: {
347: super.uninstallUI(c);
348:
349: uninstallDefaults();
350: uninstallListeners();
351: c.removeAll();
352: }
353:
354:
355: protected JSpinner spinner;
356:
357:
358: private PropertyChangeListener listener = createPropertyChangeListener();
359:
360:
364: private class DefaultLayoutManager implements LayoutManager
365: {
366:
371: public void layoutContainer(Container parent)
372: {
373: synchronized (parent.getTreeLock())
374: {
375: Insets i = parent.getInsets();
376: boolean l2r = parent.getComponentOrientation().isLeftToRight();
377:
384: Dimension e = prefSize(editor);
385: Dimension n = prefSize(next);
386: Dimension p = prefSize(previous);
387: Dimension s = parent.getSize();
388:
389: int x = l2r ? i.left : i.right;
390: int y = i.top;
391: int w = Math.max(p.width, n.width);
392: int h = (s.height - i.bottom) / 2;
393: int e_width = s.width - w - i.left - i.right;
394:
395: if (l2r)
396: {
397: setBounds(editor, x, y, e_width, 2 * h);
398: x += e_width;
399: setBounds(next, x, y, w, h);
400: y += h;
401: setBounds(previous, x, y, w, h);
402: }
403: else
404: {
405: setBounds(next, x, y + (s.height - e.height) / 2, w, h);
406: y += h;
407: setBounds(previous, x, y + (s.height - e.height) / 2, w, h);
408: x += w;
409: y -= h;
410: setBounds(editor, x, y, e_width, e.height);
411: }
412: }
413: }
414:
415:
422: public Dimension minimumLayoutSize(Container parent)
423: {
424: Dimension d = new Dimension();
425:
426: if (editor != null)
427: {
428: Dimension tmp = editor.getMinimumSize();
429: d.width += tmp.width;
430: d.height = tmp.height;
431: }
432:
433: int nextWidth = 0;
434: int previousWidth = 0;
435:
436: if (next != null)
437: {
438: Dimension tmp = next.getMinimumSize();
439: nextWidth = tmp.width;
440: }
441: if (previous != null)
442: {
443: Dimension tmp = previous.getMinimumSize();
444: previousWidth = tmp.width;
445: }
446:
447: d.width += Math.max(nextWidth, previousWidth);
448:
449: return d;
450: }
451:
452:
459: public Dimension preferredLayoutSize(Container parent)
460: {
461: Dimension d = new Dimension();
462:
463: if (editor != null)
464: {
465: Dimension tmp = editor.getPreferredSize();
466: d.width += Math.max(tmp.width, 40);
467: d.height = tmp.height;
468: }
469:
470: int nextWidth = 0;
471: int previousWidth = 0;
472:
473: if (next != null)
474: {
475: Dimension tmp = next.getPreferredSize();
476: nextWidth = tmp.width;
477: }
478: if (previous != null)
479: {
480: Dimension tmp = previous.getPreferredSize();
481: previousWidth = tmp.width;
482: }
483:
484: d.width += Math.max(nextWidth, previousWidth);
485: Insets insets = parent.getInsets();
486: d.width = d.width + insets.left + insets.right;
487: d.height = d.height + insets.top + insets.bottom;
488: return d;
489: }
490:
491:
496: public void removeLayoutComponent(Component child)
497: {
498: if (child == editor)
499: editor = null;
500: else if (child == next)
501: next = null;
502: else if (previous == child)
503: previous = null;
504: }
505:
506:
512: public void addLayoutComponent(String name, Component child)
513: {
514: if ("Editor".equals(name))
515: editor = child;
516: else if ("Next".equals(name))
517: next = child;
518: else if ("Previous".equals(name))
519: previous = child;
520: }
521:
522:
529: private Dimension prefSize(Component c)
530: {
531: if (c == null)
532: return new Dimension();
533: else
534: return c.getPreferredSize();
535: }
536:
537:
546: private void setBounds(Component c, int x, int y, int w, int h)
547: {
548: if (c != null)
549: c.setBounds(x, y, w, h);
550: }
551:
552:
553: private Component editor;
554:
555:
556: private Component next;
557:
558:
559: private Component previous;
560: }
561: }