OSDN Git Service

2005-04-19 Roman Kennke <roman@kennke.org>
[pf3gnuchains/gcc-fork.git] / libjava / javax / swing / JSpinner.java
1 /* JSpinner.java --
2    Copyright (C) 2004, 2005  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package javax.swing;
40
41 import java.awt.Component;
42 import java.awt.Container;
43 import java.awt.Dimension;
44 import java.awt.LayoutManager;
45 import java.beans.PropertyChangeEvent;
46 import java.beans.PropertyChangeListener;
47 import java.text.DecimalFormat;
48 import java.text.ParseException;
49
50 import javax.swing.border.EtchedBorder;
51 import javax.swing.event.ChangeEvent;
52 import javax.swing.event.ChangeListener;
53 import javax.swing.plaf.SpinnerUI;
54
55
56 /**
57  * A JSpinner is a component which typically contains a numeric value and a
58  * way to manipulate the value.
59  *
60  * @author Ka-Hing Cheung
61  * 
62  * @since 1.4
63  */
64 public class JSpinner extends JComponent
65 {
66   /**
67    * DOCUMENT ME!
68    */
69   public static class StubEditor extends JLabel implements ChangeListener
70   {
71     /** DOCUMENT ME! */
72     private JLabel label;
73
74     /** DOCUMENT ME! */
75     private JButton up;
76
77     /** DOCUMENT ME! */
78     private JButton down;
79
80     /** DOCUMENT ME! */
81     private JSpinner spinner;
82
83     /**
84      * Creates a new StubEditor object.
85      *
86      * @param spinner DOCUMENT ME!
87      */
88     public StubEditor(JSpinner spinner)
89     {
90       this.spinner = spinner;
91       setBorder(new EtchedBorder());
92       setHorizontalAlignment(SwingConstants.TRAILING);
93       stateChanged(null); /* fill in the label */
94     }
95
96     /**
97      * DOCUMENT ME!
98      *
99      * @param evt DOCUMENT ME!
100      */
101     public void stateChanged(ChangeEvent evt)
102     {
103       setText(String.valueOf(spinner.getValue()));
104     }
105   }
106
107   /**
108    * DOCUMENT ME!
109    */
110   public static class DefaultEditor extends JPanel implements ChangeListener,
111                                                               PropertyChangeListener,
112                                                               LayoutManager
113   {
114     private JSpinner spinner;
115     
116     /**
117      * For compatability with Sun's JDK 1.4.2 rev. 5
118      */
119     private static final long serialVersionUID = -5317788736173368172L;
120
121     /**
122      * Creates a new <code>DefaultEditor</code> object.
123      *
124      * @param spinner the <code>JSpinner</code> associated with this editor
125      */
126     public DefaultEditor(JSpinner spinner)
127     {
128       this.spinner = spinner;
129       
130       spinner.addChangeListener(this);
131     }
132
133     /**
134      * Returns the <code>JSpinner</code> object for this editor.
135      */
136     public JSpinner getSpinner()
137     {
138       return spinner;
139     }
140     
141     /**
142      * DOCUMENT ME!
143      */
144     public void commitEdit()
145       throws ParseException
146     {
147     } /* TODO */
148
149     /**
150      * DOCUMENT ME!
151      *
152      * @param spinner DOCUMENT ME!
153      */
154     public void dismiss(JSpinner spinner)
155     {
156       spinner.removeChangeListener(this);
157     }
158
159     /**
160      * DOCUMENT ME!
161      *
162      * @return DOCUMENT ME!
163      */
164     public JFormattedTextField getTextField()
165     {
166       return null;
167     } /* TODO */
168     
169     /**
170      * DOCUMENT ME!
171      *
172      * @param parent DOCUMENT ME!
173      */
174     public void layoutContainer(Container parent)
175     {
176     } /* TODO */
177     
178     /**
179      * DOCUMENT ME!
180      *
181      * @param parent DOCUMENT ME!
182      *
183      * @return DOCUMENT ME!
184      */
185     public Dimension minimumLayoutSize(Container parent)
186     {
187       return null;
188     } /* TODO */
189     
190     /**
191      * DOCUMENT ME!
192      *
193      * @param parent DOCUMENT ME!
194      *
195      * @return DOCUMENT ME!
196      */
197     public Dimension preferredLayoutSize(Container parent)
198     {
199       return null;
200     } /* TODO */
201     
202     /**
203      * DOCUMENT ME!
204      *
205      * @param event DOCUMENT ME!
206      */
207     public void propertyChange(PropertyChangeEvent event)
208     {
209     } /* TODO */
210     
211     /**
212      * DOCUMENT ME!
213      *
214      * @param event DOCUMENT ME!
215      */
216     public void stateChanged(ChangeEvent event)
217     {
218     } /* TODO */
219     
220     /* no-ops */
221     public void removeLayoutComponent(Component child)
222     {
223     }
224
225     /**
226      * DOCUMENT ME!
227      *
228      * @param name DOCUMENT ME!
229      * @param child DOCUMENT ME!
230      */
231     public void addLayoutComponent(String name, Component child)
232     {
233     }
234   }
235
236   /**
237    * DOCUMENT ME!
238    */
239   public static class NumberEditor extends DefaultEditor
240   {
241     /**
242      * For compatability with Sun's JDK
243      */
244     private static final long serialVersionUID = 3791956183098282942L;
245
246     /**
247      * Creates a new NumberEditor object.
248      *
249      * @param spinner DOCUMENT ME!
250      */
251     public NumberEditor(JSpinner spinner)
252     {
253       super(spinner);
254     }
255
256     /**
257      * Creates a new NumberEditor object.
258      *
259      * @param spinner DOCUMENT ME!
260      */
261     public NumberEditor(JSpinner spinner, String decimalFormatPattern)
262     {
263       super(spinner);
264     }
265
266     /**
267      * DOCUMENT ME!
268      *
269      * @return DOCUMENT ME!
270      */
271     public DecimalFormat getFormat()
272     {
273       return null;
274     }
275
276     public SpinnerNumberModel getModel()
277     {
278       return (SpinnerNumberModel) getSpinner().getModel();
279     }
280   }
281
282   /** DOCUMENT ME! */
283   private SpinnerModel model;
284
285   /** DOCUMENT ME! */
286   private JComponent editor;
287
288   /** DOCUMENT ME! */
289   private ChangeListener listener = new ChangeListener()
290     {
291       public void stateChanged(ChangeEvent evt)
292       {
293         fireStateChanged();
294       }
295     };
296
297   /**
298    * Creates a JSpinner with <code>SpinnerNumberModel</code>
299    *
300    * @see javax.swing.SpinnerNumberModel
301    */
302   public JSpinner()
303   {
304     this(new SpinnerNumberModel());
305   }
306
307   /**
308    * Creates a JSpinner with the specific model and sets the default editor
309    *
310    * @param model DOCUMENT ME!
311    */
312   public JSpinner(SpinnerModel model)
313   {
314     this.model = model;
315     model.addChangeListener(listener);
316     setEditor(createEditor(model));
317     updateUI();
318   }
319
320   /**
321    * If the editor is <code>JSpinner.DefaultEditor</code>, then forwards the
322    * call to it, otherwise do nothing.
323    *
324    * @throws ParseException DOCUMENT ME!
325    */
326   public void commitEdit() throws ParseException
327   {
328     if (editor instanceof DefaultEditor)
329       ((DefaultEditor) editor).commitEdit();
330   }
331
332   /**
333    * Gets the current editor
334    *
335    * @return the current editor
336    *
337    * @see #setEditor
338    */
339   public JComponent getEditor()
340   {
341     return editor;
342   }
343
344   /**
345    * Changes the current editor to the new editor. This methods should remove
346    * the old listeners (if any) and adds the new listeners (if any).
347    *
348    * @param editor the new editor
349    *
350    * @throws IllegalArgumentException DOCUMENT ME!
351    *
352    * @see #getEditor
353    */
354   public void setEditor(JComponent editor)
355   {
356     if (editor == null)
357       throw new IllegalArgumentException("editor may not be null");
358
359     if (this.editor instanceof DefaultEditor)
360       ((DefaultEditor) editor).dismiss(this);
361     else if (this.editor instanceof ChangeListener)
362       removeChangeListener((ChangeListener) this.editor);
363
364     if (editor instanceof ChangeListener)
365       addChangeListener((ChangeListener) editor);
366
367     this.editor = editor;
368   }
369
370   /**
371    * Gets the underly model.
372    *
373    * @return the underly model
374    */
375   public SpinnerModel getModel()
376   {
377     return model;
378   }
379
380   /**
381    * Sets a new underlying model.
382    *
383    * @param newModel the new model to set
384    *
385    * @exception IllegalArgumentException if newModel is <code>null</code>
386    */
387   public void setModel(SpinnerModel newModel)
388   {
389     if (newModel == null)
390       throw new IllegalArgumentException();
391     
392     if (model == newModel)
393       return;
394
395     SpinnerModel oldModel = model;
396     model = newModel;
397     firePropertyChange("model", oldModel, newModel);
398
399     if (editor == null)
400       setEditor(createEditor(model));
401   }
402
403   /**
404    * Gets the next value without changing the current value.
405    *
406    * @return the next value
407    *
408    * @see javax.swing.SpinnerModel#getNextValue
409    */
410   public Object getNextValue()
411   {
412     return model.getNextValue();
413   }
414
415   /**
416    * Gets the previous value without changing the current value.
417    *
418    * @return the previous value
419    *
420    * @see javax.swing.SpinnerModel#getPreviousValue
421    */
422   public Object getPreviousValue()
423   {
424     return model.getPreviousValue();
425   }
426
427   /**
428    * Gets the <code>SpinnerUI</code> that handles this spinner
429    *
430    * @return the <code>SpinnerUI</code>
431    */
432   public SpinnerUI getUI()
433   {
434     return (SpinnerUI) ui;
435   }
436
437   /**
438    * Gets the current value of the spinner, according to the underly model,
439    * not the UI.
440    *
441    * @return the current value
442    *
443    * @see javax.swing.SpinnerModel#getValue
444    */
445   public Object getValue()
446   {
447     return model.getValue();
448   }
449
450   /**
451    * DOCUMENT ME!
452    *
453    * @param value DOCUMENT ME!
454    */
455   public void setValue(Object value)
456   {
457     model.setValue(value);
458   }
459
460   /**
461    * This method returns a name to identify which look and feel class will be
462    * the UI delegate for this spinner.
463    *
464    * @return The UIClass identifier. "SpinnerUI"
465    */
466   public String getUIClassID()
467   {
468     return "SpinnerUI";
469   }
470
471   /**
472    * This method resets the spinner's UI delegate to the default UI for the
473    * current look and feel.
474    */
475   public void updateUI()
476   {
477     setUI((SpinnerUI) UIManager.getUI(this));
478   }
479
480   /**
481    * This method sets the spinner's UI delegate.
482    *
483    * @param ui The spinner's UI delegate.
484    */
485   public void setUI(SpinnerUI ui)
486   {
487     super.setUI(ui);
488   }
489
490   /**
491    * Adds a <code>ChangeListener</code>
492    *
493    * @param listener the listener to add
494    */
495   public void addChangeListener(ChangeListener listener)
496   {
497     listenerList.add(ChangeListener.class, listener);
498   }
499
500   /**
501    * Remove a particular listener
502    *
503    * @param listener the listener to remove
504    */
505   public void removeChangeListener(ChangeListener listener)
506   {
507     listenerList.remove(ChangeListener.class, listener);
508   }
509
510   /**
511    * Gets all the <code>ChangeListener</code>s
512    *
513    * @return all the <code>ChangeListener</code>s
514    */
515   public ChangeListener[] getChangeListeners()
516   {
517     return (ChangeListener[]) listenerList.getListeners(ChangeListener.class);
518   }
519
520   /**
521    * Fires a <code>ChangeEvent</code> to all the <code>ChangeListener</code>s
522    * added to this <code>JSpinner</code>
523    */
524   protected void fireStateChanged()
525   {
526     ChangeEvent evt = new ChangeEvent(this);
527     ChangeListener[] listeners = getChangeListeners();
528
529     for (int i = 0; i < listeners.length; ++i)
530       listeners[i].stateChanged(evt);
531   }
532
533   /**
534    * Creates an editor for this <code>JSpinner</code>. Really, it should be a
535    * <code>JSpinner.DefaultEditor</code>, but since that should be
536    * implemented by a JFormattedTextField, and one is not written, I am just
537    * using a dummy one backed by a JLabel.
538    *
539    * @param model DOCUMENT ME!
540    *
541    * @return the default editor
542    */
543   protected JComponent createEditor(SpinnerModel model)
544   {
545     return new StubEditor(this);
546   } /* TODO */}