classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FYI: StyledText stuff, part 1


From: Roman Kennke
Subject: [cp-patches] FYI: StyledText stuff, part 1
Date: Fri, 29 Jul 2005 11:59:40 +0200
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050317)

Hi,

I am working on the javax.swing.text package, especially the stuff that is needed for styled content. Here comes part 1. Basically, this fully implements all the dummy methods and classes in StyledEditorKit. This pulls in some more classes (*View), which I added as stub classes (sorry for more stubs, I tried to keep the stubs to a minimum, but I don't want to hold back the StyledEditorKit until I finish ALL Views, which is alot of more work).

2005-07-29  Roman Kennke  <address@hidden>

        * javax/swing/text/BoxView.java: New class.
        * javax/swing/text/CompositeView.java: New class.
        * javax/swing/text/FlowView.java: New class.
        * javax/swing/text/GlyphView.java: New class.
        * javax/swing/text/IconView.java: New class.
        * javax/swing/text/ParagraphView.java: New class.
        * javax/swing/text/StyledEditorKit.java
        (UnderLineAction.constructor): Added TODO comment.
        (UnderLineAction.actionPerformed): Implemented this method. This
        toggles the underline attribute to a piece of content.
        (ItalicAction.constructor): Added TODO comment.
        (ItalicAction.actionPerformed): Implemented this method. This
        toggles the italic attribute to a piece of content.
        (BoldAction.constructor): Added TODO comment.
        (BoldAction.actionPerformed): Implemented this method. This
        toggles the bold attribute to a piece of content.
        (AlignmentAction.constructor): Implemented this constructor.
        (AlignmentAction.actionPerformed): Implemented this method. This
        sets the alignment attribute for a piece of content.
        (ForegroundAction.constructor): Implemented this constructor.
        (ForegroundAction.actionPerformed): Implemented this method. This
        sets the foreground attribute for a piece of content.
        (FontSizeAction.constructor): Implemented this constructor.
        (FontSizeAction.actionPerformed): Implemented this method. This
        sets the font size attribute for a piece of content.
        (FontFamilyAction.constructor): Implemented this constructor.
        (FontFamilyAction.actionPerformed): Implemented this method. This
        sets the font family attribute for a piece of content.
        (StyledTextAction.constructor): Removed TODO comment.
        (StyledTextAction.getEditor): Implemented this method.
        (StyledTextAction.getStyledDocument): Implemented this method.
        (StyledTextAction.getStyledEditorKit): Implemented this method.
        (StyledTextAction.setCharacterAttributes): Implemented this method.
        (StyledTextAction.setParagraphAttributes): Implemented this method.
(StyledViewFactory.constructor): Removed. This is not necessary here.
        (StyledViewFactory.create): Implemented this method.
        (AttributeTracker): Removed this inner class.
        (CaretTracker): New inner class.
        (CaretTracker.caretUpdate):  Implemented this method. This meeps
        track of the caret in a text component and updates some fields
        of the StyledEditorKit.
        (constructor): Implemented this constructor.
        The inputAttributes are initialized here.
        (clone): Implemented this method.
        (getActions): Implemented this method.
        (getInputAttributes): Implemented this method.
        (getCharacterAttributeRun): Implemented this method.
        (createDefaultDocument): Implemented this method.
        (install): Implemented this method.
        (deinstall): Implemented this method.
        (getViewFactory): Implemented this method.
        (createInputAttributes): Implemented this method.
        * javax/swing/text/View.java
(modelToView(int, Shape, Position.Bias)): Added new abstract method.
        (modelToView(int, Position.Bias, int, Position.Bias, Shape):
        New method.

/Roman
? javax/swing/text/LabelView.java
Index: javax/swing/text/BoxView.java
===================================================================
RCS file: javax/swing/text/BoxView.java
diff -N javax/swing/text/BoxView.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ javax/swing/text/BoxView.java       29 Jul 2005 09:33:05 -0000
@@ -0,0 +1,87 @@
+/* BoxView.java -- An composite view
+   Copyright (C) 2005  Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package javax.swing.text;
+
+import java.awt.Graphics;
+import java.awt.Shape;
+
+// TODO: Implement this class.
+public class BoxView
+  extends CompositeView
+{
+
+  /**
+   * Creates a new <code>BoxView</code> for the given
+   * <code>Element</code>.
+   *
+   * @param element the element that is rendered by this BoxView
+   */
+  public BoxView(Element element)
+  {
+    super(element);
+  }
+
+
+  /**
+   * Renders the <code>Element</code> that is associated with this
+   * <code>View</code>.
+   *
+   * @param g the <code>Graphics</code> context to render to
+   * @param a the allocated region for the <code>Element</code>
+   */
+  public void paint(Graphics g, Shape a)
+  {
+    // TODO: Implement me.
+  }
+
+
+  /**
+   * Returns the preferred span of the content managed by this
+   * <code>View</code> along the specified <code>axis</code>.
+   *
+   * @param axis the axis
+   *
+   * @return the preferred span of this <code>View</code>.
+   */
+  public float getPreferredSpan(int axis)
+  {
+    // TODO: Implement me.
+    return 0F;
+  }
+}
Index: javax/swing/text/CompositeView.java
===================================================================
RCS file: javax/swing/text/CompositeView.java
diff -N javax/swing/text/CompositeView.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ javax/swing/text/CompositeView.java 29 Jul 2005 09:33:05 -0000
@@ -0,0 +1,84 @@
+/* CompositeView.java -- An abstract view that manages child views
+   Copyright (C) 2005  Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package javax.swing.text;
+
+import java.awt.Shape;
+
+// TODO: Implement this class.
+public abstract class CompositeView
+  extends View
+{
+
+  /**
+   * Creates a new <code>CompositeView</code> for the given
+   * <code>Element</code>.
+   *
+   * @param element the element that is rendered by this CompositeView
+   */
+  public CompositeView(Element element)
+  {
+    super(element);
+  }
+
+  /**
+   * Maps a position in the document into the coordinate space of the View.
+   * The output rectangle usually reflects the font height but has a width
+   * of zero.
+   *
+   * @param pos the position of the character in the model
+   * @param a the area that is occupied by the view
+   * @param bias either address@hidden Position.Bias.Forward} or
+   *        address@hidden Position.Bias.Backward} depending on the preferred
+   *        direction bias. If <code>null</code> this defaults to
+   *        <code>Position.Bias.Forward</code>
+   *
+   * @return a rectangle that gives the location of the document position
+   *         inside the view coordinate space
+   *
+   * @throws BadLocationException if <code>pos</code> is invalid
+   * @throws IllegalArgumentException if b is not one of the above listed
+   *         valid values
+   */
+  public Shape modelToView(int pos, Shape a, Position.Bias b)
+    throws BadLocationException
+  {
+    // Implement me.
+    return null;
+  }
+}
Index: javax/swing/text/FlowView.java
===================================================================
RCS file: javax/swing/text/FlowView.java
diff -N javax/swing/text/FlowView.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ javax/swing/text/FlowView.java      29 Jul 2005 09:33:05 -0000
@@ -0,0 +1,56 @@
+/* FlowView.java -- A composite View
+   Copyright (C) 2005  Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package javax.swing.text;
+
+// TODO: Implement this class.
+public class FlowView
+  extends BoxView
+{
+
+  /**
+   * Creates a new <code>FlowView</code> for the given
+   * <code>Element</code>.
+   *
+   * @param element the element that is rendered by this FlowView
+   */
+  public FlowView(Element element)
+  {
+    super(element);
+  }
+}
Index: javax/swing/text/GlyphView.java
===================================================================
RCS file: javax/swing/text/GlyphView.java
diff -N javax/swing/text/GlyphView.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ javax/swing/text/GlyphView.java     29 Jul 2005 09:33:05 -0000
@@ -0,0 +1,111 @@
+/* GlyphView.java -- A view to render styled text
+   Copyright (C) 2005  Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package javax.swing.text;
+
+import java.awt.Graphics;
+import java.awt.Shape;
+
+// TODO: Implement this class.
+public class GlyphView
+  extends View
+{
+
+  /**
+   * Creates a new <code>GlyphView</code> for the given <code>Element</code>.
+   *
+   * @param element the element that is rendered by this GlyphView
+   */
+  public GlyphView(Element element)
+  {
+    super(element);
+  }
+
+  /**
+   * Renders the <code>Element</code> that is associated with this
+   * <code>View</code>.
+   *
+   * @param g the <code>Graphics</code> context to render to
+   * @param a the allocated region for the <code>Element</code>
+   */
+  public void paint(Graphics g, Shape a)
+  {
+    // TODO: Implement me.
+  }
+
+
+  /**
+   * Returns the preferred span of the content managed by this
+   * <code>View</code> along the specified <code>axis</code>.
+   *
+   * @param axis the axis
+   *
+   * @return the preferred span of this <code>View</code>.
+   */
+  public float getPreferredSpan(int axis)
+  {
+    // TODO: Implement me.
+    return 0F;
+  }
+
+  /**
+   * Maps a position in the document into the coordinate space of the View.
+   * The output rectangle usually reflects the font height but has a width
+   * of zero.
+   *
+   * @param pos the position of the character in the model
+   * @param a the area that is occupied by the view
+   * @param bias either address@hidden Position.Bias.Forward} or
+   *        address@hidden Position.Bias.Backward} depending on the preferred
+   *        direction bias. If <code>null</code> this defaults to
+   *        <code>Position.Bias.Forward</code>
+   *
+   * @return a rectangle that gives the location of the document position
+   *         inside the view coordinate space
+   *
+   * @throws BadLocationException if <code>pos</code> is invalid
+   * @throws IllegalArgumentException if b is not one of the above listed
+   *         valid values
+   */
+  public Shape modelToView(int pos, Shape a, Position.Bias b)
+    throws BadLocationException
+  {
+    // Implement me.
+    return null;
+  }
+}
Index: javax/swing/text/IconView.java
===================================================================
RCS file: javax/swing/text/IconView.java
diff -N javax/swing/text/IconView.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ javax/swing/text/IconView.java      29 Jul 2005 09:33:05 -0000
@@ -0,0 +1,110 @@
+/* IconView.java -- A view to render icons
+   Copyright (C) 2005  Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package javax.swing.text;
+
+import java.awt.Graphics;
+import java.awt.Shape;
+
+// TODO: Implement this class.
+public class IconView
+  extends View
+{
+
+  /**
+   * Creates a new <code>IconView</code> for the given <code>Element</code>.
+   *
+   * @param element the element that is rendered by this IconView
+   */
+  public IconView(Element element)
+  {
+    super(element);
+  }
+
+  /**
+   * Renders the <code>Element</code> that is associated with this
+   * <code>View</code>.
+   *
+   * @param g the <code>Graphics</code> context to render to
+   * @param a the allocated region for the <code>Element</code>
+   */
+  public void paint(Graphics g, Shape a)
+  {
+    // TODO: Implement me.
+  }
+
+  /**
+   * Returns the preferred span of the content managed by this
+   * <code>View</code> along the specified <code>axis</code>.
+   *
+   * @param axis the axis
+   *
+   * @return the preferred span of this <code>View</code>.
+   */
+  public float getPreferredSpan(int axis)
+  {
+    // TODO: Implement me.
+    return 0F;
+  }
+
+  /**
+   * Maps a position in the document into the coordinate space of the View.
+   * The output rectangle usually reflects the font height but has a width
+   * of zero.
+   *
+   * @param pos the position of the character in the model
+   * @param a the area that is occupied by the view
+   * @param bias either address@hidden Position.Bias.Forward} or
+   *        address@hidden Position.Bias.Backward} depending on the preferred
+   *        direction bias. If <code>null</code> this defaults to
+   *        <code>Position.Bias.Forward</code>
+   *
+   * @return a rectangle that gives the location of the document position
+   *         inside the view coordinate space
+   *
+   * @throws BadLocationException if <code>pos</code> is invalid
+   * @throws IllegalArgumentException if b is not one of the above listed
+   *         valid values
+   */
+  public Shape modelToView(int pos, Shape a, Position.Bias b)
+    throws BadLocationException
+  {
+    // Implement me.
+    return null;
+  }
+}
Index: javax/swing/text/ParagraphView.java
===================================================================
RCS file: javax/swing/text/ParagraphView.java
diff -N javax/swing/text/ParagraphView.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ javax/swing/text/ParagraphView.java 29 Jul 2005 09:33:05 -0000
@@ -0,0 +1,56 @@
+/* ParagraphView.java -- A composite View
+   Copyright (C) 2005  Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package javax.swing.text;
+
+// TODO: Implement this class.
+public class ParagraphView
+  extends FlowView
+{
+
+  /**
+   * Creates a new <code>ParagraphView</code> for the given
+   * <code>Element</code>.
+   *
+   * @param element the element that is rendered by this ParagraphView
+   */
+    public ParagraphView(Element element)
+  {
+    super(element);
+  }
+}
Index: javax/swing/text/StyledEditorKit.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/StyledEditorKit.java,v
retrieving revision 1.8
diff -u -r1.8 StyledEditorKit.java
--- javax/swing/text/StyledEditorKit.java       2 Jul 2005 20:32:51 -0000       
1.8
+++ javax/swing/text/StyledEditorKit.java       29 Jul 2005 09:33:05 -0000
@@ -46,6 +46,7 @@
 
 import javax.swing.Action;
 import javax.swing.JEditorPane;
+import javax.swing.JTextPane;
 import javax.swing.event.CaretEvent;
 import javax.swing.event.CaretListener;
 
@@ -68,8 +69,7 @@
      */
     public UnderlineAction()
     {
-      super("TODO");
-      // TODO
+      super("TODO"); // TODO: Figure out name for this action.
     }
 
     /**
@@ -78,7 +78,13 @@
      */
     public void actionPerformed(ActionEvent event)
     {
-      // TODO
+      JEditorPane editor = getEditor(event);
+      StyledDocument doc = getStyledDocument(editor);
+      Element el = doc.getCharacterElement(editor.getSelectionStart());
+      boolean isUnderline = StyleConstants.isUnderline(el.getAttributes());
+      SimpleAttributeSet atts = new SimpleAttributeSet();
+      StyleConstants.setUnderline(atts, ! isUnderline);
+      setCharacterAttributes(editor, atts, false);
     }
   }
 
@@ -92,8 +98,7 @@
      */
     public ItalicAction()
     {
-      super("TODO");
-      // TODO
+      super("TODO"); // TODO: Figure out correct name of this Action.
     }
 
     /**
@@ -102,7 +107,13 @@
      */
     public void actionPerformed(ActionEvent event)
     {
-      // TODO
+      JEditorPane editor = getEditor(event);
+      StyledDocument doc = getStyledDocument(editor);
+      Element el = doc.getCharacterElement(editor.getSelectionStart());
+      boolean isItalic = StyleConstants.isItalic(el.getAttributes());
+      SimpleAttributeSet atts = new SimpleAttributeSet();
+      StyleConstants.setItalic(atts, ! isItalic);
+      setCharacterAttributes(editor, atts, false);
     }
   }
 
@@ -116,8 +127,7 @@
      */
     public BoldAction()
     {
-      super("TODO");
-      // TODO
+      super("TODO"); // TODO: Figure out correct name of this Action.
     }
 
     /**
@@ -126,7 +136,13 @@
      */
     public void actionPerformed(ActionEvent event)
     {
-      // TODO
+      JEditorPane editor = getEditor(event);
+      StyledDocument doc = getStyledDocument(editor);
+      Element el = doc.getCharacterElement(editor.getSelectionStart());
+      boolean isBold = StyleConstants.isBold(el.getAttributes());
+      SimpleAttributeSet atts = new SimpleAttributeSet();
+      StyleConstants.setItalic(atts, ! isBold);
+      setCharacterAttributes(editor, atts, false);
     }
   }
 
@@ -147,8 +163,8 @@
      */
     public AlignmentAction(String nm, int a)
     {
-      super("TODO");
-      // TODO
+      super(nm);
+      this.a = a;
     }
 
     /**
@@ -157,7 +173,9 @@
      */
     public void actionPerformed(ActionEvent event)
     {
-      // TODO
+      SimpleAttributeSet atts = new SimpleAttributeSet();
+      StyleConstants.setAlignment(atts, a);
+      setParagraphAttributes(getEditor(event), atts, false);
     }
   }
 
@@ -178,8 +196,8 @@
      */
     public ForegroundAction(String nm, Color fg)
     {
-      super("TODO");
-      // TODO
+      super(nm);
+      this.fg = fg;
     }
 
     /**
@@ -188,7 +206,9 @@
      */
     public void actionPerformed(ActionEvent event)
     {
-      // TODO
+      SimpleAttributeSet atts = new SimpleAttributeSet();
+      StyleConstants.setForeground(atts, fg);
+      setCharacterAttributes(getEditor(event), atts, false);
     }
   }
 
@@ -209,8 +229,8 @@
      */
     public FontSizeAction(String nm, int size)
     {
-      super("TODO");
-      // TODO
+      super(nm);
+      this.size = size;
     }
 
     /**
@@ -219,7 +239,9 @@
      */
     public void actionPerformed(ActionEvent event)
     {
-      // TODO
+      SimpleAttributeSet atts = new SimpleAttributeSet();
+      StyleConstants.setFontSize(atts, size);
+      setCharacterAttributes(getEditor(event), atts, false);
     }
   }
 
@@ -240,8 +262,8 @@
      */
     public FontFamilyAction(String nm, String family)
     {
-      super("TODO");
-      // TODO
+      super(nm);
+      this.family = family;
     }
 
     /**
@@ -250,7 +272,9 @@
      */
     public void actionPerformed(ActionEvent event)
     {
-      // TODO
+      SimpleAttributeSet atts = new SimpleAttributeSet();
+      StyleConstants.setFontFamily(atts, family);
+      setCharacterAttributes(getEditor(event), atts, false);
     }
   }
 
@@ -266,7 +290,6 @@
     public StyledTextAction(String nm)
     {
       super(nm);
-      // TODO
     }
 
     /**
@@ -276,7 +299,7 @@
      */
     protected final JEditorPane getEditor(ActionEvent event)
     {
-      return null; // TODO
+      return (JEditorPane) getTextComponent(event);
     }
 
     /**
@@ -285,11 +308,39 @@
      * @param value1 TODO
      * @param value2 TODO
      */
-    protected final void setCharacterAttributes(JEditorPane value0,
-                                                AttributeSet value1,
-                                                boolean value2)
-    {
-      // TODO
+    protected final void setCharacterAttributes(JEditorPane editor,
+                                                AttributeSet atts,
+                                                boolean replace)
+    {
+      Document doc = editor.getDocument();
+      if (doc instanceof StyledDocument)
+       {
+         StyledDocument styleDoc = (StyledDocument) editor.getDocument();
+         EditorKit kit = editor.getEditorKit();
+         if (!(kit instanceof StyledEditorKit))
+           {
+             StyledEditorKit styleKit = (StyledEditorKit) kit;
+             int start = editor.getSelectionStart();
+             int end = editor.getSelectionEnd();
+             int dot = editor.getCaret().getDot();
+             if (start == dot && end == dot)
+               {
+                 // If there is no selection, then we only update the
+                 // input attributes.
+                 MutableAttributeSet inputAttributes =
+                   styleKit.getInputAttributes();
+                 inputAttributes.addAttributes(atts);
+               }
+             else
+               styleDoc.setCharacterAttributes(start, end, atts, replace);
+           }
+         else
+           throw new AssertionError("The EditorKit for StyledTextActions "
+                                    + "is expected to be a StyledEditorKit");
+       }
+      else
+       throw new AssertionError("The Document for StyledTextActions is "
+                                + "expected to be a StyledDocument.");
     }
 
     /**
@@ -297,9 +348,14 @@
      * @param value0 TODO
      * @returns StyledDocument
      */
-    protected final StyledDocument getStyledDocument(JEditorPane value0)
+    protected final StyledDocument getStyledDocument(JEditorPane editor)
     {
-      return null; // TODO
+      Document doc = editor.getDocument();
+      if (!(doc instanceof StyledDocument))
+       throw new AssertionError("The Document for StyledEditorKits is "
+                                + "expected to be a StyledDocument.");
+
+      return (StyledDocument) doc;
     }
 
     /**
@@ -307,9 +363,14 @@
      * @param value0 TODO
      * @returns StyledEditorKit
      */
-    protected final StyledEditorKit getStyledEditorKit(JEditorPane value0)
+    protected final StyledEditorKit getStyledEditorKit(JEditorPane editor)
     {
-      return null; // TODO
+      EditorKit kit = editor.getEditorKit();
+      if (!(kit instanceof StyledEditorKit))
+       throw new AssertionError("The EditorKit for StyledDocuments is "
+                                + "expected to be a StyledEditorKit.");
+
+      return (StyledEditorKit) kit;
     }
 
     /**
@@ -318,11 +379,39 @@
      * @param value1 TODO
      * @param value2 TODO
      */
-    protected final void setParagraphAttributes(JEditorPane value0,
-                                                AttributeSet value1,
-                                                boolean value2)
-    {
-      // TODO
+    protected final void setParagraphAttributes(JEditorPane editor,
+                                                AttributeSet atts,
+                                                boolean replace)
+    {
+      Document doc = editor.getDocument();
+      if (doc instanceof StyledDocument)
+       {
+         StyledDocument styleDoc = (StyledDocument) editor.getDocument();
+         EditorKit kit = editor.getEditorKit();
+         if (!(kit instanceof StyledEditorKit))
+           {
+             StyledEditorKit styleKit = (StyledEditorKit) kit;
+             int start = editor.getSelectionStart();
+             int end = editor.getSelectionEnd();
+             int dot = editor.getCaret().getDot();
+             if (start == dot && end == dot)
+               {
+                 // If there is no selection, then we only update the
+                 // input attributes.
+                 MutableAttributeSet inputAttributes =
+                   styleKit.getInputAttributes();
+                 inputAttributes.addAttributes(atts);
+               }
+             else
+               styleDoc.setParagraphAttributes(start, end, atts, replace);
+           }
+         else
+           throw new AssertionError("The EditorKit for StyledTextActions "
+                                    + "is expected to be a StyledEditorKit");
+       }
+      else
+       throw new AssertionError("The Document for StyledTextActions is "
+                                + "expected to be a StyledDocument.");
     }
   }
 
@@ -333,66 +422,55 @@
     implements ViewFactory
   {
     /**
-     * Constructor StyledViewFactory
-     */
-    StyledViewFactory()
-    {
-      // TODO
-    }
-
-    /**
      * create
      * @param value0 TODO
      * @returns View
      */
-    public View create(Element value0)
+    public View create(Element element)
     {
-      return null; // TODO
+      String name = element.getName();
+      View view = null;
+      if (name.equals(AbstractDocument.ContentElementName))
+       view = new LabelView(element);
+      else if (name.equals(AbstractDocument.ParagraphElementName))
+       view = new ParagraphView(element);
+      else if (name.equals(AbstractDocument.SectionElementName))
+       view = new BoxView(element);
+      else if (name.equals(StyleConstants.ComponentElementName))
+       view = new ComponentView(element);
+      else if (name.equals(StyleConstants.IconElementName))
+       view = new IconView(element);
+
+      return null;
     }
   }
 
   /**
    * AttributeTracker
    */
-  class AttributeTracker
-    implements CaretListener, PropertyChangeListener, Serializable
+  class CaretTracker
+    implements CaretListener
   {
     /**
-     * Constructor AttributeTracker
-     * @param value0 TODO
-     */
-    AttributeTracker(StyledEditorKit value0)
-    {
-      // TODO
-    }
-
-    /**
-     * updateInputAttributes
-     * @param value0 TODO
-     * @param value1 TODO
-     * @param value2 TODO
-     */
-    void updateInputAttributes(int value0, int value1, JTextComponent value2)
-    {
-      // TODO
-    }
-
-    /**
-     * propertyChange
-     * @param value0 TODO
-     */
-    public void propertyChange(PropertyChangeEvent value0)
-    {
-      // TODO
-    }
-
-    /**
      * caretUpdate
      * @param value0 TODO
      */
-    public void caretUpdate(CaretEvent value0)
+    public void caretUpdate(CaretEvent ev)
     {
-      // TODO
+      Object source = ev.getSource();
+      if (!(source instanceof JTextComponent))
+       throw new AssertionError("CaretEvents are expected to come from a"
+                                + "JTextComponent.");
+
+      JTextComponent text = (JTextComponent) source;
+      Document doc = text.getDocument();
+      if (!(doc instanceof StyledDocument))
+       throw new AssertionError("The Document used by StyledEditorKits is"
+                                + "expected to be a StyledDocument");
+
+      StyledDocument styleDoc = (StyledDocument) doc;
+      currentRun = styleDoc.getCharacterElement(ev.getDot());
+      createInputAttributes(currentRun, inputAttributes);
     }
   }
 
@@ -402,21 +480,27 @@
   Element currentRun;
 
   /**
-   * currentParagraph
+   * inputAttributes
+   */
+  MutableAttributeSet inputAttributes;
+
+  /**
+   * The CaretTracker that keeps track of the current input attributes, and
+   * the current character run Element.
    */
-  Element currentParagraph;
+  CaretTracker caretTracker;
 
   /**
-   * inputAttributes
+   * The ViewFactory for StyledEditorKits.
    */
-  MutableAttributeSet inputAttributes;
+  StyledViewFactory viewFactory;
 
   /**
    * Constructor StyledEditorKit
    */
   public StyledEditorKit()
   {
-    // TODO
+    inputAttributes = new SimpleAttributeSet();
   }
 
   /**
@@ -425,7 +509,9 @@
    */
   public Object clone()
   {
-    return null; // TODO
+    StyledEditorKit clone = (StyledEditorKit) super.clone();
+    // FIXME: Investigate which fields must be copied.
+    return clone;
   }
 
   /**
@@ -434,7 +520,10 @@
    */
   public Action[] getActions()
   {
-    return null; // TODO
+    Action[] actions1 = super.getActions();
+    Action[] myActions = new Action[] { new BoldAction(), new ItalicAction(),
+                                       new UnderlineAction() };
+    return TextAction.augmentList(actions1, myActions);
   }
 
   /**
@@ -443,7 +532,7 @@
    */
   public MutableAttributeSet getInputAttributes()
   {
-    return null; // TODO
+    return inputAttributes;
   }
 
   /**
@@ -452,7 +541,7 @@
    */
   public Element getCharacterAttributeRun()
   {
-    return null; // TODO
+    return currentRun;
   }
 
   /**
@@ -461,7 +550,7 @@
    */
   public Document createDefaultDocument()
   {
-    return null; // TODO
+    return new DefaultStyledDocument();
   }
 
   /**
@@ -470,7 +559,8 @@
    */
   public void install(JEditorPane component)
   {
-    // TODO
+    CaretTracker tracker = new CaretTracker();
+    component.addCaretListener(tracker);
   }
 
   /**
@@ -479,7 +569,10 @@
    */
   public void deinstall(JEditorPane component)
   {
-    // TODO
+    CaretTracker t = caretTracker;
+    if (t != null)
+      component.removeCaretListener(t);
+    caretTracker = null;
   }
 
   /**
@@ -488,7 +581,9 @@
    */
   public ViewFactory getViewFactory()
   {
-    return null; // TODO
+    if (viewFactory == null)
+      viewFactory = new StyledViewFactory();
+    return viewFactory;
   }
 
   /**
@@ -496,8 +591,12 @@
    * @param element TODO
    * @param set TODO
    */
-  protected void createInputAttributes(Element element, MutableAttributeSet 
set)
+  protected void createInputAttributes(Element element,
+                                      MutableAttributeSet set)
   {
-    // TODO
+    AttributeSet atts = element.getAttributes();
+    set.removeAttributes(set);
+    // FIXME: Filter out component, icon and element name attributes.
+    set.addAttributes(atts);
   }
 }
Index: javax/swing/text/View.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/View.java,v
retrieving revision 1.13
diff -u -r1.13 View.java
--- javax/swing/text/View.java  2 Jul 2005 20:32:51 -0000       1.13
+++ javax/swing/text/View.java  29 Jul 2005 09:33:05 -0000
@@ -459,5 +459,58 @@
     if (ec != null)
       preferenceChanged(this, true, true);
   }
-}
 
+  /**
+   * Maps a position in the document into the coordinate space of the View.
+   * The output rectangle usually reflects the font height but has a width
+   * of zero.
+   *
+   * @param pos the position of the character in the model
+   * @param a the area that is occupied by the view
+   * @param bias either address@hidden Position.Bias.Forward} or
+   *        address@hidden Position.Bias.Backward} depending on the preferred
+   *        direction bias. If <code>null</code> this defaults to
+   *        <code>Position.Bias.Forward</code>
+   *
+   * @return a rectangle that gives the location of the document position
+   *         inside the view coordinate space
+   *
+   * @throws BadLocationException if <code>pos</code> is invalid
+   * @throws IllegalArgumentException if b is not one of the above listed
+   *         valid values
+   */
+  public abstract Shape modelToView(int pos, Shape a, Position.Bias b)
+    throws BadLocationException;
+
+  /**
+   * Maps a region in the document into the coordinate space of the View.
+   *
+   * @param p1 the beginning position inside the document
+   * @param b1 the direction bias for the beginning position
+   * @param p2 the end position inside the document
+   * @param b2 the direction bias for the end position
+   * @param a the area that is occupied by the view
+   *
+   * @return a rectangle that gives the span of the document region
+   *         inside the view coordinate space
+   *
+   * @throws BadLocationException if <code>p1</code> or <code>p2</code> are
+   *         invalid
+   * @throws IllegalArgumentException if b1 or b2 is not one of the above
+   *         listed valid values
+   */
+  public Shape modelToView(int p1, Position.Bias b1,
+                          int p2, Position.Bias b2, Shape a)
+    throws BadLocationException
+  {
+    if (b1 != Position.Bias.Forward && b1 != Position.Bias.Backward)
+      throw new IllegalArgumentException
+       ("b1 must be either Position.Bias.Forward or Position.Bias.Backward");
+    if (b2 != Position.Bias.Forward && b2 != Position.Bias.Backward)
+      throw new IllegalArgumentException
+       ("b2 must be either Position.Bias.Forward or Position.Bias.Backward");
+    Shape s1 = modelToView(p1, a, b1);
+    Shape s2 = modelToView(p2, a, b2);
+    return s1.getBounds().union(s2.getBounds());
+  }
+}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]