Patch: minor AWT improvements

Tom Tromey tromey@cygnus.com
Sun Jul 30 16:15:00 GMT 2000


I'm checking this in.  It adds various things to AWT.  Some of them,
no doubt, are still wrong.

2000-07-30  Tom Tromey  <tromey@cygnus.com>

	* java/awt/Container.java (remove(int)): Wrote.
	(remove(Component)): Wrote.
	(add(Component)): Wrote.
	(add(Component,int)): Wrote.
	(removeAll): Wrote.
	(addNotify): Set our own peer.
	* java/awt/Scrollbar.java (listeners): Changed type.
	(Scrollbar): Don't initialize listeners.
	(addNotify): Wrote.
	(setValue): Call setValues.
	(setMinimum): Likewise.
	(setMaxPriority): Likewise.
	(setVisibleAmount): Likewise.
	(setValues): Wrote.
	(setUnitIncrement): Forward to peer.
	(setLineIncrement): Call setUnitIncrement.
	(setPageIncrement): Call setBlockIncrement.
	(setBlockIncrement): Forward to peer.
	(addAdjustmentListener): Rewrote.
	(removeAdjustmentListener): Rewrote.
	(processAdjustmentEvent): Rewrote.
	(paramString): Wrote.
	* Makefile.in: Rebuilt.
	* Makefile.am (awt_java_source_files): Added Button.java.
	* java/awt/Button.java: New file.
	* java/awt/Toolkit.java (createLabel): Declare.
	(createButton): Likewise.
	(createScrollbar): Likewise.
	(createContainer): Likewise.
	* java/awt/Label.java (addNotify): Wrote.
	(setAlignment): Call setAlignment in the peer.
	(setText): Call setText in the peer.

Tom

Index: java/awt/Button.java
===================================================================
RCS file: Button.java
diff -N Button.java
--- /dev/null	Tue May  5 13:32:27 1998
+++ Button.java	Sun Jul 30 16:14:08 2000
@@ -0,0 +1,94 @@
+/* Copyright (C) 2000  Free Software Foundation
+
+   This file is part of libjava.
+
+This software is copyrighted work licensed under the terms of the
+Libjava License.  Please consult the file "LIBJAVA_LICENSE" for
+details.  */
+
+package java.awt;
+import java.awt.peer.ButtonPeer;
+import java.awt.peer.ComponentPeer;
+import java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
+
+/**
+ * @author Tom Tromey <tromey@cygnus.com>
+ * @date July 30, 2000
+ */
+
+public class Button extends Component
+{
+  public Button ()
+  {
+    this (null);
+  }
+
+  public Button (String label)
+  {
+    this.label = label;
+  }
+
+  public void addActionListener (ActionListener l)
+  {
+    listeners = AWTEventMulticaster.add (listeners, l);
+  }
+
+  public void addNotify ()
+  {
+    peer = (ComponentPeer) getToolkit ().createButton (this);
+  }
+
+  public String getActionCommand ()
+  {
+    return command;
+  }
+
+  public String getLabel ()
+  {
+    return label;
+  }
+
+  protected String paramString ()
+  {
+    return "Button[" + label + "]";
+  }
+
+  protected void processActionEvent (ActionEvent e)
+  {
+    if (listeners != null)
+      listeners.actionPerformed (e);
+  }
+
+  protected void processEvent (AWTEvent e)
+  {
+    if (e instanceof ActionEvent)
+      processActionEvent ((ActionEvent) e);
+    else
+      super.processEvent (e);
+  }
+
+  public void removeActionListener (ActionListener l)
+  {
+    listeners = AWTEventMulticaster.remove (listeners, l);
+  }
+
+  public void setActionCommand (String command)
+  {
+    this.command = (command == null) ? label : command;
+  }
+
+  public void setLabel (String label)
+  {
+    this.label = label;
+    if (peer != null)
+      {
+	ButtonPeer bp = (ButtonPeer) peer;
+	bp.setLabel (label);
+      }
+  }
+
+  private String label;
+  private String command;
+  private ActionListener listeners;
+}
Index: java/awt/Component.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/awt/Component.java,v
retrieving revision 1.5
diff -u -r1.5 Component.java
--- Component.java	2000/07/12 03:32:06	1.5
+++ Component.java	2000/07/30 23:14:09
@@ -112,6 +112,7 @@
   }
   
   /** @deprecated */
+  // However, Classpath's Gtk peers rely on it.
   public java.awt.peer.ComponentPeer getPeer()
   {
     return peer;
Index: java/awt/Container.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/awt/Container.java,v
retrieving revision 1.4
diff -u -r1.4 Container.java
--- Container.java	2000/07/12 03:32:06	1.4
+++ Container.java	2000/07/30 23:14:09
@@ -12,6 +12,8 @@
 import java.io.PrintStream;
 import java.io.PrintWriter;
 import java.util.EventListener;
+import java.awt.peer.ComponentPeer;
+import java.awt.peer.ContainerPeer;
 
 /* A very incomplete placeholder. */
 
@@ -70,10 +72,9 @@
     return getInsets();
   }
   
-  public Component add(Component comp)
+  public Component add (Component comp)
   {
-    // FIXME
-    return null;
+    return add (comp, -1);
   }
   
   public Component add(String name, Component comp)
@@ -84,8 +85,27 @@
 
   public Component add(Component comp, int index)
   {
-    // FIXME
-    return null;
+    // This isn't the most efficient implementation.  We could do less
+    // copying when growing the array.  It probably doesn't matter.
+    if (ncomponents >= component.length)
+      {
+	int nl = component.length * 2;
+	Component[] c = new Component[nl];
+	System.arraycopy (component, 0, c, 0, ncomponents);
+	component = c;
+      }
+
+    if (index == -1)
+      component[ncomponents++] = comp;
+    else
+      {
+	System.arraycopy (component, index, component, index + 1,
+			  ncomponents - index);
+	component[index] = comp;
+	++ncomponents;
+      }
+
+    return comp;
   }
 
   public void add(Component comp, Object constraints)
@@ -103,19 +123,27 @@
     // FIXME
   }
 
-  public void remove(int index)
+  public void remove (int index)
   {
-    // FIXME
+    System.arraycopy (component, index + 1, component, index,
+		      ncomponents - index - 1);
+    component[--ncomponents] = null;
   }
 
-  public void remove(Component comp)
+  public void remove (Component comp)
   {
-    // FIXME
+    for (int i = 0; i < ncomponents; ++i)
+      if (component[i] == comp)
+	{
+	  remove (i);
+	  break;
+	}
   }
 
   public void removeAll()
   {
-    // FIXME
+    while (ncomponents >= 0)
+      component[--ncomponents] = null;
   }
 
   public LayoutManager getLayout()
@@ -317,7 +345,8 @@
   {
     for (int i = ncomponents;  --i >= 0; )
       component[i].addNotify();
-  }  
+    peer = (ComponentPeer) getToolkit ().createContainer (this);
+  }
 
   public void removeNotify()
   {
Index: java/awt/Label.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/awt/Label.java,v
retrieving revision 1.1
diff -u -r1.1 Label.java
--- Label.java	2000/07/23 00:24:14	1.1
+++ Label.java	2000/07/30 23:14:09
@@ -7,14 +7,14 @@
 details.  */
 
 package java.awt;
+import java.awt.peer.ComponentPeer;
+import java.awt.peer.LabelPeer;
 
 /**
  * @author Tom Tromey <tromey@cygnus.com>
  * @date April 12, 2000
  */
 
-/* Status: addNotify() not written.  */
-
 public class Label extends Component
 {
   public static final int CENTER = 1;
@@ -41,7 +41,7 @@
 
   public void addNotify ()
   {
-    // FIXME
+    peer = (ComponentPeer) getToolkit ().createLabel (this);
   }
 
   public int getAlignment ()
@@ -64,11 +64,21 @@
     if (alignment != CENTER && alignment != LEFT && alignment != RIGHT)
       throw new IllegalArgumentException ();
     this.alignment = alignment;
+    if (peer != null)
+      {
+	LabelPeer lp = (LabelPeer) peer;
+	lp.setAlignment (alignment);
+      }
   }
 
   public void setText (String text)
   {
     this.text = text;
+    if (peer != null)
+      {
+	LabelPeer lp = (LabelPeer) peer;
+	lp.setText (text);
+      }
   }
 
   private String text;
Index: java/awt/Scrollbar.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/awt/Scrollbar.java,v
retrieving revision 1.1
diff -u -r1.1 Scrollbar.java
--- Scrollbar.java	2000/07/23 00:24:14	1.1
+++ Scrollbar.java	2000/07/30 23:14:09
@@ -8,8 +8,8 @@
 
 package java.awt;
 import java.awt.event.*;
-import java.util.Vector;
-import java.util.Enumeration;
+import java.awt.peer.ScrollbarPeer;
+import java.awt.peer.ComponentPeer;
 
 /**
  * @author Tom Tromey <tromey@cygnus.com>
@@ -47,14 +47,13 @@
     this.minimum = minimum;
     this.maximum = maximum;
     this.unit = 1;
-    this.listeners = new Vector ();
 
     this.block = 0; // FIXME
   }
 
   public void addNotify ()
   {
-    // FIXME
+    peer = (ComponentPeer) getToolkit ().createScrollbar (this);
   }
 
   public int getOrientation ()
@@ -76,11 +75,7 @@
 
   public void setValue (int value)
   {
-    if (value < minimum)
-      value = minimum;
-    else if (value > maximum)
-      value = maximum;
-    this.value = value;
+    setValues (value, visible, minimum, maximum);
   }
 
   public int getMinimum ()
@@ -90,8 +85,7 @@
 
   public void setMinimum (int minimum)
   {
-    // FIXME: what if it is > max?
-    this.minimum = minimum;
+    setValues (value, visible, minimum, maximum);
   }
 
   public int getMaximum ()
@@ -101,8 +95,7 @@
 
   public void setMaximum (int maximum)
   {
-    // FIXME: what if it is < min?
-    this.maximum = maximum;
+    setValues (value, visible, minimum, maximum);
   }
 
   public int getVisibleAmount ()
@@ -117,17 +110,23 @@
 
   public void setVisibleAmount (int visible)
   {
-    this.visible = visible;
+    setValues (value, visible, minimum, maximum);
   }
 
   public void setUnitIncrement (int v)
   {
     unit = v;
+    if (peer != null)
+      {
+	ScrollbarPeer sp = (ScrollbarPeer) peer;
+	sp.setLineIncrement (v);
+      }
   }
 
+  /** @deprecated */
   public void setLineIncrement (int v)
   {
-    unit = v;
+    setUnitIncrement (v);
   }
 
   public int getUnitIncrement ()
@@ -143,11 +142,16 @@
   public void setBlockIncrement (int v)
   {
     block = v;
+    if (peer != null)
+      {
+	ScrollbarPeer sp = (ScrollbarPeer) peer;
+	sp.setPageIncrement (v);
+      }
   }
 
   public void setPageIncrement (int v)
   {
-    block = v;
+    setBlockIncrement (v);
   }
 
   public int getBlockIncrement ()
@@ -163,22 +167,33 @@
   public synchronized void setValues (int value, int visible,
 				      int minimum, int maximum)
   {
-    // fixme;
+    if (maximum < minimum)
+      maximum = minimum;
+    if (value < minimum)
+      value = minimum;
+    if (value > maximum)
+      value = maximum;
+
+    this.value = value;
+    this.visible = visible;
+    this.minimum = minimum;
+    this.maximum = maximum;
+
+    if (peer != null)
+      {
+	ScrollbarPeer sp = (ScrollbarPeer) peer;
+	sp.setValues (value, visible, minimum, maximum);
+      }
   }
 
   public void addAdjustmentListener (AdjustmentListener l)
   {
-    if (l != null)
-      {
-	listeners.addElement (l);
-	enableEvents (0);	// FIXME
-      }
+    listeners = AWTEventMulticaster.add (listeners, l);
   }
 
   public void removeAdjustmentListener (AdjustmentListener l)
   {
-    if (l != null)
-      listeners.remove (l);
+    listeners = AWTEventMulticaster.add (listeners, l);
   }
 
   protected void processEvent (AWTEvent e)
@@ -191,20 +206,23 @@
 
   protected void processAdjustmentEvent (AdjustmentEvent e)
   {
-    Enumeration en = listeners.elements ();
-    while (en.hasMoreElements ())
-      {
-	AdjustmentListener l = (AdjustmentListener) en.nextElement ();
-	l.adjustmentValueChanged (e);
-      }
+    if (listeners != null)
+      listeners.adjustmentValueChanged (e);
   }
 
   protected String paramString ()
   {
-    return null;		// FIXME
+    return ("Scrollbar["
+	    + ((orientation == VERTICAL) ? "VERTICAL" : "HORIZONTAL") + ","
+	    + value + ","
+	    + visible + ","
+	    + minimum + ","
+	    + maximum + ","
+	    + unit + ","
+	    + block + "]");
   }
 
-  private Vector listeners;
+  private AdjustmentListener listeners;
   private int orientation;
   private int value;
   private int visible;
Index: java/awt/Toolkit.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/awt/Toolkit.java,v
retrieving revision 1.7
diff -u -r1.7 Toolkit.java
--- Toolkit.java	2000/07/12 03:32:06	1.7
+++ Toolkit.java	2000/07/30 23:14:09
@@ -27,6 +27,11 @@
   protected abstract FramePeer createFrame(Frame target);
   public abstract Image getImage(URL url);
 
+  protected abstract ButtonPeer createButton (Button target);
+  protected abstract ContainerPeer createContainer (Container target);
+  protected abstract LabelPeer createLabel (Label target);
+  protected abstract ScrollbarPeer createScrollbar (Scrollbar target);
+
   public final EventQueue getSystemEventQueue()
   {
     return systemEventQueue;


More information about the Java-patches mailing list