38 |
package javax.swing.text; |
package javax.swing.text; |
39 |
|
|
40 |
import java.awt.Component; |
import java.awt.Component; |
41 |
|
import java.awt.Container; |
42 |
import java.awt.Graphics; |
import java.awt.Graphics; |
43 |
|
import java.awt.Rectangle; |
44 |
import java.awt.Shape; |
import java.awt.Shape; |
45 |
|
|
46 |
|
import javax.swing.SwingConstants; |
47 |
|
import javax.swing.SwingUtilities; |
48 |
|
|
49 |
/** |
/** |
50 |
* A {@link View} implementation that is able to render arbitrary |
* A {@link View} implementation that is able to render arbitrary |
51 |
* {@link Component}s. This uses the attribute |
* {@link Component}s. This uses the attribute |
55 |
* this <code>ComponentView</code>, so this view must not be shared between |
* this <code>ComponentView</code>, so this view must not be shared between |
56 |
* multiple <code>JTextComponent</code>s. |
* multiple <code>JTextComponent</code>s. |
57 |
* |
* |
58 |
|
* @author Roman Kennke (kennke@aicas.com) |
59 |
* @author original author unknown |
* @author original author unknown |
|
* @author Roman Kennke (roman@kennke.org) |
|
60 |
*/ |
*/ |
|
// FIXME: This class is a complete stub and needs to be implemented properly. |
|
61 |
public class ComponentView extends View |
public class ComponentView extends View |
62 |
{ |
{ |
63 |
|
|
64 |
|
/** |
65 |
|
* The component that is displayed by this view. |
66 |
|
*/ |
67 |
|
private Component comp; |
68 |
|
|
69 |
/** |
/** |
70 |
* Creates a new instance of <code>ComponentView</code> for the specified |
* Creates a new instance of <code>ComponentView</code> for the specified |
71 |
* <code>Element</code>. |
* <code>Element</code>. |
85 |
* |
* |
86 |
* @return the component that is rendered |
* @return the component that is rendered |
87 |
*/ |
*/ |
88 |
protected Component createComponent() |
protected Component createComponent() |
89 |
{ |
{ |
90 |
return StyleConstants.getComponent(getElement().getAttributes()); |
return StyleConstants.getComponent(getElement().getAttributes()); |
91 |
} |
} |
99 |
*/ |
*/ |
100 |
public float getAlignment(int axis) |
public float getAlignment(int axis) |
101 |
{ |
{ |
102 |
return 0; |
float align; |
103 |
|
if (axis == X_AXIS) |
104 |
|
align = getComponent().getAlignmentX(); |
105 |
|
else if (axis == Y_AXIS) |
106 |
|
align = getComponent().getAlignmentY(); |
107 |
|
else |
108 |
|
throw new IllegalArgumentException(); |
109 |
|
return align; |
110 |
} |
} |
111 |
|
|
112 |
/** |
/** |
118 |
*/ |
*/ |
119 |
public final Component getComponent() |
public final Component getComponent() |
120 |
{ |
{ |
121 |
return null; |
if (comp == null) |
122 |
|
comp = createComponent(); |
123 |
|
return comp; |
124 |
} |
} |
125 |
|
|
126 |
/** |
/** |
135 |
*/ |
*/ |
136 |
public float getMaximumSpan(int axis) |
public float getMaximumSpan(int axis) |
137 |
{ |
{ |
138 |
return 0; |
float span; |
139 |
|
if (axis == X_AXIS) |
140 |
|
span = getComponent().getMaximumSize().width; |
141 |
|
else if (axis == Y_AXIS) |
142 |
|
span = getComponent().getMaximumSize().height; |
143 |
|
else |
144 |
|
throw new IllegalArgumentException(); |
145 |
|
return span; |
146 |
} |
} |
147 |
|
|
148 |
public float getMinimumSpan(int axis) |
public float getMinimumSpan(int axis) |
149 |
{ |
{ |
150 |
// TODO: Implement this properly. |
float span; |
151 |
return 0; |
if (axis == X_AXIS) |
152 |
|
span = getComponent().getMinimumSize().width; |
153 |
|
else if (axis == Y_AXIS) |
154 |
|
span = getComponent().getMinimumSize().height; |
155 |
|
else |
156 |
|
throw new IllegalArgumentException(); |
157 |
|
return span; |
158 |
} |
} |
159 |
|
|
160 |
public float getPreferredSpan(int axis) |
public float getPreferredSpan(int axis) |
161 |
{ |
{ |
162 |
// TODO: Implement this properly. |
float span; |
163 |
return 0; |
if (axis == X_AXIS) |
164 |
|
span = getComponent().getPreferredSize().width; |
165 |
|
else if (axis == Y_AXIS) |
166 |
|
span = getComponent().getPreferredSize().height; |
167 |
|
else |
168 |
|
throw new IllegalArgumentException(); |
169 |
|
return span; |
170 |
} |
} |
171 |
|
|
172 |
public Shape modelToView(int pos, Shape a, Position.Bias b) |
public Shape modelToView(int pos, Shape a, Position.Bias b) |
173 |
throws BadLocationException |
throws BadLocationException |
174 |
{ |
{ |
175 |
// TODO: Implement this properly. |
Element el = getElement(); |
176 |
return null; |
if (pos < el.getStartOffset() || pos >= el.getEndOffset()) |
177 |
|
throw new BadLocationException("Illegal offset for this view", pos); |
178 |
|
Rectangle r = a.getBounds(); |
179 |
|
Component c = getComponent(); |
180 |
|
return new Rectangle(r.x, r.y, c.getWidth(), c.getHeight()); |
181 |
} |
} |
182 |
|
|
183 |
|
/** |
184 |
|
* The real painting behavour is performed by normal component painting, |
185 |
|
* triggered by the text component that hosts this view. This method does |
186 |
|
* not paint by itself. However, it sets the size of the component according |
187 |
|
* to the allocation that is passed here. |
188 |
|
* |
189 |
|
* @param g the graphics context |
190 |
|
* @param a the allocation of the child |
191 |
|
*/ |
192 |
public void paint(Graphics g, Shape a) |
public void paint(Graphics g, Shape a) |
193 |
{ |
{ |
194 |
// TODO: Implement this properly. |
Rectangle r = a.getBounds(); |
195 |
|
getComponent().setBounds(r.x, r.y, r.width, r.height); |
196 |
} |
} |
197 |
|
|
198 |
public void setParent(View p) |
/** |
199 |
|
* This sets up the component when the view is added to its parent, or |
200 |
|
* cleans up the view when it is removed from its parent. |
201 |
|
* |
202 |
|
* When this view is added to a parent view, the component of this view |
203 |
|
* is added to the container that hosts this view. When <code>p</code> is |
204 |
|
* <code>null</code>, then the view is removed from it's parent and we have |
205 |
|
* to also remove the component from it's parent container. |
206 |
|
* |
207 |
|
* @param p the parent view or <code>null</code> if this view is removed |
208 |
|
* from it's parent |
209 |
|
*/ |
210 |
|
public void setParent(final View p) |
211 |
{ |
{ |
212 |
// TODO: Implement this properly. |
if (SwingUtilities.isEventDispatchThread()) |
213 |
|
setParentImpl(p); |
214 |
|
else |
215 |
|
SwingUtilities.invokeLater |
216 |
|
(new Runnable() |
217 |
|
{ |
218 |
|
public void run() |
219 |
|
{ |
220 |
|
setParentImpl(p); |
221 |
|
} |
222 |
|
}); |
223 |
} |
} |
224 |
|
|
225 |
public void setSize(float width, float height) |
/** |
226 |
|
* The implementation of {@link #setParent}. This is package private to |
227 |
|
* avoid a synthetic accessor method. |
228 |
|
* |
229 |
|
* @param p the parent view to set |
230 |
|
*/ |
231 |
|
void setParentImpl(View p) |
232 |
{ |
{ |
233 |
// TODO: Implement this properly. |
if (p != null) |
234 |
|
{ |
235 |
|
Component c = getComponent(); |
236 |
|
p.getContainer().add(c); |
237 |
|
} |
238 |
|
else |
239 |
|
{ |
240 |
|
Component c = getComponent(); |
241 |
|
Container parent = c.getParent(); |
242 |
|
parent.remove(c); |
243 |
|
comp = null; |
244 |
|
} |
245 |
} |
} |
246 |
|
|
|
public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) |
|
|
{ |
|
|
// TODO: Implement this properly. |
|
|
return 0; |
|
|
} |
|
|
|
|
247 |
/** |
/** |
248 |
* Maps coordinates from the <code>View</code>'s space into a position |
* Maps coordinates from the <code>View</code>'s space into a position |
249 |
* in the document model. |
* in the document model. |
256 |
* @return the position in the document that corresponds to the screen |
* @return the position in the document that corresponds to the screen |
257 |
* coordinates <code>x, y</code> |
* coordinates <code>x, y</code> |
258 |
*/ |
*/ |
259 |
public int viewToModel(float x, float y, Shape a, Position.Bias b) |
public int viewToModel(float x, float y, Shape a, Position.Bias[] b) |
260 |
{ |
{ |
261 |
// FIXME: Implement this properly. |
// The element should only have one character position and it is clear |
262 |
return 0; |
// that this position is the position that best matches the given screen |
263 |
|
// coordinates, simply because this view has only this one position. |
264 |
|
Element el = getElement(); |
265 |
|
return el.getStartOffset(); |
266 |
} |
} |
267 |
} |
} |