author | malenkov |
Tue, 29 Oct 2013 17:01:06 +0400 | |
changeset 21278 | ef8a3a2a72f2 |
parent 21256 | dfb1f9090caa |
child 21982 | fd6e5fe509df |
permissions | -rw-r--r-- |
2 | 1 |
/* |
17689
c6db20805a87
8014863: Line break calculations in Java 7 are incorrect.
mcherkas
parents:
9035
diff
changeset
|
2 |
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package javax.swing.text; |
|
26 |
||
27 |
import java.awt.*; |
|
28 |
import javax.swing.SwingConstants; |
|
29 |
import javax.swing.event.*; |
|
30 |
||
31 |
/** |
|
32 |
* <p> |
|
33 |
* A very important part of the text package is the <code>View</code> class. |
|
34 |
* As the name suggests it represents a view of the text model, |
|
35 |
* or a piece of the text model. |
|
36 |
* It is this class that is responsible for the look of the text component. |
|
37 |
* The view is not intended to be some completely new thing that one must |
|
38 |
* learn, but rather is much like a lightweight component. |
|
39 |
* <p> |
|
40 |
By default, a view is very light. It contains a reference to the parent |
|
41 |
view from which it can fetch many things without holding state, and it |
|
42 |
contains a reference to a portion of the model (<code>Element</code>). |
|
43 |
A view does not |
|
44 |
have to exactly represent an element in the model, that is simply a typical |
|
45 |
and therefore convenient mapping. A view can alternatively maintain a couple |
|
46 |
of Position objects to maintain its location in the model (i.e. represent |
|
47 |
a fragment of an element). This is typically the result of formatting where |
|
48 |
views have been broken down into pieces. The convenience of a substantial |
|
49 |
relationship to the element makes it easier to build factories to produce the |
|
50 |
views, and makes it easier to keep track of the view pieces as the model is |
|
51 |
changed and the view must be changed to reflect the model. Simple views |
|
52 |
therefore represent an Element directly and complex views do not. |
|
53 |
<p> |
|
54 |
A view has the following responsibilities: |
|
55 |
<dl> |
|
56 |
||
57 |
<dt><b>Participate in layout.</b> |
|
58 |
<dd> |
|
59 |
<p>The view has a <code>setSize</code> method which is like |
|
60 |
<code>doLayout</code> and <code>setSize</code> in <code>Component</code> combined. |
|
61 |
The view has a <code>preferenceChanged</code> method which is |
|
62 |
like <code>invalidate</code> in <code>Component</code> except that one can |
|
63 |
invalidate just one axis |
|
64 |
and the child requesting the change is identified. |
|
65 |
<p>A View expresses the size that it would like to be in terms of three |
|
66 |
values, a minimum, a preferred, and a maximum span. Layout in a view is |
|
67 |
can be done independently upon each axis. For a properly functioning View |
|
68 |
implementation, the minimum span will be <= the preferred span which in turn |
|
69 |
will be <= the maximum span. |
|
70 |
</p> |
|
20451
4cedf4e1560a
8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents:
20428
diff
changeset
|
71 |
<p style="text-align:center"><img src="doc-files/View-flexibility.jpg" |
2 | 72 |
alt="The above text describes this graphic."> |
73 |
<p>The minimum set of methods for layout are: |
|
74 |
<ul> |
|
20158
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
20126
diff
changeset
|
75 |
<li>{@link #getMinimumSpan(int) getMinimumSpan} |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
20126
diff
changeset
|
76 |
<li>{@link #getPreferredSpan(int) getPreferredSpan} |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
20126
diff
changeset
|
77 |
<li>{@link #getMaximumSpan(int) getMaximumSpan} |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
20126
diff
changeset
|
78 |
<li>{@link #getAlignment(int) getAlignment} |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
20126
diff
changeset
|
79 |
<li>{@link #preferenceChanged(javax.swing.text.View, boolean, boolean) preferenceChanged} |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
20126
diff
changeset
|
80 |
<li>{@link #setSize(float, float) setSize} |
2 | 81 |
</ul> |
82 |
||
83 |
<p>The <code>setSize</code> method should be prepared to be called a number of times |
|
84 |
(i.e. It may be called even if the size didn't change). |
|
85 |
The <code>setSize</code> method |
|
86 |
is generally called to make sure the View layout is complete prior to trying |
|
87 |
to perform an operation on it that requires an up-to-date layout. A view's |
|
88 |
size should <em>always</em> be set to a value within the minimum and maximum |
|
89 |
span specified by that view. Additionally, the view must always call the |
|
90 |
<code>preferenceChanged</code> method on the parent if it has changed the |
|
91 |
values for the |
|
92 |
layout it would like, and expects the parent to honor. The parent View is |
|
93 |
not required to recognize a change until the <code>preferenceChanged</code> |
|
94 |
has been sent. |
|
95 |
This allows parent View implementations to cache the child requirements if |
|
96 |
desired. The calling sequence looks something like the following: |
|
97 |
</p> |
|
20451
4cedf4e1560a
8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents:
20428
diff
changeset
|
98 |
<p style="text-align:center"> |
2 | 99 |
<img src="doc-files/View-layout.jpg" |
100 |
alt="Sample calling sequence between parent view and child view: |
|
101 |
setSize, getMinimum, getPreferred, getMaximum, getAlignment, setSize"> |
|
102 |
<p>The exact calling sequence is up to the layout functionality of |
|
103 |
the parent view (if the view has any children). The view may collect |
|
104 |
the preferences of the children prior to determining what it will give |
|
105 |
each child, or it might iteratively update the children one at a time. |
|
106 |
</p> |
|
107 |
||
108 |
<dt><b>Render a portion of the model.</b> |
|
109 |
<dd> |
|
110 |
<p>This is done in the paint method, which is pretty much like a component |
|
111 |
paint method. Views are expected to potentially populate a fairly large |
|
112 |
tree. A <code>View</code> has the following semantics for rendering: |
|
113 |
</p> |
|
114 |
<ul> |
|
115 |
<li>The view gets its allocation from the parent at paint time, so it |
|
116 |
must be prepared to redo layout if the allocated area is different from |
|
117 |
what it is prepared to deal with. |
|
118 |
<li>The coordinate system is the same as the hosting <code>Component</code> |
|
119 |
(i.e. the <code>Component</code> returned by the |
|
7959 | 120 |
{@link #getContainer getContainer} method). |
2 | 121 |
This means a child view lives in the same coordinate system as the parent |
122 |
view unless the parent has explicitly changed the coordinate system. |
|
123 |
To schedule itself to be repainted a view can call repaint on the hosting |
|
124 |
<code>Component</code>. |
|
125 |
<li>The default is to <em>not clip</em> the children. It is more efficient |
|
126 |
to allow a view to clip only if it really feels it needs clipping. |
|
127 |
<li>The <code>Graphics</code> object given is not initialized in any way. |
|
128 |
A view should set any settings needed. |
|
129 |
<li>A <code>View</code> is inherently transparent. While a view may render into its |
|
130 |
entire allocation, typically a view does not. Rendering is performed by |
|
21278 | 131 |
traversing down the tree of <code>View</code> implementations. |
2 | 132 |
Each <code>View</code> is responsible |
133 |
for rendering its children. This behavior is depended upon for thread |
|
134 |
safety. While view implementations do not necessarily have to be implemented |
|
135 |
with thread safety in mind, other view implementations that do make use of |
|
136 |
concurrency can depend upon a tree traversal to guarantee thread safety. |
|
137 |
<li>The order of views relative to the model is up to the implementation. |
|
138 |
Although child views will typically be arranged in the same order that they |
|
139 |
occur in the model, they may be visually arranged in an entirely different |
|
140 |
order. View implementations may have Z-Order associated with them if the |
|
141 |
children are overlapping. |
|
142 |
</ul> |
|
143 |
<p>The methods for rendering are: |
|
144 |
<ul> |
|
20158
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
20126
diff
changeset
|
145 |
<li>{@link #paint(java.awt.Graphics, java.awt.Shape) paint} |
2 | 146 |
</ul> |
147 |
<p> |
|
148 |
||
149 |
<dt><b>Translate between the model and view coordinate systems.</b> |
|
150 |
<dd> |
|
151 |
<p>Because the view objects are produced from a factory and therefore cannot |
|
152 |
necessarily be counted upon to be in a particular pattern, one must be able |
|
153 |
to perform translation to properly locate spatial representation of the model. |
|
154 |
The methods for doing this are: |
|
155 |
<ul> |
|
20158
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
20126
diff
changeset
|
156 |
<li>{@link #modelToView(int, javax.swing.text.Position.Bias, int, javax.swing.text.Position.Bias, java.awt.Shape) modelToView} |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
20126
diff
changeset
|
157 |
<li>{@link #viewToModel(float, float, java.awt.Shape, javax.swing.text.Position.Bias[]) viewToModel} |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
20126
diff
changeset
|
158 |
<li>{@link #getDocument() getDocument} |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
20126
diff
changeset
|
159 |
<li>{@link #getElement() getElement} |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
20126
diff
changeset
|
160 |
<li>{@link #getStartOffset() getStartOffset} |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
20126
diff
changeset
|
161 |
<li>{@link #getEndOffset() getEndOffset} |
2 | 162 |
</ul> |
163 |
<p>The layout must be valid prior to attempting to make the translation. |
|
164 |
The translation is not valid, and must not be attempted while changes |
|
165 |
are being broadcasted from the model via a <code>DocumentEvent</code>. |
|
166 |
</p> |
|
167 |
||
168 |
<dt><b>Respond to changes from the model.</b> |
|
169 |
<dd> |
|
170 |
<p>If the overall view is represented by many pieces (which is the best situation |
|
171 |
if one want to be able to change the view and write the least amount of new code), |
|
172 |
it would be impractical to have a huge number of <code>DocumentListener</code>s. |
|
173 |
If each |
|
174 |
view listened to the model, only a few would actually be interested in the |
|
175 |
changes broadcasted at any given time. Since the model has no knowledge of |
|
176 |
views, it has no way to filter the broadcast of change information. The view |
|
177 |
hierarchy itself is instead responsible for propagating the change information. |
|
178 |
At any level in the view hierarchy, that view knows enough about its children to |
|
179 |
best distribute the change information further. Changes are therefore broadcasted |
|
180 |
starting from the root of the view hierarchy. |
|
181 |
The methods for doing this are: |
|
182 |
<ul> |
|
7959 | 183 |
<li>{@link #insertUpdate insertUpdate} |
184 |
<li>{@link #removeUpdate removeUpdate} |
|
185 |
<li>{@link #changedUpdate changedUpdate} |
|
2 | 186 |
</ul> |
187 |
<p> |
|
188 |
</dl> |
|
189 |
* |
|
190 |
* @author Timothy Prinzing |
|
191 |
*/ |
|
192 |
public abstract class View implements SwingConstants { |
|
193 |
||
194 |
/** |
|
195 |
* Creates a new <code>View</code> object. |
|
196 |
* |
|
197 |
* @param elem the <code>Element</code> to represent |
|
198 |
*/ |
|
199 |
public View(Element elem) { |
|
200 |
this.elem = elem; |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Returns the parent of the view. |
|
205 |
* |
|
206 |
* @return the parent, or <code>null</code> if none exists |
|
207 |
*/ |
|
208 |
public View getParent() { |
|
209 |
return parent; |
|
210 |
} |
|
211 |
||
212 |
/** |
|
213 |
* Returns a boolean that indicates whether |
|
214 |
* the view is visible or not. By default |
|
215 |
* all views are visible. |
|
216 |
* |
|
217 |
* @return always returns true |
|
218 |
*/ |
|
219 |
public boolean isVisible() { |
|
220 |
return true; |
|
221 |
} |
|
222 |
||
223 |
||
224 |
/** |
|
225 |
* Determines the preferred span for this view along an |
|
226 |
* axis. |
|
227 |
* |
|
228 |
* @param axis may be either <code>View.X_AXIS</code> or |
|
229 |
* <code>View.Y_AXIS</code> |
|
230 |
* @return the span the view would like to be rendered into. |
|
231 |
* Typically the view is told to render into the span |
|
232 |
* that is returned, although there is no guarantee. |
|
233 |
* The parent may choose to resize or break the view |
|
234 |
* @see View#getPreferredSpan |
|
235 |
*/ |
|
236 |
public abstract float getPreferredSpan(int axis); |
|
237 |
||
238 |
/** |
|
239 |
* Determines the minimum span for this view along an |
|
240 |
* axis. |
|
241 |
* |
|
242 |
* @param axis may be either <code>View.X_AXIS</code> or |
|
243 |
* <code>View.Y_AXIS</code> |
|
244 |
* @return the minimum span the view can be rendered into |
|
245 |
* @see View#getPreferredSpan |
|
246 |
*/ |
|
247 |
public float getMinimumSpan(int axis) { |
|
248 |
int w = getResizeWeight(axis); |
|
249 |
if (w == 0) { |
|
250 |
// can't resize |
|
251 |
return getPreferredSpan(axis); |
|
252 |
} |
|
253 |
return 0; |
|
254 |
} |
|
255 |
||
256 |
/** |
|
257 |
* Determines the maximum span for this view along an |
|
258 |
* axis. |
|
259 |
* |
|
260 |
* @param axis may be either <code>View.X_AXIS</code> or |
|
261 |
* <code>View.Y_AXIS</code> |
|
262 |
* @return the maximum span the view can be rendered into |
|
263 |
* @see View#getPreferredSpan |
|
264 |
*/ |
|
265 |
public float getMaximumSpan(int axis) { |
|
266 |
int w = getResizeWeight(axis); |
|
267 |
if (w == 0) { |
|
268 |
// can't resize |
|
269 |
return getPreferredSpan(axis); |
|
270 |
} |
|
271 |
return Integer.MAX_VALUE; |
|
272 |
} |
|
273 |
||
274 |
/** |
|
275 |
* Child views can call this on the parent to indicate that |
|
276 |
* the preference has changed and should be reconsidered |
|
277 |
* for layout. By default this just propagates upward to |
|
278 |
* the next parent. The root view will call |
|
279 |
* <code>revalidate</code> on the associated text component. |
|
280 |
* |
|
281 |
* @param child the child view |
|
282 |
* @param width true if the width preference has changed |
|
283 |
* @param height true if the height preference has changed |
|
284 |
* @see javax.swing.JComponent#revalidate |
|
285 |
*/ |
|
286 |
public void preferenceChanged(View child, boolean width, boolean height) { |
|
287 |
View parent = getParent(); |
|
288 |
if (parent != null) { |
|
289 |
parent.preferenceChanged(this, width, height); |
|
290 |
} |
|
291 |
} |
|
292 |
||
293 |
/** |
|
294 |
* Determines the desired alignment for this view along an |
|
295 |
* axis. The desired alignment is returned. This should be |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
296 |
* a value >= 0.0 and <= 1.0, where 0 indicates alignment at |
2 | 297 |
* the origin and 1.0 indicates alignment to the full span |
298 |
* away from the origin. An alignment of 0.5 would be the |
|
299 |
* center of the view. |
|
300 |
* |
|
301 |
* @param axis may be either <code>View.X_AXIS</code> or |
|
302 |
* <code>View.Y_AXIS</code> |
|
303 |
* @return the value 0.5 |
|
304 |
*/ |
|
305 |
public float getAlignment(int axis) { |
|
306 |
return 0.5f; |
|
307 |
} |
|
308 |
||
309 |
/** |
|
310 |
* Renders using the given rendering surface and area on that |
|
311 |
* surface. The view may need to do layout and create child |
|
312 |
* views to enable itself to render into the given allocation. |
|
313 |
* |
|
314 |
* @param g the rendering surface to use |
|
315 |
* @param allocation the allocated region to render into |
|
316 |
*/ |
|
317 |
public abstract void paint(Graphics g, Shape allocation); |
|
318 |
||
319 |
/** |
|
320 |
* Establishes the parent view for this view. This is |
|
321 |
* guaranteed to be called before any other methods if the |
|
322 |
* parent view is functioning properly. This is also |
|
323 |
* the last method called, since it is called to indicate |
|
324 |
* the view has been removed from the hierarchy as |
|
325 |
* well. When this method is called to set the parent to |
|
326 |
* null, this method does the same for each of its children, |
|
21278 | 327 |
* propagating the notification that they have been |
2 | 328 |
* disconnected from the view tree. If this is |
329 |
* reimplemented, <code>super.setParent()</code> should |
|
330 |
* be called. |
|
331 |
* |
|
332 |
* @param parent the new parent, or <code>null</code> if the view is |
|
333 |
* being removed from a parent |
|
334 |
*/ |
|
335 |
public void setParent(View parent) { |
|
336 |
// if the parent is null then propogate down the view tree |
|
337 |
if (parent == null) { |
|
338 |
for (int i = 0; i < getViewCount(); i++) { |
|
339 |
if (getView(i).getParent() == this) { |
|
340 |
// in FlowView.java view might be referenced |
|
341 |
// from two super-views as a child. see logicalView |
|
342 |
getView(i).setParent(null); |
|
343 |
} |
|
344 |
} |
|
345 |
} |
|
346 |
this.parent = parent; |
|
347 |
} |
|
348 |
||
349 |
/** |
|
350 |
* Returns the number of views in this view. Since |
|
351 |
* the default is to not be a composite view this |
|
352 |
* returns 0. |
|
353 |
* |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
354 |
* @return the number of views >= 0 |
2 | 355 |
* @see View#getViewCount |
356 |
*/ |
|
357 |
public int getViewCount() { |
|
358 |
return 0; |
|
359 |
} |
|
360 |
||
361 |
/** |
|
362 |
* Gets the <i>n</i>th child view. Since there are no |
|
363 |
* children by default, this returns <code>null</code>. |
|
364 |
* |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
365 |
* @param n the number of the view to get, >= 0 && < getViewCount() |
2 | 366 |
* @return the view |
367 |
*/ |
|
368 |
public View getView(int n) { |
|
369 |
return null; |
|
370 |
} |
|
371 |
||
372 |
||
373 |
/** |
|
374 |
* Removes all of the children. This is a convenience |
|
375 |
* call to <code>replace</code>. |
|
376 |
* |
|
377 |
* @since 1.3 |
|
378 |
*/ |
|
379 |
public void removeAll() { |
|
380 |
replace(0, getViewCount(), null); |
|
381 |
} |
|
382 |
||
383 |
/** |
|
384 |
* Removes one of the children at the given position. |
|
385 |
* This is a convenience call to <code>replace</code>. |
|
386 |
* @since 1.3 |
|
387 |
*/ |
|
388 |
public void remove(int i) { |
|
389 |
replace(i, 1, null); |
|
390 |
} |
|
391 |
||
392 |
/** |
|
393 |
* Inserts a single child view. This is a convenience |
|
394 |
* call to <code>replace</code>. |
|
395 |
* |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
396 |
* @param offs the offset of the view to insert before >= 0 |
2 | 397 |
* @param v the view |
398 |
* @see #replace |
|
399 |
* @since 1.3 |
|
400 |
*/ |
|
401 |
public void insert(int offs, View v) { |
|
402 |
View[] one = new View[1]; |
|
403 |
one[0] = v; |
|
404 |
replace(offs, 0, one); |
|
405 |
} |
|
406 |
||
407 |
/** |
|
408 |
* Appends a single child view. This is a convenience |
|
409 |
* call to <code>replace</code>. |
|
410 |
* |
|
411 |
* @param v the view |
|
412 |
* @see #replace |
|
413 |
* @since 1.3 |
|
414 |
*/ |
|
415 |
public void append(View v) { |
|
416 |
View[] one = new View[1]; |
|
417 |
one[0] = v; |
|
418 |
replace(getViewCount(), 0, one); |
|
419 |
} |
|
420 |
||
421 |
/** |
|
422 |
* Replaces child views. If there are no views to remove |
|
423 |
* this acts as an insert. If there are no views to |
|
424 |
* add this acts as a remove. Views being removed will |
|
425 |
* have the parent set to <code>null</code>, and the internal reference |
|
426 |
* to them removed so that they can be garbage collected. |
|
427 |
* This is implemented to do nothing, because by default |
|
428 |
* a view has no children. |
|
429 |
* |
|
430 |
* @param offset the starting index into the child views to insert |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
431 |
* the new views. This should be a value >= 0 and <= getViewCount |
2 | 432 |
* @param length the number of existing child views to remove |
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
433 |
* This should be a value >= 0 and <= (getViewCount() - offset). |
2 | 434 |
* @param views the child views to add. This value can be |
435 |
* <code>null</code> to indicate no children are being added |
|
436 |
* (useful to remove). |
|
437 |
* @since 1.3 |
|
438 |
*/ |
|
439 |
public void replace(int offset, int length, View[] views) { |
|
440 |
} |
|
441 |
||
442 |
/** |
|
443 |
* Returns the child view index representing the given position in |
|
444 |
* the model. By default a view has no children so this is implemented |
|
445 |
* to return -1 to indicate there is no valid child index for any |
|
446 |
* position. |
|
447 |
* |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
448 |
* @param pos the position >= 0 |
2 | 449 |
* @return index of the view representing the given position, or |
450 |
* -1 if no view represents that position |
|
451 |
* @since 1.3 |
|
452 |
*/ |
|
453 |
public int getViewIndex(int pos, Position.Bias b) { |
|
454 |
return -1; |
|
455 |
} |
|
456 |
||
457 |
/** |
|
458 |
* Fetches the allocation for the given child view. |
|
459 |
* This enables finding out where various views |
|
460 |
* are located, without assuming how the views store |
|
461 |
* their location. This returns <code>null</code> since the |
|
462 |
* default is to not have any child views. |
|
463 |
* |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
464 |
* @param index the index of the child, >= 0 && < |
2 | 465 |
* <code>getViewCount()</code> |
466 |
* @param a the allocation to this view |
|
467 |
* @return the allocation to the child |
|
468 |
*/ |
|
469 |
public Shape getChildAllocation(int index, Shape a) { |
|
470 |
return null; |
|
471 |
} |
|
472 |
||
473 |
/** |
|
474 |
* Provides a way to determine the next visually represented model |
|
475 |
* location at which one might place a caret. |
|
476 |
* Some views may not be visible, |
|
477 |
* they might not be in the same order found in the model, or they just |
|
478 |
* might not allow access to some of the locations in the model. |
|
21256
dfb1f9090caa
7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents:
20451
diff
changeset
|
479 |
* This method enables specifying a position to convert |
dfb1f9090caa
7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents:
20451
diff
changeset
|
480 |
* within the range of >=0. If the value is -1, a position |
dfb1f9090caa
7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents:
20451
diff
changeset
|
481 |
* will be calculated automatically. If the value < -1, |
dfb1f9090caa
7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents:
20451
diff
changeset
|
482 |
* the {@code BadLocationException} will be thrown. |
2 | 483 |
* |
21256
dfb1f9090caa
7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents:
20451
diff
changeset
|
484 |
* @param pos the position to convert |
2 | 485 |
* @param a the allocated region in which to render |
486 |
* @param direction the direction from the current position that can |
|
487 |
* be thought of as the arrow keys typically found on a keyboard. |
|
488 |
* This will be one of the following values: |
|
489 |
* <ul> |
|
490 |
* <li>SwingConstants.WEST |
|
491 |
* <li>SwingConstants.EAST |
|
492 |
* <li>SwingConstants.NORTH |
|
493 |
* <li>SwingConstants.SOUTH |
|
494 |
* </ul> |
|
495 |
* @return the location within the model that best represents the next |
|
496 |
* location visual position |
|
21256
dfb1f9090caa
7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents:
20451
diff
changeset
|
497 |
* @exception BadLocationException the given position is not a valid |
dfb1f9090caa
7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents:
20451
diff
changeset
|
498 |
* position within the document |
2 | 499 |
* @exception IllegalArgumentException if <code>direction</code> |
500 |
* doesn't have one of the legal values above |
|
501 |
*/ |
|
502 |
public int getNextVisualPositionFrom(int pos, Position.Bias b, Shape a, |
|
503 |
int direction, Position.Bias[] biasRet) |
|
504 |
throws BadLocationException { |
|
7964
fda4ca3f7b24
6735293: javax.swing.text.NavigationFilter.getNextVisualPositionFrom() not always throws BadLocationException
rupashka
parents:
7959
diff
changeset
|
505 |
if (pos < -1) { |
fda4ca3f7b24
6735293: javax.swing.text.NavigationFilter.getNextVisualPositionFrom() not always throws BadLocationException
rupashka
parents:
7959
diff
changeset
|
506 |
// -1 is a reserved value, see the code below |
fda4ca3f7b24
6735293: javax.swing.text.NavigationFilter.getNextVisualPositionFrom() not always throws BadLocationException
rupashka
parents:
7959
diff
changeset
|
507 |
throw new BadLocationException("Invalid position", pos); |
fda4ca3f7b24
6735293: javax.swing.text.NavigationFilter.getNextVisualPositionFrom() not always throws BadLocationException
rupashka
parents:
7959
diff
changeset
|
508 |
} |
2 | 509 |
|
510 |
biasRet[0] = Position.Bias.Forward; |
|
511 |
switch (direction) { |
|
512 |
case NORTH: |
|
513 |
case SOUTH: |
|
514 |
{ |
|
515 |
if (pos == -1) { |
|
516 |
pos = (direction == NORTH) ? Math.max(0, getEndOffset() - 1) : |
|
517 |
getStartOffset(); |
|
518 |
break; |
|
519 |
} |
|
520 |
JTextComponent target = (JTextComponent) getContainer(); |
|
521 |
Caret c = (target != null) ? target.getCaret() : null; |
|
522 |
// YECK! Ideally, the x location from the magic caret position |
|
523 |
// would be passed in. |
|
524 |
Point mcp; |
|
525 |
if (c != null) { |
|
526 |
mcp = c.getMagicCaretPosition(); |
|
527 |
} |
|
528 |
else { |
|
529 |
mcp = null; |
|
530 |
} |
|
531 |
int x; |
|
532 |
if (mcp == null) { |
|
533 |
Rectangle loc = target.modelToView(pos); |
|
534 |
x = (loc == null) ? 0 : loc.x; |
|
535 |
} |
|
536 |
else { |
|
537 |
x = mcp.x; |
|
538 |
} |
|
539 |
if (direction == NORTH) { |
|
540 |
pos = Utilities.getPositionAbove(target, pos, x); |
|
541 |
} |
|
542 |
else { |
|
543 |
pos = Utilities.getPositionBelow(target, pos, x); |
|
544 |
} |
|
545 |
} |
|
546 |
break; |
|
547 |
case WEST: |
|
548 |
if(pos == -1) { |
|
549 |
pos = Math.max(0, getEndOffset() - 1); |
|
550 |
} |
|
551 |
else { |
|
552 |
pos = Math.max(0, pos - 1); |
|
553 |
} |
|
554 |
break; |
|
555 |
case EAST: |
|
556 |
if(pos == -1) { |
|
557 |
pos = getStartOffset(); |
|
558 |
} |
|
559 |
else { |
|
560 |
pos = Math.min(pos + 1, getDocument().getLength()); |
|
561 |
} |
|
562 |
break; |
|
563 |
default: |
|
564 |
throw new IllegalArgumentException("Bad direction: " + direction); |
|
565 |
} |
|
566 |
return pos; |
|
567 |
} |
|
568 |
||
569 |
/** |
|
570 |
* Provides a mapping, for a given character, |
|
571 |
* from the document model coordinate space |
|
572 |
* to the view coordinate space. |
|
573 |
* |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
574 |
* @param pos the position of the desired character (>=0) |
2 | 575 |
* @param a the area of the view, which encompasses the requested character |
576 |
* @param b the bias toward the previous character or the |
|
577 |
* next character represented by the offset, in case the |
|
578 |
* position is a boundary of two views; <code>b</code> will have one |
|
579 |
* of these values: |
|
580 |
* <ul> |
|
581 |
* <li> <code>Position.Bias.Forward</code> |
|
582 |
* <li> <code>Position.Bias.Backward</code> |
|
583 |
* </ul> |
|
584 |
* @return the bounding box, in view coordinate space, |
|
585 |
* of the character at the specified position |
|
586 |
* @exception BadLocationException if the specified position does |
|
587 |
* not represent a valid location in the associated document |
|
588 |
* @exception IllegalArgumentException if <code>b</code> is not one of the |
|
589 |
* legal <code>Position.Bias</code> values listed above |
|
590 |
* @see View#viewToModel |
|
591 |
*/ |
|
592 |
public abstract Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException; |
|
593 |
||
594 |
/** |
|
595 |
* Provides a mapping, for a given region, |
|
596 |
* from the document model coordinate space |
|
597 |
* to the view coordinate space. The specified region is |
|
598 |
* created as a union of the first and last character positions. |
|
599 |
* |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
600 |
* @param p0 the position of the first character (>=0) |
2 | 601 |
* @param b0 the bias of the first character position, |
602 |
* toward the previous character or the |
|
603 |
* next character represented by the offset, in case the |
|
604 |
* position is a boundary of two views; <code>b0</code> will have one |
|
605 |
* of these values: |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
606 |
* <ul style="list-style-type:none"> |
2 | 607 |
* <li> <code>Position.Bias.Forward</code> |
608 |
* <li> <code>Position.Bias.Backward</code> |
|
609 |
* </ul> |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
610 |
* @param p1 the position of the last character (>=0) |
2 | 611 |
* @param b1 the bias for the second character position, defined |
612 |
* one of the legal values shown above |
|
613 |
* @param a the area of the view, which encompasses the requested region |
|
614 |
* @return the bounding box which is a union of the region specified |
|
615 |
* by the first and last character positions |
|
616 |
* @exception BadLocationException if the given position does |
|
617 |
* not represent a valid location in the associated document |
|
618 |
* @exception IllegalArgumentException if <code>b0</code> or |
|
619 |
* <code>b1</code> are not one of the |
|
620 |
* legal <code>Position.Bias</code> values listed above |
|
621 |
* @see View#viewToModel |
|
622 |
*/ |
|
623 |
public Shape modelToView(int p0, Position.Bias b0, int p1, Position.Bias b1, Shape a) throws BadLocationException { |
|
624 |
Shape s0 = modelToView(p0, a, b0); |
|
625 |
Shape s1; |
|
626 |
if (p1 == getEndOffset()) { |
|
627 |
try { |
|
628 |
s1 = modelToView(p1, a, b1); |
|
629 |
} catch (BadLocationException ble) { |
|
630 |
s1 = null; |
|
631 |
} |
|
632 |
if (s1 == null) { |
|
633 |
// Assume extends left to right. |
|
634 |
Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : |
|
635 |
a.getBounds(); |
|
636 |
s1 = new Rectangle(alloc.x + alloc.width - 1, alloc.y, |
|
637 |
1, alloc.height); |
|
638 |
} |
|
639 |
} |
|
640 |
else { |
|
641 |
s1 = modelToView(p1, a, b1); |
|
642 |
} |
|
643 |
Rectangle r0 = s0.getBounds(); |
|
644 |
Rectangle r1 = (s1 instanceof Rectangle) ? (Rectangle) s1 : |
|
645 |
s1.getBounds(); |
|
646 |
if (r0.y != r1.y) { |
|
647 |
// If it spans lines, force it to be the width of the view. |
|
648 |
Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : |
|
649 |
a.getBounds(); |
|
650 |
r0.x = alloc.x; |
|
651 |
r0.width = alloc.width; |
|
652 |
} |
|
653 |
r0.add(r1); |
|
654 |
return r0; |
|
655 |
} |
|
656 |
||
657 |
/** |
|
658 |
* Provides a mapping from the view coordinate space to the logical |
|
659 |
* coordinate space of the model. The <code>biasReturn</code> |
|
660 |
* argument will be filled in to indicate that the point given is |
|
661 |
* closer to the next character in the model or the previous |
|
662 |
* character in the model. |
|
663 |
* |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
664 |
* @param x the X coordinate >= 0 |
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
665 |
* @param y the Y coordinate >= 0 |
2 | 666 |
* @param a the allocated region in which to render |
667 |
* @return the location within the model that best represents the |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
668 |
* given point in the view >= 0. The <code>biasReturn</code> |
2 | 669 |
* argument will be |
670 |
* filled in to indicate that the point given is closer to the next |
|
671 |
* character in the model or the previous character in the model. |
|
672 |
*/ |
|
673 |
public abstract int viewToModel(float x, float y, Shape a, Position.Bias[] biasReturn); |
|
674 |
||
675 |
/** |
|
676 |
* Gives notification that something was inserted into |
|
677 |
* the document in a location that this view is responsible for. |
|
678 |
* To reduce the burden to subclasses, this functionality is |
|
679 |
* spread out into the following calls that subclasses can |
|
680 |
* reimplement: |
|
681 |
* <ol> |
|
7959 | 682 |
* <li>{@link #updateChildren updateChildren} is called |
2 | 683 |
* if there were any changes to the element this view is |
684 |
* responsible for. If this view has child views that are |
|
685 |
* represent the child elements, then this method should do |
|
686 |
* whatever is necessary to make sure the child views correctly |
|
687 |
* represent the model. |
|
7959 | 688 |
* <li>{@link #forwardUpdate forwardUpdate} is called |
2 | 689 |
* to forward the DocumentEvent to the appropriate child views. |
7959 | 690 |
* <li>{@link #updateLayout updateLayout} is called to |
2 | 691 |
* give the view a chance to either repair its layout, to reschedule |
692 |
* layout, or do nothing. |
|
693 |
* </ol> |
|
694 |
* |
|
695 |
* @param e the change information from the associated document |
|
696 |
* @param a the current allocation of the view |
|
697 |
* @param f the factory to use to rebuild if the view has children |
|
698 |
* @see View#insertUpdate |
|
699 |
*/ |
|
700 |
public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) { |
|
701 |
if (getViewCount() > 0) { |
|
702 |
Element elem = getElement(); |
|
703 |
DocumentEvent.ElementChange ec = e.getChange(elem); |
|
704 |
if (ec != null) { |
|
705 |
if (! updateChildren(ec, e, f)) { |
|
706 |
// don't consider the element changes they |
|
707 |
// are for a view further down. |
|
708 |
ec = null; |
|
709 |
} |
|
710 |
} |
|
711 |
forwardUpdate(ec, e, a, f); |
|
712 |
updateLayout(ec, e, a); |
|
713 |
} |
|
714 |
} |
|
715 |
||
716 |
/** |
|
717 |
* Gives notification that something was removed from the document |
|
718 |
* in a location that this view is responsible for. |
|
719 |
* To reduce the burden to subclasses, this functionality is |
|
720 |
* spread out into the following calls that subclasses can |
|
721 |
* reimplement: |
|
722 |
* <ol> |
|
7959 | 723 |
* <li>{@link #updateChildren updateChildren} is called |
2 | 724 |
* if there were any changes to the element this view is |
725 |
* responsible for. If this view has child views that are |
|
726 |
* represent the child elements, then this method should do |
|
727 |
* whatever is necessary to make sure the child views correctly |
|
728 |
* represent the model. |
|
7959 | 729 |
* <li>{@link #forwardUpdate forwardUpdate} is called |
2 | 730 |
* to forward the DocumentEvent to the appropriate child views. |
7959 | 731 |
* <li>{@link #updateLayout updateLayout} is called to |
2 | 732 |
* give the view a chance to either repair its layout, to reschedule |
733 |
* layout, or do nothing. |
|
734 |
* </ol> |
|
735 |
* |
|
736 |
* @param e the change information from the associated document |
|
737 |
* @param a the current allocation of the view |
|
738 |
* @param f the factory to use to rebuild if the view has children |
|
739 |
* @see View#removeUpdate |
|
740 |
*/ |
|
741 |
public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) { |
|
742 |
if (getViewCount() > 0) { |
|
743 |
Element elem = getElement(); |
|
744 |
DocumentEvent.ElementChange ec = e.getChange(elem); |
|
745 |
if (ec != null) { |
|
746 |
if (! updateChildren(ec, e, f)) { |
|
747 |
// don't consider the element changes they |
|
748 |
// are for a view further down. |
|
749 |
ec = null; |
|
750 |
} |
|
751 |
} |
|
752 |
forwardUpdate(ec, e, a, f); |
|
753 |
updateLayout(ec, e, a); |
|
754 |
} |
|
755 |
} |
|
756 |
||
757 |
/** |
|
758 |
* Gives notification from the document that attributes were changed |
|
759 |
* in a location that this view is responsible for. |
|
760 |
* To reduce the burden to subclasses, this functionality is |
|
761 |
* spread out into the following calls that subclasses can |
|
762 |
* reimplement: |
|
763 |
* <ol> |
|
7959 | 764 |
* <li>{@link #updateChildren updateChildren} is called |
2 | 765 |
* if there were any changes to the element this view is |
766 |
* responsible for. If this view has child views that are |
|
767 |
* represent the child elements, then this method should do |
|
768 |
* whatever is necessary to make sure the child views correctly |
|
769 |
* represent the model. |
|
7959 | 770 |
* <li>{@link #forwardUpdate forwardUpdate} is called |
2 | 771 |
* to forward the DocumentEvent to the appropriate child views. |
7959 | 772 |
* <li>{@link #updateLayout updateLayout} is called to |
2 | 773 |
* give the view a chance to either repair its layout, to reschedule |
774 |
* layout, or do nothing. |
|
775 |
* </ol> |
|
776 |
* |
|
777 |
* @param e the change information from the associated document |
|
778 |
* @param a the current allocation of the view |
|
779 |
* @param f the factory to use to rebuild if the view has children |
|
780 |
* @see View#changedUpdate |
|
781 |
*/ |
|
782 |
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) { |
|
783 |
if (getViewCount() > 0) { |
|
784 |
Element elem = getElement(); |
|
785 |
DocumentEvent.ElementChange ec = e.getChange(elem); |
|
786 |
if (ec != null) { |
|
787 |
if (! updateChildren(ec, e, f)) { |
|
788 |
// don't consider the element changes they |
|
789 |
// are for a view further down. |
|
790 |
ec = null; |
|
791 |
} |
|
792 |
} |
|
793 |
forwardUpdate(ec, e, a, f); |
|
794 |
updateLayout(ec, e, a); |
|
795 |
} |
|
796 |
} |
|
797 |
||
798 |
/** |
|
799 |
* Fetches the model associated with the view. |
|
800 |
* |
|
801 |
* @return the view model, <code>null</code> if none |
|
802 |
* @see View#getDocument |
|
803 |
*/ |
|
804 |
public Document getDocument() { |
|
805 |
return elem.getDocument(); |
|
806 |
} |
|
807 |
||
808 |
/** |
|
809 |
* Fetches the portion of the model for which this view is |
|
810 |
* responsible. |
|
811 |
* |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
812 |
* @return the starting offset into the model >= 0 |
2 | 813 |
* @see View#getStartOffset |
814 |
*/ |
|
815 |
public int getStartOffset() { |
|
816 |
return elem.getStartOffset(); |
|
817 |
} |
|
818 |
||
819 |
/** |
|
820 |
* Fetches the portion of the model for which this view is |
|
821 |
* responsible. |
|
822 |
* |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
823 |
* @return the ending offset into the model >= 0 |
2 | 824 |
* @see View#getEndOffset |
825 |
*/ |
|
826 |
public int getEndOffset() { |
|
827 |
return elem.getEndOffset(); |
|
828 |
} |
|
829 |
||
830 |
/** |
|
831 |
* Fetches the structural portion of the subject that this |
|
832 |
* view is mapped to. The view may not be responsible for the |
|
833 |
* entire portion of the element. |
|
834 |
* |
|
835 |
* @return the subject |
|
836 |
* @see View#getElement |
|
837 |
*/ |
|
838 |
public Element getElement() { |
|
839 |
return elem; |
|
840 |
} |
|
841 |
||
842 |
/** |
|
843 |
* Fetch a <code>Graphics</code> for rendering. |
|
844 |
* This can be used to determine |
|
845 |
* font characteristics, and will be different for a print view |
|
846 |
* than a component view. |
|
847 |
* |
|
848 |
* @return a <code>Graphics</code> object for rendering |
|
849 |
* @since 1.3 |
|
850 |
*/ |
|
851 |
public Graphics getGraphics() { |
|
852 |
// PENDING(prinz) this is a temporary implementation |
|
853 |
Component c = getContainer(); |
|
854 |
return c.getGraphics(); |
|
855 |
} |
|
856 |
||
857 |
/** |
|
858 |
* Fetches the attributes to use when rendering. By default |
|
859 |
* this simply returns the attributes of the associated element. |
|
860 |
* This method should be used rather than using the element |
|
861 |
* directly to obtain access to the attributes to allow |
|
862 |
* view-specific attributes to be mixed in or to allow the |
|
863 |
* view to have view-specific conversion of attributes by |
|
864 |
* subclasses. |
|
865 |
* Each view should document what attributes it recognizes |
|
866 |
* for the purpose of rendering or layout, and should always |
|
867 |
* access them through the <code>AttributeSet</code> returned |
|
868 |
* by this method. |
|
869 |
*/ |
|
870 |
public AttributeSet getAttributes() { |
|
871 |
return elem.getAttributes(); |
|
872 |
} |
|
873 |
||
874 |
/** |
|
875 |
* Tries to break this view on the given axis. This is |
|
876 |
* called by views that try to do formatting of their |
|
877 |
* children. For example, a view of a paragraph will |
|
878 |
* typically try to place its children into row and |
|
879 |
* views representing chunks of text can sometimes be |
|
880 |
* broken down into smaller pieces. |
|
881 |
* <p> |
|
882 |
* This is implemented to return the view itself, which |
|
883 |
* represents the default behavior on not being |
|
884 |
* breakable. If the view does support breaking, the |
|
885 |
* starting offset of the view returned should be the |
|
886 |
* given offset, and the end offset should be less than |
|
887 |
* or equal to the end offset of the view being broken. |
|
888 |
* |
|
889 |
* @param axis may be either <code>View.X_AXIS</code> or |
|
890 |
* <code>View.Y_AXIS</code> |
|
891 |
* @param offset the location in the document model |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
892 |
* that a broken fragment would occupy >= 0. This |
2 | 893 |
* would be the starting offset of the fragment |
894 |
* returned |
|
895 |
* @param pos the position along the axis that the |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
896 |
* broken view would occupy >= 0. This may be useful for |
2 | 897 |
* things like tab calculations |
898 |
* @param len specifies the distance along the axis |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
899 |
* where a potential break is desired >= 0 |
2 | 900 |
* @return the fragment of the view that represents the |
901 |
* given span, if the view can be broken. If the view |
|
902 |
* doesn't support breaking behavior, the view itself is |
|
903 |
* returned. |
|
904 |
* @see ParagraphView |
|
905 |
*/ |
|
906 |
public View breakView(int axis, int offset, float pos, float len) { |
|
907 |
return this; |
|
908 |
} |
|
909 |
||
910 |
/** |
|
911 |
* Creates a view that represents a portion of the element. |
|
912 |
* This is potentially useful during formatting operations |
|
913 |
* for taking measurements of fragments of the view. If |
|
914 |
* the view doesn't support fragmenting (the default), it |
|
915 |
* should return itself. |
|
916 |
* |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
917 |
* @param p0 the starting offset >= 0. This should be a value |
2 | 918 |
* greater or equal to the element starting offset and |
919 |
* less than the element ending offset. |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
920 |
* @param p1 the ending offset > p0. This should be a value |
2 | 921 |
* less than or equal to the elements end offset and |
922 |
* greater than the elements starting offset. |
|
923 |
* @return the view fragment, or itself if the view doesn't |
|
924 |
* support breaking into fragments |
|
925 |
* @see LabelView |
|
926 |
*/ |
|
927 |
public View createFragment(int p0, int p1) { |
|
928 |
return this; |
|
929 |
} |
|
930 |
||
931 |
/** |
|
932 |
* Determines how attractive a break opportunity in |
|
933 |
* this view is. This can be used for determining which |
|
934 |
* view is the most attractive to call <code>breakView</code> |
|
935 |
* on in the process of formatting. A view that represents |
|
936 |
* text that has whitespace in it might be more attractive |
|
937 |
* than a view that has no whitespace, for example. The |
|
938 |
* higher the weight, the more attractive the break. A |
|
939 |
* value equal to or lower than <code>BadBreakWeight</code> |
|
940 |
* should not be considered for a break. A value greater |
|
941 |
* than or equal to <code>ForcedBreakWeight</code> should |
|
942 |
* be broken. |
|
943 |
* <p> |
|
944 |
* This is implemented to provide the default behavior |
|
945 |
* of returning <code>BadBreakWeight</code> unless the length |
|
946 |
* is greater than the length of the view in which case the |
|
947 |
* entire view represents the fragment. Unless a view has |
|
948 |
* been written to support breaking behavior, it is not |
|
949 |
* attractive to try and break the view. An example of |
|
950 |
* a view that does support breaking is <code>LabelView</code>. |
|
951 |
* An example of a view that uses break weight is |
|
952 |
* <code>ParagraphView</code>. |
|
953 |
* |
|
954 |
* @param axis may be either <code>View.X_AXIS</code> or |
|
955 |
* <code>View.Y_AXIS</code> |
|
956 |
* @param pos the potential location of the start of the |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
957 |
* broken view >= 0. This may be useful for calculating tab |
2 | 958 |
* positions |
959 |
* @param len specifies the relative length from <em>pos</em> |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
960 |
* where a potential break is desired >= 0 |
2 | 961 |
* @return the weight, which should be a value between |
962 |
* ForcedBreakWeight and BadBreakWeight |
|
963 |
* @see LabelView |
|
964 |
* @see ParagraphView |
|
965 |
* @see #BadBreakWeight |
|
966 |
* @see #GoodBreakWeight |
|
967 |
* @see #ExcellentBreakWeight |
|
968 |
* @see #ForcedBreakWeight |
|
969 |
*/ |
|
970 |
public int getBreakWeight(int axis, float pos, float len) { |
|
971 |
if (len > getPreferredSpan(axis)) { |
|
972 |
return GoodBreakWeight; |
|
973 |
} |
|
974 |
return BadBreakWeight; |
|
975 |
} |
|
976 |
||
977 |
/** |
|
978 |
* Determines the resizability of the view along the |
|
979 |
* given axis. A value of 0 or less is not resizable. |
|
980 |
* |
|
981 |
* @param axis may be either <code>View.X_AXIS</code> or |
|
982 |
* <code>View.Y_AXIS</code> |
|
983 |
* @return the weight |
|
984 |
*/ |
|
985 |
public int getResizeWeight(int axis) { |
|
986 |
return 0; |
|
987 |
} |
|
988 |
||
989 |
/** |
|
990 |
* Sets the size of the view. This should cause |
|
991 |
* layout of the view along the given axis, if it |
|
992 |
* has any layout duties. |
|
993 |
* |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
994 |
* @param width the width >= 0 |
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
995 |
* @param height the height >= 0 |
2 | 996 |
*/ |
997 |
public void setSize(float width, float height) { |
|
998 |
} |
|
999 |
||
1000 |
/** |
|
1001 |
* Fetches the container hosting the view. This is useful for |
|
1002 |
* things like scheduling a repaint, finding out the host |
|
1003 |
* components font, etc. The default implementation |
|
1004 |
* of this is to forward the query to the parent view. |
|
1005 |
* |
|
1006 |
* @return the container, <code>null</code> if none |
|
1007 |
*/ |
|
1008 |
public Container getContainer() { |
|
1009 |
View v = getParent(); |
|
1010 |
return (v != null) ? v.getContainer() : null; |
|
1011 |
} |
|
1012 |
||
1013 |
/** |
|
1014 |
* Fetches the <code>ViewFactory</code> implementation that is feeding |
|
1015 |
* the view hierarchy. Normally the views are given this |
|
1016 |
* as an argument to updates from the model when they |
|
1017 |
* are most likely to need the factory, but this |
|
1018 |
* method serves to provide it at other times. |
|
1019 |
* |
|
1020 |
* @return the factory, <code>null</code> if none |
|
1021 |
*/ |
|
1022 |
public ViewFactory getViewFactory() { |
|
1023 |
View v = getParent(); |
|
1024 |
return (v != null) ? v.getViewFactory() : null; |
|
1025 |
} |
|
1026 |
||
1027 |
/** |
|
1028 |
* Returns the tooltip text at the specified location. The default |
|
1029 |
* implementation returns the value from the child View identified by |
|
1030 |
* the passed in location. |
|
1031 |
* |
|
1032 |
* @since 1.4 |
|
1033 |
* @see JTextComponent#getToolTipText |
|
1034 |
*/ |
|
1035 |
public String getToolTipText(float x, float y, Shape allocation) { |
|
1036 |
int viewIndex = getViewIndex(x, y, allocation); |
|
1037 |
if (viewIndex >= 0) { |
|
1038 |
allocation = getChildAllocation(viewIndex, allocation); |
|
1039 |
Rectangle rect = (allocation instanceof Rectangle) ? |
|
1040 |
(Rectangle)allocation : allocation.getBounds(); |
|
1041 |
if (rect.contains(x, y)) { |
|
1042 |
return getView(viewIndex).getToolTipText(x, y, allocation); |
|
1043 |
} |
|
1044 |
} |
|
1045 |
return null; |
|
1046 |
} |
|
1047 |
||
1048 |
/** |
|
1049 |
* Returns the child view index representing the given position in |
|
1050 |
* the view. This iterates over all the children returning the |
|
1051 |
* first with a bounds that contains <code>x</code>, <code>y</code>. |
|
1052 |
* |
|
1053 |
* @param x the x coordinate |
|
1054 |
* @param y the y coordinate |
|
1055 |
* @param allocation current allocation of the View. |
|
1056 |
* @return index of the view representing the given location, or |
|
1057 |
* -1 if no view represents that position |
|
1058 |
* @since 1.4 |
|
1059 |
*/ |
|
1060 |
public int getViewIndex(float x, float y, Shape allocation) { |
|
1061 |
for (int counter = getViewCount() - 1; counter >= 0; counter--) { |
|
1062 |
Shape childAllocation = getChildAllocation(counter, allocation); |
|
1063 |
||
1064 |
if (childAllocation != null) { |
|
1065 |
Rectangle rect = (childAllocation instanceof Rectangle) ? |
|
1066 |
(Rectangle)childAllocation : childAllocation.getBounds(); |
|
1067 |
||
1068 |
if (rect.contains(x, y)) { |
|
1069 |
return counter; |
|
1070 |
} |
|
1071 |
} |
|
1072 |
} |
|
1073 |
return -1; |
|
1074 |
} |
|
1075 |
||
1076 |
/** |
|
1077 |
* Updates the child views in response to receiving notification |
|
1078 |
* that the model changed, and there is change record for the |
|
1079 |
* element this view is responsible for. This is implemented |
|
1080 |
* to assume the child views are directly responsible for the |
|
1081 |
* child elements of the element this view represents. The |
|
1082 |
* <code>ViewFactory</code> is used to create child views for each element |
|
1083 |
* specified as added in the <code>ElementChange</code>, starting at the |
|
1084 |
* index specified in the given <code>ElementChange</code>. The number of |
|
1085 |
* child views representing the removed elements specified are |
|
1086 |
* removed. |
|
1087 |
* |
|
1088 |
* @param ec the change information for the element this view |
|
1089 |
* is responsible for. This should not be <code>null</code> if |
|
1090 |
* this method gets called |
|
1091 |
* @param e the change information from the associated document |
|
1092 |
* @param f the factory to use to build child views |
|
1093 |
* @return whether or not the child views represent the |
|
1094 |
* child elements of the element this view is responsible |
|
1095 |
* for. Some views create children that represent a portion |
|
1096 |
* of the element they are responsible for, and should return |
|
1097 |
* false. This information is used to determine if views |
|
1098 |
* in the range of the added elements should be forwarded to |
|
1099 |
* or not |
|
1100 |
* @see #insertUpdate |
|
1101 |
* @see #removeUpdate |
|
1102 |
* @see #changedUpdate |
|
1103 |
* @since 1.3 |
|
1104 |
*/ |
|
1105 |
protected boolean updateChildren(DocumentEvent.ElementChange ec, |
|
1106 |
DocumentEvent e, ViewFactory f) { |
|
1107 |
Element[] removedElems = ec.getChildrenRemoved(); |
|
1108 |
Element[] addedElems = ec.getChildrenAdded(); |
|
1109 |
View[] added = null; |
|
1110 |
if (addedElems != null) { |
|
1111 |
added = new View[addedElems.length]; |
|
1112 |
for (int i = 0; i < addedElems.length; i++) { |
|
1113 |
added[i] = f.create(addedElems[i]); |
|
1114 |
} |
|
1115 |
} |
|
1116 |
int nremoved = 0; |
|
1117 |
int index = ec.getIndex(); |
|
1118 |
if (removedElems != null) { |
|
1119 |
nremoved = removedElems.length; |
|
1120 |
} |
|
1121 |
replace(index, nremoved, added); |
|
1122 |
return true; |
|
1123 |
} |
|
1124 |
||
1125 |
/** |
|
1126 |
* Forwards the given <code>DocumentEvent</code> to the child views |
|
1127 |
* that need to be notified of the change to the model. |
|
1128 |
* If there were changes to the element this view is |
|
1129 |
* responsible for, that should be considered when |
|
1130 |
* forwarding (i.e. new child views should not get |
|
1131 |
* notified). |
|
1132 |
* |
|
1133 |
* @param ec changes to the element this view is responsible |
|
1134 |
* for (may be <code>null</code> if there were no changes). |
|
1135 |
* @param e the change information from the associated document |
|
1136 |
* @param a the current allocation of the view |
|
1137 |
* @param f the factory to use to rebuild if the view has children |
|
1138 |
* @see #insertUpdate |
|
1139 |
* @see #removeUpdate |
|
1140 |
* @see #changedUpdate |
|
1141 |
* @since 1.3 |
|
1142 |
*/ |
|
1143 |
protected void forwardUpdate(DocumentEvent.ElementChange ec, |
|
1144 |
DocumentEvent e, Shape a, ViewFactory f) { |
|
20126 | 1145 |
calculateUpdateIndexes(e); |
1146 |
||
1147 |
int hole0 = lastUpdateIndex + 1; |
|
2 | 1148 |
int hole1 = hole0; |
1149 |
Element[] addedElems = (ec != null) ? ec.getChildrenAdded() : null; |
|
1150 |
if ((addedElems != null) && (addedElems.length > 0)) { |
|
1151 |
hole0 = ec.getIndex(); |
|
1152 |
hole1 = hole0 + addedElems.length - 1; |
|
1153 |
} |
|
1154 |
||
1155 |
// forward to any view not in the forwarding hole |
|
1156 |
// formed by added elements (i.e. they will be updated |
|
1157 |
// by initialization. |
|
20126 | 1158 |
for (int i = firstUpdateIndex; i <= lastUpdateIndex; i++) { |
2 | 1159 |
if (! ((i >= hole0) && (i <= hole1))) { |
20126 | 1160 |
View v = getView(i); |
2 | 1161 |
if (v != null) { |
1162 |
Shape childAlloc = getChildAllocation(i, a); |
|
1163 |
forwardUpdateToView(v, e, childAlloc, f); |
|
1164 |
} |
|
1165 |
} |
|
1166 |
} |
|
1167 |
} |
|
1168 |
||
1169 |
/** |
|
20126 | 1170 |
* Calculates the first and the last indexes of the child views |
1171 |
* that need to be notified of the change to the model. |
|
1172 |
* @param e the change information from the associated document |
|
1173 |
*/ |
|
1174 |
void calculateUpdateIndexes(DocumentEvent e) { |
|
1175 |
int pos = e.getOffset(); |
|
1176 |
firstUpdateIndex = getViewIndex(pos, Position.Bias.Forward); |
|
1177 |
if (firstUpdateIndex == -1 && e.getType() == DocumentEvent.EventType.REMOVE && |
|
1178 |
pos >= getEndOffset()) { |
|
1179 |
// Event beyond our offsets. We may have represented this, that is |
|
1180 |
// the remove may have removed one of our child Elements that |
|
1181 |
// represented this, so, we should forward to last element. |
|
1182 |
firstUpdateIndex = getViewCount() - 1; |
|
1183 |
} |
|
1184 |
lastUpdateIndex = firstUpdateIndex; |
|
1185 |
View v = (firstUpdateIndex >= 0) ? getView(firstUpdateIndex) : null; |
|
1186 |
if (v != null) { |
|
1187 |
if ((v.getStartOffset() == pos) && (pos > 0)) { |
|
1188 |
// If v is at a boundary, forward the event to the previous |
|
1189 |
// view too. |
|
1190 |
firstUpdateIndex = Math.max(firstUpdateIndex - 1, 0); |
|
1191 |
} |
|
1192 |
} |
|
1193 |
if (e.getType() != DocumentEvent.EventType.REMOVE) { |
|
1194 |
lastUpdateIndex = getViewIndex(pos + e.getLength(), Position.Bias.Forward); |
|
1195 |
if (lastUpdateIndex < 0) { |
|
1196 |
lastUpdateIndex = getViewCount() - 1; |
|
1197 |
} |
|
1198 |
} |
|
1199 |
firstUpdateIndex = Math.max(firstUpdateIndex, 0); |
|
1200 |
} |
|
1201 |
||
1202 |
/** |
|
2 | 1203 |
* Forwards the <code>DocumentEvent</code> to the give child view. This |
1204 |
* simply messages the view with a call to <code>insertUpdate</code>, |
|
1205 |
* <code>removeUpdate</code>, or <code>changedUpdate</code> depending |
|
1206 |
* upon the type of the event. This is called by |
|
7959 | 1207 |
* {@link #forwardUpdate forwardUpdate} to forward |
2 | 1208 |
* the event to children that need it. |
1209 |
* |
|
1210 |
* @param v the child view to forward the event to |
|
1211 |
* @param e the change information from the associated document |
|
1212 |
* @param a the current allocation of the view |
|
1213 |
* @param f the factory to use to rebuild if the view has children |
|
1214 |
* @see #forwardUpdate |
|
1215 |
* @since 1.3 |
|
1216 |
*/ |
|
1217 |
protected void forwardUpdateToView(View v, DocumentEvent e, |
|
1218 |
Shape a, ViewFactory f) { |
|
1219 |
DocumentEvent.EventType type = e.getType(); |
|
1220 |
if (type == DocumentEvent.EventType.INSERT) { |
|
1221 |
v.insertUpdate(e, a, f); |
|
1222 |
} else if (type == DocumentEvent.EventType.REMOVE) { |
|
1223 |
v.removeUpdate(e, a, f); |
|
1224 |
} else { |
|
1225 |
v.changedUpdate(e, a, f); |
|
1226 |
} |
|
1227 |
} |
|
1228 |
||
1229 |
/** |
|
1230 |
* Updates the layout in response to receiving notification of |
|
1231 |
* change from the model. This is implemented to call |
|
1232 |
* <code>preferenceChanged</code> to reschedule a new layout |
|
1233 |
* if the <code>ElementChange</code> record is not <code>null</code>. |
|
1234 |
* |
|
1235 |
* @param ec changes to the element this view is responsible |
|
1236 |
* for (may be <code>null</code> if there were no changes) |
|
1237 |
* @param e the change information from the associated document |
|
1238 |
* @param a the current allocation of the view |
|
1239 |
* @see #insertUpdate |
|
1240 |
* @see #removeUpdate |
|
1241 |
* @see #changedUpdate |
|
1242 |
* @since 1.3 |
|
1243 |
*/ |
|
1244 |
protected void updateLayout(DocumentEvent.ElementChange ec, |
|
1245 |
DocumentEvent e, Shape a) { |
|
1246 |
if ((ec != null) && (a != null)) { |
|
1247 |
// should damage more intelligently |
|
1248 |
preferenceChanged(null, true, true); |
|
1249 |
Container host = getContainer(); |
|
1250 |
if (host != null) { |
|
1251 |
host.repaint(); |
|
1252 |
} |
|
1253 |
} |
|
1254 |
} |
|
1255 |
||
1256 |
/** |
|
1257 |
* The weight to indicate a view is a bad break |
|
1258 |
* opportunity for the purpose of formatting. This |
|
1259 |
* value indicates that no attempt should be made to |
|
1260 |
* break the view into fragments as the view has |
|
1261 |
* not been written to support fragmenting. |
|
1262 |
* |
|
1263 |
* @see #getBreakWeight |
|
1264 |
* @see #GoodBreakWeight |
|
1265 |
* @see #ExcellentBreakWeight |
|
1266 |
* @see #ForcedBreakWeight |
|
1267 |
*/ |
|
1268 |
public static final int BadBreakWeight = 0; |
|
1269 |
||
1270 |
/** |
|
1271 |
* The weight to indicate a view supports breaking, |
|
1272 |
* but better opportunities probably exist. |
|
1273 |
* |
|
1274 |
* @see #getBreakWeight |
|
1275 |
* @see #BadBreakWeight |
|
1276 |
* @see #ExcellentBreakWeight |
|
1277 |
* @see #ForcedBreakWeight |
|
1278 |
*/ |
|
1279 |
public static final int GoodBreakWeight = 1000; |
|
1280 |
||
1281 |
/** |
|
1282 |
* The weight to indicate a view supports breaking, |
|
1283 |
* and this represents a very attractive place to |
|
1284 |
* break. |
|
1285 |
* |
|
1286 |
* @see #getBreakWeight |
|
1287 |
* @see #BadBreakWeight |
|
1288 |
* @see #GoodBreakWeight |
|
1289 |
* @see #ForcedBreakWeight |
|
1290 |
*/ |
|
1291 |
public static final int ExcellentBreakWeight = 2000; |
|
1292 |
||
1293 |
/** |
|
1294 |
* The weight to indicate a view supports breaking, |
|
1295 |
* and must be broken to be represented properly |
|
1296 |
* when placed in a view that formats its children |
|
1297 |
* by breaking them. |
|
1298 |
* |
|
1299 |
* @see #getBreakWeight |
|
1300 |
* @see #BadBreakWeight |
|
1301 |
* @see #GoodBreakWeight |
|
1302 |
* @see #ExcellentBreakWeight |
|
1303 |
*/ |
|
1304 |
public static final int ForcedBreakWeight = 3000; |
|
1305 |
||
1306 |
/** |
|
1307 |
* Axis for format/break operations. |
|
1308 |
*/ |
|
1309 |
public static final int X_AXIS = HORIZONTAL; |
|
1310 |
||
1311 |
/** |
|
1312 |
* Axis for format/break operations. |
|
1313 |
*/ |
|
1314 |
public static final int Y_AXIS = VERTICAL; |
|
1315 |
||
1316 |
/** |
|
1317 |
* Provides a mapping from the document model coordinate space |
|
1318 |
* to the coordinate space of the view mapped to it. This is |
|
1319 |
* implemented to default the bias to <code>Position.Bias.Forward</code> |
|
1320 |
* which was previously implied. |
|
1321 |
* |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
1322 |
* @param pos the position to convert >= 0 |
2 | 1323 |
* @param a the allocated region in which to render |
1324 |
* @return the bounding box of the given position is returned |
|
1325 |
* @exception BadLocationException if the given position does |
|
1326 |
* not represent a valid location in the associated document |
|
1327 |
* @see View#modelToView |
|
1328 |
* @deprecated |
|
1329 |
*/ |
|
1330 |
@Deprecated |
|
1331 |
public Shape modelToView(int pos, Shape a) throws BadLocationException { |
|
1332 |
return modelToView(pos, a, Position.Bias.Forward); |
|
1333 |
} |
|
1334 |
||
1335 |
||
1336 |
/** |
|
1337 |
* Provides a mapping from the view coordinate space to the logical |
|
1338 |
* coordinate space of the model. |
|
1339 |
* |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
1340 |
* @param x the X coordinate >= 0 |
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
1341 |
* @param y the Y coordinate >= 0 |
2 | 1342 |
* @param a the allocated region in which to render |
1343 |
* @return the location within the model that best represents the |
|
20428
929cd48fca8a
8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents:
20158
diff
changeset
|
1344 |
* given point in the view >= 0 |
2 | 1345 |
* @see View#viewToModel |
1346 |
* @deprecated |
|
1347 |
*/ |
|
1348 |
@Deprecated |
|
1349 |
public int viewToModel(float x, float y, Shape a) { |
|
1350 |
sharedBiasReturn[0] = Position.Bias.Forward; |
|
1351 |
return viewToModel(x, y, a, sharedBiasReturn); |
|
1352 |
} |
|
1353 |
||
1354 |
// static argument available for viewToModel calls since only |
|
1355 |
// one thread at a time may call this method. |
|
1356 |
static final Position.Bias[] sharedBiasReturn = new Position.Bias[1]; |
|
1357 |
||
1358 |
private View parent; |
|
1359 |
private Element elem; |
|
1360 |
||
20126 | 1361 |
/** |
1362 |
* The index of the first child view to be notified. |
|
1363 |
*/ |
|
1364 |
int firstUpdateIndex; |
|
1365 |
||
1366 |
/** |
|
1367 |
* The index of the last child view to be notified. |
|
1368 |
*/ |
|
1369 |
int lastUpdateIndex; |
|
1370 |
||
2 | 1371 |
}; |