54 * Construct a box view that does asynchronous layout.
55 *
56 * @param elem the element of the model to represent
57 * @param axis the axis to tile along. This can be
58 * either X_AXIS or Y_AXIS.
59 */
60 public AsyncBoxView(Element elem, int axis) {
61 super(elem);
62 stats = new ArrayList<ChildState>();
63 this.axis = axis;
64 locator = new ChildLocator();
65 flushTask = new FlushTask();
66 minorSpan = Short.MAX_VALUE;
67 estimatedMajorSpan = false;
68 }
69
70 /**
71 * Fetch the major axis (the axis the children
72 * are tiled along). This will have a value of
73 * either X_AXIS or Y_AXIS.
74 */
75 public int getMajorAxis() {
76 return axis;
77 }
78
79 /**
80 * Fetch the minor axis (the axis orthogonal
81 * to the tiled axis). This will have a value of
82 * either X_AXIS or Y_AXIS.
83 */
84 public int getMinorAxis() {
85 return (axis == X_AXIS) ? Y_AXIS : X_AXIS;
86 }
87
88 /**
89 * Get the top part of the margin around the view.
90 */
91 public float getTopInset() {
92 return topInset;
93 }
94
95 /**
96 * Set the top part of the margin around the view.
97 *
98 * @param i the value of the inset
99 */
100 public void setTopInset(float i) {
101 topInset = i;
102 }
103
104 /**
105 * Get the bottom part of the margin around the view.
106 */
107 public float getBottomInset() {
108 return bottomInset;
109 }
110
111 /**
112 * Set the bottom part of the margin around the view.
113 *
114 * @param i the value of the inset
115 */
116 public void setBottomInset(float i) {
117 bottomInset = i;
118 }
119
120 /**
121 * Get the left part of the margin around the view.
122 */
123 public float getLeftInset() {
124 return leftInset;
125 }
126
127 /**
128 * Set the left part of the margin around the view.
129 *
130 * @param i the value of the inset
131 */
132 public void setLeftInset(float i) {
133 leftInset = i;
134 }
135
136 /**
137 * Get the right part of the margin around the view.
138 */
139 public float getRightInset() {
140 return rightInset;
141 }
142
143 /**
144 * Set the right part of the margin around the view.
145 *
146 * @param i the value of the inset
147 */
148 public void setRightInset(float i) {
149 rightInset = i;
150 }
151
152 /**
153 * Fetch the span along an axis that is taken up by the insets.
154 *
155 * @param axis the axis to determine the total insets along,
156 * either X_AXIS or Y_AXIS.
157 * @since 1.4
158 */
159 protected float getInsetSpan(int axis) {
160 float margin = (axis == X_AXIS) ?
161 getLeftInset() + getRightInset() : getTopInset() + getBottomInset();
162 return margin;
163 }
164
165 /**
166 * Set the estimatedMajorSpan property that determines if the
167 * major span should be treated as being estimated. If this
168 * property is true, the value of setSize along the major axis
169 * will change the requirements along the major axis and incremental
170 * changes will be ignored until all of the children have been updated
171 * (which will cause the property to automatically be set to false).
172 * If the property is false the value of the majorSpan will be
173 * considered to be accurate and incremental changes will be
174 * added into the total as they are calculated.
175 *
176 * @since 1.4
177 */
178 protected void setEstimatedMajorSpan(boolean isEstimated) {
179 estimatedMajorSpan = isEstimated;
180 }
181
182 /**
183 * Is the major span currently estimated?
184 *
185 * @since 1.4
186 */
187 protected boolean getEstimatedMajorSpan() {
188 return estimatedMajorSpan;
189 }
190
191 /**
192 * Fetch the object representing the layout state of
193 * of the child at the given index.
194 *
195 * @param index the child index. This should be a
196 * value >= 0 and < getViewCount().
197 */
198 protected ChildState getChildState(int index) {
199 synchronized(stats) {
200 if ((index >= 0) && (index < stats.size())) {
201 return stats.get(index);
202 }
203 return null;
204 }
205 }
206
207 /**
208 * Fetch the queue to use for layout.
209 */
210 protected LayoutQueue getLayoutQueue() {
211 return LayoutQueue.getDefaultQueue();
212 }
213
214 /**
215 * New ChildState records are created through
216 * this method to allow subclasses the extend
217 * the ChildState records to do/hold more
218 */
219 protected ChildState createChildState(View v) {
220 return new ChildState(v);
221 }
222
223 /**
224 * Requirements changed along the major axis.
225 * This is called by the thread doing layout for
226 * the given ChildState object when it has completed
227 * fetching the child views new preferences.
228 * Typically this would be the layout thread, but
229 * might be the event thread if it is trying to update
230 * something immediately (such as to perform a
231 * model/view translation).
232 * <p>
233 * This is implemented to mark the major axis as having
234 * changed so that a future check to see if the requirements
235 * need to be published to the parent view will consider
236 * the major axis. If the span along the major axis is
237 * not estimated, it is updated by the given delta to reflect
238 * the incremental change. The delta is ignored if the
239 * major span is estimated.
240 */
241 protected synchronized void majorRequirementChange(ChildState cs, float delta) {
242 if (estimatedMajorSpan == false) {
243 majorSpan += delta;
244 }
245 majorChanged = true;
246 }
247
248 /**
249 * Requirements changed along the minor axis.
250 * This is called by the thread doing layout for
251 * the given ChildState object when it has completed
252 * fetching the child views new preferences.
253 * Typically this would be the layout thread, but
254 * might be the GUI thread if it is trying to update
255 * something immediately (such as to perform a
256 * model/view translation).
257 */
258 protected synchronized void minorRequirementChange(ChildState cs) {
259 minorChanged = true;
260 }
261
262 /**
263 * Publish the changes in preferences upward to the parent
264 * view. This is normally called by the layout thread.
265 */
266 protected void flushRequirementChanges() {
267 AbstractDocument doc = (AbstractDocument) getDocument();
268 try {
269 doc.readLock();
270
271 View parent = null;
272 boolean horizontal = false;
273 boolean vertical = false;
274
275 synchronized(this) {
276 // perform tasks that iterate over the children while
401 * @see #setParent
402 */
403 protected void loadChildren(ViewFactory f) {
404 Element e = getElement();
405 int n = e.getElementCount();
406 if (n > 0) {
407 View[] added = new View[n];
408 for (int i = 0; i < n; i++) {
409 added[i] = f.create(e.getElement(i));
410 }
411 replace(0, 0, added);
412 }
413 }
414
415 /**
416 * Fetches the child view index representing the given position in
417 * the model. This is implemented to fetch the view in the case
418 * where there is a child view for each child element.
419 *
420 * @param pos the position >= 0
421 * @return index of the view representing the given position, or
422 * -1 if no view represents that position
423 */
424 protected synchronized int getViewIndexAtPosition(int pos, Position.Bias b) {
425 boolean isBackward = (b == Position.Bias.Backward);
426 pos = (isBackward) ? Math.max(0, pos - 1) : pos;
427 Element elem = getElement();
428 return elem.getElementIndex(pos);
429 }
430
431 /**
432 * Update the layout in response to receiving notification of
433 * change from the model. This is implemented to note the
434 * change on the ChildLocator so that offsets of the children
435 * will be correctly computed.
436 *
437 * @param ec changes to the element this view is responsible
438 * for (may be null if there were no changes).
439 * @param e the change information from the associated document
440 * @param a the current allocation of the view
929 * needs to remain fairly stable until the layout thread
930 * decides to publish an update to the parent.
931 * @since 1.3
932 */
933 public class ChildLocator {
934
935 /**
936 * construct a child locator.
937 */
938 public ChildLocator() {
939 lastAlloc = new Rectangle();
940 childAlloc = new Rectangle();
941 }
942
943 /**
944 * Notification that a child changed. This can effect
945 * whether or not new offset calculations are needed.
946 * This is called by a ChildState object that has
947 * changed it's major span. This can therefore be
948 * called by multiple threads.
949 */
950 public synchronized void childChanged(ChildState cs) {
951 if (lastValidOffset == null) {
952 lastValidOffset = cs;
953 } else if (cs.getChildView().getStartOffset() <
954 lastValidOffset.getChildView().getStartOffset()) {
955 lastValidOffset = cs;
956 }
957 }
958
959 /**
960 * Paint the children that intersect the clip area.
961 */
962 public synchronized void paintChildren(Graphics g) {
963 Rectangle clip = g.getClipBounds();
964 float targetOffset = (axis == X_AXIS) ?
965 clip.x - lastAlloc.x : clip.y - lastAlloc.y;
966 int index = getViewIndexAtVisualOffset(targetOffset);
967 int n = getViewCount();
968 float offs = getChildState(index).getMajorOffset();
969 for (int i = index; i < n; i++) {
970 ChildState cs = getChildState(i);
971 cs.setMajorOffset(offs);
972 Shape ca = getChildAllocation(i);
973 if (intersectsClip(ca, clip)) {
974 synchronized (cs) {
975 View v = cs.getChildView();
976 v.paint(g, ca);
977 }
978 } else {
979 // done painting intersection
980 break;
981 }
982 offs += cs.getMajorSpan();
983 }
984 }
985
986 /**
987 * Fetch the allocation to use for a child view.
988 * This will update the offsets for all children
989 * not yet updated before the given index.
990 */
991 public synchronized Shape getChildAllocation(int index, Shape a) {
992 if (a == null) {
993 return null;
994 }
995 setAllocation(a);
996 ChildState cs = getChildState(index);
997 if (lastValidOffset == null) {
998 lastValidOffset = getChildState(0);
999 }
1000 if (cs.getChildView().getStartOffset() >
1001 lastValidOffset.getChildView().getStartOffset()) {
1002 // offsets need to be updated
1003 updateChildOffsetsToIndex(index);
1004 }
1005 Shape ca = getChildAllocation(index);
1006 return ca;
1007 }
1008
1009 /**
1014 * on this object, and would typically be followed
1015 * with one or more calls to getChildAllocation that
1016 * should also be in the synchronized block.
1017 *
1018 * @param x the X coordinate >= 0
1019 * @param y the Y coordinate >= 0
1020 * @param a the allocation to the View
1021 * @return the nearest child index
1022 */
1023 public int getViewIndexAtPoint(float x, float y, Shape a) {
1024 setAllocation(a);
1025 float targetOffset = (axis == X_AXIS) ? x - lastAlloc.x : y - lastAlloc.y;
1026 int index = getViewIndexAtVisualOffset(targetOffset);
1027 return index;
1028 }
1029
1030 /**
1031 * Fetch the allocation to use for a child view.
1032 * <em>This does not update the offsets in the ChildState
1033 * records.</em>
1034 */
1035 protected Shape getChildAllocation(int index) {
1036 ChildState cs = getChildState(index);
1037 if (! cs.isLayoutValid()) {
1038 cs.run();
1039 }
1040 if (axis == X_AXIS) {
1041 childAlloc.x = lastAlloc.x + (int) cs.getMajorOffset();
1042 childAlloc.y = lastAlloc.y + (int) cs.getMinorOffset();
1043 childAlloc.width = (int) cs.getMajorSpan();
1044 childAlloc.height = (int) cs.getMinorSpan();
1045 } else {
1046 childAlloc.y = lastAlloc.y + (int) cs.getMajorOffset();
1047 childAlloc.x = lastAlloc.x + (int) cs.getMinorOffset();
1048 childAlloc.height = (int) cs.getMajorSpan();
1049 childAlloc.width = (int) cs.getMinorSpan();
1050 }
1051 childAlloc.x += (int)getLeftInset();
1052 childAlloc.y += (int)getRightInset();
1053 return childAlloc;
1054 }
1055
1056 /**
1057 * Copy the currently allocated shape into the Rectangle
1058 * used to store the current allocation. This would be
1059 * a floating point rectangle in a Java2D-specific implementation.
1060 */
1061 protected void setAllocation(Shape a) {
1062 if (a instanceof Rectangle) {
1063 lastAlloc.setBounds((Rectangle) a);
1064 } else {
1065 lastAlloc.setBounds(a.getBounds());
1066 }
1067 setSize(lastAlloc.width, lastAlloc.height);
1068 }
1069
1070 /**
1071 * Locate the view responsible for an offset into the box
1072 * along the major axis. Make sure that offsets are set
1073 * on the ChildState objects up to the given target span
1074 * past the desired offset.
1075 *
1076 * @return index of the view representing the given visual
1077 * location (targetOffset), or -1 if no view represents
1078 * that location
1079 */
1080 protected int getViewIndexAtVisualOffset(float targetOffset) {
1081 int n = getViewCount();
1082 if (n > 0) {
1083 boolean lastValid = (lastValidOffset != null);
1084
1085 if (lastValidOffset == null) {
1086 lastValidOffset = getChildState(0);
1087 }
1088 if (targetOffset > majorSpan) {
1089 // should only get here on the first time display.
1090 if (!lastValid) {
1091 return 0;
1092 }
1093 int pos = lastValidOffset.getChildView().getStartOffset();
1094 int index = getViewIndex(pos, Position.Bias.Forward);
1184 }
1185
1186 /**
1187 * A record representing the layout state of a
1188 * child view. It is runnable as a task on another
1189 * thread. All access to the child view that is
1190 * based upon a read-lock on the model should synchronize
1191 * on this object (i.e. The layout thread and the GUI
1192 * thread can both have a read lock on the model at the
1193 * same time and are not protected from each other).
1194 * Access to a child view hierarchy is serialized via
1195 * synchronization on the ChildState instance.
1196 * @since 1.3
1197 */
1198 public class ChildState implements Runnable {
1199
1200 /**
1201 * Construct a child status. This needs to start
1202 * out as fairly large so we don't falsely begin with
1203 * the idea that all of the children are visible.
1204 * @since 1.4
1205 */
1206 public ChildState(View v) {
1207 child = v;
1208 minorValid = false;
1209 majorValid = false;
1210 childSizeValid = false;
1211 child.setParent(AsyncBoxView.this);
1212 }
1213
1214 /**
1215 * Fetch the child view this record represents
1216 */
1217 public View getChildView() {
1218 return child;
1219 }
1220
1221 /**
1222 * Update the child state. This should be
1223 * called by the thread that desires to spend
1224 * time updating the child state (intended to
1225 * be the layout thread).
1226 * <p>
1227 * This acquires a read lock on the associated
1228 * document for the duration of the update to
1229 * ensure the model is not changed while it is
1230 * operating. The first thing to do would be
1231 * to see if any work actually needs to be done.
1232 * The following could have conceivably happened
1233 * while the state was waiting to be updated:
1234 * <ol>
1235 * <li>The child may have been removed from the
1305 synchronized(this) {
1306 if (! childSizeValid) {
1307 float w;
1308 float h;
1309 if (axis == X_AXIS) {
1310 w = span;
1311 h = getMinorSpan();
1312 } else {
1313 w = getMinorSpan();
1314 h = span;
1315 }
1316 childSizeValid = true;
1317 child.setSize(w, h);
1318 }
1319 }
1320
1321 }
1322
1323 /**
1324 * What is the span along the minor axis.
1325 */
1326 public float getMinorSpan() {
1327 if (max < minorSpan) {
1328 return max;
1329 }
1330 // make it the target width, or as small as it can get.
1331 return Math.max(min, minorSpan);
1332 }
1333
1334 /**
1335 * What is the offset along the minor axis
1336 */
1337 public float getMinorOffset() {
1338 if (max < minorSpan) {
1339 // can't make the child this wide, align it
1340 float align = child.getAlignment(getMinorAxis());
1341 return ((minorSpan - max) * align);
1342 }
1343 return 0f;
1344 }
1345
1346 /**
1347 * What is the span along the major axis.
1348 */
1349 public float getMajorSpan() {
1350 return span;
1351 }
1352
1353 /**
1354 * Get the offset along the major axis
1355 */
1356 public float getMajorOffset() {
1357 return offset;
1358 }
1359
1360 /**
1361 * This method should only be called by the ChildLocator,
1362 * it is simply a convenient place to hold the cached
1363 * location.
1364 */
1365 public void setMajorOffset(float offs) {
1366 offset = offs;
1367 }
1368
1369 /**
1370 * Mark preferences changed for this child.
1371 *
1372 * @param width true if the width preference has changed
1373 * @param height true if the height preference has changed
1374 * @see javax.swing.JComponent#revalidate
1375 */
1376 public void preferenceChanged(boolean width, boolean height) {
1377 if (axis == X_AXIS) {
1378 if (width) {
1379 majorValid = false;
1380 }
1381 if (height) {
1382 minorValid = false;
1383 }
1384 } else {
1385 if (width) {
1386 minorValid = false;
1387 }
1388 if (height) {
1389 majorValid = false;
1390 }
1391 }
1392 childSizeValid = false;
1393 }
1394
1395 /**
1396 * Has the child view been laid out.
1397 */
1398 public boolean isLayoutValid() {
1399 return (minorValid && majorValid && childSizeValid);
1400 }
1401
1402 // minor axis
1403 private float min;
1404 private float pref;
1405 private float max;
1406 private boolean minorValid;
1407
1408 // major axis
1409 private float span;
1410 private float offset;
1411 private boolean majorValid;
1412
1413 private View child;
1414 private boolean childSizeValid;
1415 }
1416
|
54 * Construct a box view that does asynchronous layout.
55 *
56 * @param elem the element of the model to represent
57 * @param axis the axis to tile along. This can be
58 * either X_AXIS or Y_AXIS.
59 */
60 public AsyncBoxView(Element elem, int axis) {
61 super(elem);
62 stats = new ArrayList<ChildState>();
63 this.axis = axis;
64 locator = new ChildLocator();
65 flushTask = new FlushTask();
66 minorSpan = Short.MAX_VALUE;
67 estimatedMajorSpan = false;
68 }
69
70 /**
71 * Fetch the major axis (the axis the children
72 * are tiled along). This will have a value of
73 * either X_AXIS or Y_AXIS.
74 * @return the major axis
75 */
76 public int getMajorAxis() {
77 return axis;
78 }
79
80 /**
81 * Fetch the minor axis (the axis orthogonal
82 * to the tiled axis). This will have a value of
83 * either X_AXIS or Y_AXIS.
84 * @return the minor axis
85 */
86 public int getMinorAxis() {
87 return (axis == X_AXIS) ? Y_AXIS : X_AXIS;
88 }
89
90 /**
91 * Get the top part of the margin around the view.
92 * @return the top part of the margin around the view
93 */
94 public float getTopInset() {
95 return topInset;
96 }
97
98 /**
99 * Set the top part of the margin around the view.
100 *
101 * @param i the value of the inset
102 */
103 public void setTopInset(float i) {
104 topInset = i;
105 }
106
107 /**
108 * Get the bottom part of the margin around the view.
109 * @return the bottom part of the margin around the view
110 */
111 public float getBottomInset() {
112 return bottomInset;
113 }
114
115 /**
116 * Set the bottom part of the margin around the view.
117 *
118 * @param i the value of the inset
119 */
120 public void setBottomInset(float i) {
121 bottomInset = i;
122 }
123
124 /**
125 * Get the left part of the margin around the view.
126 * @return the left part of the margin around the view
127 */
128 public float getLeftInset() {
129 return leftInset;
130 }
131
132 /**
133 * Set the left part of the margin around the view.
134 *
135 * @param i the value of the inset
136 */
137 public void setLeftInset(float i) {
138 leftInset = i;
139 }
140
141 /**
142 * Get the right part of the margin around the view.
143 * @return the right part of the margin around the view
144 */
145 public float getRightInset() {
146 return rightInset;
147 }
148
149 /**
150 * Set the right part of the margin around the view.
151 *
152 * @param i the value of the inset
153 */
154 public void setRightInset(float i) {
155 rightInset = i;
156 }
157
158 /**
159 * Fetch the span along an axis that is taken up by the insets.
160 * @return the span along an axis that is taken up by the insets
161 *
162 * @param axis the axis to determine the total insets along,
163 * either X_AXIS or Y_AXIS.
164 * @since 1.4
165 */
166 protected float getInsetSpan(int axis) {
167 float margin = (axis == X_AXIS) ?
168 getLeftInset() + getRightInset() : getTopInset() + getBottomInset();
169 return margin;
170 }
171
172 /**
173 * Set the estimatedMajorSpan property that determines if the
174 * major span should be treated as being estimated. If this
175 * property is true, the value of setSize along the major axis
176 * will change the requirements along the major axis and incremental
177 * changes will be ignored until all of the children have been updated
178 * (which will cause the property to automatically be set to false).
179 * If the property is false the value of the majorSpan will be
180 * considered to be accurate and incremental changes will be
181 * added into the total as they are calculated.
182 *
183 * @param isEstimated new value for the estimatedMajorSpan property
184 * @since 1.4
185 */
186 protected void setEstimatedMajorSpan(boolean isEstimated) {
187 estimatedMajorSpan = isEstimated;
188 }
189
190 /**
191 * Is the major span currently estimated?
192 * @return whether or not the major span currently estimated
193 *
194 * @since 1.4
195 */
196 protected boolean getEstimatedMajorSpan() {
197 return estimatedMajorSpan;
198 }
199
200 /**
201 * Fetch the object representing the layout state of
202 * of the child at the given index.
203 * @return the object representing the layout state of
204 * of the child at the given index
205 *
206 * @param index the child index. This should be a
207 * value >= 0 and < getViewCount().
208 */
209 protected ChildState getChildState(int index) {
210 synchronized(stats) {
211 if ((index >= 0) && (index < stats.size())) {
212 return stats.get(index);
213 }
214 return null;
215 }
216 }
217
218 /**
219 * Fetch the queue to use for layout.
220 * @return the queue to use for layout
221 */
222 protected LayoutQueue getLayoutQueue() {
223 return LayoutQueue.getDefaultQueue();
224 }
225
226 /**
227 * New ChildState records are created through
228 * this method to allow subclasses the extend
229 * the ChildState records to do/hold more.
230 * @return new child state
231 * @param v the view
232 */
233 protected ChildState createChildState(View v) {
234 return new ChildState(v);
235 }
236
237 /**
238 * Requirements changed along the major axis.
239 * This is called by the thread doing layout for
240 * the given ChildState object when it has completed
241 * fetching the child views new preferences.
242 * Typically this would be the layout thread, but
243 * might be the event thread if it is trying to update
244 * something immediately (such as to perform a
245 * model/view translation).
246 * <p>
247 * This is implemented to mark the major axis as having
248 * changed so that a future check to see if the requirements
249 * need to be published to the parent view will consider
250 * the major axis. If the span along the major axis is
251 * not estimated, it is updated by the given delta to reflect
252 * the incremental change. The delta is ignored if the
253 * major span is estimated.
254 * @param cs the child state
255 * @param delta the delta
256 */
257 protected synchronized void majorRequirementChange(ChildState cs, float delta) {
258 if (estimatedMajorSpan == false) {
259 majorSpan += delta;
260 }
261 majorChanged = true;
262 }
263
264 /**
265 * Requirements changed along the minor axis.
266 * This is called by the thread doing layout for
267 * the given ChildState object when it has completed
268 * fetching the child views new preferences.
269 * Typically this would be the layout thread, but
270 * might be the GUI thread if it is trying to update
271 * something immediately (such as to perform a
272 * model/view translation).
273 * @param cs the child state
274 */
275 protected synchronized void minorRequirementChange(ChildState cs) {
276 minorChanged = true;
277 }
278
279 /**
280 * Publish the changes in preferences upward to the parent
281 * view. This is normally called by the layout thread.
282 */
283 protected void flushRequirementChanges() {
284 AbstractDocument doc = (AbstractDocument) getDocument();
285 try {
286 doc.readLock();
287
288 View parent = null;
289 boolean horizontal = false;
290 boolean vertical = false;
291
292 synchronized(this) {
293 // perform tasks that iterate over the children while
418 * @see #setParent
419 */
420 protected void loadChildren(ViewFactory f) {
421 Element e = getElement();
422 int n = e.getElementCount();
423 if (n > 0) {
424 View[] added = new View[n];
425 for (int i = 0; i < n; i++) {
426 added[i] = f.create(e.getElement(i));
427 }
428 replace(0, 0, added);
429 }
430 }
431
432 /**
433 * Fetches the child view index representing the given position in
434 * the model. This is implemented to fetch the view in the case
435 * where there is a child view for each child element.
436 *
437 * @param pos the position >= 0
438 * @param b the position bias
439 * @return index of the view representing the given position, or
440 * -1 if no view represents that position
441 */
442 protected synchronized int getViewIndexAtPosition(int pos, Position.Bias b) {
443 boolean isBackward = (b == Position.Bias.Backward);
444 pos = (isBackward) ? Math.max(0, pos - 1) : pos;
445 Element elem = getElement();
446 return elem.getElementIndex(pos);
447 }
448
449 /**
450 * Update the layout in response to receiving notification of
451 * change from the model. This is implemented to note the
452 * change on the ChildLocator so that offsets of the children
453 * will be correctly computed.
454 *
455 * @param ec changes to the element this view is responsible
456 * for (may be null if there were no changes).
457 * @param e the change information from the associated document
458 * @param a the current allocation of the view
947 * needs to remain fairly stable until the layout thread
948 * decides to publish an update to the parent.
949 * @since 1.3
950 */
951 public class ChildLocator {
952
953 /**
954 * construct a child locator.
955 */
956 public ChildLocator() {
957 lastAlloc = new Rectangle();
958 childAlloc = new Rectangle();
959 }
960
961 /**
962 * Notification that a child changed. This can effect
963 * whether or not new offset calculations are needed.
964 * This is called by a ChildState object that has
965 * changed it's major span. This can therefore be
966 * called by multiple threads.
967 * @param cs the child state
968 */
969 public synchronized void childChanged(ChildState cs) {
970 if (lastValidOffset == null) {
971 lastValidOffset = cs;
972 } else if (cs.getChildView().getStartOffset() <
973 lastValidOffset.getChildView().getStartOffset()) {
974 lastValidOffset = cs;
975 }
976 }
977
978 /**
979 * Paint the children that intersect the clip area.
980 * @param g the rendering surface to use
981 */
982 public synchronized void paintChildren(Graphics g) {
983 Rectangle clip = g.getClipBounds();
984 float targetOffset = (axis == X_AXIS) ?
985 clip.x - lastAlloc.x : clip.y - lastAlloc.y;
986 int index = getViewIndexAtVisualOffset(targetOffset);
987 int n = getViewCount();
988 float offs = getChildState(index).getMajorOffset();
989 for (int i = index; i < n; i++) {
990 ChildState cs = getChildState(i);
991 cs.setMajorOffset(offs);
992 Shape ca = getChildAllocation(i);
993 if (intersectsClip(ca, clip)) {
994 synchronized (cs) {
995 View v = cs.getChildView();
996 v.paint(g, ca);
997 }
998 } else {
999 // done painting intersection
1000 break;
1001 }
1002 offs += cs.getMajorSpan();
1003 }
1004 }
1005
1006 /**
1007 * Fetch the allocation to use for a child view.
1008 * This will update the offsets for all children
1009 * not yet updated before the given index.
1010 * @param index the child index
1011 * @param a the allocation
1012 * @return the allocation to use for a child view
1013 */
1014 public synchronized Shape getChildAllocation(int index, Shape a) {
1015 if (a == null) {
1016 return null;
1017 }
1018 setAllocation(a);
1019 ChildState cs = getChildState(index);
1020 if (lastValidOffset == null) {
1021 lastValidOffset = getChildState(0);
1022 }
1023 if (cs.getChildView().getStartOffset() >
1024 lastValidOffset.getChildView().getStartOffset()) {
1025 // offsets need to be updated
1026 updateChildOffsetsToIndex(index);
1027 }
1028 Shape ca = getChildAllocation(index);
1029 return ca;
1030 }
1031
1032 /**
1037 * on this object, and would typically be followed
1038 * with one or more calls to getChildAllocation that
1039 * should also be in the synchronized block.
1040 *
1041 * @param x the X coordinate >= 0
1042 * @param y the Y coordinate >= 0
1043 * @param a the allocation to the View
1044 * @return the nearest child index
1045 */
1046 public int getViewIndexAtPoint(float x, float y, Shape a) {
1047 setAllocation(a);
1048 float targetOffset = (axis == X_AXIS) ? x - lastAlloc.x : y - lastAlloc.y;
1049 int index = getViewIndexAtVisualOffset(targetOffset);
1050 return index;
1051 }
1052
1053 /**
1054 * Fetch the allocation to use for a child view.
1055 * <em>This does not update the offsets in the ChildState
1056 * records.</em>
1057 * @param index the index
1058 * @return the allocation to use for a child view
1059 */
1060 protected Shape getChildAllocation(int index) {
1061 ChildState cs = getChildState(index);
1062 if (! cs.isLayoutValid()) {
1063 cs.run();
1064 }
1065 if (axis == X_AXIS) {
1066 childAlloc.x = lastAlloc.x + (int) cs.getMajorOffset();
1067 childAlloc.y = lastAlloc.y + (int) cs.getMinorOffset();
1068 childAlloc.width = (int) cs.getMajorSpan();
1069 childAlloc.height = (int) cs.getMinorSpan();
1070 } else {
1071 childAlloc.y = lastAlloc.y + (int) cs.getMajorOffset();
1072 childAlloc.x = lastAlloc.x + (int) cs.getMinorOffset();
1073 childAlloc.height = (int) cs.getMajorSpan();
1074 childAlloc.width = (int) cs.getMinorSpan();
1075 }
1076 childAlloc.x += (int)getLeftInset();
1077 childAlloc.y += (int)getRightInset();
1078 return childAlloc;
1079 }
1080
1081 /**
1082 * Copy the currently allocated shape into the Rectangle
1083 * used to store the current allocation. This would be
1084 * a floating point rectangle in a Java2D-specific implementation.
1085 * @param a the allocation
1086 */
1087 protected void setAllocation(Shape a) {
1088 if (a instanceof Rectangle) {
1089 lastAlloc.setBounds((Rectangle) a);
1090 } else {
1091 lastAlloc.setBounds(a.getBounds());
1092 }
1093 setSize(lastAlloc.width, lastAlloc.height);
1094 }
1095
1096 /**
1097 * Locate the view responsible for an offset into the box
1098 * along the major axis. Make sure that offsets are set
1099 * on the ChildState objects up to the given target span
1100 * past the desired offset.
1101 * @param targetOffset the target offset
1102 *
1103 * @return index of the view representing the given visual
1104 * location (targetOffset), or -1 if no view represents
1105 * that location
1106 */
1107 protected int getViewIndexAtVisualOffset(float targetOffset) {
1108 int n = getViewCount();
1109 if (n > 0) {
1110 boolean lastValid = (lastValidOffset != null);
1111
1112 if (lastValidOffset == null) {
1113 lastValidOffset = getChildState(0);
1114 }
1115 if (targetOffset > majorSpan) {
1116 // should only get here on the first time display.
1117 if (!lastValid) {
1118 return 0;
1119 }
1120 int pos = lastValidOffset.getChildView().getStartOffset();
1121 int index = getViewIndex(pos, Position.Bias.Forward);
1211 }
1212
1213 /**
1214 * A record representing the layout state of a
1215 * child view. It is runnable as a task on another
1216 * thread. All access to the child view that is
1217 * based upon a read-lock on the model should synchronize
1218 * on this object (i.e. The layout thread and the GUI
1219 * thread can both have a read lock on the model at the
1220 * same time and are not protected from each other).
1221 * Access to a child view hierarchy is serialized via
1222 * synchronization on the ChildState instance.
1223 * @since 1.3
1224 */
1225 public class ChildState implements Runnable {
1226
1227 /**
1228 * Construct a child status. This needs to start
1229 * out as fairly large so we don't falsely begin with
1230 * the idea that all of the children are visible.
1231 * @param v the view
1232 * @since 1.4
1233 */
1234 public ChildState(View v) {
1235 child = v;
1236 minorValid = false;
1237 majorValid = false;
1238 childSizeValid = false;
1239 child.setParent(AsyncBoxView.this);
1240 }
1241
1242 /**
1243 * Fetch the child view this record represents.
1244 * @return the child view this record represents
1245 */
1246 public View getChildView() {
1247 return child;
1248 }
1249
1250 /**
1251 * Update the child state. This should be
1252 * called by the thread that desires to spend
1253 * time updating the child state (intended to
1254 * be the layout thread).
1255 * <p>
1256 * This acquires a read lock on the associated
1257 * document for the duration of the update to
1258 * ensure the model is not changed while it is
1259 * operating. The first thing to do would be
1260 * to see if any work actually needs to be done.
1261 * The following could have conceivably happened
1262 * while the state was waiting to be updated:
1263 * <ol>
1264 * <li>The child may have been removed from the
1334 synchronized(this) {
1335 if (! childSizeValid) {
1336 float w;
1337 float h;
1338 if (axis == X_AXIS) {
1339 w = span;
1340 h = getMinorSpan();
1341 } else {
1342 w = getMinorSpan();
1343 h = span;
1344 }
1345 childSizeValid = true;
1346 child.setSize(w, h);
1347 }
1348 }
1349
1350 }
1351
1352 /**
1353 * What is the span along the minor axis.
1354 * @return the span along the minor axis
1355 */
1356 public float getMinorSpan() {
1357 if (max < minorSpan) {
1358 return max;
1359 }
1360 // make it the target width, or as small as it can get.
1361 return Math.max(min, minorSpan);
1362 }
1363
1364 /**
1365 * What is the offset along the minor axis
1366 * @return the offset along the minor axis
1367 */
1368 public float getMinorOffset() {
1369 if (max < minorSpan) {
1370 // can't make the child this wide, align it
1371 float align = child.getAlignment(getMinorAxis());
1372 return ((minorSpan - max) * align);
1373 }
1374 return 0f;
1375 }
1376
1377 /**
1378 * What is the span along the major axis.
1379 * @return the span along the major axis
1380 */
1381 public float getMajorSpan() {
1382 return span;
1383 }
1384
1385 /**
1386 * Get the offset along the major axis.
1387 * @return the offset along the major axis
1388 */
1389 public float getMajorOffset() {
1390 return offset;
1391 }
1392
1393 /**
1394 * This method should only be called by the ChildLocator,
1395 * it is simply a convenient place to hold the cached
1396 * location.
1397 * @param offs offsets
1398 */
1399 public void setMajorOffset(float offs) {
1400 offset = offs;
1401 }
1402
1403 /**
1404 * Mark preferences changed for this child.
1405 *
1406 * @param width true if the width preference has changed
1407 * @param height true if the height preference has changed
1408 * @see javax.swing.JComponent#revalidate
1409 */
1410 public void preferenceChanged(boolean width, boolean height) {
1411 if (axis == X_AXIS) {
1412 if (width) {
1413 majorValid = false;
1414 }
1415 if (height) {
1416 minorValid = false;
1417 }
1418 } else {
1419 if (width) {
1420 minorValid = false;
1421 }
1422 if (height) {
1423 majorValid = false;
1424 }
1425 }
1426 childSizeValid = false;
1427 }
1428
1429 /**
1430 * Has the child view been laid out.
1431 * @return whether or not the child view been laid out.
1432 */
1433 public boolean isLayoutValid() {
1434 return (minorValid && majorValid && childSizeValid);
1435 }
1436
1437 // minor axis
1438 private float min;
1439 private float pref;
1440 private float max;
1441 private boolean minorValid;
1442
1443 // major axis
1444 private float span;
1445 private float offset;
1446 private boolean majorValid;
1447
1448 private View child;
1449 private boolean childSizeValid;
1450 }
1451
|