author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 3938 | ef327bd847c0 |
child 11093 | e753252dc8a9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, 2006, 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 |
||
26 |
package sun.awt.X11; |
|
27 |
||
28 |
import java.awt.*; |
|
29 |
import java.awt.event.MouseEvent; |
|
30 |
import java.awt.event.MouseWheelEvent; |
|
31 |
import java.awt.event.AdjustmentEvent; |
|
32 |
import java.util.List; |
|
33 |
import java.util.ArrayList; |
|
34 |
import java.util.Iterator; |
|
35 |
import sun.awt.motif.X11FontMetrics; |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
36 |
import sun.util.logging.PlatformLogger; |
2 | 37 |
|
38 |
// FIXME: implement multi-select |
|
39 |
/* |
|
40 |
* Class to paint a list of items, possibly with scrollbars |
|
41 |
* This class paints all items with the same font |
|
42 |
* For now, this class manages the list of items and painting thereof, but not |
|
43 |
* posting of Item or ActionEvents |
|
44 |
*/ |
|
45 |
public class ListHelper implements XScrollbarClient { |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
46 |
private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.ListHelper"); |
2 | 47 |
|
48 |
private final int FOCUS_INSET = 1; |
|
49 |
||
50 |
private final int BORDER_WIDTH; // Width of border drawn around the list |
|
51 |
// of items |
|
52 |
private final int ITEM_MARGIN; // Margin between the border of the list |
|
53 |
// of items and and item's bg, and between |
|
54 |
// items |
|
55 |
private final int TEXT_SPACE; // Space between the edge of an item and |
|
56 |
// the text |
|
57 |
||
58 |
private final int SCROLLBAR_WIDTH; // Width of a scrollbar |
|
59 |
||
60 |
private java.util.List items; // List of items |
|
61 |
||
62 |
// TODO: maybe this would be better as a simple int[] |
|
63 |
private java.util.List selected; // List of selected items |
|
64 |
private boolean multiSelect; // Can multiple items be selected |
|
65 |
// at once? |
|
66 |
private int focusedIndex; |
|
67 |
||
68 |
private int maxVisItems; // # items visible without a vsb |
|
69 |
private XVerticalScrollbar vsb; // null if unsupported |
|
70 |
private boolean vsbVis; |
|
71 |
private XHorizontalScrollbar hsb; // null if unsupported |
|
72 |
private boolean hsbVis; |
|
73 |
||
74 |
private Font font; |
|
75 |
private FontMetrics fm; |
|
76 |
||
77 |
private XWindow peer; // So far, only needed for painting |
|
78 |
// on notifyValue() |
|
79 |
private Color[] colors; // Passed in for painting on notifyValue() |
|
80 |
||
81 |
// Holds the true if mouse is dragging outside of the area of the list |
|
82 |
// The flag is used at the moment of the dragging and releasing mouse |
|
83 |
// See 6243382 for more information |
|
84 |
boolean mouseDraggedOutVertically = false; |
|
85 |
private volatile boolean vsbVisibilityChanged = false; |
|
86 |
||
87 |
/* |
|
88 |
* Comment |
|
89 |
*/ |
|
90 |
public ListHelper(XWindow peer, |
|
91 |
Color[] colors, |
|
92 |
int initialSize, |
|
93 |
boolean multiSelect, |
|
94 |
boolean scrollVert, |
|
95 |
boolean scrollHoriz, |
|
96 |
Font font, |
|
97 |
int maxVisItems, |
|
98 |
int SPACE, |
|
99 |
int MARGIN, |
|
100 |
int BORDER, |
|
101 |
int SCROLLBAR) { |
|
102 |
this.peer = peer; |
|
103 |
this.colors = colors; |
|
104 |
this.multiSelect = multiSelect; |
|
105 |
items = new ArrayList(initialSize); |
|
106 |
selected = new ArrayList(1); |
|
107 |
selected.add(Integer.valueOf(-1)); |
|
108 |
||
109 |
this.maxVisItems = maxVisItems; |
|
110 |
if (scrollVert) { |
|
111 |
vsb = new XVerticalScrollbar(this); |
|
112 |
vsb.setValues(0, 0, 0, 0, 1, maxVisItems - 1); |
|
113 |
} |
|
114 |
if (scrollHoriz) { |
|
115 |
hsb = new XHorizontalScrollbar(this); |
|
116 |
hsb.setValues(0, 0, 0, 0, 1, 1); |
|
117 |
} |
|
118 |
||
119 |
setFont(font); |
|
120 |
TEXT_SPACE = SPACE; |
|
121 |
ITEM_MARGIN = MARGIN; |
|
122 |
BORDER_WIDTH = BORDER; |
|
123 |
SCROLLBAR_WIDTH = SCROLLBAR; |
|
124 |
} |
|
125 |
||
126 |
public Component getEventSource() { |
|
127 |
return peer.getEventSource(); |
|
128 |
} |
|
129 |
||
130 |
/**********************************************************************/ |
|
131 |
/* List management methods */ |
|
132 |
/**********************************************************************/ |
|
133 |
||
134 |
public void add(String item) { |
|
135 |
items.add(item); |
|
136 |
updateScrollbars(); |
|
137 |
} |
|
138 |
||
139 |
public void add(String item, int index) { |
|
140 |
items.add(index, item); |
|
141 |
updateScrollbars(); |
|
142 |
} |
|
143 |
||
144 |
public void remove(String item) { |
|
145 |
// FIXME: need to clean up select list, too? |
|
146 |
items.remove(item); |
|
147 |
updateScrollbars(); |
|
148 |
// Is vsb visible now? |
|
149 |
} |
|
150 |
||
151 |
public void remove(int index) { |
|
152 |
// FIXME: need to clean up select list, too? |
|
153 |
items.remove(index); |
|
154 |
updateScrollbars(); |
|
155 |
// Is vsb visible now? |
|
156 |
} |
|
157 |
||
158 |
public void removeAll() { |
|
159 |
items.removeAll(items); |
|
160 |
updateScrollbars(); |
|
161 |
} |
|
162 |
||
163 |
public void setMultiSelect(boolean ms) { |
|
164 |
multiSelect = ms; |
|
165 |
} |
|
166 |
||
167 |
/* |
|
168 |
* docs.....definitely docs |
|
169 |
* merely keeps internal track of which items are selected for painting |
|
170 |
* dealing with target Components happens elsewhere |
|
171 |
*/ |
|
172 |
public void select(int index) { |
|
173 |
if (index > getItemCount() - 1) { |
|
174 |
index = (isEmpty() ? -1 : 0); |
|
175 |
} |
|
176 |
if (multiSelect) { |
|
177 |
assert false : "Implement ListHelper.select() for multiselect"; |
|
178 |
} |
|
179 |
else if (getSelectedIndex() != index) { |
|
180 |
selected.remove(0); |
|
181 |
selected.add(Integer.valueOf(index)); |
|
182 |
makeVisible(index); |
|
183 |
} |
|
184 |
} |
|
185 |
||
186 |
/* docs */ |
|
187 |
public void deselect(int index) { |
|
188 |
assert(false); |
|
189 |
} |
|
190 |
||
191 |
/* docs */ |
|
192 |
/* if called for multiselect, return -1 */ |
|
193 |
public int getSelectedIndex() { |
|
194 |
if (!multiSelect) { |
|
195 |
Integer val = (Integer)selected.get(0); |
|
196 |
return val.intValue(); |
|
197 |
} |
|
198 |
return -1; |
|
199 |
} |
|
200 |
||
201 |
int[] getSelectedIndexes() { assert(false); return null;} |
|
202 |
||
203 |
/* |
|
204 |
* A getter method for XChoicePeer. |
|
205 |
* Returns vsbVisiblityChanged value and sets it to false. |
|
206 |
*/ |
|
207 |
public boolean checkVsbVisibilityChangedAndReset(){ |
|
208 |
boolean returnVal = vsbVisibilityChanged; |
|
209 |
vsbVisibilityChanged = false; |
|
210 |
return returnVal; |
|
211 |
} |
|
212 |
||
213 |
public boolean isEmpty() { |
|
214 |
return items.isEmpty(); |
|
215 |
} |
|
216 |
||
217 |
public int getItemCount() { |
|
218 |
return items.size(); |
|
219 |
} |
|
220 |
||
221 |
public String getItem(int index) { |
|
222 |
return (String) items.get(index); |
|
223 |
} |
|
224 |
||
225 |
/**********************************************************************/ |
|
226 |
/* GUI-related methods */ |
|
227 |
/**********************************************************************/ |
|
228 |
||
229 |
public void setFocusedIndex(int index) { |
|
230 |
focusedIndex = index; |
|
231 |
} |
|
232 |
||
233 |
public boolean isFocusedIndex(int index) { |
|
234 |
return index == focusedIndex; |
|
235 |
} |
|
236 |
||
237 |
public void setFont(Font newFont) { |
|
238 |
if (newFont != font) { |
|
239 |
font = newFont; |
|
240 |
fm = Toolkit.getDefaultToolkit().getFontMetrics(font); |
|
241 |
// Also cache stuff like fontHeight? |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
/* |
|
246 |
* Returns width of the text of the longest item |
|
247 |
*/ |
|
248 |
public int getMaxItemWidth() { |
|
249 |
int m = 0; |
|
250 |
int end = getItemCount(); |
|
251 |
for(int i = 0 ; i < end ; i++) { |
|
252 |
int l = fm.stringWidth(getItem(i)); |
|
253 |
m = Math.max(m, l); |
|
254 |
} |
|
255 |
return m; |
|
256 |
} |
|
257 |
||
258 |
/* |
|
259 |
* Height of an item (this doesn't include ITEM_MARGIN) |
|
260 |
*/ |
|
261 |
int getItemHeight() { |
|
262 |
return fm.getHeight() + (2*TEXT_SPACE); |
|
263 |
} |
|
264 |
||
265 |
public int y2index(int y) { |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
266 |
if (log.isLoggable(PlatformLogger.FINE)) { |
2 | 267 |
log.fine("y=" + y +", firstIdx=" + firstDisplayedIndex() +", itemHeight=" + getItemHeight() |
268 |
+ ",item_margin=" + ITEM_MARGIN); |
|
269 |
} |
|
270 |
// See 6243382 for more information |
|
271 |
int newIdx = firstDisplayedIndex() + ((y - 2*ITEM_MARGIN) / (getItemHeight() + 2*ITEM_MARGIN)); |
|
272 |
return newIdx; |
|
273 |
} |
|
274 |
||
275 |
/* write these |
|
276 |
int index2y(int); |
|
277 |
public int numItemsDisplayed() {} |
|
278 |
*/ |
|
279 |
||
280 |
public int firstDisplayedIndex() { |
|
281 |
if (vsbVis) { |
|
282 |
return vsb.getValue(); |
|
283 |
} |
|
284 |
return 0; |
|
285 |
} |
|
286 |
||
287 |
public int lastDisplayedIndex() { |
|
288 |
// FIXME: need to account for horiz scroll bar |
|
289 |
if (hsbVis) { |
|
290 |
assert false : "Implement for horiz scroll bar"; |
|
291 |
} |
|
292 |
||
293 |
return vsbVis ? vsb.getValue() + maxVisItems - 1: getItemCount() - 1; |
|
294 |
} |
|
295 |
||
296 |
/* |
|
297 |
* If the given index is not visible in the List, scroll so that it is. |
|
298 |
*/ |
|
299 |
public void makeVisible(int index) { |
|
300 |
if (vsbVis) { |
|
301 |
if (index < firstDisplayedIndex()) { |
|
302 |
vsb.setValue(index); |
|
303 |
} |
|
304 |
else if (index > lastDisplayedIndex()) { |
|
305 |
vsb.setValue(index - maxVisItems + 1); |
|
306 |
} |
|
307 |
} |
|
308 |
} |
|
309 |
||
310 |
// FIXME: multi-select needs separate focused index |
|
311 |
public void up() { |
|
312 |
int curIdx = getSelectedIndex(); |
|
313 |
int numItems = getItemCount(); |
|
314 |
int newIdx; |
|
315 |
||
316 |
assert curIdx >= 0; |
|
317 |
||
318 |
if (curIdx == 0) { |
|
319 |
newIdx = numItems - 1; |
|
320 |
} |
|
321 |
else { |
|
322 |
newIdx = --curIdx; |
|
323 |
} |
|
324 |
// focus(newIdx); |
|
325 |
select(newIdx); |
|
326 |
} |
|
327 |
||
328 |
public void down() { |
|
329 |
int newIdx = (getSelectedIndex() + 1) % getItemCount(); |
|
330 |
select(newIdx); |
|
331 |
} |
|
332 |
||
333 |
public void pageUp() { |
|
334 |
// FIXME: for multi-select, move the focused item, not the selected item |
|
335 |
if (vsbVis && firstDisplayedIndex() > 0) { |
|
336 |
if (multiSelect) { |
|
337 |
assert false : "Implement pageUp() for multiSelect"; |
|
338 |
} |
|
339 |
else { |
|
340 |
int selectionOffset = getSelectedIndex() - firstDisplayedIndex(); |
|
341 |
// the vsb does bounds checking |
|
342 |
int newIdx = firstDisplayedIndex() - vsb.getBlockIncrement(); |
|
343 |
vsb.setValue(newIdx); |
|
344 |
select(firstDisplayedIndex() + selectionOffset); |
|
345 |
} |
|
346 |
} |
|
347 |
} |
|
348 |
public void pageDown() { |
|
349 |
if (vsbVis && lastDisplayedIndex() < getItemCount() - 1) { |
|
350 |
if (multiSelect) { |
|
351 |
assert false : "Implement pageDown() for multiSelect"; |
|
352 |
} |
|
353 |
else { |
|
354 |
int selectionOffset = getSelectedIndex() - firstDisplayedIndex(); |
|
355 |
// the vsb does bounds checking |
|
356 |
int newIdx = lastDisplayedIndex(); |
|
357 |
vsb.setValue(newIdx); |
|
358 |
select(firstDisplayedIndex() + selectionOffset); |
|
359 |
} |
|
360 |
} |
|
361 |
} |
|
362 |
public void home() {} |
|
363 |
public void end() {} |
|
364 |
||
365 |
||
366 |
public boolean isVSBVisible() { return vsbVis; } |
|
367 |
public boolean isHSBVisible() { return hsbVis; } |
|
368 |
||
369 |
public XVerticalScrollbar getVSB() { return vsb; } |
|
370 |
public XHorizontalScrollbar getHSB() { return hsb; } |
|
371 |
||
372 |
public boolean isInVertSB(Rectangle bounds, int x, int y) { |
|
373 |
if (vsbVis) { |
|
374 |
assert vsb != null : "Vert scrollbar is visible, yet is null?"; |
|
375 |
int sbHeight = hsbVis ? bounds.height - SCROLLBAR_WIDTH : bounds.height; |
|
376 |
return (x <= bounds.width) && |
|
377 |
(x >= bounds.width - SCROLLBAR_WIDTH) && |
|
378 |
(y >= 0) && |
|
379 |
(y <= sbHeight); |
|
380 |
} |
|
381 |
return false; |
|
382 |
} |
|
383 |
||
384 |
public boolean isInHorizSB(Rectangle bounds, int x, int y) { |
|
385 |
if (hsbVis) { |
|
386 |
assert hsb != null : "Horiz scrollbar is visible, yet is null?"; |
|
387 |
||
388 |
int sbWidth = vsbVis ? bounds.width - SCROLLBAR_WIDTH : bounds.width; |
|
389 |
return (x <= sbWidth) && |
|
390 |
(x >= 0) && |
|
391 |
(y >= bounds.height - SCROLLBAR_WIDTH) && |
|
392 |
(y <= bounds.height); |
|
393 |
} |
|
394 |
return false; |
|
395 |
} |
|
396 |
||
397 |
public void handleVSBEvent(MouseEvent e, Rectangle bounds, int x, int y) { |
|
398 |
int sbHeight = hsbVis ? bounds.height - SCROLLBAR_WIDTH : bounds.height; |
|
399 |
||
400 |
vsb.handleMouseEvent(e.getID(), |
|
401 |
e.getModifiers(), |
|
402 |
x - (bounds.width - SCROLLBAR_WIDTH), |
|
403 |
y); |
|
404 |
} |
|
405 |
||
406 |
/* |
|
407 |
* Called when items are added/removed. |
|
408 |
* Update whether the scrollbar is visible or not, scrollbar values |
|
409 |
*/ |
|
410 |
void updateScrollbars() { |
|
411 |
boolean oldVsbVis = vsbVis; |
|
412 |
vsbVis = vsb != null && items.size() > maxVisItems; |
|
413 |
if (vsbVis) { |
|
414 |
vsb.setValues(vsb.getValue(), getNumItemsDisplayed(), |
|
415 |
vsb.getMinimum(), items.size()); |
|
416 |
} |
|
417 |
||
418 |
// 6405689. If Vert Scrollbar gets disappeared from the dropdown menu we should repaint whole dropdown even if |
|
419 |
// no actual resize gets invoked. This is needed because some painting artifacts remained between dropdown items |
|
420 |
// but draw3DRect doesn't clear the area inside. Instead it just paints lines as borders. |
|
421 |
vsbVisibilityChanged = (vsbVis != oldVsbVis); |
|
422 |
// FIXME: check if added item makes a hsb necessary (if supported, that of course) |
|
423 |
} |
|
424 |
||
425 |
public int getNumItemsDisplayed() { |
|
426 |
return items.size() > maxVisItems ? maxVisItems : items.size(); |
|
427 |
} |
|
428 |
||
429 |
public void repaintScrollbarRequest(XScrollbar sb) { |
|
430 |
Graphics g = peer.getGraphics(); |
|
431 |
Rectangle bounds = peer.getBounds(); |
|
432 |
if ((sb == vsb) && vsbVis) { |
|
433 |
paintVSB(g, XComponentPeer.getSystemColors(), bounds); |
|
434 |
} |
|
435 |
else if ((sb == hsb) && hsbVis) { |
|
436 |
paintHSB(g, XComponentPeer.getSystemColors(), bounds); |
|
437 |
} |
|
438 |
g.dispose(); |
|
439 |
} |
|
440 |
||
441 |
public void notifyValue(XScrollbar obj, int type, int v, boolean isAdjusting) { |
|
442 |
if (obj == vsb) { |
|
443 |
int oldScrollValue = vsb.getValue(); |
|
444 |
vsb.setValue(v); |
|
445 |
boolean needRepaint = (oldScrollValue != vsb.getValue()); |
|
446 |
// See 6243382 for more information |
|
447 |
if (mouseDraggedOutVertically){ |
|
448 |
int oldItemValue = getSelectedIndex(); |
|
449 |
int newItemValue = getSelectedIndex() + v - oldScrollValue; |
|
450 |
select(newItemValue); |
|
451 |
needRepaint = needRepaint || (getSelectedIndex() != oldItemValue); |
|
452 |
} |
|
453 |
||
454 |
// FIXME: how are we going to paint!? |
|
455 |
Graphics g = peer.getGraphics(); |
|
456 |
Rectangle bounds = peer.getBounds(); |
|
457 |
int first = v; |
|
458 |
int last = Math.min(getItemCount() - 1, |
|
459 |
v + maxVisItems); |
|
460 |
if (needRepaint) { |
|
461 |
paintItems(g, colors, bounds, first, last); |
|
462 |
} |
|
463 |
g.dispose(); |
|
464 |
||
465 |
} |
|
466 |
else if ((XHorizontalScrollbar)obj == hsb) { |
|
467 |
hsb.setValue(v); |
|
468 |
// FIXME: how are we going to paint!? |
|
469 |
} |
|
470 |
} |
|
471 |
||
472 |
public void updateColors(Color[] newColors) { |
|
473 |
colors = newColors; |
|
474 |
} |
|
475 |
||
476 |
/* |
|
477 |
public void paintItems(Graphics g, |
|
478 |
Color[] colors, |
|
479 |
Rectangle bounds, |
|
480 |
Font font, |
|
481 |
int first, |
|
482 |
int last, |
|
483 |
XVerticalScrollbar vsb, |
|
484 |
XHorizontalScrollbar hsb) { |
|
485 |
*/ |
|
486 |
public void paintItems(Graphics g, |
|
487 |
Color[] colors, |
|
488 |
Rectangle bounds) { |
|
489 |
// paint border |
|
490 |
// paint items |
|
491 |
// paint scrollbars |
|
492 |
// paint focus? |
|
493 |
||
494 |
} |
|
495 |
public void paintAllItems(Graphics g, |
|
496 |
Color[] colors, |
|
497 |
Rectangle bounds) { |
|
498 |
paintItems(g, colors, bounds, |
|
499 |
firstDisplayedIndex(), lastDisplayedIndex()); |
|
500 |
} |
|
501 |
public void paintItems(Graphics g, |
|
502 |
Color[] colors, |
|
503 |
Rectangle bounds, |
|
504 |
int first, |
|
505 |
int last) { |
|
506 |
peer.flush(); |
|
507 |
int x = BORDER_WIDTH + ITEM_MARGIN; |
|
508 |
int width = bounds.width - 2*ITEM_MARGIN - 2*BORDER_WIDTH - (vsbVis ? SCROLLBAR_WIDTH : 0); |
|
509 |
int height = getItemHeight(); |
|
510 |
int y = BORDER_WIDTH + ITEM_MARGIN; |
|
511 |
||
512 |
for (int i = first; i <= last ; i++) { |
|
513 |
paintItem(g, colors, getItem(i), |
|
514 |
x, y, width, height, |
|
515 |
isItemSelected(i), |
|
516 |
isFocusedIndex(i)); |
|
517 |
y += height + 2*ITEM_MARGIN; |
|
518 |
} |
|
519 |
||
520 |
if (vsbVis) { |
|
521 |
paintVSB(g, XComponentPeer.getSystemColors(), bounds); |
|
522 |
} |
|
523 |
if (hsbVis) { |
|
524 |
paintHSB(g, XComponentPeer.getSystemColors(), bounds); |
|
525 |
} |
|
526 |
peer.flush(); |
|
527 |
// FIXME: if none of the items were focused, paint focus around the |
|
528 |
// entire list. This is how java.awt.List should work. |
|
529 |
} |
|
530 |
||
531 |
/* |
|
532 |
* comment about what is painted (i.e. the focus rect |
|
533 |
*/ |
|
534 |
public void paintItem(Graphics g, |
|
535 |
Color[] colors, |
|
536 |
String string, |
|
537 |
int x, int y, int width, int height, |
|
538 |
boolean selected, |
|
539 |
boolean focused) { |
|
540 |
//System.out.println("LP.pI(): x="+x+" y="+y+" w="+width+" h="+height); |
|
541 |
//g.setColor(colors[BACKGROUND_COLOR]); |
|
542 |
||
543 |
// FIXME: items shouldn't draw into the scrollbar |
|
544 |
||
545 |
if (selected) { |
|
546 |
g.setColor(colors[XComponentPeer.FOREGROUND_COLOR]); |
|
547 |
} |
|
548 |
else { |
|
549 |
g.setColor(colors[XComponentPeer.BACKGROUND_COLOR]); |
|
550 |
} |
|
551 |
g.fillRect(x, y, width, height); |
|
552 |
||
553 |
if (focused) { |
|
554 |
//g.setColor(colors[XComponentPeer.FOREGROUND_COLOR]); |
|
555 |
g.setColor(Color.BLACK); |
|
556 |
g.drawRect(x + FOCUS_INSET, |
|
557 |
y + FOCUS_INSET, |
|
558 |
width - 2*FOCUS_INSET, |
|
559 |
height - 2*FOCUS_INSET); |
|
560 |
} |
|
561 |
||
562 |
if (selected) { |
|
563 |
g.setColor(colors[XComponentPeer.BACKGROUND_COLOR]); |
|
564 |
} |
|
565 |
else { |
|
566 |
g.setColor(colors[XComponentPeer.FOREGROUND_COLOR]); |
|
567 |
} |
|
568 |
g.setFont(font); |
|
569 |
//Rectangle clip = g.getClipBounds(); |
|
570 |
//g.clipRect(x, y, width, height); |
|
571 |
//g.drawString(string, x + TEXT_SPACE, y + TEXT_SPACE + ITEM_MARGIN); |
|
572 |
||
573 |
int fontAscent = fm.getAscent(); |
|
574 |
int fontDescent = fm.getDescent(); |
|
575 |
||
576 |
g.drawString(string, x + TEXT_SPACE, y + (height + fm.getMaxAscent() - fm.getMaxDescent())/2); |
|
577 |
//g.clipRect(clip.x, clip.y, clip.width, clip.height); |
|
578 |
} |
|
579 |
||
580 |
boolean isItemSelected(int index) { |
|
581 |
Iterator itr = selected.iterator(); |
|
582 |
while (itr.hasNext()) { |
|
583 |
Integer val = (Integer)itr.next(); |
|
584 |
if (val.intValue() == index) { |
|
585 |
return true; |
|
586 |
} |
|
587 |
} |
|
588 |
return false; |
|
589 |
} |
|
590 |
||
591 |
public void paintVSB(Graphics g, Color colors[], Rectangle bounds) { |
|
592 |
int height = bounds.height - 2*BORDER_WIDTH - (hsbVis ? (SCROLLBAR_WIDTH-2) : 0); |
|
593 |
Graphics ng = g.create(); |
|
594 |
||
595 |
g.setColor(colors[XComponentPeer.BACKGROUND_COLOR]); |
|
596 |
try { |
|
597 |
ng.translate(bounds.width - BORDER_WIDTH - SCROLLBAR_WIDTH, |
|
598 |
BORDER_WIDTH); |
|
599 |
// Update scrollbar's size |
|
600 |
vsb.setSize(SCROLLBAR_WIDTH, bounds.height); |
|
601 |
vsb.paint(ng, colors, true); |
|
602 |
} finally { |
|
603 |
ng.dispose(); |
|
604 |
} |
|
605 |
} |
|
606 |
||
607 |
public void paintHSB(Graphics g, Color colors[], Rectangle bounds) { |
|
608 |
||
609 |
} |
|
610 |
||
611 |
/* |
|
612 |
* Helper method for Components with integrated scrollbars. |
|
613 |
* Pass in the vertical and horizontal scroll bar (or null for none/hidden) |
|
614 |
* and the MouseWheelEvent, and the appropriate scrollbar will be scrolled |
|
615 |
* correctly. |
|
616 |
* Returns whether or not scrolling actually took place. This will indicate |
|
617 |
* whether or not repainting is required. |
|
618 |
*/ |
|
619 |
static boolean doWheelScroll(XVerticalScrollbar vsb, |
|
620 |
XHorizontalScrollbar hsb, |
|
621 |
MouseWheelEvent e) { |
|
622 |
XScrollbar scroll = null; |
|
623 |
int wheelRotation; |
|
624 |
||
625 |
// Determine which, if any, sb to scroll |
|
626 |
if (vsb != null) { |
|
627 |
scroll = vsb; |
|
628 |
} |
|
629 |
else if (hsb != null) { |
|
630 |
scroll = hsb; |
|
631 |
} |
|
632 |
else { // Neither scrollbar is showing |
|
633 |
return false; |
|
634 |
} |
|
635 |
||
636 |
wheelRotation = e.getWheelRotation(); |
|
637 |
||
638 |
// Check if scroll is necessary |
|
639 |
if ((wheelRotation < 0 && scroll.getValue() > scroll.getMinimum()) || |
|
640 |
(wheelRotation > 0 && scroll.getValue() < scroll.getMaximum()) || |
|
641 |
wheelRotation != 0) { |
|
642 |
||
643 |
int type = e.getScrollType(); |
|
644 |
int incr; |
|
645 |
if (type == MouseWheelEvent.WHEEL_BLOCK_SCROLL) { |
|
646 |
incr = wheelRotation * scroll.getBlockIncrement(); |
|
647 |
} |
|
648 |
else { // type is WHEEL_UNIT_SCROLL |
|
649 |
incr = e.getUnitsToScroll() * scroll.getUnitIncrement(); |
|
650 |
} |
|
651 |
scroll.setValue(scroll.getValue() + incr); |
|
652 |
return true; |
|
653 |
} |
|
654 |
return false; |
|
655 |
} |
|
656 |
||
657 |
/* |
|
658 |
* Helper method for XChoicePeer with integrated vertical scrollbar. |
|
659 |
* Start or stop vertical scrolling when mouse dragged in / out the area of the list if it's required |
|
660 |
* Restoring Motif behavior |
|
661 |
* See 6243382 for more information |
|
662 |
*/ |
|
663 |
void trackMouseDraggedScroll(int mouseX, int mouseY, int listWidth, int listHeight){ |
|
664 |
||
665 |
if (!mouseDraggedOutVertically){ |
|
666 |
if (vsb.beforeThumb(mouseX, mouseY)) { |
|
667 |
vsb.setMode(AdjustmentEvent.UNIT_DECREMENT); |
|
668 |
} else { |
|
669 |
vsb.setMode(AdjustmentEvent.UNIT_INCREMENT); |
|
670 |
} |
|
671 |
} |
|
672 |
||
673 |
if(!mouseDraggedOutVertically && (mouseY < 0 || mouseY >= listHeight)){ |
|
674 |
mouseDraggedOutVertically = true; |
|
675 |
vsb.startScrollingInstance(); |
|
676 |
} |
|
677 |
||
678 |
if (mouseDraggedOutVertically && mouseY >= 0 && mouseY < listHeight && mouseX >= 0 && mouseX < listWidth){ |
|
679 |
mouseDraggedOutVertically = false; |
|
680 |
vsb.stopScrollingInstance(); |
|
681 |
} |
|
682 |
} |
|
683 |
||
684 |
/* |
|
685 |
* Helper method for XChoicePeer with integrated vertical scrollbar. |
|
686 |
* Stop vertical scrolling when mouse released in / out the area of the list if it's required |
|
687 |
* Restoring Motif behavior |
|
688 |
* see 6243382 for more information |
|
689 |
*/ |
|
690 |
void trackMouseReleasedScroll(){ |
|
691 |
||
692 |
if (mouseDraggedOutVertically){ |
|
693 |
mouseDraggedOutVertically = false; |
|
694 |
vsb.stopScrollingInstance(); |
|
695 |
} |
|
696 |
||
697 |
} |
|
698 |
} |