This is the mail archive of the java-patches@gcc.gnu.org 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]
Other format: [Raw text]

[gui][patch] various swing fixes


hi,

I committed this patch to java-gui-branch. it just fixes up a bunch of little issues exposed by recent runs through the java swing tutorial examples.

I will now commence my "monthly" branch -> trunk merge.

-graydon

2004-06-16 Graydon Hoare <graydon@redhat.com>

	* gnu/java/awt/peer/gtk/GdkGraphics2D.java
	(setComposite): Accept AlphaComposite arguments.
	* gnu/java/awt/peer/gtk/GdkPixbufDecoder.java
	(createBufferedImage): Add new overloads.
	* gnu/java/awt/peer/gtk/GtkToolkit.java
	(createImage): Use GdkPixbufDecoder.createBufferedImage
	when useGraphics2D() is true.
	(getImage): Delegate to createImage.
	* javax/swing/JList.java
	(isSelectionEmpty):
	(getFirstVisibleIndex):
	(getLastVisibleIndex):
	(setSelectedValue):
	(ensureIndexIsVisible): New methods.
	* javax/swing/Timer.java: Reimplement.
--- gnu/java/awt/peer/gtk/GdkGraphics2D.java	21 May 2004 23:34:09 -0000	1.7.2.11
+++ gnu/java/awt/peer/gtk/GdkGraphics2D.java	16 Jun 2004 19:05:18 -0000
@@ -1260,6 +1260,17 @@
 
   public void setComposite(Composite comp)
   {
+    if (comp instanceof AlphaComposite)
+      {
+        AlphaComposite a = (AlphaComposite) comp;
+        cairoSetOperator(a.getRule());
+        Color c = getColor();
+        setColor(new Color(c.getRed(),
+                           c.getGreen(),
+                           c.getBlue(),
+                           (int) (a.getAlpha() * ((float) c.getAlpha()))));
+      }
+    else
     throw new java.lang.UnsupportedOperationException ();
   }
 
--- gnu/java/awt/peer/gtk/GdkPixbufDecoder.java	2 Dec 2003 19:56:30 -0000	1.3
+++ gnu/java/awt/peer/gtk/GdkPixbufDecoder.java	16 Jun 2004 19:05:18 -0000
@@ -220,4 +220,31 @@
     dec.startProduction (bb);
     return bb.getBufferedImage ();
   }
+
+  public static BufferedImage createBufferedImage (URL u)
+  {
+    BufferedImageBuilder bb = new BufferedImageBuilder ();
+    GdkPixbufDecoder dec = new GdkPixbufDecoder (u);
+    dec.startProduction (bb);
+    return bb.getBufferedImage ();
+  }
+
+  public static BufferedImage createBufferedImage (byte[] imagedata, int imageoffset,
+                                                   int imagelength)
+  {
+    BufferedImageBuilder bb = new BufferedImageBuilder ();
+    GdkPixbufDecoder dec = new GdkPixbufDecoder (imagedata, imageoffset, imagelength);
+    dec.startProduction (bb);
+    return bb.getBufferedImage ();
+  }
+  
+  public static BufferedImage createBufferedImage (ImageProducer producer)
+  {
+    BufferedImageBuilder bb = new BufferedImageBuilder ();
+    producer.startProduction(bb);
+    return bb.getBufferedImage ();
+  }
+  
+
+
 }
--- gnu/java/awt/peer/gtk/GtkToolkit.java	12 May 2004 23:40:04 -0000	1.8.2.4
+++ gnu/java/awt/peer/gtk/GtkToolkit.java	16 Jun 2004 19:05:18 -0000
@@ -129,26 +129,58 @@
 
   public Image createImage (String filename)
   {
-    return new GtkImage (new GdkPixbufDecoder (filename), null);
+    if (useGraphics2D())
+      return GdkPixbufDecoder.createBufferedImage (filename);
+    else
+      {
+        GdkPixbufDecoder d = new GdkPixbufDecoder (filename);
+        GtkImage image = new GtkImage (d, null);
+        d.startProduction (image);
+        return image;        
+      }
   }
 
   public Image createImage (URL url)
   {
-    return new GtkImage (new GdkPixbufDecoder (url), null);
+    if (useGraphics2D())
+      return GdkPixbufDecoder.createBufferedImage (url);
+    else
+      {
+        GdkPixbufDecoder d = new GdkPixbufDecoder (url);
+        GtkImage image = new GtkImage (d, null);
+        d.startProduction (image);
+        return image;        
+      }
   }
 
   public Image createImage (ImageProducer producer) 
   {
-    return new GtkImage (producer, null);
+    if (useGraphics2D())
+      return GdkPixbufDecoder.createBufferedImage (producer);
+    else
+      {
+        GtkImage image = new GtkImage (producer, null);
+        producer.startProduction (image);
+        return image;        
+      }
   }
 
   public Image createImage (byte[] imagedata, int imageoffset,
 			    int imagelength)
   {
-    return new GtkImage (new GdkPixbufDecoder (imagedata,
+    if (useGraphics2D())
+      return GdkPixbufDecoder.createBufferedImage (imagedata,
 					       imageoffset,
-					       imagelength),
-			 null);
+                                                   imagelength);
+    else
+      {
+        GdkPixbufDecoder d = new GdkPixbufDecoder (imagedata,
+                                                   imageoffset, 
+                                                   imagelength);
+        GtkImage image = new GtkImage (d, null);
+        d.startProduction (image);
+        return image;        
+      }
   }
 
   public ColorModel getColorModel () 
@@ -175,18 +207,12 @@
 
   public Image getImage (String filename) 
   {
-    GdkPixbufDecoder d = new GdkPixbufDecoder (filename);
-    GtkImage image = new GtkImage (d, null);
-    d.startProduction (image);
-    return image;
+    return createImage (filename);
   }
 
   public Image getImage (URL url) 
   {
-    GdkPixbufDecoder d = new GdkPixbufDecoder (url);
-    GtkImage image = new GtkImage (d, null);
-    d.startProduction (image);
-    return image;
+    return createImage (url);
   }
 
   public PrintJob getPrintJob (Frame frame, String jobtitle, Properties props) 
--- javax/swing/JList.java	7 Jun 2004 14:00:33 -0000	1.4.2.5
+++ javax/swing/JList.java	16 Jun 2004 19:05:19 -0000
@@ -39,6 +39,7 @@
 
 import java.awt.Color;
 import java.awt.Component;
+import java.awt.ComponentOrientation;
 import java.awt.Dimension;
 import java.awt.Point;
 import java.awt.Rectangle;
@@ -560,6 +561,52 @@
   }
 
   /**
+   * Returns <code>true</code> if the model's selection is empty, otherwise
+   * <code>false</code>. 
+   *
+   * @return The return value of {@link ListSelectionModel#isSelectionEmpty}
+   */
+  public boolean isSelectionEmpty()
+  {
+    return selectionModel.isSelectionEmpty();
+  }
+
+  /**
+   * Returns the list index of the upper left or upper right corner of the
+   * {@link #visibleRect} property, depending on the {@link
+   * #componentOrientation} property.
+   *
+   * @return The index of the first visible list cell, or <code>-1</code>
+   * if none is visible.
+   */
+  public int getFirstVisibleIndex()
+  {
+    ComponentOrientation or = getComponentOrientation();
+    Rectangle r = getVisibleRect();
+    if (or == ComponentOrientation.RIGHT_TO_LEFT)
+      r.translate((int) r.getWidth(), 0);
+    return getUI().locationToIndex(this, r.getLocation());      
+  }
+
+  /**
+   * Returns the list index of the lower right or lower left corner of the
+   * {@link #visibleRect} property, depending on the {@link
+   * #componentOrientation} property.
+   *
+   * @return The index of the first visible list cell, or <code>-1</code>
+   * if none is visible.
+   */
+  public int getLastVisibleIndex()
+  {
+    ComponentOrientation or = getComponentOrientation();
+    Rectangle r = getVisibleRect();
+    r.translate(0, (int) r.getHeight());
+    if (or == ComponentOrientation.LEFT_TO_RIGHT)
+      r.translate((int) r.getWidth(), 0);
+    return getUI().locationToIndex(this, r.getLocation());      
+  }
+
+  /**
    * Returns the indices of values in the {@link #model} property which are
    * selected.
    *
@@ -682,6 +729,44 @@
   }
 
   /**
+   * Sets the selection to cover only the specified value, if it
+   * exists in the model. 
+   *
+   * @param obj The object to select
+   * @param scroll Whether to scroll the list to make the newly selected
+   * value visible
+   *
+   * @see #ensureIndexIsVisible
+   */
+
+  public void setSelectedValue(Object obj, boolean scroll)
+  {
+    for (int i = 0; i < model.getSize(); ++i)
+      {
+        if (model.getElementAt(i).equals(obj))
+          {
+            setSelectedIndex(i);
+            if (scroll)
+              ensureIndexIsVisible(i);
+            break;
+          }
+      }
+  }
+
+  /**
+   * Scrolls this list to make the specified cell visible. This
+   * only works if the list is contained within a viewport.
+   *
+   * @param i The list index to make visible
+   *
+   * @see JComponent#scrollRectToVisible
+   */
+  public void ensureIndexIsVisible(int i)
+  {
+    scrollRectToVisible(getUI().getCellBounds(this, i, i));
+  }
+
+  /**
    * Sets the {@link #model} property of the list to a new anonymous
    * {@link AbstractListModel} subclass which accesses the provided Object
    * array directly.
--- javax/swing/Timer.java	10 Jun 2004 10:32:39 -0000	1.3.18.4
+++ javax/swing/Timer.java	16 Jun 2004 19:05:19 -0000
@@ -51,50 +51,103 @@
   
   protected EventListenerList listenerList = new EventListenerList();
   
-  int ticks;
-  static boolean verbose;
+  // This object manages a "queue" of virtual actionEvents, maintained as a
+  // simple long counter. When the timer expires, a new event is queued,
+  // and a dispatcher object is pushed into the system event queue. When
+  // the system thread runs the dispatcher, it will fire as many
+  // ActionEvents as have been queued, unless the timer is set to
+  // coalescing mode, in which case it will fire only one ActionEvent.
+
+  private long queue;
+  private Object queueLock = new Object();
+  private void queueEvent()
+  {
+    synchronized (queueLock)
+      {
+        queue++;
+        if (queue == 1)
+          SwingUtilities.invokeLater(new Runnable() { void run() { drainEvents(); } });
+      }
+  }
+
+  private void drainEvents()
+  {
+    synchronized (queueLock)
+      {
+        if (isCoalesce())
+          {
+            if (queue > 0)
+              fireActionPerformed();
+          }
+        else
+          {
+            while(queue > 0)
+              {                  
+                fireActionPerformed();
+                queue--;
+              }          
+          }
+        queue = 0;
+      }
+  }
+  
+
+  static boolean logTimers;
+  boolean coalesce = true;
+  boolean repeats = true;
   boolean running;
-  boolean repeat_ticks = true;
-  long interval, init_delay;
+  int ticks;
+  int delay;
+  int initialDelay;
     
-  class Waker extends Thread
+  private class Waker 
+    extends Thread
   {
     public void run()
     {
       running = true;
-      try {
-	sleep(init_delay);
+      try 
+        {
+
+          sleep(initialDelay);
 		
 	while (running)
 	  {
-	    sleep(interval);
+              sleep(delay);
 
-	    if (verbose)
-	      {
+              if (logTimers)
 		System.out.println("javax.swing.Timer -> clocktick");
-	      }
-
-	    ticks++;
-	    fireActionPerformed();
   
-	    if (! repeat_ticks)
+              if (! repeats)
 	      break;
 	  }
 	running = false;
-      } catch (Exception e) {
+      } 
+      catch (Exception e) 
+        {
 	System.out.println("swing.Timer::" + e);
       }
     }
   }
 
-  public Timer(int delay, ActionListener listener)
+  public Timer(int d, ActionListener listener)
   {
-    interval = delay;
+    delay = d;
 
     if (listener != null)
       addActionListener(listener);
   }
 
+  public void setCoalesce(boolean c)
+  {
+    coalesce = c;
+  }
+
+  public boolean isCoalesce()
+  {
+    return coalesce;
+  }
+
   public void addActionListener(ActionListener listener)
   {
     listenerList.add (ActionListener.class, listener);
@@ -133,37 +186,47 @@
 
   void fireActionPerformed ()
   {
-    fireActionPerformed (new ActionEvent (this, ticks, "Timer"));
+    fireActionPerformed (new ActionEvent (this, ticks++, "Timer"));
   }
 
-  public static void setLogTimers(boolean flag)
+  public static void setLogTimers(boolean lt)
   {
-    verbose = flag;
+    logTimers = lt;
   }
 
   public static boolean getLogTimers()
   {
-    return verbose;
+    return logTimers;
   }
     
-  public void setDelay(int delay)
+  public void setDelay(int d)
   {
-    interval = delay;
+    delay = d;
   }
 
   public int getDelay()
   {
-    return (int)interval;
+    return delay;
   }
 
-  public void setInitialDelay(int initialDelay)
+  public void setInitialDelay(int i)
   {
-    init_delay = initialDelay;
+    initialDelay = i;
   }
 
-  public void setRepeats(boolean flag)
+  public int getInitialDelay()
   {
-    repeat_ticks = flag;
+    return initialDelay;
+  }
+
+  public void setRepeats(boolean r)
+  {
+    repeats = r;
+  }
+
+  public boolean isRepeats()
+  {
+    return repeats;
   }
 
   public boolean isRunning()
@@ -181,6 +244,15 @@
     new Waker().start();
   }
 
+  public void restart()
+  {
+    synchronized (queueLock)
+      {
+        queue = 0;
+      }
+    start();
+  }
+
   public void stop()
   {
     running = false;

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