[kaffe] CVS kaffe (robilad): Resynced with GNU Classpath: awt and swing fixes

Kaffe CVS cvs-commits at kaffe.org
Sat Jun 11 16:00:34 PDT 2005


PatchSet 6636 
Date: 2005/06/11 22:55:55
Author: robilad
Branch: HEAD
Tag: (none) 
Log:
Resynced with GNU Classpath: awt and swing fixes

Members: 
	ChangeLog:1.4162->1.4163 
	libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GdkGraphics2D.c:1.12->1.13 
	libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GtkPanelPeer.c:1.4->1.5 
	libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GtkWindowPeer.c:1.18->1.19 
	libraries/javalib/Makefile.am:1.363->1.364 
	libraries/javalib/Makefile.in:1.453->1.454 
	libraries/javalib/all.files:1.135->1.136 
	libraries/javalib/gnu/java/awt/peer/gtk/GtkPanelPeer.java:1.3->1.4 
	libraries/javalib/java/awt/MenuItem.java:1.20->1.21 
	libraries/javalib/javax/swing/JTextArea.java:1.11->1.12 
	libraries/javalib/javax/swing/plaf/basic/BasicSplitPaneUI.java:1.7->1.8 
	libraries/javalib/javax/swing/text/InternationalFormatter.java:INITIAL->1.1 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.4162 kaffe/ChangeLog:1.4163
--- kaffe/ChangeLog:1.4162	Sat Jun 11 22:06:07 2005
+++ kaffe/ChangeLog	Sat Jun 11 22:55:55 2005
@@ -2,6 +2,48 @@
 
 	Resynced with GNU Classpath.
 	
+	2005-06-10  Roman Kennke  <roman at kennke.org>
+
+	* javax/swing/text/InternationalFormatter.java
+	Implemented new class.
+
+	2005-06-10  Roman Kennke  <roman at kennke.org>
+
+	* javax/swing/plaf/basic/BasicSplitPaneUI.java
+	(BasicHorizontalLayoutManager.preferredLayoutSize): fixed return
+	statement.
+	(BasicVerticalLayoutManager.preferredLayoutSize): Likewise.
+	
+	2005-06-10  Roman Kennke  <roman at kennke.org>
+
+	* javax/swing/JTextArea.java
+	(getPreferredSize): Implemented new method. This is overridden
+	in order to support custom set rows and columns.
+
+	2005-06-10  Sven de Marothy  <sven at physto.se>
+
+	* native/jni/gtk-peer/gnu_java_awt_peer_gtk_GdkGraphics2D.c
+	(gdkDrawDrawable): Check if cairo pattern is non-null before 
+	setting its matrix.
+	
+	2005-06-09  Thomas Fitzsimmons  <fitzsim at redhat.com>
+
+	* gnu/java/awt/peer/gtk/GtkPanelPeer.java,
+	gnu_java_awt_peer_gtk_GtkPanelPeer.c (connectSignals): New method.
+	* include/gnu_java_awt_peer_gtk_GtkPanelPeer.h: Regenerate.
+	* native/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkWindowPeer.c
+	(window_focus_in_cb, window_focus_out_cb): Remove FIXMEs.
+
+	* native/jni/gtk-peer/gnu_java_awt_peer_gtk_GdkGraphics2D.c
+	(copyState): Check if cairo pattern is null before copying it.
+	(drawPixels): Check if cairo pattern is null before retrieving it.
+
+	* java/awt/MenuItem.java (label): Initialize to empty string.
+	
+2005-06-11  Dalibor Topic  <robilad at kaffe.org>
+
+	Resynced with GNU Classpath.
+	
 	2005-06-08  Bryce McKinlay  <mckinlay at redhat.com>
 
 	* java/rmi/server/RMIClassLoader.java (getClassLoader): Make public.
Index: kaffe/libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GdkGraphics2D.c
diff -u kaffe/libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GdkGraphics2D.c:1.12 kaffe/libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GdkGraphics2D.c:1.13
--- kaffe/libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GdkGraphics2D.c:1.12	Sat Jun 11 22:04:52 2005
+++ kaffe/libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GdkGraphics2D.c	Sat Jun 11 22:55:59 2005
@@ -403,7 +403,8 @@
 	init_graphics2d_as_pixbuf (g);
     }
 
-  cairo_pattern_set_filter (g->pattern, CAIRO_FILTER_FAST);
+  if (g->pattern)
+    cairo_pattern_set_filter (g->pattern, CAIRO_FILTER_FAST);
 
   NSA_SET_G2D_PTR (env, obj, g);
   gdk_threads_leave();
@@ -503,9 +504,9 @@
   height = min (s_height, d_height);
 
   cairo_get_matrix (src->cr, &matrix);
-  cairo_matrix_translate (&matrix, (double)-x, (double)-y);
-  cairo_pattern_set_matrix (src->pattern, &matrix);
-
+  cairo_matrix_translate (&matrix, (double)-x, (double)-y); 
+  if (src->pattern)
+    cairo_pattern_set_matrix (src->pattern, &matrix); 
   tmp_op = cairo_get_operator (dst->cr); 
   cairo_set_operator(dst->cr, CAIRO_OPERATOR_SOURCE); 
   cairo_set_source_surface (dst->cr, src->surface, width, height);
@@ -513,7 +514,8 @@
   cairo_set_operator(dst->cr, tmp_op);
 
   cairo_matrix_translate (&matrix, (double)x, (double)y);
-  cairo_pattern_set_matrix (src->pattern, &matrix);
+  if (src->pattern)
+    cairo_pattern_set_matrix (src->pattern, &matrix);
 
   gdk_flush();
 
@@ -826,7 +828,8 @@
 
    p = cairo_pattern_create_for_surface (surf);
    cairo_pattern_set_matrix (p, &mat);
-   cairo_pattern_set_filter (p, cairo_pattern_get_filter (gr->pattern));
+   if (gr->pattern)
+     cairo_pattern_set_filter (p, cairo_pattern_get_filter (gr->pattern));
    cairo_set_source_surface (gr->cr, surf, w, h);
    cairo_paint (gr->cr);
    cairo_surface_destroy (surf);
Index: kaffe/libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GtkPanelPeer.c
diff -u kaffe/libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GtkPanelPeer.c:1.4 kaffe/libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GtkPanelPeer.c:1.5
--- kaffe/libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GtkPanelPeer.c:1.4	Sat Oct  9 22:18:26 2004
+++ kaffe/libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GtkPanelPeer.c	Sat Jun 11 22:55:59 2005
@@ -40,6 +40,13 @@
 #include "gnu_java_awt_peer_gtk_GtkComponentPeer.h"
 #include "gnu_java_awt_peer_gtk_GtkPanelPeer.h"
 
+static gboolean panel_focus_in_cb (GtkWidget * widget,
+                                   GdkEventFocus *event,
+                                   jobject peer);
+static gboolean panel_focus_out_cb (GtkWidget * widget,
+                                    GdkEventFocus *event,
+                                    jobject peer);
+
 JNIEXPORT void JNICALL 
 Java_gnu_java_awt_peer_gtk_GtkPanelPeer_create
   (JNIEnv *env, jobject obj)
@@ -49,7 +56,7 @@
   NSA_SET_GLOBAL_REF (env, obj);
 
   gdk_threads_enter ();
-  
+
   widget = gtk_fixed_new ();
 
   gtk_fixed_set_has_window (GTK_FIXED (widget), TRUE);
@@ -60,3 +67,47 @@
 
   NSA_SET_PTR (env, obj, widget);
 }
+
+JNIEXPORT void JNICALL
+Java_gnu_java_awt_peer_gtk_GtkPanelPeer_connectSignals
+  (JNIEnv *env, jobject obj)
+{
+  void *ptr;
+  jobject *gref;
+
+  ptr = NSA_GET_PTR (env, obj);
+  gref = NSA_GET_GLOBAL_REF (env, obj);
+
+  gdk_threads_enter ();
+
+  g_signal_connect (GTK_OBJECT (ptr), "event",
+                    G_CALLBACK (pre_event_handler), *gref);
+
+  g_signal_connect (G_OBJECT (ptr), "focus-in-event",
+                    G_CALLBACK (panel_focus_in_cb), *gref);
+
+  g_signal_connect (G_OBJECT (ptr), "focus-out-event",
+                    G_CALLBACK (panel_focus_out_cb), *gref);
+
+  g_signal_connect_after (G_OBJECT (ptr), "realize",
+                          G_CALLBACK (connect_awt_hook_cb), *gref);
+
+  gdk_threads_leave ();
+}
+
+static gboolean
+panel_focus_in_cb (GtkWidget * widget  __attribute__((unused)),
+		    GdkEventFocus *event  __attribute__((unused)),
+		    jobject peer __attribute__((unused)))
+{
+  return TRUE;
+}
+
+static gboolean
+panel_focus_out_cb (GtkWidget * widget __attribute__((unused)),
+		     GdkEventFocus *event __attribute__((unused)),
+		     jobject peer __attribute__((unused)))
+{
+  return TRUE;
+}
+
Index: kaffe/libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GtkWindowPeer.c
diff -u kaffe/libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GtkWindowPeer.c:1.18 kaffe/libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GtkWindowPeer.c:1.19
--- kaffe/libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GtkWindowPeer.c:1.18	Sun May 15 16:53:53 2005
+++ kaffe/libraries/clib/awt/classpath-gtk/gtk-peer/gnu_java_awt_peer_gtk_GtkWindowPeer.c	Sat Jun 11 22:55:59 2005
@@ -561,8 +561,6 @@
                               postWindowEventID,
                               (jint) AWT_WINDOW_GAINED_FOCUS,
                               (jobject) NULL, (jint) 0);
-  /* FIXME: somewhere after this is handled, the child window is
-     getting an expose event. */
   gdk_threads_enter ();
   return FALSE;
 }
@@ -577,8 +575,6 @@
                               postWindowEventID,
                               (jint) AWT_WINDOW_LOST_FOCUS,
                               (jobject) NULL, (jint) 0);
-  /* FIXME: somewhere after this is handled, the child window is
-     getting an expose event. */
   gdk_threads_enter ();
   return FALSE;
 }
Index: kaffe/libraries/javalib/Makefile.am
diff -u kaffe/libraries/javalib/Makefile.am:1.363 kaffe/libraries/javalib/Makefile.am:1.364
--- kaffe/libraries/javalib/Makefile.am:1.363	Sat Jun 11 22:04:52 2005
+++ kaffe/libraries/javalib/Makefile.am	Sat Jun 11 22:55:59 2005
@@ -3872,6 +3872,7 @@
 	javax/swing/text/FieldView.java \
 	javax/swing/text/GapContent.java \
 	javax/swing/text/Highlighter.java \
+	javax/swing/text/InternationalFormatter.java \
 	javax/swing/text/JTextComponent.java \
 	javax/swing/text/Keymap.java \
 	javax/swing/text/LayeredHighlighter.java \
Index: kaffe/libraries/javalib/Makefile.in
diff -u kaffe/libraries/javalib/Makefile.in:1.453 kaffe/libraries/javalib/Makefile.in:1.454
--- kaffe/libraries/javalib/Makefile.in:1.453	Sat Jun 11 22:04:53 2005
+++ kaffe/libraries/javalib/Makefile.in	Sat Jun 11 22:56:00 2005
@@ -4448,6 +4448,7 @@
 	javax/swing/text/FieldView.java \
 	javax/swing/text/GapContent.java \
 	javax/swing/text/Highlighter.java \
+	javax/swing/text/InternationalFormatter.java \
 	javax/swing/text/JTextComponent.java \
 	javax/swing/text/Keymap.java \
 	javax/swing/text/LayeredHighlighter.java \
Index: kaffe/libraries/javalib/all.files
diff -u kaffe/libraries/javalib/all.files:1.135 kaffe/libraries/javalib/all.files:1.136
--- kaffe/libraries/javalib/all.files:1.135	Sat Jun 11 20:23:06 2005
+++ kaffe/libraries/javalib/all.files	Sat Jun 11 22:56:03 2005
@@ -3276,6 +3276,7 @@
 javax/swing/text/ComponentView.java
 javax/swing/text/DefaultCaret.java
 javax/swing/text/DefaultEditorKit.java
+javax/swing/text/DefaultFormatter.java
 javax/swing/text/DefaultHighlighter.java
 javax/swing/text/DefaultStyledDocument.java
 javax/swing/text/DocumentFilter.java
@@ -3299,6 +3300,7 @@
 javax/swing/text/html/parser/ParserDelegator.java
 javax/swing/text/html/parser/Parser.java
 javax/swing/text/html/parser/TagElement.java
+javax/swing/text/InternationalFormatter.java
 javax/swing/text/JTextComponent.java
 javax/swing/text/Keymap.java
 javax/swing/text/LayeredHighlighter.java
Index: kaffe/libraries/javalib/gnu/java/awt/peer/gtk/GtkPanelPeer.java
diff -u kaffe/libraries/javalib/gnu/java/awt/peer/gtk/GtkPanelPeer.java:1.3 kaffe/libraries/javalib/gnu/java/awt/peer/gtk/GtkPanelPeer.java:1.4
--- kaffe/libraries/javalib/gnu/java/awt/peer/gtk/GtkPanelPeer.java:1.3	Sat Oct  9 22:18:50 2004
+++ kaffe/libraries/javalib/gnu/java/awt/peer/gtk/GtkPanelPeer.java	Sat Jun 11 22:56:04 2005
@@ -65,4 +65,6 @@
       }
     super.handleEvent (event);
   }
+
+  native void connectSignals ();
 }
Index: kaffe/libraries/javalib/java/awt/MenuItem.java
diff -u kaffe/libraries/javalib/java/awt/MenuItem.java:1.20 kaffe/libraries/javalib/java/awt/MenuItem.java:1.21
--- kaffe/libraries/javalib/java/awt/MenuItem.java:1.20	Sun May 15 09:46:16 2005
+++ kaffe/libraries/javalib/java/awt/MenuItem.java	Sat Jun 11 22:56:04 2005
@@ -94,7 +94,7 @@
   * @serial This menu item's label
   * This is package-private to avoid an accessor method.
   */
-String label;
+String label = "";
 
 /**
   * @serial The shortcut for this menu item, if any
Index: kaffe/libraries/javalib/javax/swing/JTextArea.java
diff -u kaffe/libraries/javalib/javax/swing/JTextArea.java:1.11 kaffe/libraries/javalib/javax/swing/JTextArea.java:1.12
--- kaffe/libraries/javalib/javax/swing/JTextArea.java:1.11	Sat Jun 11 20:59:48 2005
+++ kaffe/libraries/javalib/javax/swing/JTextArea.java	Sat Jun 11 22:56:05 2005
@@ -47,6 +47,7 @@
 import javax.swing.text.Element;
 import javax.swing.text.JTextComponent;
 import javax.swing.text.PlainDocument;
+import javax.swing.text.View;
 
 /**
  * The <code>JTextArea</code> component provides a multi-line area for displaying
@@ -188,7 +189,6 @@
     setText(text);
     setRows(rows);
     setColumns(columns);
-    setPreferredSize(new Dimension(440, 150));
   }
 
   /**
@@ -528,5 +528,23 @@
       {
 	// This cannot happen as we check offset above.
       }
+  }
+
+  /**
+   * Returns the preferred size for the JTextArea. This is the maximum of
+   * the size that is needed to display the content and the requested size
+   * as per {@link #getColumns} and {@link #getRows}.
+   *
+   * @return the preferred size of the JTextArea
+   */
+  public Dimension getPreferredSize()
+  {
+    int reqWidth = getColumns() * getColumnWidth();
+    int reqHeight = getRows() * getRowHeight();
+    View view = getUI().getRootView(this);
+    int neededWidth = (int) view.getPreferredSpan(View.HORIZONTAL);
+    int neededHeight = (int) view.getPreferredSpan(View.VERTICAL);
+    return new Dimension(Math.max(reqWidth, neededWidth),
+                          Math.max(reqHeight, neededHeight));
   }
 }
Index: kaffe/libraries/javalib/javax/swing/plaf/basic/BasicSplitPaneUI.java
diff -u kaffe/libraries/javalib/javax/swing/plaf/basic/BasicSplitPaneUI.java:1.7 kaffe/libraries/javalib/javax/swing/plaf/basic/BasicSplitPaneUI.java:1.8
--- kaffe/libraries/javalib/javax/swing/plaf/basic/BasicSplitPaneUI.java:1.7	Sat Jun 11 21:43:18 2005
+++ kaffe/libraries/javalib/javax/swing/plaf/basic/BasicSplitPaneUI.java	Sat Jun 11 22:56:05 2005
@@ -348,7 +348,7 @@
 		    height = Math.max(height, dims.height);
 	        }
 	    }
-	  return new Dimension(500, 500); //width, height);	
+	  return new Dimension(width, height);	
         }
       return null;
     }
@@ -613,7 +613,7 @@
 		  width = Math.max(width, dims.width);
 	        }
 	    }
-	  return new Dimension(500, 500); //width, height);
+	  return new Dimension(width, height);
         }
       return null;
     }
===================================================================
Checking out kaffe/libraries/javalib/javax/swing/text/InternationalFormatter.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/javax/swing/text/InternationalFormatter.java,v
VERS: 1.1
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/javax/swing/text/InternationalFormatter.java	Sat Jun 11 23:00:34 2005
@@ -0,0 +1,353 @@
+/* InternationalFormatter.java --
+Copyright (C) 2005  Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+package javax.swing.text;
+
+import java.text.AttributedCharacterIterator;
+import java.text.Format;
+import java.text.ParseException;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import javax.swing.Action;
+import javax.swing.JFormattedTextField;
+
+/**
+ * This extends {@link DefaultFormatter} so that the value to string
+ * conversion is done via a {@link Format} object. This allows
+ * various additional formats to be handled by JFormattedField.
+ *
+ * @author Roman Kennke (roman at kennke.org)
+ */
+public class InternationalFormatter
+  extends DefaultFormatter
+{
+
+  /** The serialVersoinUID. */
+  private static final long serialVersionUID = 6941977820906408656L;
+
+  /** The format that handles value to string conversion. */
+  Format format;
+
+  /** The minimal permissable value. */
+  Comparable minimum;
+
+  /** The maximal permissable value. */
+  Comparable maximum;
+
+  /**
+   * Creates a new InternationalFormatter with no Format specified.
+   */
+  public InternationalFormatter()
+  {
+    super();
+    minimum = null;
+    maximum = null;
+    format = null;
+  }
+
+  /**
+   * Creates a new InternationalFormatter that uses the specified
+   * Format object for value to string conversion.
+   *
+   * @param format the Format object to use for value to string conversion
+   */
+  public InternationalFormatter(Format format)
+  {
+    this();
+    setFormat(format);
+  }
+
+  /**
+   * Sets the Format object that is used to convert values to strings.
+   *
+   * @param format the Format to use for value to string conversion
+   *
+   * @see Format
+   */
+  public void setFormat(Format format)
+  {
+    this.format = format;
+  }
+
+  /**
+   * Returns the currently used Format object that is used to format
+   * the JFormattedField.
+   *
+   * @return the current Format
+   */
+  public Format getFormat()
+  {
+    return format;
+  }
+
+  /**
+   * Sets the minimum value that is allowed by this Formatter. The minimum
+   * value is given as an object that implements the {@link Comparable}
+   * interface.
+   *
+   * If <code>minValue</code> is null, then the Formatter has no restrictions
+   * at the lower end.
+   *
+   * If value class is not yet specified and <code>minValue</code> is not
+   * null, then <code>valueClass</code> is set to the class of the minimum
+   * value.
+   *
+   * @param minValue the minimum permissable value
+   *
+   * @see Comparable
+   */
+  public void setMinimum(Comparable minValue)
+  {
+    minimum = minValue;
+    if (valueClass == null && minValue != null)
+      valueClass = minValue.getClass();
+  }
+
+  /**
+   * Returns the minimal value that is allowed by this Formatter.
+   *
+   * A <code>null</code> value means that there is no restriction.
+   *
+   * @return the minimal value that is allowed by this Formatter or
+   *     <code>null</code> if there is no restriction
+   */
+  public Comparable getMinimum()
+  {
+    return minimum;
+  }
+
+  /**
+   * Sets the maximum value that is allowed by this Formatter. The maximum
+   * value is given as an object that implements the {@link Comparable}
+   * interface.
+   *
+   * If <code>maxValue</code> is null, then the Formatter has no restrictions
+   * at the upper end.
+   *
+   * If value class is not yet specified and <code>maxValue</code> is not
+   * null, then <code>valueClass</code> is set to the class of the maximum
+   * value.
+   *
+   * @param maxValue the maximum permissable value
+   *
+   * @see Comparable
+   */
+  public void setMaximum(Comparable maxValue)
+  {
+    maximum = maxValue;
+    if (valueClass == null && maxValue != null)
+      valueClass = maxValue.getClass();
+  }
+
+  /**
+   * Returns the maximal value that is allowed by this Formatter.
+   *
+   * A <code>null</code> value means that there is no restriction.
+   *
+   * @return the maximal value that is allowed by this Formatter or
+   *     <code>null</code> if there is no restriction
+   */
+  public Comparable getMaximum()
+  {
+    return maximum;
+  }
+
+  /**
+   * Installs the formatter on the specified {@link JFormattedTextField}.
+   *
+   * This method does the following things:
+   * <ul>
+   * <li>Display the value of #valueToString in the
+   *  <code>JFormattedTextField</code></li>
+   * <li>Install the Actions from #getActions on the <code>JTextField</code>
+   * </li>
+   * <li>Install the DocumentFilter returned by #getDocumentFilter</li>
+   * <li>Install the NavigationFilter returned by #getNavigationFilter</li>
+   * </ul>
+   *
+   * This method is typically not overridden by subclasses. Instead override
+   * one of the mentioned methods in order to customize behaviour.
+   *
+   * @param ftf the {@link JFormattedTextField} in which this formatter
+   *     is installed 
+   */
+  public void install(JFormattedTextField ftf)
+  {
+    super.install(ftf);
+  }
+
+  /**
+   * Converts a value object into a String. This is done by invoking
+   * {@link Format#format} on the specified <code>Format</code> object.
+   * If no format is set, then {@link DefaultFormatter#valueToString(Object)}
+   * is called as a fallback.
+   *
+   * @param value the value to be converted
+   *
+   * @return the string representation of the value
+   *
+   * @throws ParseException if the value cannot be converted
+   */
+  public String valueToString(Object value)
+    throws ParseException
+  {
+    if (format != null)
+      return format.format(value);
+    else
+      return super.valueToString(value);
+  }
+
+  /**
+   * Converts a String (from the JFormattedTextField input) to a value.
+   * This is achieved by invoking {@link Format#parseObject(String)} on
+   * the specified <code>Format</code> object.
+   *
+   * This implementation differs slightly from {@link DefaultFormatter},
+   * it does:
+   * <ol>
+   * <li>Convert the string to an <code>Object</code> using the
+   *   <code>Formatter</code>.</li>
+   * <li>If a <code>valueClass</code> has been set, this object is passed to
+   *   {@link DefaultFormatter#stringToValue(String)} so that the value
+   *   has the correct type. This may or may not work correctly, depending on
+   *   the implementation of toString() in the value class and if the class
+   *   implements a constructor that takes one String as argument.</li>
+   * <li>If no {@link ParseException} has been thrown so far, we check if the
+   *   value exceeds either <code>minimum</code> or <code>maximum</code> if
+   *   one of those has been specified and throw a <code>ParseException</code>
+   *   if it does.</li>
+   * <li>Return the value.</li>
+   * </ol>
+   *
+   * If no format has been specified, then
+   * {@link DefaultFormatter#stringToValue(String)} is invoked as fallback.
+   *
+   * @param string the string to convert
+   *
+   * @return the value for the string
+   *
+   * @throws ParseException if the string cannot be converted into
+   *     a value object (e.g. invalid input)
+   */
+  public Object stringToValue(String string)
+    throws ParseException
+  {
+    if (format != null)
+      {
+        Object o = format.parseObject(string);
+
+        // If a value class has been set, call super in order to get
+        // the class right. That is what the JDK API docs suggest, so we do
+        // it that way.
+        if (valueClass != null)
+          o = super.stringToValue(o.toString());
+
+        // Check for minimum and maximum bounds
+        if (minimum != null && minimum.compareTo(o) > 0)
+          throw new ParseException("The value may not be less than the"
+                                    + " specified minimum", 0);
+        if (maximum != null && minimum.compareTo(o) < 0)
+          throw new ParseException("The value may not be greater than the"
+                                    + " specified maximum", 0);
+        return o;
+      }
+    else
+      return super.stringToValue(string);
+  }
+
+  /**
+   * Returns the {@link Format.Field} constants that are associated with
+   * the specified position in the text.
+   *
+   * If <code>offset</code> is not a valid location in the input field,
+   * an empty array of fields is returned.
+   *
+   * @param offset the position in the text from which we want to fetch
+   *     the fields constants
+   *
+   * @return the field values associated with the specified position in
+   *     the text
+   */
+  public Format.Field[] getFields(int offset)
+  {
+    // TODO: don't know if this is correct
+    AttributedCharacterIterator aci = format.formatToCharacterIterator
+                                         (getFormattedTextField().getValue());
+    aci.setIndex(offset);
+    Map atts = aci.getAttributes();
+    Set keys = atts.keySet();
+    Format.Field[] fields = new Format.Field[keys.size()];
+    int index = 0;
+    for (Iterator i = keys.iterator(); i.hasNext(); index++)
+      fields[index] = (Format.Field) i.next();
+    return fields;
+  }
+
+  /**
+   * This creates and returns a clone of this Formatter.
+   *
+   * @return a clone of this formatter
+   *
+   * @throws CloneNotSupportedException not thrown here, since cloning is
+   *     supported
+   */
+  public Object clone()
+    throws CloneNotSupportedException
+  {
+    // TODO: it has to be considered, if we should return a deep or shallow
+    // clone here. for now we return a shallow clone
+    Object clone = super.clone();
+    return clone;
+  }
+
+  /**
+   * Returns the Actions that are supported by this Formatter.
+   *
+   * @specnote the JDK API docs say here: <cite>If
+   *     <code>getSupportsIncrement</code> returns true, this returns two
+   *     Actions suitable for incrementing/decrementing the value.</cite>
+   *     The questsion is, which method <code>getSupportsIncrement</code>?
+   *     There is no such method in the whole API! So we just call
+   *     super.getActions here.
+   */
+  public Action[] getActions()
+  {
+    return super.getActions();
+  }
+}




More information about the kaffe mailing list