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: javax.swing.text views


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi list,


I just commited the attached patch to add some new stuff to 
javax.swing.text views and to fix some stuff in it.


Michael


2004-08-14  Michael Koch  <konqueror@gmx.de>

	* javax/swing/text/PlainView.java
	(selectedColor): New field.
	(unselectedColor): Likewise.
	(font): Likewise.
	(updateMetrics): New method.
	(lineToRect): Likewise.
	(modelToView): Likewise.
	(drawSelectedText): Use color from JTextComponent ad draw with
	Utilities class.
	(drawUnselectedText): Likewise.
	(paint): Initialize helper fields.
	* javax/swing/text/View.java
	(getChildAllocation): New method.
	(getViewIndex): Likewise.
	(getToolTipText): Likewise.
- -- 
Homepage: http://www.worldforge.org/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBHcP8WSOgCCdjSDsRAqLWAKCQPBCkz1hHEBelajCApgLgg8dexQCgn+Nd
Xw8AxUMI2mzczaLACkRGAqo=
=Md+h
-----END PGP SIGNATURE-----
Index: javax/swing/text/PlainView.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/text/PlainView.java,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 PlainView.java
--- javax/swing/text/PlainView.java	29 Jun 2004 11:30:50 -0000	1.1.2.1
+++ javax/swing/text/PlainView.java	14 Aug 2004 07:42:59 -0000
@@ -39,6 +39,8 @@
 package javax.swing.text;
 
 import java.awt.Color;
+import java.awt.Component;
+import java.awt.Font;
 import java.awt.FontMetrics;
 import java.awt.Graphics;
 import java.awt.Rectangle;
@@ -48,12 +50,70 @@
 public class PlainView extends View
   implements TabExpander
 {
+  private Color selectedColor;
+  private Color unselectedColor;
+  private Font font;
+  
   protected FontMetrics metrics;
 
   public PlainView(Element elem)
   {
     super(elem);
   }
+
+  /**
+   * @since 1.4
+   */
+  protected void updateMetrics()
+  {
+    Component component = getContainer();
+    Font font = component.getFont();
+
+    if (this.font != font)
+      {
+	this.font = font;
+	metrics = component.getFontMetrics(font);
+      }
+  }
+  
+  /**
+   * @since 1.4
+   */
+  protected Rectangle lineToRect(Shape a, int line)
+  {
+    // Ensure metrics are up-to-date.
+    updateMetrics();
+    
+    Rectangle rect = a.getBounds();
+    int fontHeight = metrics.getHeight();
+    return new Rectangle(rect.x, rect.y + (line * fontHeight),
+			 rect.width, fontHeight);
+  }
+
+  protected Shape modelToView(int position, Shape a, Position.Bias b)
+    throws BadLocationException
+  {
+    Document document = getDocument();
+
+    // Get rectangle of the line containing position.
+    int lineIndex = getElement().getElementIndex(position);
+    Rectangle rect = lineToRect(a, lineIndex);
+
+    // Get the rectangle for position.
+    Element line = getElement().getElement(lineIndex);
+    int lineStart = line.getStartOffset();
+    Segment segment = new Segment();
+    document.getText(lineStart, position - lineStart, segment);
+    int xoffset = Utilities.getTabbedTextWidth(segment, metrics, rect.x,
+					       this, lineStart);
+
+    // Calc the real rectangle.
+    rect.x += xoffset;
+    rect.width = 1;
+    rect.height = metrics.getHeight();
+
+    return rect;
+  }
   
   public void drawLine(int lineIndex, Graphics g, int x, int y)
   {
@@ -73,24 +133,28 @@
   public int drawSelectedText(Graphics g, int x, int y, int p0, int p1)
     throws BadLocationException
   {
-    String text = getDocument().getText(p0, p1);
-    g.setColor(Color.WHITE);
-    g.drawString(text, x, y);
-    return metrics.stringWidth(text);
+    g.setColor(selectedColor);
+    Segment segment = new Segment();
+    getDocument().getText(p0, p1 - p0, segment);
+    return Utilities.drawTabbedText(segment, x, y, g, this, 0);
   }
 
   public int drawUnselectedText(Graphics g, int x, int y, int p0, int p1)
     throws BadLocationException
   {
-    String text = getDocument().getText(p0, p1);
-    g.setColor(Color.BLACK);
-    g.drawString(text, x, y);
-    return metrics.stringWidth(text);
+    g.setColor(unselectedColor);
+    Segment segment = new Segment();
+    getDocument().getText(p0, p1 - p0, segment);
+    return Utilities.drawTabbedText(segment, x, y, g, this, 0);
   }
 
   public void paint(Graphics g, Shape s)
   {
-    System.out.println("Michael: PlainView.paint");
+    JTextComponent textComponent = (JTextComponent) getContainer();
+
+    g.setFont(textComponent.getFont());
+    selectedColor = textComponent.getSelectedTextColor();
+    unselectedColor = textComponent.getForeground();
     
     Rectangle rect = s.getBounds();
 
@@ -119,4 +183,5 @@
 
     return 10;
   }
-}
\ No newline at end of file
+}
+
Index: javax/swing/text/View.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/text/View.java,v
retrieving revision 1.2.8.4
diff -u -r1.2.8.4 View.java
--- javax/swing/text/View.java	21 Jul 2004 09:05:19 -0000	1.2.8.4
+++ javax/swing/text/View.java	14 Aug 2004 07:42:59 -0000
@@ -174,5 +174,36 @@
   {
     return elt.getEndOffset();
   }
+
+  public Shape getChildAllocation(int index, Shape a)
+  {
+    return null;
+  }
+  
+  /**
+   * @since 1.4
+   */
+  public int getViewIndex(float x, float y, Shape allocation)
+  {
+    return -1;
+  }
+  
+  /**
+   * @since 1.4
+   */
+  public String getToolTipText(float x, float y, Shape allocation)
+  {
+    int index = getViewIndex(x, y, allocation);
+
+    if (index < -1)
+      return null;
+
+    Shape childAllocation = getChildAllocation(index, allocation);
+
+    if (childAllocation.getBounds().contains(x, y))
+      return getView(index).getToolTipText(x, y, childAllocation);
+
+    return null;
+  }
 }
 

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