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:
69: public class BasicSpinnerUI extends SpinnerUI
70: {
71:
79: public static ComponentUI createUI(JComponent c)
80: {
81: return new BasicSpinnerUI();
82: }
83:
84:
92: protected JComponent createEditor()
93: {
94: return spinner.getEditor();
95: }
96:
97:
106: protected LayoutManager createLayout()
107: {
108: return new DefaultLayoutManager();
109: }
110:
111:
116: protected Component createNextButton()
117: {
118: JButton button = new BasicArrowButton(BasicArrowButton.NORTH);
119: return button;
120: }
121:
122:
127: protected Component createPreviousButton()
128: {
129: JButton button = new BasicArrowButton(BasicArrowButton.SOUTH);
130: return button;
131: }
132:
133:
143: protected PropertyChangeListener createPropertyChangeListener()
144: {
145: return new PropertyChangeListener()
146: {
147: public void propertyChange(PropertyChangeEvent evt)
148: {
149:
150:
151: if ("editor".equals(evt.getPropertyName()))
152: BasicSpinnerUI.this.replaceEditor((JComponent) evt.getOldValue(),
153: (JComponent) evt.getNewValue());
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: spinner.setLayout(createLayout());
173: spinner.setOpaque(true);
174: }
175:
176:
184: protected void installListeners()
185: {
186: spinner.addPropertyChangeListener(listener);
187: }
188:
189:
192: protected void installNextButtonListeners(Component c)
193: {
194: c.addMouseListener(new MouseAdapter()
195: {
196: public void mousePressed(MouseEvent evt)
197: {
198: if (! spinner.isEnabled())
199: return;
200: increment();
201: timer.setInitialDelay(500);
202: timer.start();
203: }
204:
205: public void mouseReleased(MouseEvent evt)
206: {
207: timer.stop();
208: }
209:
210: void increment()
211: {
212: Object next = BasicSpinnerUI.this.spinner.getNextValue();
213: if (next != null)
214: BasicSpinnerUI.this.spinner.getModel().setValue(next);
215: }
216:
217: volatile boolean mouseDown = false;
218: Timer timer = new Timer(50,
219: new ActionListener()
220: {
221: public void actionPerformed(ActionEvent event)
222: {
223: increment();
224: }
225: });
226: });
227: }
228:
229:
232: protected void installPreviousButtonListeners(Component c)
233: {
234: c.addMouseListener(new MouseAdapter()
235: {
236: public void mousePressed(MouseEvent evt)
237: {
238: if (! spinner.isEnabled())
239: return;
240: decrement();
241: timer.setInitialDelay(500);
242: timer.start();
243: }
244:
245: public void mouseReleased(MouseEvent evt)
246: {
247: timer.stop();
248: }
249:
250: void decrement()
251: {
252: Object prev = BasicSpinnerUI.this.spinner.getPreviousValue();
253: if (prev != null)
254: BasicSpinnerUI.this.spinner.getModel().setValue(prev);
255: }
256:
257: volatile boolean mouseDown = false;
258: Timer timer = new Timer(50,
259: new ActionListener()
260: {
261: public void actionPerformed(ActionEvent event)
262: {
263: decrement();
264: }
265: });
266: });
267: }
268:
269:
282: public void installUI(JComponent c)
283: {
284: super.installUI(c);
285:
286: spinner = (JSpinner) c;
287:
288: installDefaults();
289: installListeners();
290:
291: Component next = createNextButton();
292: Component previous = createPreviousButton();
293:
294: installNextButtonListeners(next);
295: installPreviousButtonListeners(previous);
296:
297: c.add(createEditor(), "Editor");
298: c.add(next, "Next");
299: c.add(previous, "Previous");
300: }
301:
302:
308: protected void replaceEditor(JComponent oldEditor, JComponent newEditor)
309: {
310: spinner.remove(oldEditor);
311: spinner.add(newEditor);
312: }
313:
314:
318: protected void uninstallDefaults()
319: {
320: spinner.setLayout(null);
321: }
322:
323:
327: protected void uninstallListeners()
328: {
329: spinner.removePropertyChangeListener(listener);
330: }
331:
332:
339: public void uninstallUI(JComponent c)
340: {
341: super.uninstallUI(c);
342:
343: uninstallDefaults();
344: uninstallListeners();
345: c.removeAll();
346: }
347:
348:
349: protected JSpinner spinner;
350:
351:
352: private PropertyChangeListener listener = createPropertyChangeListener();
353:
354:
357: private class DefaultLayoutManager implements LayoutManager
358: {
359:
364: public void layoutContainer(Container parent)
365: {
366: synchronized (parent.getTreeLock())
367: {
368: Insets i = parent.getInsets();
369: boolean l2r = parent.getComponentOrientation().isLeftToRight();
370:
377: Dimension e = minSize(editor);
378: Dimension n = minSize(next);
379: Dimension p = minSize(previous);
380: Dimension s = spinner.getPreferredSize();
381:
382: int x = l2r ? i.left : i.right;
383: int y = i.top;
384: int w = Math.max(p.width, n.width);
385: int h = Math.max(p.height, n.height);
386: h = Math.max(h, e.height / 2);
387: int e_width = s.width - w;
388:
389: if (l2r)
390: {
391: setBounds(editor, x, y + (s.height - e.height) / 2, e_width,
392: e.height);
393: x += e_width;
394:
395: setBounds(next, x, y, w, h);
396: y += h;
397:
398: setBounds(previous, x, y, w, h);
399: }
400: else
401: {
402: setBounds(next, x, y + (s.height - e.height) / 2, w, h);
403: y += h;
404:
405: setBounds(previous, x, y, w, h);
406: x += w;
407: y -= h;
408:
409: setBounds(editor, x, y, e_width, e.height);
410: }
411: }
412: }
413:
414:
421: public Dimension minimumLayoutSize(Container parent)
422: {
423: Dimension d = new Dimension();
424:
425: if (editor != null)
426: {
427: Dimension tmp = editor.getMinimumSize();
428: d.width += tmp.width;
429: d.height = tmp.height;
430: }
431:
432: int nextWidth = 0;
433: int previousWidth = 0;
434: int otherHeight = 0;
435:
436: if (next != null)
437: {
438: Dimension tmp = next.getMinimumSize();
439: nextWidth = tmp.width;
440: otherHeight += tmp.height;
441: }
442: if (previous != null)
443: {
444: Dimension tmp = previous.getMinimumSize();
445: previousWidth = tmp.width;
446: otherHeight += tmp.height;
447: }
448:
449: d.height = Math.max(d.height, otherHeight);
450: d.width += Math.max(nextWidth, previousWidth);
451:
452: return d;
453: }
454:
455:
462: public Dimension preferredLayoutSize(Container parent)
463: {
464: Dimension d = new Dimension();
465:
466: if (editor != null)
467: {
468: Dimension tmp = editor.getPreferredSize();
469: d.width += Math.max(tmp.width, 40);
470: d.height = tmp.height;
471: }
472:
473: int nextWidth = 0;
474: int previousWidth = 0;
475: int otherHeight = 0;
476:
477: if (next != null)
478: {
479: Dimension tmp = next.getPreferredSize();
480: nextWidth = tmp.width;
481: otherHeight += tmp.height;
482: }
483: if (previous != null)
484: {
485: Dimension tmp = previous.getPreferredSize();
486: previousWidth = tmp.width;
487: otherHeight += tmp.height;
488: }
489:
490: d.height = Math.max(d.height, otherHeight);
491: d.width += Math.max(nextWidth, previousWidth);
492:
493: return d;
494: }
495:
496:
501: public void removeLayoutComponent(Component child)
502: {
503: if (child == editor)
504: editor = null;
505: else if (child == next)
506: next = null;
507: else if (previous == child)
508: previous = null;
509: }
510:
511:
517: public void addLayoutComponent(String name, Component child)
518: {
519: if ("Editor".equals(name))
520: editor = child;
521: else if ("Next".equals(name))
522: next = child;
523: else if ("Previous".equals(name))
524: previous = child;
525: }
526:
527:
534: private Dimension minSize(Component c)
535: {
536: if (c == null)
537: return new Dimension();
538: else
539: return c.getMinimumSize();
540: }
541:
542:
551: private void setBounds(Component c, int x, int y, int w, int h)
552: {
553: if (c != null)
554: c.setBounds(x, y, w, h);
555: }
556:
557:
558: private Component editor;
559:
560:
561: private Component next;
562:
563:
564: private Component previous;
565: }
566: }