This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
[gui] Patch: javax.swing.text fixes
- From: Michael Koch <konqueror at gmx dot de>
- To: java-patches at gcc dot gnu dot org
- Date: Wed, 23 Jun 2004 11:06:00 +0200
- Subject: [gui] Patch: javax.swing.text fixes
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi list,
I just commited the attached patch to extend javax.swing.text more.
Michael
2004-06-23 Michael Koch <konqueror@gmx.de>
* javax/swing/plaf/basic/BasicTextUI.java
(BasicTextUI): Made abstract.
(BasicCaret): New inner class.
(view): Don't explicitely initialize with "null".
(textComponent): New field.
(textColor): Removed.
(disabledTextColor): Removed.
(normalBackgroundColor): Removed.
(RootView): Removed commented out inner class.
(createUI): Removed.
(createCaret): New method.
(getComponent): Likewise.
(installUI): Initialize textComponent only.
(getPreferredSize): Use installed JTextComponent.
(setView): New method.
(create): Likewise.
* javax/swing/text/JTextComponent.java
(highlighter): New field.
(caretColor): Likewise.
(disabledTextColor): Likewise.
(seletedTextColor): Likewise.
(selectionColor): Likewise.
(setUI): New method.
(getCaretColor): Likewise.
(setCaretColor): Likewise.
(getDisabledColor): Likewise.
(setDisabledColor): Likewise.
(getSelectedTextColor): Likewise.
(setSelectedTextColor): Likewise.
(getSelectionColor): Likewise.
(setSelectionColor): Likewise.
(getHighlighter): Likewise.
(setHighlighter): Likewise.
(replaceSelection): Likewise.
- --
Homepage: http://www.worldforge.org/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFA2Uf4WSOgCCdjSDsRAncvAJ4+U/P4hQku/ToNIQl9kRR4O4LviQCdG767
5PtHbz7bFeQqSim+SakwZM4=
=qY1P
-----END PGP SIGNATURE-----
Index: javax/swing/plaf/basic/BasicTextUI.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/plaf/basic/BasicTextUI.java,v
retrieving revision 1.4.16.1
diff -u -b -B -r1.4.16.1 BasicTextUI.java
--- javax/swing/plaf/basic/BasicTextUI.java 10 Jun 2004 08:49:07 -0000 1.4.16.1
+++ javax/swing/plaf/basic/BasicTextUI.java 23 Jun 2004 09:00:49 -0000
@@ -1,5 +1,5 @@
/* BasicTextUI.java
- Copyright (C) 2002, 2003 Free Software Foundation, Inc.
+ Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -45,7 +47,10 @@
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.TextUI;
+import javax.swing.plaf.UIResource;
import javax.swing.text.BadLocationException;
+import javax.swing.text.Caret;
+import javax.swing.text.DefaultCaret;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.EditorKit;
import javax.swing.text.Element;
@@ -55,65 +60,46 @@
import javax.swing.text.ViewFactory;
-public class BasicTextUI extends TextUI
+public abstract class BasicTextUI extends TextUI
implements ViewFactory
{
+ public static class BasicCaret extends DefaultCaret
+ implements UIResource
+ {
+ public BasicCaret()
+ {
+ }
+ }
+
+ View view;
+ JTextComponent textComponent;
int gap = 3;
- View view = null; // was: new RootView();
- Color textColor;
- Color disabledTextColor;
- Color normalBackgroundColor;
EditorKit kit = new DefaultEditorKit();
- /* *****************************************************************
- * This View is way too incomplete to be of any use. To avoid errors
- * when compiling with the Sun JDK, it has been commented out.
- * -- Sascha Brawer (brawer@dandelis.ch)
- *
- * (begin of commented out section)
- class RootView extends View
- {
- RootView()
+ public BasicTextUI()
{
- super(null);
}
- public void paint(Graphics g, Shape s)
- {
- if (view != null)
- {
- Rectangle r = s.getBounds();
- view.setSize((int)r.getWidth(),
- (int)r.getHeight());
- view.paint(g, s);
- }
- }
- }
- * (end of commented out section)
- *************************************************************** */
- public BasicTextUI()
+ protected Caret createCaret()
{
+ return new BasicCaret();
}
- public static ComponentUI createUI(final JComponent c)
+ protected final JTextComponent getComponent()
{
- return new BasicTextUI();
+ return textComponent;
}
public void installUI(final JComponent c)
{
super.installUI(c);
- textColor = new Color(0, 0, 0);
- disabledTextColor = new Color(130, 130, 130);
- normalBackgroundColor = new Color(192, 192, 192);
+ textComponent = (JTextComponent) c;
}
public Dimension getPreferredSize(JComponent c)
{
- JTextComponent b = (JTextComponent) c;
-
- View v = getRootView(b);
+ View v = getRootView(textComponent);
float w = v.getPreferredSpan(View.X_AXIS);
float h = v.getPreferredSpan(View.Y_AXIS);
@@ -181,4 +167,15 @@
// subclasses have to implement this to get this functionality
return null;
}
+
+ public View create(Element elem, int p0, int p1)
+ {
+ // subclasses have to implement this to get this functionality
+ return null;
+ }
+
+ protected final void setView(View v)
+ {
+ view = v;
+ }
}
Index: javax/swing/text/JTextComponent.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/text/JTextComponent.java,v
retrieving revision 1.4.2.9
diff -u -b -B -r1.4.2.9 JTextComponent.java
--- javax/swing/text/JTextComponent.java 20 Jun 2004 15:27:41 -0000 1.4.2.9
+++ javax/swing/text/JTextComponent.java 23 Jun 2004 09:00:49 -0000
@@ -38,6 +38,7 @@
package javax.swing.text;
import java.awt.AWTEvent;
+import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
@@ -283,6 +284,11 @@
private Document doc;
private Caret caret;
+ private Highlighter highlighter;
+ private Color caretColor;
+ private Color disabledTextColor;
+ private Color selectedTextColor;
+ private Color selectionColor;
private boolean editable;
public JTextComponent()
@@ -394,6 +400,11 @@
return (TextUI) ui;
}
+ public void setUI(TextUI newUI)
+ {
+ super.setUI(newUI);
+ }
+
public void updateUI()
{
setUI((TextUI) UIManager.getUI(this));
@@ -460,6 +471,50 @@
caret = newCaret;
}
+ public Color getCaretColor()
+ {
+ return caretColor;
+ }
+
+ public void setCaretColor(Color newColor)
+ {
+ firePropertyChange("caretColor", caretColor, newColor);
+ caretColor = newColor;
+ }
+
+ public Color getDisabledTextColor()
+ {
+ return disabledTextColor;
+ }
+
+ public void setDisabledTextColor(Color newColor)
+ {
+ firePropertyChange("disabledTextColor", caretColor, newColor);
+ disabledTextColor = newColor;
+ }
+
+ public Color getSelectedTextColor()
+ {
+ return selectedTextColor;
+ }
+
+ public void setSelectedTextColor(Color newColor)
+ {
+ firePropertyChange("selectedTextColor", caretColor, newColor);
+ selectedTextColor = newColor;
+ }
+
+ public Color getSelectionColor()
+ {
+ return selectionColor;
+ }
+
+ public void setSelectionColor(Color newColor)
+ {
+ firePropertyChange("selectionColor", caretColor, newColor);
+ selectionColor = newColor;
+ }
+
/**
* Retrisves the current caret position.
*
@@ -501,6 +556,17 @@
caret.moveDot(position);
}
+ public Highlighter getHighlighter()
+ {
+ return highlighter;
+ }
+
+ public void setHighlighter(Highlighter newHighlighter)
+ {
+ firePropertyChange("highlighter", highlighter, newHighlighter);
+ highlighter = newHighlighter;
+ }
+
/**
* Returns the start postion of the currently selected text.
*
@@ -569,6 +635,34 @@
select(0, doc.getLength());
}
+ public synchronized void replaceSelection(String content)
+ {
+ int dot = caret.getDot();
+ int mark = caret.getMark();
+
+ // If content is empty delete selection.
+ if (content == null)
+ {
+ caret.setDot(dot);
+ return;
+ }
+
+ try
+ {
+ // Remove selected text.
+ if (dot != mark)
+ doc.remove(Math.min(dot, mark), Math.max(dot, mark));
+
+ // Insert new text.
+ doc.insertString(Math.min(dot, mark), content, null);
+ }
+ catch (BadLocationException e)
+ {
+ // This should never happen.
+ System.out.println("Michael: JTextComponent.replaceSelection: Error");
+ }
+ }
+
public boolean getScrollableTracksViewportHeight()
{
if (getParent() instanceof JViewport)