39 |
package javax.swing.text; |
package javax.swing.text; |
40 |
|
|
41 |
import java.awt.Graphics; |
import java.awt.Graphics; |
42 |
|
import java.awt.Rectangle; |
43 |
import java.awt.Shape; |
import java.awt.Shape; |
44 |
|
|
45 |
// TODO: Implement this class. |
import javax.swing.Icon; |
46 |
|
import javax.swing.JTextPane; |
47 |
|
import javax.swing.SwingConstants; |
48 |
|
|
49 |
|
/** |
50 |
|
* A View that can render an icon. This view is created by the |
51 |
|
* {@link StyledEditorKit}'s view factory for all elements that have name |
52 |
|
* {@link StyleConstants#IconElementName}. This is usually created by |
53 |
|
* inserting an icon into <code>JTextPane</code> using |
54 |
|
* {@link JTextPane#insertIcon(Icon)} |
55 |
|
* |
56 |
|
* The icon is determined using the attribute |
57 |
|
* {@link StyleConstants#IconAttribute}, which's value must be an {@link Icon}. |
58 |
|
* |
59 |
|
* @author Roman Kennke (kennke@aicas.com) |
60 |
|
*/ |
61 |
public class IconView |
public class IconView |
62 |
extends View |
extends View |
63 |
{ |
{ |
81 |
*/ |
*/ |
82 |
public void paint(Graphics g, Shape a) |
public void paint(Graphics g, Shape a) |
83 |
{ |
{ |
84 |
// TODO: Implement me. |
Icon icon = StyleConstants.getIcon(getElement().getAttributes()); |
85 |
|
Rectangle b = a.getBounds(); |
86 |
|
icon.paintIcon(getContainer(), g, b.x, b.y); |
87 |
} |
} |
88 |
|
|
89 |
/** |
/** |
96 |
*/ |
*/ |
97 |
public float getPreferredSpan(int axis) |
public float getPreferredSpan(int axis) |
98 |
{ |
{ |
99 |
// TODO: Implement me. |
Icon icon = StyleConstants.getIcon(getElement().getAttributes()); |
100 |
return 0F; |
float span; |
101 |
|
if (axis == X_AXIS) |
102 |
|
span = icon.getIconWidth(); |
103 |
|
else if (axis == Y_AXIS) |
104 |
|
span = icon.getIconHeight(); |
105 |
|
else |
106 |
|
throw new IllegalArgumentException(); |
107 |
|
return span; |
108 |
} |
} |
109 |
|
|
110 |
/** |
/** |
129 |
public Shape modelToView(int pos, Shape a, Position.Bias b) |
public Shape modelToView(int pos, Shape a, Position.Bias b) |
130 |
throws BadLocationException |
throws BadLocationException |
131 |
{ |
{ |
132 |
// Implement me. |
Element el = getElement(); |
133 |
return null; |
if (pos < el.getStartOffset() || pos >= el.getEndOffset()) |
134 |
|
throw new BadLocationException("Illegal offset for this view", pos); |
135 |
|
Rectangle r = a.getBounds(); |
136 |
|
Icon icon = StyleConstants.getIcon(el.getAttributes()); |
137 |
|
return new Rectangle(r.x, r.y, icon.getIconWidth(), icon.getIconHeight()); |
138 |
} |
} |
139 |
|
|
140 |
/** |
/** |
151 |
*/ |
*/ |
152 |
public int viewToModel(float x, float y, Shape a, Position.Bias[] b) |
public int viewToModel(float x, float y, Shape a, Position.Bias[] b) |
153 |
{ |
{ |
154 |
// FIXME: not implemented |
// The element should only have one character position and it is clear |
155 |
return 0; |
// that this position is the position that best matches the given screen |
156 |
|
// coordinates, simply because this view has only this one position. |
157 |
|
Element el = getElement(); |
158 |
|
return el.getStartOffset(); |
159 |
} |
} |
160 |
|
|
161 |
} |
} |