summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Kennke <roman@kennke.org>2005-11-17 20:31:49 +0000
committerRoman Kennke <roman@kennke.org>2005-11-17 20:31:49 +0000
commit584494d1775742903b97413501668d05977810ce (patch)
tree5c8307a9cf697415e655f971486e33dacc1de6b5
parent127f0bc944c760b8f88b040da3858651af19a017 (diff)
downloadclasspath-584494d1775742903b97413501668d05977810ce.tar.gz
2005-11-17 Roman Kennke <kennke@aicas.com>
* javax/swing/text/AbstractDocument.java (LeafElement.getName): If super.getName() returns something non-null, then return that instead of ContentElementName. * javax/swing/text/ComponentView.java (comp): New field. (getAlignment): Implemented previously stubbed method. (getComponent): Implemented previously stubbed method. (getMaximumSpan): Implemented previously stubbed method. (getMinimumSpan): Implemented previously stubbed method. (getPreferredSpan): Implemented previously stubbed method. (modelToView): Implemented previously stubbed method. (paint): Implemented previously stubbed method. (setParent): Implemented previously stubbed method. (setSize): Removed unneeded method. (viewToModel): Implemented previously stubbed method. * javax/swing/text/FlowView.java (insertUpdate): Forward this event to the logical view. * javax/swing/text/IconView.java (paint): Implemented previously stubbed method. (getPreferredSpan): Implemented previously stubbed method. (modelToView): Implemented previously stubbed method. (viewToModel): Implemented previously stubbed method. * javax/swing/text/ParagraphView.java (firstLineIndent): New field. (justification): New field. (lineSpacing): New field. (tabSet): New field. (changedUpdate): New method. (setPropertiesFromAttributes): New method. (setFirstLineIndent): New method. (setJustification): New method. (setLineSpacing): New method. (getLayoutView): New method. (getLayoutViewCount): New method. (getTabSet): New method.
-rw-r--r--ChangeLog38
-rw-r--r--javax/swing/text/AbstractDocument.java5
-rw-r--r--javax/swing/text/ComponentView.java148
-rw-r--r--javax/swing/text/FlowView.java3
-rw-r--r--javax/swing/text/IconView.java47
-rw-r--r--javax/swing/text/ParagraphView.java113
6 files changed, 314 insertions, 40 deletions
diff --git a/ChangeLog b/ChangeLog
index 77be93149..f5f8c692e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,41 @@
+2005-11-17 Roman Kennke <kennke@aicas.com>
+
+ * javax/swing/text/AbstractDocument.java
+ (LeafElement.getName): If super.getName() returns something
+ non-null, then return that instead of ContentElementName.
+ * javax/swing/text/ComponentView.java
+ (comp): New field.
+ (getAlignment): Implemented previously stubbed method.
+ (getComponent): Implemented previously stubbed method.
+ (getMaximumSpan): Implemented previously stubbed method.
+ (getMinimumSpan): Implemented previously stubbed method.
+ (getPreferredSpan): Implemented previously stubbed method.
+ (modelToView): Implemented previously stubbed method.
+ (paint): Implemented previously stubbed method.
+ (setParent): Implemented previously stubbed method.
+ (setSize): Removed unneeded method.
+ (viewToModel): Implemented previously stubbed method.
+ * javax/swing/text/FlowView.java
+ (insertUpdate): Forward this event to the logical view.
+ * javax/swing/text/IconView.java
+ (paint): Implemented previously stubbed method.
+ (getPreferredSpan): Implemented previously stubbed method.
+ (modelToView): Implemented previously stubbed method.
+ (viewToModel): Implemented previously stubbed method.
+ * javax/swing/text/ParagraphView.java
+ (firstLineIndent): New field.
+ (justification): New field.
+ (lineSpacing): New field.
+ (tabSet): New field.
+ (changedUpdate): New method.
+ (setPropertiesFromAttributes): New method.
+ (setFirstLineIndent): New method.
+ (setJustification): New method.
+ (setLineSpacing): New method.
+ (getLayoutView): New method.
+ (getLayoutViewCount): New method.
+ (getTabSet): New method.
+
2005-11-17 Anthony Balkissoon <abalkiss@redhat.com>
* javax/swing/plaf/basic/BasicEditorPaneUI.java:
diff --git a/javax/swing/text/AbstractDocument.java b/javax/swing/text/AbstractDocument.java
index 612ca2d53..a32442556 100644
--- a/javax/swing/text/AbstractDocument.java
+++ b/javax/swing/text/AbstractDocument.java
@@ -2120,7 +2120,10 @@ public abstract class AbstractDocument implements Document, Serializable
*/
public String getName()
{
- return ContentElementName;
+ String name = super.getName();
+ if (name == null)
+ name = ContentElementName;
+ return name;
}
/**
diff --git a/javax/swing/text/ComponentView.java b/javax/swing/text/ComponentView.java
index 16112c8f4..830dda3ec 100644
--- a/javax/swing/text/ComponentView.java
+++ b/javax/swing/text/ComponentView.java
@@ -38,10 +38,13 @@ exception statement from your version. */
package javax.swing.text;
import java.awt.Component;
+import java.awt.Container;
import java.awt.Graphics;
+import java.awt.Rectangle;
import java.awt.Shape;
import javax.swing.SwingConstants;
+import javax.swing.SwingUtilities;
/**
* A {@link View} implementation that is able to render arbitrary
@@ -52,12 +55,17 @@ import javax.swing.SwingConstants;
* this <code>ComponentView</code>, so this view must not be shared between
* multiple <code>JTextComponent</code>s.
*
+ * @author Roman Kennke (kennke@aicas.com)
* @author original author unknown
- * @author Roman Kennke (roman@kennke.org)
*/
-// FIXME: This class is a complete stub and needs to be implemented properly.
public class ComponentView extends View
{
+
+ /**
+ * The component that is displayed by this view.
+ */
+ private Component comp;
+
/**
* Creates a new instance of <code>ComponentView</code> for the specified
* <code>Element</code>.
@@ -77,7 +85,7 @@ public class ComponentView extends View
*
* @return the component that is rendered
*/
- protected Component createComponent()
+ protected Component createComponent()
{
return StyleConstants.getComponent(getElement().getAttributes());
}
@@ -91,7 +99,14 @@ public class ComponentView extends View
*/
public float getAlignment(int axis)
{
- return 0;
+ float align;
+ if (axis == X_AXIS)
+ align = getComponent().getAlignmentX();
+ else if (axis == Y_AXIS)
+ align = getComponent().getAlignmentY();
+ else
+ throw new IllegalArgumentException();
+ return align;
}
/**
@@ -103,7 +118,9 @@ public class ComponentView extends View
*/
public final Component getComponent()
{
- return null;
+ if (comp == null)
+ comp = createComponent();
+ return comp;
}
/**
@@ -118,49 +135,115 @@ public class ComponentView extends View
*/
public float getMaximumSpan(int axis)
{
- return 0;
+ float span;
+ if (axis == X_AXIS)
+ span = getComponent().getMaximumSize().width;
+ else if (axis == Y_AXIS)
+ span = getComponent().getMaximumSize().height;
+ else
+ throw new IllegalArgumentException();
+ return span;
}
public float getMinimumSpan(int axis)
{
- // TODO: Implement this properly.
- return 0;
+ float span;
+ if (axis == X_AXIS)
+ span = getComponent().getMinimumSize().width;
+ else if (axis == Y_AXIS)
+ span = getComponent().getMinimumSize().height;
+ else
+ throw new IllegalArgumentException();
+ return span;
}
public float getPreferredSpan(int axis)
{
- // TODO: Implement this properly.
- return 0;
+ float span;
+ if (axis == X_AXIS)
+ span = getComponent().getPreferredSize().width;
+ else if (axis == Y_AXIS)
+ span = getComponent().getPreferredSize().height;
+ else
+ throw new IllegalArgumentException();
+ return span;
}
public Shape modelToView(int pos, Shape a, Position.Bias b)
throws BadLocationException
{
- // TODO: Implement this properly.
- return null;
+ Element el = getElement();
+ if (pos < el.getStartOffset() || pos >= el.getEndOffset())
+ throw new BadLocationException("Illegal offset for this view", pos);
+ Rectangle r = a.getBounds();
+ Component c = getComponent();
+ return new Rectangle(r.x, r.y, c.getWidth(), c.getHeight());
}
-
+
+ /**
+ * The real painting behavour is performed by normal component painting,
+ * triggered by the text component that hosts this view. This method does
+ * not paint by itself. However, it sets the size of the component according
+ * to the allocation that is passed here.
+ *
+ * @param g the graphics context
+ * @param a the allocation of the child
+ */
public void paint(Graphics g, Shape a)
{
- // TODO: Implement this properly.
+ Rectangle r = a.getBounds();
+ getComponent().setBounds(r.x, r.y, r.width, r.height);
}
-
- public void setParent(View p)
+
+ /**
+ * This sets up the component when the view is added to its parent, or
+ * cleans up the view when it is removed from its parent.
+ *
+ * When this view is added to a parent view, the component of this view
+ * is added to the container that hosts this view. When <code>p</code> is
+ * <code>null</code>, then the view is removed from it's parent and we have
+ * to also remove the component from it's parent container.
+ *
+ * @param p the parent view or <code>null</code> if this view is removed
+ * from it's parent
+ */
+ public void setParent(final View p)
{
- // TODO: Implement this properly.
+ if (SwingUtilities.isEventDispatchThread())
+ setParentImpl(p);
+ else
+ SwingUtilities.invokeLater
+ (new Runnable()
+ {
+ public void run()
+ {
+ setParentImpl(p);
+ }
+ });
}
-
- public void setSize(float width, float height)
+
+ /**
+ * The implementation of {@link #setParent}. This is package private to
+ * avoid a synthetic accessor method.
+ *
+ * @param p the parent view to set
+ */
+ void setParentImpl(View p)
{
- // TODO: Implement this properly.
+ if (p != null)
+ {
+ Component c = getComponent();
+ p.getContainer().add(c);
+ }
+ else
+ {
+ Component c = getComponent();
+ Container parent = c.getParent();
+ parent.remove(c);
+ comp = null;
+ }
}
- public int viewToModel(float x, float y, Shape a, Position.Bias[] bias)
- {
- // TODO: Implement this properly.
- return 0;
- }
-
/**
* Maps coordinates from the <code>View</code>'s space into a position
* in the document model.
@@ -173,10 +256,13 @@ public class ComponentView extends View
* @return the position in the document that corresponds to the screen
* coordinates <code>x, y</code>
*/
- public int viewToModel(float x, float y, Shape a, Position.Bias b)
+ public int viewToModel(float x, float y, Shape a, Position.Bias[] b)
{
- // FIXME: Implement this properly.
- return 0;
+ // The element should only have one character position and it is clear
+ // that this position is the position that best matches the given screen
+ // coordinates, simply because this view has only this one position.
+ Element el = getElement();
+ return el.getStartOffset();
}
/**
@@ -205,7 +291,7 @@ public class ComponentView extends View
Position.Bias[] biasRet)
throws BadLocationException
{
- // TODO: Implement this properly.
- throw new AssertionError("Not implemented yet.");
+ // FIXME: Implement this method.
+ throw new AssertionError("Not yet implemented");
}
}
diff --git a/javax/swing/text/FlowView.java b/javax/swing/text/FlowView.java
index fd6785b6f..765f515a2 100644
--- a/javax/swing/text/FlowView.java
+++ b/javax/swing/text/FlowView.java
@@ -601,6 +601,9 @@ public abstract class FlowView extends BoxView
*/
public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory vf)
{
+ // First we must send the insertUpdate to the logical view so it can
+ // be updated accordingly.
+ layoutPool.insertUpdate(changes, a, vf);
strategy.insertUpdate(this, changes, getInsideAllocation(a));
}
diff --git a/javax/swing/text/IconView.java b/javax/swing/text/IconView.java
index 6dd0f7ad3..86c27dd5f 100644
--- a/javax/swing/text/IconView.java
+++ b/javax/swing/text/IconView.java
@@ -39,11 +39,25 @@ exception statement from your version. */
package javax.swing.text;
import java.awt.Graphics;
+import java.awt.Rectangle;
import java.awt.Shape;
+import javax.swing.Icon;
+import javax.swing.JTextPane;
import javax.swing.SwingConstants;
-// TODO: Implement this class.
+/**
+ * A View that can render an icon. This view is created by the
+ * {@link StyledEditorKit}'s view factory for all elements that have name
+ * {@link StyleConstants#IconElementName}. This is usually created by
+ * inserting an icon into <code>JTextPane</code> using
+ * {@link JTextPane#insertIcon(Icon)}
+ *
+ * The icon is determined using the attribute
+ * {@link StyleConstants#IconAttribute}, which's value must be an {@link Icon}.
+ *
+ * @author Roman Kennke (kennke@aicas.com)
+ */
public class IconView
extends View
{
@@ -67,7 +81,9 @@ public class IconView
*/
public void paint(Graphics g, Shape a)
{
- // TODO: Implement me.
+ Icon icon = StyleConstants.getIcon(getElement().getAttributes());
+ Rectangle b = a.getBounds();
+ icon.paintIcon(getContainer(), g, b.x, b.y);
}
/**
@@ -80,8 +96,15 @@ public class IconView
*/
public float getPreferredSpan(int axis)
{
- // TODO: Implement me.
- return 0F;
+ Icon icon = StyleConstants.getIcon(getElement().getAttributes());
+ float span;
+ if (axis == X_AXIS)
+ span = icon.getIconWidth();
+ else if (axis == Y_AXIS)
+ span = icon.getIconHeight();
+ else
+ throw new IllegalArgumentException();
+ return span;
}
/**
@@ -106,8 +129,12 @@ public class IconView
public Shape modelToView(int pos, Shape a, Position.Bias b)
throws BadLocationException
{
- // Implement me.
- return null;
+ Element el = getElement();
+ if (pos < el.getStartOffset() || pos >= el.getEndOffset())
+ throw new BadLocationException("Illegal offset for this view", pos);
+ Rectangle r = a.getBounds();
+ Icon icon = StyleConstants.getIcon(el.getAttributes());
+ return new Rectangle(r.x, r.y, icon.getIconWidth(), icon.getIconHeight());
}
/**
@@ -124,8 +151,11 @@ public class IconView
*/
public int viewToModel(float x, float y, Shape a, Position.Bias[] b)
{
- // FIXME: not implemented
- return 0;
+ // The element should only have one character position and it is clear
+ // that this position is the position that best matches the given screen
+ // coordinates, simply because this view has only this one position.
+ Element el = getElement();
+ return el.getStartOffset();
}
/**
@@ -157,4 +187,5 @@ public class IconView
// TODO: Implement this properly.
throw new AssertionError("Not implemented yet.");
}
+
}
diff --git a/javax/swing/text/ParagraphView.java b/javax/swing/text/ParagraphView.java
index 6fb121f94..c48645031 100644
--- a/javax/swing/text/ParagraphView.java
+++ b/javax/swing/text/ParagraphView.java
@@ -38,6 +38,10 @@ exception statement from your version. */
package javax.swing.text;
+import java.awt.Shape;
+
+import javax.swing.event.DocumentEvent;
+
/**
* A {@link FlowView} that flows it's children horizontally and boxes the rows
* vertically.
@@ -67,6 +71,26 @@ public class ParagraphView extends FlowView implements TabExpander
}
/**
+ * The indentation of the first line of the paragraph.
+ */
+ protected int firstLineIndent;
+
+ /**
+ * The justification of the paragraph.
+ */
+ private int justification;
+
+ /**
+ * The line spacing of this paragraph.
+ */
+ private float lineSpacing;
+
+ /**
+ * The TabSet of this paragraph.
+ */
+ private TabSet tabSet;
+
+ /**
* Creates a new <code>ParagraphView</code> for the given
* <code>Element</code>.
*
@@ -116,4 +140,93 @@ public class ParagraphView extends FlowView implements TabExpander
else
return 0.0F;
}
+
+ /**
+ * Receives notification when some attributes of the displayed element
+ * changes. This triggers a refresh of the cached attributes of this
+ * paragraph.
+ *
+ * @param ev the document event
+ * @param a the allocation of this view
+ * @param fv the view factory to use for creating new child views
+ */
+ public void changedUpdate(DocumentEvent ev, Shape a, ViewFactory fv)
+ {
+ setPropertiesFromAttributes();
+ }
+
+ /**
+ * Fetches the cached properties from the element's attributes.
+ */
+ protected void setPropertiesFromAttributes()
+ {
+ Element el = getElement();
+ AttributeSet atts = el.getAttributes();
+ setFirstLineIndent(StyleConstants.getFirstLineIndent(atts));
+ setLineSpacing(StyleConstants.getLineSpacing(atts));
+ setJustification(StyleConstants.getAlignment(atts));
+ tabSet = StyleConstants.getTabSet(atts);
+ }
+
+ /**
+ * Sets the indentation of the first line of the paragraph.
+ *
+ * @param i the indentation to set
+ */
+ protected void setFirstLineIndent(float i)
+ {
+ firstLineIndent = (int) i;
+ }
+
+ /**
+ * Sets the justification of the paragraph.
+ *
+ * @param j the justification to set
+ */
+ protected void setJustification(int j)
+ {
+ justification = j;
+ }
+
+ /**
+ * Sets the line spacing for this paragraph.
+ *
+ * @param s the line spacing to set
+ */
+ protected void setLineSpacing(float s)
+ {
+ lineSpacing = s;
+ }
+
+ /**
+ * Returns the i-th view from the logical views, before breaking into rows.
+ *
+ * @param i the index of the logical view to return
+ *
+ * @return the i-th view from the logical views, before breaking into rows
+ */
+ protected View getLayoutView(int i)
+ {
+ return layoutPool.getView(i);
+ }
+
+ /**
+ * Returns the number of logical child views.
+ *
+ * @return the number of logical child views
+ */
+ protected int getLayoutViewCount()
+ {
+ return layoutPool.getViewCount();
+ }
+
+ /**
+ * Returns the TabSet used by this ParagraphView.
+ *
+ * @return the TabSet used by this ParagraphView
+ */
+ protected TabSet getTabSet()
+ {
+ return tabSet;
+ }
}