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: import ;
61:
62:
70: public class BasicSpinnerUI extends SpinnerUI
71: {
72:
80: public static ComponentUI createUI(JComponent c)
81: {
82: return new BasicSpinnerUI();
83: }
84:
85:
93: protected JComponent createEditor()
94: {
95: return spinner.getEditor();
96: }
97:
98:
107: protected LayoutManager createLayout()
108: {
109: return new DefaultLayoutManager();
110: }
111:
112:
117: protected Component createNextButton()
118: {
119: JButton button = new BasicArrowButton(BasicArrowButton.NORTH);
120: return button;
121: }
122:
123:
128: protected Component createPreviousButton()
129: {
130: JButton button = new BasicArrowButton(BasicArrowButton.SOUTH);
131: return button;
132: }
133:
134:
144: protected PropertyChangeListener createPropertyChangeListener()
145: {
146: return new PropertyChangeListener()
147: {
148: public void propertyChange(PropertyChangeEvent evt)
149: {
150:
151:
152: if ("editor".equals(evt.getPropertyName()))
153: BasicSpinnerUI.this.replaceEditor((JComponent) evt.getOldValue(),
154: (JComponent) evt.getNewValue());
155: }
156: };
157: }
158:
159:
168: protected void installDefaults()
169: {
170:
173: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
174:
180: spinner.setLayout(createLayout());
181: spinner.setOpaque(true);
182: }
183:
184:
192: protected void installListeners()
193: {
194: spinner.addPropertyChangeListener(listener);
195: }
196:
197:
200: protected void installNextButtonListeners(Component c)
201: {
202: c.addMouseListener(new MouseAdapter()
203: {
204: public void mousePressed(MouseEvent evt)
205: {
206: if (! spinner.isEnabled())
207: return;
208: increment();
209: timer.setInitialDelay(500);
210: timer.start();
211: }
212:
213: public void mouseReleased(MouseEvent evt)
214: {
215: timer.stop();
216: }
217:
218: void increment()
219: {
220: Object next = BasicSpinnerUI.this.spinner.getNextValue();
221: if (next != null)
222: BasicSpinnerUI.this.spinner.getModel().setValue(next);
223: }
224:
225: volatile boolean mouseDown = false;
226: Timer timer = new Timer(50,
227: new ActionListener()
228: {
229: public void actionPerformed(ActionEvent event)
230: {
231: increment();
232: }
233: });
234: });
235: }
236:
237:
240: protected void installPreviousButtonListeners(Component c)
241: {
242: c.addMouseListener(new MouseAdapter()
243: {
244: public void mousePressed(MouseEvent evt)
245: {
246: if (! spinner.isEnabled())
247: return;
248: decrement();
249: timer.setInitialDelay(500);
250: timer.start();
251: }
252:
253: public void mouseReleased(MouseEvent evt)
254: {
255: timer.stop();
256: }
257:
258: void decrement()
259: {
260: Object prev = BasicSpinnerUI.this.spinner.getPreviousValue();
261: if (prev != null)
262: BasicSpinnerUI.this.spinner.getModel().setValue(prev);
263: }
264:
265: volatile boolean mouseDown = false;
266: Timer timer = new Timer(50,
267: new ActionListener()
268: {
269: public void actionPerformed(ActionEvent event)
270: {
271: decrement();
272: }
273: });
274: });
275: }
276:
277:
290: public void installUI(JComponent c)
291: {
292: super.installUI(c);
293:
294: spinner = (JSpinner) c;
295:
296: installDefaults();
297: installListeners();
298:
299: Component next = createNextButton();
300: Component previous = createPreviousButton();
301:
302: installNextButtonListeners(next);
303: installPreviousButtonListeners(previous);
304:
305: c.add(createEditor(), "Editor");
306: c.add(next, "Next");
307: c.add(previous, "Previous");
308: }
309:
310:
316: protected void replaceEditor(JComponent oldEditor, JComponent newEditor)
317: {
318: spinner.remove(oldEditor);
319: spinner.add(newEditor);
320: }
321:
322:
326: protected void uninstallDefaults()
327: {
328: spinner.setLayout(null);
329: }
330:
331:
335: protected void uninstallListeners()
336: {
337: spinner.removePropertyChangeListener(listener);
338: }
339:
340:
347: public void uninstallUI(JComponent c)
348: {
349: super.uninstallUI(c);
350:
351: uninstallDefaults();
352: uninstallListeners();
353: c.removeAll();
354: }
355:
356:
357: protected JSpinner spinner;
358:
359:
360: private PropertyChangeListener listener = createPropertyChangeListener();
361:
362:
365: private class DefaultLayoutManager implements LayoutManager
366: {
367:
372: public void layoutContainer(Container parent)
373: {
374: synchronized (parent.getTreeLock())
375: {
376: Insets i = parent.getInsets();
377: boolean l2r = parent.getComponentOrientation().isLeftToRight();
378:
385: Dimension e = minSize(editor);
386: Dimension n = minSize(next);
387: Dimension p = minSize(previous);
388: Dimension s = spinner.getPreferredSize();
389:
390: int x = l2r ? i.left : i.right;
391: int y = i.top;
392: int w = Math.max(p.width, n.width);
393: int h = Math.max(p.height, n.height);
394: h = Math.max(h, e.height / 2);
395: int e_width = s.width - w;
396:
397: if (l2r)
398: {
399: setBounds(editor, x, y + (s.height - e.height) / 2, e_width,
400: e.height);
401: x += e_width;
402:
403: setBounds(next, x, y, w, h);
404: y += h;
405:
406: setBounds(previous, x, y, w, h);
407: }
408: else
409: {
410: setBounds(next, x, y + (s.height - e.height) / 2, w, h);
411: y += h;
412:
413: setBounds(previous, x, y, w, h);
414: x += w;
415: y -= h;
416:
417: setBounds(editor, x, y, e_width, e.height);
418: }
419: }
420: }
421:
422:
429: public Dimension minimumLayoutSize(Container parent)
430: {
431: Dimension d = new Dimension();
432:
433: if (editor != null)
434: {
435: Dimension tmp = editor.getMinimumSize();
436: d.width += tmp.width;
437: d.height = tmp.height;
438: }
439:
440: int nextWidth = 0;
441: int previousWidth = 0;
442: int otherHeight = 0;
443:
444: if (next != null)
445: {
446: Dimension tmp = next.getMinimumSize();
447: nextWidth = tmp.width;
448: otherHeight += tmp.height;
449: }
450: if (previous != null)
451: {
452: Dimension tmp = previous.getMinimumSize();
453: previousWidth = tmp.width;
454: otherHeight += tmp.height;
455: }
456:
457: d.height = Math.max(d.height, otherHeight);
458: d.width += Math.max(nextWidth, previousWidth);
459:
460: return d;
461: }
462:
463:
470: public Dimension preferredLayoutSize(Container parent)
471: {
472: Dimension d = new Dimension();
473:
474: if (editor != null)
475: {
476: Dimension tmp = editor.getPreferredSize();
477: d.width += Math.max(tmp.width, 40);
478: d.height = tmp.height;
479: }
480:
481: int nextWidth = 0;
482: int previousWidth = 0;
483: int otherHeight = 0;
484:
485: if (next != null)
486: {
487: Dimension tmp = next.getPreferredSize();
488: nextWidth = tmp.width;
489: otherHeight += tmp.height;
490: }
491: if (previous != null)
492: {
493: Dimension tmp = previous.getPreferredSize();
494: previousWidth = tmp.width;
495: otherHeight += tmp.height;
496: }
497:
498: d.height = Math.max(d.height, otherHeight);
499: d.width += Math.max(nextWidth, previousWidth);
500:
501: return d;
502: }
503:
504:
509: public void removeLayoutComponent(Component child)
510: {
511: if (child == editor)
512: editor = null;
513: else if (child == next)
514: next = null;
515: else if (previous == child)
516: previous = null;
517: }
518:
519:
525: public void addLayoutComponent(String name, Component child)
526: {
527: if ("Editor".equals(name))
528: editor = child;
529: else if ("Next".equals(name))
530: next = child;
531: else if ("Previous".equals(name))
532: previous = child;
533: }
534:
535:
542: private Dimension minSize(Component c)
543: {
544: if (c == null)
545: return new Dimension();
546: else
547: return c.getMinimumSize();
548: }
549:
550:
559: private void setBounds(Component c, int x, int y, int w, int h)
560: {
561: if (c != null)
562: c.setBounds(x, y, w, h);
563: }
564:
565:
566: private Component editor;
567:
568:
569: private Component next;
570:
571:
572: private Component previous;
573: }
574: }