[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[cp-patches] FYI: StyledText stuff, part 3
From: |
Roman Kennke |
Subject: |
[cp-patches] FYI: StyledText stuff, part 3 |
Date: |
Fri, 29 Jul 2005 13:07:41 +0200 |
User-agent: |
Mozilla Thunderbird 1.0.2 (X11/20050317) |
Here comes the third part. This implements most of JTextPane and fixes
some things in the UI classes that are needed for JTextPane.
2005-07-29 Roman Kennke <address@hidden>
* javax/swing/JTextPane.java
(constructor()): Implemented this constructor. Initialize the
EditorKit and set the Document to null.
(constructor(StyledDocument)): Implemented this constructor.
Initialize the EditorKit and Document.
(getUIClassID): Inlined the constant String.
(setDocument): Implemented this method.
(getStyledDocument): Likewise.
(setStyledDocument): Likewise.
(replaceSelection): Likewise.
(insertComponent): Clarified the TODO comment.
(insertIcon): Clarified the TODO comment.
(addStyle): Implemented this method.
(removeStyle): Likewise.
(getStyle): Likewise.
(getLogicalStyle): Likewise.
(setLogicalStyle): Likewise.
(getCharacterAttributes): Likewise.
(setCharacterAttributes): Likewise.
(getParagraphAttributes): Likewise.
(getInputAttributes): Likewise.
(getStyledEditorKit): Likewise.
(createDefaultEditorKit): Likewise.
(setEditorKit): Likewise.
* javax/swing/plaf/basic/BasicEditorPaneUI.java
(getEditorKit): Implemented this method.
* javax/swing/plaf/basic/BasicTextUI.java
(RootView.getViewFactory): Ask the installed EditorKit for its
ViewFactory.
(RootView.setView): Set this as the parent of the installed real
root view.
(RootView.modelToView): Don't cast to PlainView here. Use View
instead.
(setView): Don't set the parent here. This is handled inside the
root view.
/Roman
Index: javax/swing/JTextPane.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JTextPane.java,v
retrieving revision 1.7
diff -u -r1.7 JTextPane.java
--- javax/swing/JTextPane.java 27 Jul 2005 15:02:37 -0000 1.7
+++ javax/swing/JTextPane.java 29 Jul 2005 11:04:37 -0000
@@ -43,8 +43,11 @@
import java.io.ObjectOutputStream;
import javax.swing.text.AttributeSet;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Caret;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
+import javax.swing.text.Element;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
@@ -54,22 +57,19 @@
/**
* JTextPane
*
+ * @author Roman Kennke (address@hidden)
* @author Andrew Selkirk
*/
public class JTextPane
extends JEditorPane
{
/**
- * uiClassID
- */
- private static final String uiClassID = "TextPaneUI";
-
- /**
* Constructor JTextPane
*/
public JTextPane()
{
- // TODO
+ setEditorKit(createDefaultEditorKit());
+ setDocument(null);
}
/**
@@ -79,7 +79,8 @@
*/
public JTextPane(StyledDocument document)
{
- // TODO
+ this();
+ setStyledDocument(document);
}
/**
@@ -89,7 +90,7 @@
*/
public String getUIClassID()
{
- return uiClassID;
+ return "TextPaneUI";
}
/**
@@ -99,7 +100,10 @@
*/
public void setDocument(Document document)
{
- super.setDocument(document); // TODO
+ if (document != null && !(document instanceof StyledDocument))
+ throw new IllegalArgumentException("JTextPane can only handle
StyledDocuments"); // TODO: Figure out exception message
+
+ setStyledDocument((StyledDocument) document);
}
/**
@@ -109,7 +113,7 @@
*/
public StyledDocument getStyledDocument()
{
- return null; // TODO
+ return (StyledDocument) super.getDocument();
}
/**
@@ -119,7 +123,7 @@
*/
public void setStyledDocument(StyledDocument document)
{
- // TODO
+ super.setDocument(document);
}
/**
@@ -129,7 +133,43 @@
*/
public void replaceSelection(String content)
{
- super.replaceSelection(content); // TODO
+ Caret caret = getCaret();
+ StyledDocument doc = getStyledDocument();
+
+ int dot = caret.getDot();
+ int mark = caret.getMark();
+
+ // If content is empty delete selection.
+ if (content == null)
+ {
+ caret.setDot(dot);
+ return;
+ }
+
+ try
+ {
+ int start = getSelectionStart();
+ int end = getSelectionEnd();
+ int contentLength = content.length();
+
+ // Remove selected text.
+ if (dot != mark)
+ doc.remove(start, end - start);
+
+ // Insert new text.
+ doc.insertString(start, content, null);
+ // Set attributes for inserted text
+ doc.setCharacterAttributes(start, contentLength, getInputAttributes(),
+ true);
+
+ // Set dot to new position.
+ setCaretPosition(start + contentLength);
+ }
+ catch (BadLocationException e)
+ {
+ throw new AssertionError
+ ("No BadLocationException should be thrown here");
+ }
}
/**
@@ -139,7 +179,9 @@
*/
public void insertComponent(Component component)
{
- // TODO
+ // TODO: One space must be inserted here with attributes set to indicate
+ // that the component must be displayed here. Have to figure out the
+ // attributes.
}
/**
@@ -149,7 +191,9 @@
*/
public void insertIcon(Icon icon)
{
- // TODO
+ // TODO: One space must be inserted here with attributes set to indicate
+ // that the icon must be displayed here. Have to figure out the
+ // attributes.
}
/**
@@ -162,7 +206,7 @@
*/
public Style addStyle(String nm, Style parent)
{
- return null; // TODO
+ return getStyledDocument().addStyle(nm, parent);
}
/**
@@ -172,7 +216,7 @@
*/
public void removeStyle(String nm)
{
- // TODO
+ getStyledDocument().removeStyle(nm);
}
/**
@@ -184,7 +228,7 @@
*/
public Style getStyle(String nm)
{
- return null; // TODO
+ return getStyledDocument().getStyle(nm);
}
/**
@@ -194,7 +238,7 @@
*/
public Style getLogicalStyle()
{
- return null; // TODO
+ return getStyledDocument().getLogicalStyle(getCaretPosition());
}
/**
@@ -204,7 +248,7 @@
*/
public void setLogicalStyle(Style style)
{
- // TODO
+ getStyledDocument().setLogicalStyle(getCaretPosition(), style);
}
/**
@@ -214,7 +258,9 @@
*/
public AttributeSet getCharacterAttributes()
{
- return SimpleAttributeSet.EMPTY; // TODO
+ StyledDocument doc = getStyledDocument();
+ Element el = doc.getCharacterElement(getCaretPosition());
+ return el.getAttributes();
}
/**
@@ -226,7 +272,19 @@
public void setCharacterAttributes(AttributeSet attribute,
boolean replace)
{
- // TODO
+ int dot = getCaret().getDot();
+ int start = getSelectionStart();
+ int end = getSelectionEnd();
+ if (start == dot && end == dot)
+ // There is no selection, update insertAttributes instead
+ {
+ MutableAttributeSet inputAttributes =
+ getStyledEditorKit().getInputAttributes();
+ inputAttributes.addAttributes(attribute);
+ }
+ else
+ getStyledDocument().setCharacterAttributes(start, end - start, attribute,
+ replace);
}
/**
@@ -236,7 +294,9 @@
*/
public AttributeSet getParagraphAttributes()
{
- return null; // TODO
+ StyledDocument doc = getStyledDocument();
+ Element el = doc.getParagraphElement(getCaretPosition());
+ return el.getAttributes();
}
/**
@@ -258,7 +318,7 @@
*/
public MutableAttributeSet getInputAttributes()
{
- return null; // TODO
+ return getStyledEditorKit().getInputAttributes();
}
/**
@@ -268,7 +328,7 @@
*/
protected final StyledEditorKit getStyledEditorKit()
{
- return null; // TODO
+ return (StyledEditorKit) getEditorKit();
}
/**
@@ -278,7 +338,7 @@
*/
protected EditorKit createDefaultEditorKit()
{
- return super.createDefaultEditorKit(); // TODO
+ return new StyledEditorKit();
}
/**
@@ -288,7 +348,9 @@
*/
public final void setEditorKit(EditorKit editor)
{
- super.setEditorKit(editor); // TODO
+ if (!(editor instanceof StyledEditorKit))
+ throw new IllegalArgumentException("JTextPanes can only handle
StyledEditorKits"); // TODO: Figure out exception message
+ super.setEditorKit(editor);
}
/**
Index: javax/swing/plaf/basic/BasicEditorPaneUI.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicEditorPaneUI.java,v
retrieving revision 1.3
diff -u -r1.3 BasicEditorPaneUI.java
--- javax/swing/plaf/basic/BasicEditorPaneUI.java 2 Jul 2005 20:32:50
-0000 1.3
+++ javax/swing/plaf/basic/BasicEditorPaneUI.java 29 Jul 2005 11:04:37
-0000
@@ -39,8 +39,11 @@
package javax.swing.plaf.basic;
import javax.swing.JComponent;
+import javax.swing.JEditorPane;
import javax.swing.plaf.ComponentUI;
+import javax.swing.text.EditorKit;
import javax.swing.text.Element;
+import javax.swing.text.JTextComponent;
import javax.swing.text.PlainView;
import javax.swing.text.View;
@@ -64,5 +67,15 @@
protected String getPropertyPrefix()
{
return "EditorPane";
+ }
+
+ /**
+ * Gets the EditorKit for the text component.
+ *
+ * @param textComponent the text component for which to fetch the editor kit
+ */
+ public EditorKit getEditorKit(JTextComponent textComponent)
+ {
+ return ((JEditorPane) textComponent).getEditorKit();
}
}
Index: javax/swing/plaf/basic/BasicTextUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTextUI.java,v
retrieving revision 1.27
diff -u -r1.27 BasicTextUI.java
--- javax/swing/plaf/basic/BasicTextUI.java 15 Jul 2005 14:37:20 -0000
1.27
+++ javax/swing/plaf/basic/BasicTextUI.java 29 Jul 2005 11:04:38 -0000
@@ -111,8 +111,12 @@
public ViewFactory getViewFactory()
{
- // FIXME: Handle EditorKit somehow.
- return BasicTextUI.this;
+ ViewFactory factory = null;
+ EditorKit editorKit = BasicTextUI.this.getEditorKit(getComponent());
+ factory = editorKit.getViewFactory();
+ if (factory == null)
+ factory = BasicTextUI.this;
+ return factory;
}
public void setView(View v)
@@ -121,7 +125,7 @@
view.setParent(null);
if (v != null)
- v.setParent(null);
+ v.setParent(this);
view = v;
}
@@ -151,7 +155,7 @@
if (view == null)
return null;
- return ((PlainView) view).modelToView(position, a, bias).getBounds();
+ return ((View) view).modelToView(position, a, bias);
}
/**
@@ -450,7 +454,7 @@
}
return am;
}
-
+
public void uninstallUI(final JComponent component)
{
super.uninstallUI(component);
@@ -613,7 +617,6 @@
protected final void setView(View view)
{
rootView.setView(view);
- view.setParent(rootView);
}
protected void modelChanged()
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [cp-patches] FYI: StyledText stuff, part 3,
Roman Kennke <=