classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FYI: Fix for JSpinner


From: Roman Kennke
Subject: [cp-patches] FYI: Fix for JSpinner
Date: Mon, 13 Jun 2005 15:20:20 +0200
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050317)

JSpinner used to use a StubEditor that was backed by a simple JPanel. I fixed the DefaultEditor and added a DateEditor that enables the JSpinner class actually to be used :-) I also removed the StubEditor since it is not part of the API anyway. Note that JSpinner is still not complete. Dates for instance cannot really be 'spun', you still have to edit them by hand. In order to make this work, there is still alot to fix in JFormattedTextField and co... :-(

2005-06-13  Roman Kennke  <address@hidden>

   * javax/swing/JSpinner.java
   (StubEditor): Removed this inner class.
   (DefaultEditor): Implemented this class and its dummy methods.
   (DateEditor): Added and implemented this inner class.


Index: javax/swing/JSpinner.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JSpinner.java,v
retrieving revision 1.9
diff -u -r1.9 JSpinner.java
--- javax/swing/JSpinner.java   27 May 2005 21:12:46 -0000      1.9
+++ javax/swing/JSpinner.java   13 Jun 2005 13:14:48 -0000
@@ -41,17 +41,19 @@
 import java.awt.Component;
 import java.awt.Container;
 import java.awt.Dimension;
+import java.awt.Insets;
 import java.awt.LayoutManager;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.text.DecimalFormat;
 import java.text.ParseException;
+import java.text.SimpleDateFormat;
 
 import javax.swing.border.EtchedBorder;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
 import javax.swing.plaf.SpinnerUI;
-
+import javax.swing.text.DateFormatter;
 
 /**
  * A JSpinner is a component which typically contains a numeric value and a
@@ -66,53 +68,15 @@
   /**
    * DOCUMENT ME!
    */
-  public static class StubEditor extends JLabel implements ChangeListener
-  {
-    /** DOCUMENT ME! */
-    private JLabel label;
-
-    /** DOCUMENT ME! */
-    private JButton up;
-
-    /** DOCUMENT ME! */
-    private JButton down;
-
-    /** DOCUMENT ME! */
-    private JSpinner spinner;
-
-    /**
-     * Creates a new StubEditor object.
-     *
-     * @param spinner DOCUMENT ME!
-     */
-    public StubEditor(JSpinner spinner)
-    {
-      this.spinner = spinner;
-      setBorder(new EtchedBorder());
-      setHorizontalAlignment(SwingConstants.TRAILING);
-      stateChanged(null); /* fill in the label */
-    }
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @param evt DOCUMENT ME!
-     */
-    public void stateChanged(ChangeEvent evt)
-    {
-      setText(String.valueOf(spinner.getValue()));
-    }
-  }
-
-  /**
-   * DOCUMENT ME!
-   */
   public static class DefaultEditor extends JPanel implements ChangeListener,
                                                               
PropertyChangeListener,
                                                               LayoutManager
   {
     private JSpinner spinner;
-    
+
+    /** The JFormattedTextField that backs the editor. */
+    JFormattedTextField ftf;
+
     /**
      * For compatability with Sun's JDK 1.4.2 rev. 5
      */
@@ -125,8 +89,12 @@
      */
     public DefaultEditor(JSpinner spinner)
     {
+      super();
+      setLayout(this);
       this.spinner = spinner;
-      
+      ftf = new JFormattedTextField();
+      add(ftf);
+      ftf.setValue(spinner.getValue());
       spinner.addChangeListener(this);
     }
 
@@ -163,8 +131,8 @@
      */
     public JFormattedTextField getTextField()
     {
-      return null;
-    } /* TODO */
+      return ftf;
+    }
     
     /**
      * DOCUMENT ME!
@@ -173,7 +141,12 @@
      */
     public void layoutContainer(Container parent)
     {
-    } /* TODO */
+      Insets insets = getInsets();
+      Dimension size = getSize();
+      ftf.setBounds(insets.left, insets.top,
+                    size.width - insets.left - insets.right,
+                    size.height - insets.top - insets.bottom);
+    }
     
     /**
      * DOCUMENT ME!
@@ -184,8 +157,11 @@
      */
     public Dimension minimumLayoutSize(Container parent)
     {
-      return null;
-    } /* TODO */
+      Insets insets = getInsets();
+      Dimension minSize = ftf.getMinimumSize();
+      return new Dimension(minSize.width + insets.left + insets.right,
+                            minSize.height + insets.top + insets.bottom);
+    }
     
     /**
      * DOCUMENT ME!
@@ -196,8 +172,11 @@
      */
     public Dimension preferredLayoutSize(Container parent)
     {
-      return null;
-    } /* TODO */
+      Insets insets = getInsets();
+      Dimension prefSize = ftf.getPreferredSize();
+      return new Dimension(prefSize.width + insets.left + insets.right,
+                            prefSize.height + insets.top + insets.bottom);
+    }
     
     /**
      * DOCUMENT ME!
@@ -279,6 +258,95 @@
     }
   }
 
+  /**
+   * An editor class for a <code>JSpinner</code> that is used
+   * for displaying and editing dates (e.g. that uses
+   * <code>SpinnerDateModel</code> as model).
+   *
+   * The editor uses a address@hidden JTextField} with the value
+   * displayed by a address@hidden DateFormatter} instance.
+   */
+  public static class DateEditor extends DefaultEditor
+  {
+
+    /** The serialVersionUID. */
+    private static final long serialVersionUID = -4279356973770397815L;
+
+    /** The DateFormat instance used to format the date. */
+    SimpleDateFormat dateFormat;
+
+    /**
+     * Creates a new instance of DateEditor for the specified
+     * <code>JSpinner</code>.
+     *
+     * @param spinner the <code>JSpinner</code> for which to
+     *     create a <code>DateEditor</code> instance
+     */
+    public DateEditor(JSpinner spinner)
+    {
+      super(spinner);
+      init(new SimpleDateFormat());
+    }
+
+    /**
+     * Creates a new instance of DateEditor for the specified
+     * <code>JSpinner</code> using the specified date format
+     * pattern.
+     *
+     * @param spinner the <code>JSpinner</code> for which to
+     *     create a <code>DateEditor</code> instance
+     * @param dateFormatPattern the date format to use
+     *
+     * @see SimpleDateFormat(String)
+     */
+    public DateEditor(JSpinner spinner, String dateFormatPattern)
+    {
+      super(spinner);
+      init(new SimpleDateFormat(dateFormatPattern));
+    }
+
+    /**
+     * Initializes the JFormattedTextField for this editor.
+     *
+     * @param the date format to use in the formatted text field
+     */
+    private void init(SimpleDateFormat format)
+    {
+      dateFormat = format;
+      getTextField().setFormatterFactory(
+        new JFormattedTextField.AbstractFormatterFactory()
+        {
+          public JFormattedTextField.AbstractFormatter
+          getFormatter(JFormattedTextField ftf)
+          {
+            return new DateFormatter(dateFormat);
+          }
+        });
+    }
+
+    /**
+     * Returns the <code>SimpleDateFormat</code> instance that is used to
+     * format the date value.
+     *
+     * @return the <code>SimpleDateFormat</code> instance that is used to
+     *     format the date value
+     */
+    public SimpleDateFormat getFormat()
+    {
+      return dateFormat;
+    }
+
+    /**
+     * Returns the address@hidden SpinnerDateModel} that is edited by this 
editor.
+     *
+     * @return the <code>SpinnerDateModel</code> that is edited by this editor
+     */
+    public SpinnerDateModel getModel()
+    {
+      return (SpinnerDateModel) getSpinner().getModel();
+    }
+  }
+
   private static final long serialVersionUID = 3412663575706551720L;
 
   /** DOCUMENT ME! */
@@ -544,5 +612,11 @@
    */
   protected JComponent createEditor(SpinnerModel model)
   {
-    return new StubEditor(this);
-  } /* TODO */}
+    if (model instanceof SpinnerDateModel)
+      return new DateEditor(this);
+    else if (model instanceof SpinnerNumberModel)
+      return new NumberEditor(this);
+    else
+      return new DefaultEditor(this);
+  }
+}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]