diff options
author | Mark Wielaard <mark@gcc.gnu.org> | 2005-11-18 00:59:33 +0000 |
---|---|---|
committer | Mark Wielaard <mark@gcc.gnu.org> | 2005-11-18 00:59:33 +0000 |
commit | ca9e049bc145ae985bc0e2dd6079dacdd51717ac (patch) | |
tree | 4c44aaa3ed1ee1b4f15732664c05cfc9214e1fa9 /libjava/classpath/javax/swing/text/ComponentView.java | |
parent | fb3a09c214e19c97d3751003d9a2ea8008f5005e (diff) | |
download | gcc-ca9e049bc145ae985bc0e2dd6079dacdd51717ac.zip gcc-ca9e049bc145ae985bc0e2dd6079dacdd51717ac.tar.gz gcc-ca9e049bc145ae985bc0e2dd6079dacdd51717ac.tar.bz2 |
Imported GNU Classpath gcj-import-20051117.
* gnu/java/net/protocol/file/Connection.java: Removed, fully merged.
* sources.am: Regenerated.
* Makefile.in: Likewise.
From-SVN: r107153
Diffstat (limited to 'libjava/classpath/javax/swing/text/ComponentView.java')
-rw-r--r-- | libjava/classpath/javax/swing/text/ComponentView.java | 148 |
1 files changed, 117 insertions, 31 deletions
diff --git a/libjava/classpath/javax/swing/text/ComponentView.java b/libjava/classpath/javax/swing/text/ComponentView.java index 16112c8..830dda3 100644 --- a/libjava/classpath/javax/swing/text/ComponentView.java +++ b/libjava/classpath/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"); } } |