[Patch][gui] PlainDocument and DefaultEditorKit

Michael Koch konqueror@gmx.de
Fri Jan 21 09:24:00 GMT 2005


Hi list,


I just commited the attached patch to load files into
javax.swing.text.PlainDocument and to display it correctly.
This patch makes it possible to actually read (the top) of the
GPL in the JEdit 4.2 installer.


Michael


2005-01-21  Michael Koch  <konqueror@gmx.de>

	* javax/swing/text/DefaultEditorKit.java
	(read): Added '\n' after each line.
	* javax/swing/text/PlainView.java
	(modelToView): Update metrics.
	(drawLine): Use offsets from element.
	(paint): Update metrics. Draw all lines.

-------------- next part --------------
Index: javax/swing/text/DefaultEditorKit.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/text/DefaultEditorKit.java,v
retrieving revision 1.4.8.9
diff -u -r1.4.8.9 DefaultEditorKit.java
--- javax/swing/text/DefaultEditorKit.java	11 Jan 2005 13:35:30 -0000	1.4.8.9
+++ javax/swing/text/DefaultEditorKit.java	21 Jan 2005 09:20:34 -0000
@@ -375,7 +375,10 @@
     StringBuffer content = new StringBuffer();
 
     while ((line = reader.readLine()) != null)
-      content.append(line);
+      {
+	content.append(line);
+	content.append("\n");
+      }
     
     document.insertString(offset, content.toString(),
 			  SimpleAttributeSet.EMPTY);
Index: javax/swing/text/PlainView.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/text/PlainView.java,v
retrieving revision 1.1.2.6
diff -u -r1.1.2.6 PlainView.java
--- javax/swing/text/PlainView.java	10 Sep 2004 19:45:24 -0000	1.1.2.6
+++ javax/swing/text/PlainView.java	21 Jan 2005 09:20:34 -0000
@@ -1,5 +1,5 @@
 /* PlainView.java -- 
-   Copyright (C) 2004  Free Software Foundation, Inc.
+   Copyright (C) 2004, 2005  Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -46,7 +46,6 @@
 import java.awt.Rectangle;
 import java.awt.Shape;
 
-
 public class PlainView extends View
   implements TabExpander
 {
@@ -93,6 +92,9 @@
   public Shape modelToView(int position, Shape a, Position.Bias b)
     throws BadLocationException
   {
+    // Ensure metrics are up-to-date.
+    updateMetrics();
+    
     Document document = getDocument();
 
     // Get rectangle of the line containing position.
@@ -121,7 +123,8 @@
       {
 	metrics = g.getFontMetrics();
 	// FIXME: Selected text are not drawn yet.
-	drawUnselectedText(g, x, y, 0, getDocument().getLength());
+	Element line = getDocument().getDefaultRootElement().getElement(lineIndex);
+	drawUnselectedText(g, x, y, line.getStartOffset(), line.getEndOffset());
 	//drawSelectedText(g, , , , );
       }
     catch (BadLocationException e)
@@ -150,6 +153,9 @@
 
   public void paint(Graphics g, Shape s)
   {
+    // Ensure metrics are up-to-date.
+    updateMetrics();
+    
     JTextComponent textComponent = (JTextComponent) getContainer();
 
     g.setFont(textComponent.getFont());
@@ -159,7 +165,15 @@
     Rectangle rect = s.getBounds();
 
     // FIXME: Text may be scrolled.
-    drawLine(0, g, rect.x, rect.y);
+    Document document = textComponent.getDocument();
+    Element root = document.getDefaultRootElement();
+    int y = rect.y;
+    
+    for (int i = 0; i < root.getElementCount(); i++)
+      {
+	drawLine(i, g, rect.x, y);
+	y += metrics.getHeight();
+      }
   }
 
   public int getTabSize()


More information about the Java-patches mailing list