This is the mail archive of the java-patches@sources.redhat.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Another AWT patch


Here's the result of some more AWT hacking.
I'm sure there are bugs and misunderstandings in this code.

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

	* java/awt/BorderLayout.java (BorderLayout()): New constructor.

	* java/awt/Frame.java (Frame): Pass `null' to Window constructor.

	* java/awt/Window.java (addNotify): Wrote.
	(addWindowListener): Wrote.
	(getLocale): Wrote.
	(getWarningString): Wrote.
	(processEvent): Wrote.
	(processWindowEvent): Wrote.
	(removeWindowListener): Wrote.
	(show): Call validate(), setVisible().
	(toBack): Wrote.
	(toFront): Wrote.

	* java/awt/Toolkit.java (createWindow): Declare.

	* java/awt/Frame.java (addNotify): Use getToolkit to find
	toolkit.

	* java/awt/Component.java (invalidate): Wrote.
	(isValid): Wrote.
	(getToolkit): Wrote.

	* java/awt/Container.java (addContainerListener): Removed
	unnecessary cast.
	(removeContainerListener): Likewise.
	(addImpl): Wrote.
	(add(Component)): Use it.
	(add(String,Component)): Likewise.
	(add(Component,int)): Likewise.
	(add(Component,Object)): Likewise.
	(add(Component,Object,int)): Likewise.
	(doLayout): Wrote.
	(getAlignmentX): Wrote.
	(getAlignmentY): Wrote.
	(getComponentAt): Wrote.
	(getMaximumSize): Wrote.
	(invalidate): Wrote.
	(list(PrintStream,int)): Wrote.
	(list(PrintWriter,int)): Wrote.
	(getMinimumSize): Wrote.
	(getPreferredSize): Wrote.
	(printComponents): Wrote.
	(processContainerEvent): Look at containerListener, not
	componentListener.
	(remove): Added event processing and peer destruction.
	(removeAll): Use remove.
	(removeNotify): Wrote.
	(validate): Wrote.
	(validateTree): Wrote.

	* java/awt/Scrollbar.java (addNotify): Do nothing if peer exists.
	* java/awt/Label.java (addNotify): Do nothing if peer exists.
	* java/awt/Container.java (addNotify): Don't create Container
	peer.
	* java/awt/Button.java (addNotify): Do nothing if peer exists.

Tom

Index: java/awt/BorderLayout.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/awt/BorderLayout.java,v
retrieving revision 1.3
diff -u -r1.3 BorderLayout.java
--- BorderLayout.java	2000/03/07 19:55:25	1.3
+++ BorderLayout.java	2000/07/31 02:03:01
@@ -1,4 +1,4 @@
-/* Copyright (C) 1999  Free Software Foundation
+/* Copyright (C) 1999, 2000  Free Software Foundation
 
    This file is part of libjava.
 
@@ -14,6 +14,11 @@
 {
   int hgap;
   int vgap;
+
+  public BorderLayout ()
+  {
+    this (0, 0);
+  }
 
   public BorderLayout (int hgap, int vgap)
   {
Index: java/awt/Button.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/awt/Button.java,v
retrieving revision 1.1
diff -u -r1.1 Button.java
--- Button.java	2000/07/30 23:19:57	1.1
+++ Button.java	2000/07/31 02:03:01
@@ -36,7 +36,8 @@
 
   public void addNotify ()
   {
-    peer = (ComponentPeer) getToolkit ().createButton (this);
+    if (peer == null)
+      peer = (ComponentPeer) getToolkit ().createButton (this);
   }
 
   public String getActionCommand ()
Index: java/awt/Component.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/awt/Component.java,v
retrieving revision 1.6
diff -u -r1.6 Component.java
--- Component.java	2000/07/30 23:19:57	1.6
+++ Component.java	2000/07/31 02:03:02
@@ -137,23 +137,25 @@
     // FIXME
     return null;
   }
-  
+
   public final Object getTreeLock()
   {
     // FIXME
     return null;
   }
-  
+
   public Toolkit getToolkit()
   {
-    // FIXME
-    return null;
+    if (peer != null)
+      return peer.getToolkit ();
+    if (parent != null)
+      return parent.getToolkit ();
+    return Toolkit.getDefaultToolkit ();
   }
-  
+
   public boolean isValid()
   {
-    // FIXME
-    return false;
+    return valid;
   }
   
   /** @since 1.2 */
@@ -518,7 +520,9 @@
   
   public void invalidate()
   {
-    // FIXME
+    valid = false;
+    if (parent != null)
+      parent.invalidate ();
   }
   
   public Graphics getGraphics()
Index: java/awt/Container.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/awt/Container.java,v
retrieving revision 1.5
diff -u -r1.5 Container.java
--- Container.java	2000/07/30 23:19:57	1.5
+++ Container.java	2000/07/31 02:03:03
@@ -29,7 +29,10 @@
 
   /* Anything else is non-serializable, and should be declared "transient". */
   transient ContainerListener containerListener;  
-  
+
+  // Insets.
+  private transient Insets myInsets;
+
   public Container()
   {
   }
@@ -62,10 +65,9 @@
 
   public Insets getInsets()
   {
-    // FIXME
-    return null;
+    return myInsets;
   }
-  
+
   /** @deprecated Use getInsets() instead. */
   public Insets insets()
   {
@@ -74,17 +76,50 @@
   
   public Component add (Component comp)
   {
-    return add (comp, -1);
+    addImpl (comp, null, -1);
+    return comp;
   }
-  
-  public Component add(String name, Component comp)
+
+  public Component add (String name, Component comp)
   {
-    // FIXME
-    return null;
+    addImpl (comp, name, -1);
+    return comp;
+  }
+
+  public Component add (Component comp, int index)
+  {
+    addImpl (comp, null, index);
+    return comp;
   }
 
-  public Component add(Component comp, int index)
+  public void add (Component comp, Object constraints)
   {
+    addImpl (comp, constraints, -1);
+  }
+
+  public void add (Component comp, Object constraints, int index)
+  {
+    addImpl (comp, constraints, index);
+  }
+
+  protected void addImpl (Component comp, Object constraints, int index)
+  {
+    if (index > ncomponents
+	|| comp instanceof Window
+	|| (comp instanceof Container
+	    && ((Container) comp).isAncestorOf (this)))
+      throw new IllegalArgumentException ();
+
+    // Reparent component, and make sure component is instantiated if
+    // we are.
+    if (comp.parent != this)
+      comp.parent.remove (comp);
+    comp.parent = this;
+    if (peer != null)
+      comp.addNotify ();
+
+    invalidate ();
+
     // 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)
@@ -94,7 +129,6 @@
 	System.arraycopy (component, 0, c, 0, ncomponents);
 	component = c;
       }
-
     if (index == -1)
       component[ncomponents++] = comp;
     else
@@ -105,45 +139,69 @@
 	++ncomponents;
       }
 
-    return comp;
-  }
+    // Notify the layout manager.
+    if (layoutMgr != null)
+      {
+	if (constraints != null && layoutMgr instanceof LayoutManager2)
+	  {
+	    LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
+	    lm2.addLayoutComponent (comp, constraints);
+	  }
+	else
+	  layoutMgr.addLayoutComponent ((String) constraints, comp);
+      }
 
-  public void add(Component comp, Object constraints)
-  {
-    // FIXME
+    ContainerEvent ce = new ContainerEvent (this,
+					    ContainerEvent.COMPONENT_ADDED,
+					    comp);
+
+    // FIXME: is this right?
+    dispatchEvent (ce);
+    if (containerListener != null)
+      containerListener.componentAdded (ce);
   }
 
-  public void add(Component comp, Object constraints, int index)
+  public void remove (int index)
   {
-    // FIXME
-  }
+    Component r = component[index];
 
-  protected void addImpl(Component comp, Object constraints, int index)
-  {
-    // FIXME
-  }
+    r.removeNotify ();
 
-  public void remove (int index)
-  {
     System.arraycopy (component, index + 1, component, index,
 		      ncomponents - index - 1);
     component[--ncomponents] = null;
+
+    invalidate ();
+
+    if (layoutMgr != null)
+      layoutMgr.removeLayoutComponent (r);
+
+    ContainerEvent ce = new ContainerEvent (this,
+					    ContainerEvent.COMPONENT_REMOVED,
+					    r);
+
+    // FIXME: is this right?
+    dispatchEvent (ce);
+    if (containerListener != null)
+      containerListener.componentAdded (ce);
   }
 
   public void remove (Component comp)
   {
     for (int i = 0; i < ncomponents; ++i)
-      if (component[i] == comp)
-	{
-	  remove (i);
-	  break;
-	}
+      {
+	if (component[i] == comp)
+	  {
+	    remove (i);
+	    break;
+	  }
+      }
   }
 
   public void removeAll()
   {
-    while (ncomponents >= 0)
-      component[--ncomponents] = null;
+    while (ncomponents > 0)
+      remove (0);
   }
 
   public LayoutManager getLayout()
@@ -159,7 +217,8 @@
   
   public void doLayout()
   {
-    // FIXME
+    if (layoutMgr != null)
+      layoutMgr.layoutContainer (this);
   }
 
   /** @deprecated Use doLayout() instead. */
@@ -170,17 +229,22 @@
 
   public void invalidate()
   {
-    // FIXME
+    super.invalidate ();
   }
 
   public void validate()
   {
-    // FIXME
+    if (! isValid ())
+      {
+	doLayout ();
+	validateTree ();
+      }
   }
 
   protected void validateTree()
   {
-    // FIXME
+    for (int i = 0; i < ncomponents; ++i)
+      component[i].validate ();
   }
 
   public void setFont(Font f)
@@ -190,8 +254,10 @@
 
   public Dimension getPreferredSize()
   {
-    // FIXME
-    return null;
+    if (layoutMgr != null)
+      return layoutMgr.preferredLayoutSize (this);
+    else
+      return super.getPreferredSize ();
   }
   
   /** @deprecated Use getPreferredSize() instead */
@@ -202,8 +268,10 @@
   
   public Dimension getMinimumSize()
   {
-    // FIXME
-    return null;
+    if (layoutMgr != null)
+      return layoutMgr.minimumLayoutSize (this);
+    else
+      return super.getMinimumSize ();
   }
   
   /** @deprecated Use getMinimumSize() instead */
@@ -214,20 +282,35 @@
   
   public Dimension getMaximumSize()
   {
-    // FIXME
-    return null;    
+    if (layoutMgr != null && layoutMgr instanceof LayoutManager2)
+      {
+	LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
+	return lm2.maximumLayoutSize (this);
+      }
+    else
+      return super.getMaximumSize ();
   }
-  
+
   public float getAlignmentX()
   {
-    // FIXME
-    return 0;
+    if (layoutMgr instanceof LayoutManager2)
+      {
+	LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
+	return lm2.getLayoutAlignmentX (this);
+      }
+    else
+      return CENTER_ALIGNMENT;
   }
 
   public float getAlignmentY()
   {
-    // FIXME
-    return 0;
+    if (layoutMgr instanceof LayoutManager2)
+      {
+	LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
+	return lm2.getLayoutAlignmentY (this);
+      }
+    else
+      return CENTER_ALIGNMENT;
   }
 
   public void paint(Graphics g)
@@ -252,7 +335,8 @@
 
   public void printComponents(Graphics g)
   {
-    // FIXME
+    for (int i = 0; i < ncomponents; ++i)
+      component[i].printAll (g);
   }
   
   void dispatchEventImpl(AWTEvent e)
@@ -267,14 +351,12 @@
 
   public void addContainerListener(ContainerListener l)
   {
-    containerListener = (ContainerListener) 
-                          AWTEventMulticaster.add(containerListener, l);
+    containerListener = AWTEventMulticaster.add (containerListener, l);
   }
 
   public void removeContainerListener(ContainerListener l)
   {
-    containerListener = (ContainerListener)
-			  AWTEventMulticaster.remove(containerListener, l);
+    containerListener = AWTEventMulticaster.remove(containerListener, l);
   }
 
   /** @since 1.3 */
@@ -294,7 +376,7 @@
   
   protected void processContainerEvent(ContainerEvent e)
   {
-    if (componentListener == null)
+    if (containerListener == null)
       return;
     switch (e.id)
       {
@@ -313,9 +395,17 @@
   {
   }
   
-  public Component getComponentAt(int x, int y)
+  public Component getComponentAt (int x, int y)
   {
-    // FIXME
+    if (! contains (x, y))
+      return null;
+    for (int i = 0; i < ncomponents; ++i)
+      {
+	int x2 = x - component[i].x;
+	int y2 = y - component[i].y;
+	if (component[i].contains (x2, y2))
+	  return component[i];
+      }
     return null;
   }
 
@@ -345,12 +435,13 @@
   {
     for (int i = ncomponents;  --i >= 0; )
       component[i].addNotify();
-    peer = (ComponentPeer) getToolkit ().createContainer (this);
   }
 
   public void removeNotify()
   {
-    // FIXME
+    for (int i = 0; i < ncomponents; ++i)
+      component[i].removeNotify ();
+    // FIXME: remove our peer.
   }
 
   public boolean isAncestorOf (Component comp)
@@ -370,13 +461,21 @@
     return "FIXME";
   }
   
-  public void list(PrintStream out, int indent)
+  public void list (PrintStream out, int indent)
   {
-    // FIXME  
+    for (int i = 0; i < indent; ++i)
+      out.print (' ');
+    out.println (toString ());
+    for (int i = 0; i < ncomponents; ++i)
+      component[i].list (out, indent + 2);
   }
-  
+
   public void list(PrintWriter out, int indent)
   {
-    // FIXME  
+    for (int i = 0; i < indent; ++i)
+      out.print (' ');
+    out.println (toString ());
+    for (int i = 0; i < ncomponents; ++i)
+      component[i].list (out, indent + 2);
   }
 }
Index: java/awt/Frame.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/awt/Frame.java,v
retrieving revision 1.5
diff -u -r1.5 Frame.java
--- Frame.java	2000/03/24 09:09:56	1.5
+++ Frame.java	2000/07/31 02:03:03
@@ -17,11 +17,13 @@
   String title;
 
   public Frame ()
-  { /* FIXME */ }
+  {
+    super (null);
+  }
 
   public Frame (String title)
   {
-    this();
+    super (null);
     setTitle(title);
   }
 
@@ -43,13 +45,7 @@
   public synchronized void addNotify ()
   {
     if (peer == null)
-      {
-	FramePeer fpeer = Toolkit.getDefaultToolkit().createFrame(this);
-	// Compiler bug requires cast ??;  FIXME?
-	peer = (java.awt.peer.ComponentPeer) fpeer;
-	if (width + height > 0)
-	  peer.setBounds(x, y, width, height);
-      }
+      peer = getToolkit ().createFrame (this);
     super.addNotify();
   }
 
Index: java/awt/Label.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/awt/Label.java,v
retrieving revision 1.2
diff -u -r1.2 Label.java
--- Label.java	2000/07/30 23:19:57	1.2
+++ Label.java	2000/07/31 02:03:03
@@ -41,7 +41,8 @@
 
   public void addNotify ()
   {
-    peer = (ComponentPeer) getToolkit ().createLabel (this);
+    if (peer == null)
+      peer = (ComponentPeer) getToolkit ().createLabel (this);
   }
 
   public int getAlignment ()
Index: java/awt/Scrollbar.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/awt/Scrollbar.java,v
retrieving revision 1.2
diff -u -r1.2 Scrollbar.java
--- Scrollbar.java	2000/07/30 23:19:57	1.2
+++ Scrollbar.java	2000/07/31 02:03:03
@@ -53,7 +53,8 @@
 
   public void addNotify ()
   {
-    peer = (ComponentPeer) getToolkit ().createScrollbar (this);
+    if (peer == null)
+      peer = (ComponentPeer) getToolkit ().createScrollbar (this);
   }
 
   public int getOrientation ()
Index: java/awt/Toolkit.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/awt/Toolkit.java,v
retrieving revision 1.8
diff -u -r1.8 Toolkit.java
--- Toolkit.java	2000/07/30 23:19:57	1.8
+++ Toolkit.java	2000/07/31 02:03:03
@@ -31,6 +31,7 @@
   protected abstract ContainerPeer createContainer (Container target);
   protected abstract LabelPeer createLabel (Label target);
   protected abstract ScrollbarPeer createScrollbar (Scrollbar target);
+  protected abstract WindowPeer createWindow (Window target);
 
   public final EventQueue getSystemEventQueue()
   {
Index: java/awt/Window.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/awt/Window.java,v
retrieving revision 1.3
diff -u -r1.3 Window.java
--- Window.java	2000/03/07 19:55:25	1.3
+++ Window.java	2000/07/31 02:03:03
@@ -1,4 +1,4 @@
-/* Copyright (C) 1999  Free Software Foundation
+/* Copyright (C) 1999, 2000  Free Software Foundation
 
    This file is part of libjava.
 
@@ -7,23 +7,139 @@
 details.  */
 
 package java.awt;
+import java.awt.event.WindowEvent;
 import java.awt.event.WindowListener;
+import java.awt.peer.WindowPeer;
+import java.awt.peer.ComponentPeer;
+import java.util.Locale;
 
 /* A very incomplete placeholder. */
 
 public class Window extends Container
 {
-  public void dispose ()
-  { /* FIXME */ }
+  public Window (Frame parent)
+  {
+    this.parent = parent;
+    // FIXME: compiler bug
+    // this.layoutMgr = new BorderLayout ();
+  }
+
+  public void addNotify ()
+  {
+    if (peer == null)
+      peer = (ComponentPeer) getToolkit ().createWindow (this);
+    super.addNotify ();
+  }
 
   public synchronized void addWindowListener (WindowListener listener)
-  { /* FIXME */ }
+  {
+    windowListener = AWTEventMulticaster.add (windowListener, listener);
+  }
+
+  public void dispose ()
+  {
+  }
+
+  public Component getFocusOwner ()
+  {
+    return null;		// FIXME
+  }
 
+  public Locale getLocale ()
+  {
+    return locale == null ? Locale.getDefault () : locale;
+  }
 
+  public String getWarningString ()
+  {
+    return warningString;
+  }
+
+  public void pack ()
+  {
+    addNotify ();
+    // FIXME
+  }
+
+  public boolean postEvent (Event evt)
+  {
+    return false;		// FIXME
+  }
+
+  protected void processEvent (AWTEvent evt)
+  {
+    if (evt instanceof WindowEvent)
+      processWindowEvent ((WindowEvent) evt);
+    else
+      super.processEvent (evt);
+  }
+
+  protected void processWindowEvent (WindowEvent evt)
+  {
+    if (windowListener != null)
+      {
+	switch (evt.getID ())
+	  {
+	  case WindowEvent.WINDOW_ACTIVATED:
+	    windowListener.windowActivated (evt);
+	    break;
+	  case WindowEvent.WINDOW_CLOSED:
+	    windowListener.windowClosed (evt);
+	    break;
+	  case WindowEvent.WINDOW_CLOSING:
+	    windowListener.windowClosing (evt);
+	    break;
+	  case WindowEvent.WINDOW_DEACTIVATED:
+	    windowListener.windowDeactivated (evt);
+	    break;
+	  case WindowEvent.WINDOW_DEICONIFIED:
+	    windowListener.windowDeiconified (evt);
+	    break;
+	  case WindowEvent.WINDOW_ICONIFIED:
+	    windowListener.windowIconified (evt);
+	    break;
+	  case WindowEvent.WINDOW_OPENED:
+	    windowListener.windowOpened (evt);
+	    break;
+	  }
+      }
+  }
+
+  public synchronized void removeWindowListener (WindowListener listener)
+  {
+    windowListener = AWTEventMulticaster.remove (windowListener, listener);
+  }
+
   public void show ()
+  {
+    addNotify ();
+    validate ();
+    setVisible (true);
+    // FIXME: is there more to it?
+  }
+
+  public void toBack ()
   {
-    addNotify();
-    // validate FIXME
-    // validate setVisible FIXME
+    if (peer != null)
+      {
+	WindowPeer wp = (WindowPeer) peer;
+	wp.toBack ();
+      }
   }
+
+  public void toFront ()
+  {
+    if (peer != null)
+      {
+	WindowPeer wp = (WindowPeer) peer;
+	wp.toFront ();
+      }
+  }
+
+  // Serialized fields, from Sun's serialization spec.
+  // private FocusManager focusMgr;  // FIXME: what is this?
+  private int state;
+  private String warningString;
+
+  private transient WindowListener windowListener;
 }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]