Merge from mainline.
[official-gcc.git] / libjava / classpath / javax / swing / JSpinner.java
blob70045ed53075eef8cf777e23505ae5ab636fcd8f
1 /* JSpinner.java --
2 Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
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.
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. */
39 package javax.swing;
41 import java.awt.Component;
42 import java.awt.Container;
43 import java.awt.Dimension;
44 import java.awt.Insets;
45 import java.awt.LayoutManager;
46 import java.beans.PropertyChangeEvent;
47 import java.beans.PropertyChangeListener;
48 import java.text.DateFormat;
49 import java.text.DecimalFormat;
50 import java.text.NumberFormat;
51 import java.text.ParseException;
52 import java.text.SimpleDateFormat;
54 import javax.swing.event.ChangeEvent;
55 import javax.swing.event.ChangeListener;
56 import javax.swing.plaf.SpinnerUI;
57 import javax.swing.text.DateFormatter;
58 import javax.swing.text.DefaultFormatterFactory;
59 import javax.swing.text.NumberFormatter;
61 /**
62 * A <code>JSpinner</code> is a component that displays a single value from
63 * a sequence of values, and provides a convenient means for selecting the
64 * previous and next values in the sequence. Typically the spinner displays
65 * a numeric value, but it is possible to display dates or arbitrary items
66 * from a list.
68 * @author Ka-Hing Cheung
70 * @since 1.4
72 public class JSpinner extends JComponent
74 /**
75 * The base class for the editor used by the {@link JSpinner} component.
76 * The editor is in fact a panel containing a {@link JFormattedTextField}
77 * component.
79 public static class DefaultEditor
80 extends JPanel
81 implements ChangeListener, PropertyChangeListener, LayoutManager
83 /** The spinner that the editor is allocated to. */
84 private JSpinner spinner;
86 /** The JFormattedTextField that backs the editor. */
87 JFormattedTextField ftf;
89 /**
90 * For compatability with Sun's JDK 1.4.2 rev. 5
92 private static final long serialVersionUID = -5317788736173368172L;
94 /**
95 * Creates a new <code>DefaultEditor</code> object. The editor is
96 * registered with the spinner as a {@link ChangeListener} here.
98 * @param spinner the <code>JSpinner</code> associated with this editor
100 public DefaultEditor(JSpinner spinner)
102 super();
103 setLayout(this);
104 this.spinner = spinner;
105 ftf = new JFormattedTextField();
106 add(ftf);
107 ftf.setValue(spinner.getValue());
108 ftf.addPropertyChangeListener(this);
109 if(getComponentOrientation().isLeftToRight())
110 ftf.setHorizontalAlignment(JTextField.RIGHT);
111 else
112 ftf.setHorizontalAlignment(JTextField.LEFT);
113 spinner.addChangeListener(this);
117 * Returns the <code>JSpinner</code> component that the editor is assigned
118 * to.
120 * @return The spinner that the editor is assigned to.
122 public JSpinner getSpinner()
124 return spinner;
128 * DOCUMENT ME!
130 public void commitEdit() throws ParseException
132 // TODO: Implement this properly.
136 * Removes the editor from the {@link ChangeListener} list maintained by
137 * the specified <code>spinner</code>.
139 * @param spinner the spinner (<code>null</code> not permitted).
141 public void dismiss(JSpinner spinner)
143 spinner.removeChangeListener(this);
147 * Returns the text field used to display and edit the current value in
148 * the spinner.
150 * @return The text field.
152 public JFormattedTextField getTextField()
154 return ftf;
158 * Sets the bounds for the child components in this container. In this
159 * case, the text field is the only component to be laid out.
161 * @param parent the parent container.
163 public void layoutContainer(Container parent)
165 Insets insets = getInsets();
166 Dimension size = getSize();
167 ftf.setBounds(insets.left, insets.top,
168 size.width - insets.left - insets.right,
169 size.height - insets.top - insets.bottom);
173 * Calculates the minimum size for this component. In this case, the
174 * text field is the only subcomponent, so the return value is the minimum
175 * size of the text field plus the insets of this component.
177 * @param parent the parent container.
179 * @return The minimum size.
181 public Dimension minimumLayoutSize(Container parent)
183 Insets insets = getInsets();
184 Dimension minSize = ftf.getMinimumSize();
185 return new Dimension(minSize.width + insets.left + insets.right,
186 minSize.height + insets.top + insets.bottom);
190 * Calculates the preferred size for this component. In this case, the
191 * text field is the only subcomponent, so the return value is the
192 * preferred size of the text field plus the insets of this component.
194 * @param parent the parent container.
196 * @return The preferred size.
198 public Dimension preferredLayoutSize(Container parent)
200 Insets insets = getInsets();
201 Dimension prefSize = ftf.getPreferredSize();
202 return new Dimension(prefSize.width + insets.left + insets.right,
203 prefSize.height + insets.top + insets.bottom);
207 * Receives notification of property changes. If the text field's 'value'
208 * property changes, the spinner's model is updated accordingly.
210 * @param event the event.
212 public void propertyChange(PropertyChangeEvent event)
214 if (event.getSource() == ftf)
216 if (event.getPropertyName().equals("value"))
217 spinner.getModel().setValue(event.getNewValue());
222 * Receives notification of changes in the state of the {@link JSpinner}
223 * that the editor belongs to - the content of the text field is updated
224 * accordingly.
226 * @param event the change event.
228 public void stateChanged(ChangeEvent event)
230 ftf.setValue(spinner.getValue());
234 * This method does nothing. It is required by the {@link LayoutManager}
235 * interface, but since this component has a single child, there is no
236 * need to use this method.
238 * @param child the child component to remove.
240 public void removeLayoutComponent(Component child)
242 // Nothing to do here.
246 * This method does nothing. It is required by the {@link LayoutManager}
247 * interface, but since this component has a single child, there is no
248 * need to use this method.
250 * @param name the name.
251 * @param child the child component to add.
253 public void addLayoutComponent(String name, Component child)
255 // Nothing to do here.
260 * A panel containing a {@link JFormattedTextField} that is configured for
261 * displaying and editing numbers. The panel is used as a subcomponent of
262 * a {@link JSpinner}.
264 * @see JSpinner#createEditor(SpinnerModel)
266 public static class NumberEditor extends DefaultEditor
269 * For compatability with Sun's JDK
271 private static final long serialVersionUID = 3791956183098282942L;
274 * Creates a new <code>NumberEditor</code> object for the specified
275 * <code>spinner</code>. The editor is registered with the spinner as a
276 * {@link ChangeListener}.
278 * @param spinner the component the editor will be used with.
280 public NumberEditor(JSpinner spinner)
282 super(spinner);
283 NumberEditorFormatter nef = new NumberEditorFormatter();
284 nef.setMinimum(getModel().getMinimum());
285 nef.setMaximum(getModel().getMaximum());
286 ftf.setFormatterFactory(new DefaultFormatterFactory(nef));
290 * Creates a new <code>NumberEditor</code> object.
292 * @param spinner the spinner.
293 * @param decimalFormatPattern the number format pattern.
295 public NumberEditor(JSpinner spinner, String decimalFormatPattern)
297 super(spinner);
298 NumberEditorFormatter nef
299 = new NumberEditorFormatter(decimalFormatPattern);
300 nef.setMinimum(getModel().getMinimum());
301 nef.setMaximum(getModel().getMaximum());
302 ftf.setFormatterFactory(new DefaultFormatterFactory(nef));
306 * Returns the format used by the text field.
308 * @return The format used by the text field.
310 public DecimalFormat getFormat()
312 NumberFormatter formatter = (NumberFormatter) ftf.getFormatter();
313 return (DecimalFormat) formatter.getFormat();
317 * Returns the model used by the editor's {@link JSpinner} component,
318 * cast to a {@link SpinnerNumberModel}.
320 * @return The model.
322 public SpinnerNumberModel getModel()
324 return (SpinnerNumberModel) getSpinner().getModel();
328 static class NumberEditorFormatter
329 extends NumberFormatter
331 public NumberEditorFormatter()
333 super(NumberFormat.getInstance());
335 public NumberEditorFormatter(String decimalFormatPattern)
337 super(new DecimalFormat(decimalFormatPattern));
342 * A <code>JSpinner</code> editor used for the {@link SpinnerListModel}.
343 * This editor uses a <code>JFormattedTextField</code> to edit the values
344 * of the spinner.
346 * @author Roman Kennke (kennke@aicas.com)
348 public static class ListEditor extends DefaultEditor
351 * Creates a new instance of <code>ListEditor</code>.
353 * @param spinner the spinner for which this editor is used
355 public ListEditor(JSpinner spinner)
357 super(spinner);
361 * Returns the spinner's model cast as a {@link SpinnerListModel}.
363 * @return The spinner's model.
365 public SpinnerListModel getModel()
367 return (SpinnerListModel) getSpinner().getModel();
372 * An editor class for a <code>JSpinner</code> that is used
373 * for displaying and editing dates (e.g. that uses
374 * <code>SpinnerDateModel</code> as model).
376 * The editor uses a {@link JTextField} with the value
377 * displayed by a {@link DateFormatter} instance.
379 public static class DateEditor extends DefaultEditor
382 /** The serialVersionUID. */
383 private static final long serialVersionUID = -4279356973770397815L;
386 * Creates a new instance of DateEditor for the specified
387 * <code>JSpinner</code>.
389 * @param spinner the <code>JSpinner</code> for which to
390 * create a <code>DateEditor</code> instance
392 public DateEditor(JSpinner spinner)
394 super(spinner);
395 DateEditorFormatter nef = new DateEditorFormatter();
396 nef.setMinimum(getModel().getStart());
397 nef.setMaximum(getModel().getEnd());
398 ftf.setFormatterFactory(new DefaultFormatterFactory(nef));
402 * Creates a new instance of DateEditor for the specified
403 * <code>JSpinner</code> using the specified date format
404 * pattern.
406 * @param spinner the <code>JSpinner</code> for which to
407 * create a <code>DateEditor</code> instance
408 * @param dateFormatPattern the date format to use
410 * @see SimpleDateFormat#SimpleDateFormat(String)
412 public DateEditor(JSpinner spinner, String dateFormatPattern)
414 super(spinner);
415 DateEditorFormatter nef = new DateEditorFormatter(dateFormatPattern);
416 nef.setMinimum(getModel().getStart());
417 nef.setMaximum(getModel().getEnd());
418 ftf.setFormatterFactory(new DefaultFormatterFactory(nef));
422 * Returns the <code>SimpleDateFormat</code> instance that is used to
423 * format the date value.
425 * @return the <code>SimpleDateFormat</code> instance that is used to
426 * format the date value
428 public SimpleDateFormat getFormat()
430 DateFormatter formatter = (DateFormatter) ftf.getFormatter();
431 return (SimpleDateFormat) formatter.getFormat();
435 * Returns the {@link SpinnerDateModel} that is edited by this editor.
437 * @return the <code>SpinnerDateModel</code> that is edited by this editor
439 public SpinnerDateModel getModel()
441 return (SpinnerDateModel) getSpinner().getModel();
445 static class DateEditorFormatter
446 extends DateFormatter
448 public DateEditorFormatter()
450 super(DateFormat.getInstance());
452 public DateEditorFormatter(String dateFormatPattern)
454 super(new SimpleDateFormat(dateFormatPattern));
458 /**
459 * A listener that forwards {@link ChangeEvent} notifications from the model
460 * to the {@link JSpinner}'s listeners.
462 class ModelListener implements ChangeListener
465 * Creates a new listener.
467 public ModelListener()
469 // nothing to do here
473 * Receives notification from the model that its state has changed.
475 * @param event the event (ignored).
477 public void stateChanged(ChangeEvent event)
479 fireStateChanged();
483 /**
484 * The model that defines the current value and permitted values for the
485 * spinner.
487 private SpinnerModel model;
489 /** The current editor. */
490 private JComponent editor;
492 private static final long serialVersionUID = 3412663575706551720L;
495 * Creates a new <code>JSpinner</code> with default instance of
496 * {@link SpinnerNumberModel} (that is, a model with value 0, step size 1,
497 * and no upper or lower limit).
499 * @see javax.swing.SpinnerNumberModel
501 public JSpinner()
503 this(new SpinnerNumberModel());
507 * Creates a new <code>JSpinner with the specified model. The
508 * {@link #createEditor(SpinnerModel)} method is used to create an editor
509 * that is suitable for the model.
511 * @param model the model (<code>null</code> not permitted).
513 * @throws NullPointerException if <code>model</code> is <code>null</code>.
515 public JSpinner(SpinnerModel model)
517 this.model = model;
518 this.editor = createEditor(model);
519 model.addChangeListener(new ModelListener());
520 updateUI();
524 * If the editor is <code>JSpinner.DefaultEditor</code>, then forwards the
525 * call to it, otherwise do nothing.
527 * @throws ParseException DOCUMENT ME!
529 public void commitEdit() throws ParseException
531 if (editor instanceof DefaultEditor)
532 ((DefaultEditor) editor).commitEdit();
536 * Gets the current editor
538 * @return the current editor
540 * @see #setEditor
542 public JComponent getEditor()
544 return editor;
548 * Changes the current editor to the new editor. The old editor is
549 * removed from the spinner's {@link ChangeEvent} list.
551 * @param editor the new editor (<code>null</code> not permitted.
553 * @throws IllegalArgumentException if <code>editor</code> is
554 * <code>null</code>.
556 * @see #getEditor
558 public void setEditor(JComponent editor)
560 if (editor == null)
561 throw new IllegalArgumentException("editor may not be null");
563 JComponent oldEditor = this.editor;
564 if (oldEditor instanceof DefaultEditor)
565 ((DefaultEditor) oldEditor).dismiss(this);
566 else if (oldEditor instanceof ChangeListener)
567 removeChangeListener((ChangeListener) oldEditor);
569 this.editor = editor;
570 firePropertyChange("editor", oldEditor, editor);
574 * Returns the model used by the {@link JSpinner} component.
576 * @return The model.
578 * @see #setModel(SpinnerModel)
580 public SpinnerModel getModel()
582 return model;
586 * Sets a new underlying model.
588 * @param newModel the new model to set
590 * @exception IllegalArgumentException if newModel is <code>null</code>
592 public void setModel(SpinnerModel newModel)
594 if (newModel == null)
595 throw new IllegalArgumentException();
597 if (model == newModel)
598 return;
600 SpinnerModel oldModel = model;
601 model = newModel;
602 firePropertyChange("model", oldModel, newModel);
603 setEditor(createEditor(model));
607 * Gets the next value without changing the current value.
609 * @return the next value
611 * @see javax.swing.SpinnerModel#getNextValue
613 public Object getNextValue()
615 return model.getNextValue();
619 * Gets the previous value without changing the current value.
621 * @return the previous value
623 * @see javax.swing.SpinnerModel#getPreviousValue
625 public Object getPreviousValue()
627 return model.getPreviousValue();
631 * Gets the <code>SpinnerUI</code> that handles this spinner
633 * @return the <code>SpinnerUI</code>
635 public SpinnerUI getUI()
637 return (SpinnerUI) ui;
641 * Gets the current value of the spinner, according to the underly model,
642 * not the UI.
644 * @return the current value
646 * @see javax.swing.SpinnerModel#getValue
648 public Object getValue()
650 return model.getValue();
654 * Sets the value in the model.
656 * @param value the new value.
658 public void setValue(Object value)
660 model.setValue(value);
664 * Returns the ID that identifies which look and feel class will be
665 * the UI delegate for this spinner.
667 * @return <code>"SpinnerUI"</code>.
669 public String getUIClassID()
671 return "SpinnerUI";
675 * This method resets the spinner's UI delegate to the default UI for the
676 * current look and feel.
678 public void updateUI()
680 setUI((SpinnerUI) UIManager.getUI(this));
684 * Sets the UI delegate for the component.
686 * @param ui The spinner's UI delegate.
688 public void setUI(SpinnerUI ui)
690 super.setUI(ui);
694 * Adds a <code>ChangeListener</code>
696 * @param listener the listener to add
698 public void addChangeListener(ChangeListener listener)
700 listenerList.add(ChangeListener.class, listener);
704 * Remove a particular listener
706 * @param listener the listener to remove
708 public void removeChangeListener(ChangeListener listener)
710 listenerList.remove(ChangeListener.class, listener);
714 * Gets all the <code>ChangeListener</code>s
716 * @return all the <code>ChangeListener</code>s
718 public ChangeListener[] getChangeListeners()
720 return (ChangeListener[]) listenerList.getListeners(ChangeListener.class);
724 * Fires a <code>ChangeEvent</code> to all the <code>ChangeListener</code>s
725 * added to this <code>JSpinner</code>
727 protected void fireStateChanged()
729 ChangeEvent evt = new ChangeEvent(this);
730 ChangeListener[] listeners = getChangeListeners();
732 for (int i = 0; i < listeners.length; ++i)
733 listeners[i].stateChanged(evt);
737 * Creates an editor that is appropriate for the specified <code>model</code>.
739 * @param model the model.
741 * @return The editor.
743 protected JComponent createEditor(SpinnerModel model)
745 if (model instanceof SpinnerDateModel)
746 return new DateEditor(this);
747 else if (model instanceof SpinnerNumberModel)
748 return new NumberEditor(this);
749 else if (model instanceof SpinnerListModel)
750 return new ListEditor(this);
751 else
752 return new DefaultEditor(this);