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 fixes in swing text painting


hi,

this patch improves the swing text components' ability to cope with focus transfers, caret movement, and the painting of multiple lines and tabs in general. I've committed it to java-gui-branch.

-graydon

2004-11-11 Graydon Hoare <graydon@redhat.com>

	* javax/swing/plaf/basic/BasicTextUI.java:
	Listen to focus events, indicate focus via caret.
	* javax/swing/text/GapContent.java (getString): Return substring.
	* javax/swing/text/PlainDocument.java (reindex): New method.
	(createDefaultRoot): Call it.
	(insertUpdate): Likewise.
	(removeUpdate): Likewise.
	* javax/swing/text/Utilities.java (drawTabbedText): Always advance
	on tab and newline, even if no painting happens.
--- javax/swing/plaf/basic/BasicTextUI.java	10 Nov 2004 07:19:49 -0000	1.4.16.16
+++ javax/swing/plaf/basic/BasicTextUI.java	11 Nov 2004 07:48:35 -0000
@@ -45,6 +45,8 @@
 import java.awt.Point;
 import java.awt.Rectangle;
 import java.awt.Shape;
+import java.awt.event.FocusEvent;
+import java.awt.event.FocusListener;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 
@@ -228,9 +230,20 @@
     caret.setBlinkRate(defaults.getInt(prefix + ".caretBlinkRate"));
   }
 
+  private FocusListener focuslistener = new FocusListener() {
+      public void focusGained(FocusEvent e) 
+      {
+        textComponent.repaint();
+      }
+      public void focusLost(FocusEvent e)
+      {
+        textComponent.repaint();
+      }
+    };
+
   protected void installListeners()
   {
-    // Do nothing here.
+    textComponent.addFocusListener(focuslistener);
   }
 
   protected String getKeymapName()
@@ -328,7 +341,7 @@
 
   protected void uninstallListeners()
   {
-    // Do nothing here.
+    textComponent.removeFocusListener(focuslistener);
   }
 
   protected void uninstallKeyboardActions()
@@ -367,7 +380,7 @@
 
     rootView.paint(g, getVisibleEditorRect());
 
-    if (caret != null)
+    if (caret != null && textComponent.hasFocus())
       caret.paint(g);
   }
 
--- javax/swing/text/GapContent.java	16 Jun 2004 10:06:47 -0000	1.2.8.2
+++ javax/swing/text/GapContent.java	11 Nov 2004 07:48:35 -0000
@@ -93,7 +93,7 @@
 
   public String getString(int where, int len) throws BadLocationException
   {
-    return buf.toString();
+    return buf.substring(where, where+len);
   }
 
   public void getChars(int where, int len, Segment txt)
--- javax/swing/text/PlainDocument.java	20 Jun 2004 15:27:41 -0000	1.1.52.3
+++ javax/swing/text/PlainDocument.java	11 Nov 2004 07:48:35 -0000
@@ -37,6 +37,8 @@
 
 package javax.swing.text;
 
+import java.util.ArrayList;
+
 public class PlainDocument extends AbstractDocument
 {
   private static final long serialVersionUID = 4758290289196893664L;
@@ -59,14 +61,53 @@
     rootElement = createDefaultRoot();
   }
 
-  protected AbstractDocument.AbstractElement createDefaultRoot()
+  protected void reindex()
+  {
+    Element[] lines;
+    try 
+      {
+        String str = content.getString(0, content.length());
+
+        ArrayList elts = new ArrayList();
+        int j = 0;
+        for (int i = str.indexOf('\n', 0); i != -1; i = str.indexOf('\n', i+1))
+          {
+            elts.add(createLeafElement(rootElement, null, j, i));
+            j = i;
+          }
+        
+        if (j < content.length())
+          elts.add(createLeafElement(rootElement, null, j, content.length()));
+        
+        lines = new Element[elts.size()];
+        for (int i = 0; i < elts.size(); ++i)
+          lines[i] = (Element) elts.get(i);
+        
+      }
+    catch (BadLocationException e)
   {
-    BranchElement rootElement =
-      (BranchElement) createBranchElement(null, null);
-    Element[] lines = new Element[1];
+        lines = new Element[1];
     lines[0] = createLeafElement(rootElement, null, 0, 1);
-    rootElement.replace(0, 0, lines);
-    return rootElement;
+      }
+
+    ((BranchElement) rootElement).replace(0, rootElement.getElementCount(), lines);
+  }
+
+  protected AbstractDocument.AbstractElement createDefaultRoot()
+  {
+    rootElement = createBranchElement(null, null);
+    reindex();
+    return (AbstractElement) rootElement;
+  }
+
+  protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr)
+  {
+    reindex();
+  }
+
+  protected void removeUpdate(DefaultDocumentEvent chng)
+  {
+    reindex();
   }
 
   public Element getDefaultRootElement()
--- javax/swing/text/Utilities.java	4 Oct 2004 16:45:49 -0000	1.1.2.6
+++ javax/swing/text/Utilities.java	11 Nov 2004 07:48:35 -0000
@@ -98,11 +98,13 @@
     for (int offset = s.offset; offset < (s.offset + s.count); ++offset)
       {
         char c = buffer[offset];
-        if (len > 0 && (c == '\t' || c == '\n'))
+        if (c == '\t' || c == '\n')
           {
+            if (len > 0) {
 	    g.drawChars(buffer, pos, len, pixelX, pixelY + ascent);            
             pixelX += pixelWidth;
             pixelWidth = 0;
+            }
             pos = offset+1;
             len = 0;
           }

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