author | yan |
Mon, 06 Oct 2008 16:45:00 +0400 | |
changeset 1966 | 12a51fb0db0d |
parent 445 | 6f717a8cacfb |
child 3938 | ef327bd847c0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
2 |
* Copyright 2003-2008 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
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 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
||
27 |
// Very much based on XListPeer from javaos |
|
28 |
||
29 |
package sun.awt.X11; |
|
30 |
||
31 |
import java.awt.*; |
|
32 |
import java.awt.event.*; |
|
33 |
import java.awt.peer.*; |
|
34 |
import java.util.Vector; |
|
35 |
import java.awt.geom.*; |
|
36 |
import java.awt.image.*; |
|
37 |
import java.util.logging.*; |
|
38 |
||
39 |
// TODO: some input actions should do nothing if Shift or Control are down |
|
40 |
||
41 |
class XListPeer extends XComponentPeer implements ListPeer, XScrollbarClient { |
|
42 |
||
43 |
private static final Logger log = Logger.getLogger("sun.awt.X11.XListPeer"); |
|
44 |
||
45 |
public final static int MARGIN = 2; |
|
46 |
public final static int SPACE = 1; |
|
47 |
public final static int SCROLLBAR_AREA = 17; // Area reserved for the |
|
48 |
// scrollbar |
|
49 |
public final static int SCROLLBAR_WIDTH = 13; // Actual width of the |
|
50 |
// scrollbar |
|
51 |
public final static int NONE = -1; |
|
52 |
public final static int WINDOW = 0; |
|
53 |
public final static int VERSCROLLBAR = 1; |
|
54 |
public final static int HORSCROLLBAR = 2; |
|
55 |
public final static int DEFAULT_VISIBLE_ROWS = 4; // From java.awt.List, |
|
56 |
public final static int HORIZ_SCROLL_AMT = 10; |
|
57 |
||
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
58 |
private final static int PAINT_VSCROLL = 2; |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
59 |
private final static int PAINT_HSCROLL = 4; |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
60 |
private final static int PAINT_ITEMS = 8; |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
61 |
private final static int PAINT_FOCUS = 16; |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
62 |
private final static int PAINT_BACKGROUND = 32; |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
63 |
private final static int PAINT_HIDEFOCUS = 64; |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
64 |
private final static int PAINT_ALL = |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
65 |
PAINT_VSCROLL | PAINT_HSCROLL | PAINT_ITEMS | PAINT_FOCUS | PAINT_BACKGROUND; |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
66 |
private final static int COPY_AREA = 128; |
2 | 67 |
|
68 |
XVerticalScrollbar vsb; |
|
69 |
XHorizontalScrollbar hsb; |
|
70 |
ListPainter painter; |
|
71 |
||
72 |
// TODO: ick - Vector? |
|
73 |
Vector items; |
|
74 |
boolean multipleSelections; |
|
75 |
int active = NONE; |
|
76 |
||
77 |
// Holds the array of the indexes of the elements which is selected |
|
78 |
// This array should be kept sorted, low to high. |
|
79 |
int selected[]; |
|
80 |
int fontHeight; |
|
81 |
int fontAscent; |
|
82 |
int fontLeading; |
|
83 |
||
84 |
// Holds the index of the item used in the previous operation (selectItem, deselectItem) |
|
85 |
// Adding of an item or clearing of the list sets this index to -1 |
|
86 |
// The index is used at the moment of the post of ACTION_PERFORMED event after the mouse double click event. |
|
87 |
int currentIndex = -1; |
|
88 |
||
89 |
// Used for tracking selection/deselection between mousePress/Release |
|
90 |
// and for ItemEvents |
|
91 |
int eventIndex = -1; |
|
92 |
int eventType = NONE; |
|
93 |
||
94 |
// Holds the index of the item that receive focus |
|
95 |
// This variable is reasonable only for multiple list |
|
96 |
// since 'focusIndex' and 'selected[0]' are equal for single-selection list |
|
97 |
int focusIndex; |
|
98 |
||
99 |
int maxLength; |
|
100 |
boolean vsbVis; // visibility of scrollbars |
|
101 |
boolean hsbVis; |
|
102 |
int listWidth; // Width of list portion of List |
|
103 |
int listHeight; // Height of list portion of List |
|
104 |
// (i.e. without scrollbars) |
|
105 |
||
106 |
private int firstTimeVisibleIndex = 0; |
|
107 |
||
108 |
// Motif Lists don't seem to inherit the background color from their |
|
109 |
// parent when an app is first started up. So, we track if the colors have |
|
110 |
// been set. See getListBackground()/getListForeground(). |
|
111 |
boolean bgColorSet; |
|
112 |
boolean fgColorSet; |
|
113 |
||
114 |
// Holds the true if mouse is dragging outside of the area of the list |
|
115 |
// The flag is used at the moment of the dragging and releasing mouse |
|
116 |
// See 6243382 for more information |
|
117 |
boolean mouseDraggedOutHorizontally = false; |
|
118 |
boolean mouseDraggedOutVertically = false; |
|
119 |
||
120 |
// Holds the true if a mouse event was originated on the scrollbar |
|
121 |
// See 6300527 for more information |
|
122 |
boolean isScrollBarOriginated = false; |
|
123 |
||
124 |
// This variable is set to true after the "mouse pressed" event and to false after the "mouse released" event |
|
125 |
// Fixed 6293432: Key events ('SPACE', 'UP', 'DOWN') aren't blocked if mouse is kept in 'PRESSED' state for List, XAWT |
|
126 |
boolean isMousePressed = false; |
|
127 |
||
128 |
/** |
|
129 |
* Create a list |
|
130 |
*/ |
|
131 |
XListPeer(List target) { |
|
132 |
super(target); |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* Overridden from XWindow |
|
137 |
*/ |
|
138 |
public void preInit(XCreateWindowParams params) { |
|
139 |
super.preInit(params); |
|
140 |
||
141 |
// Stuff that must be initialized before layout() is called |
|
142 |
items = new Vector(); |
|
143 |
createVerScrollbar(); |
|
144 |
createHorScrollbar(); |
|
145 |
||
146 |
painter = new ListPainter(); |
|
147 |
||
148 |
// See 6246467 for more information |
|
149 |
bgColorSet = target.isBackgroundSet(); |
|
150 |
fgColorSet = target.isForegroundSet(); |
|
151 |
} |
|
152 |
||
153 |
public void postInit(XCreateWindowParams params) { |
|
154 |
super.postInit(params); |
|
155 |
initFontMetrics(); |
|
156 |
// TODO: more efficient way? |
|
157 |
// do we really want/need a copy of all the items? |
|
158 |
// get all items from target |
|
159 |
List l = (List)target; |
|
160 |
int stop = l.getItemCount(); |
|
161 |
for (int i = 0 ; i < stop; i++) { |
|
162 |
items.addElement(l.getItem(i)); |
|
163 |
} |
|
164 |
||
165 |
/* make the visible position visible. */ |
|
166 |
int index = l.getVisibleIndex(); |
|
167 |
if (index >= 0) { |
|
168 |
// Can't call makeVisible since it check scroll bar, |
|
169 |
// initialize scroll bar instead |
|
170 |
vsb.setValues(index, 0, 0, items.size()); |
|
171 |
} |
|
172 |
||
173 |
// NOTE: needs to have target set |
|
174 |
maxLength = maxLength(); |
|
175 |
||
176 |
// get the index containing all indexes to selected items |
|
177 |
int sel[] = l.getSelectedIndexes(); |
|
178 |
selected = new int[sel.length]; |
|
179 |
// TODO: shouldn't this be arraycopy()? |
|
180 |
for (int i = 0 ; i < sel.length ; i ++) { |
|
181 |
selected[i] = sel[i]; |
|
182 |
} |
|
183 |
// The select()ed item should become the focused item, but we don't |
|
184 |
// get the select() call because the peer generally hasn't yet been |
|
185 |
// created during app initialization. |
|
186 |
// TODO: For multi-select lists, it should be the highest selected index |
|
187 |
if (sel.length > 0) { |
|
188 |
setFocusIndex(sel[sel.length - 1]); |
|
189 |
} |
|
190 |
else { |
|
191 |
setFocusIndex(0); |
|
192 |
} |
|
193 |
||
194 |
multipleSelections = l.isMultipleMode(); |
|
195 |
} |
|
196 |
||
197 |
||
198 |
/** |
|
199 |
* add Vertical Scrollbar |
|
200 |
*/ |
|
201 |
void createVerScrollbar() { |
|
202 |
vsb = new XVerticalScrollbar(this); |
|
203 |
vsb.setValues(0, 0, 0, 0, 1, 1); |
|
204 |
} |
|
205 |
||
206 |
||
207 |
/** |
|
208 |
* add Horizontal scrollbar |
|
209 |
*/ |
|
210 |
void createHorScrollbar() { |
|
211 |
hsb = new XHorizontalScrollbar(this); |
|
212 |
hsb.setValues(0, 0, 0, 0, HORIZ_SCROLL_AMT, HORIZ_SCROLL_AMT); |
|
213 |
} |
|
214 |
||
215 |
/* New method name for 1.1 */ |
|
216 |
public void add(String item, int index) { |
|
217 |
addItem(item, index); |
|
218 |
} |
|
219 |
||
220 |
/* New method name for 1.1 */ |
|
221 |
public void removeAll() { |
|
222 |
clear(); |
|
223 |
maxLength = 0; |
|
224 |
} |
|
225 |
||
226 |
/* New method name for 1.1 */ |
|
227 |
public void setMultipleMode (boolean b) { |
|
228 |
setMultipleSelections(b); |
|
229 |
} |
|
230 |
||
231 |
/* New method name for 1.1 */ |
|
232 |
public Dimension getPreferredSize(int rows) { |
|
233 |
return preferredSize(rows); |
|
234 |
} |
|
235 |
||
236 |
/* New method name for 1.1 */ |
|
237 |
public Dimension getMinimumSize(int rows) { |
|
238 |
return minimumSize(rows); |
|
239 |
} |
|
240 |
||
241 |
/** |
|
242 |
* Minimum size. |
|
243 |
*/ |
|
244 |
public Dimension minimumSize() { |
|
245 |
return minimumSize(DEFAULT_VISIBLE_ROWS); |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* return the preferredSize |
|
250 |
*/ |
|
251 |
public Dimension preferredSize(int v) { |
|
252 |
return minimumSize(v); |
|
253 |
} |
|
254 |
||
255 |
/** |
|
256 |
* return the minimumsize |
|
257 |
*/ |
|
258 |
public Dimension minimumSize(int v) { |
|
259 |
FontMetrics fm = getFontMetrics(getFont()); |
|
260 |
initFontMetrics(); |
|
261 |
return new Dimension(20 + fm.stringWidth("0123456789abcde"), |
|
262 |
getItemHeight() * v + (2*MARGIN)); |
|
263 |
} |
|
264 |
||
265 |
/** |
|
266 |
* Calculate font metrics |
|
267 |
*/ |
|
268 |
void initFontMetrics() { |
|
269 |
FontMetrics fm = getFontMetrics(getFont()); |
|
270 |
fontHeight = fm.getHeight(); |
|
271 |
fontAscent = fm.getAscent(); |
|
272 |
fontLeading = fm.getLeading(); |
|
273 |
} |
|
274 |
||
275 |
||
276 |
/** |
|
277 |
* return the length of the largest item in the list |
|
278 |
*/ |
|
279 |
int maxLength() { |
|
280 |
FontMetrics fm = getFontMetrics(getFont()); |
|
281 |
int m = 0; |
|
282 |
int end = items.size(); |
|
283 |
for(int i = 0 ; i < end ; i++) { |
|
284 |
int l = fm.stringWidth(((String)items.elementAt(i))); |
|
285 |
m = Math.max(m, l); |
|
286 |
} |
|
287 |
return m; |
|
288 |
} |
|
289 |
||
290 |
/** |
|
291 |
* Calculates the width of item's label |
|
292 |
*/ |
|
293 |
int getItemWidth(int i) { |
|
294 |
FontMetrics fm = getFontMetrics(getFont()); |
|
295 |
return fm.stringWidth((String)items.elementAt(i)); |
|
296 |
} |
|
297 |
||
298 |
/** |
|
299 |
* return the on-screen width of the given string "str" |
|
300 |
*/ |
|
301 |
int stringLength(String str) { |
|
302 |
FontMetrics fm = getFontMetrics(target.getFont()); |
|
303 |
return fm.stringWidth(str); |
|
304 |
} |
|
305 |
||
306 |
public void setForeground(Color c) { |
|
307 |
fgColorSet = true; |
|
308 |
super.setForeground(c); |
|
309 |
} |
|
310 |
||
311 |
public void setBackground(Color c) { |
|
312 |
bgColorSet = true; |
|
313 |
super.setBackground(c); |
|
314 |
} |
|
315 |
||
316 |
/** |
|
317 |
* Returns the color that should be used to paint the background of |
|
318 |
* the list of items. Note that this is not the same as |
|
319 |
* target.getBackground() which is the color of the scrollbars, and the |
|
320 |
* lower-right corner of the Component when the scrollbars are displayed. |
|
321 |
*/ |
|
322 |
private Color getListBackground(Color[] colors) { |
|
323 |
if (bgColorSet) { |
|
324 |
return colors[BACKGROUND_COLOR]; |
|
325 |
} |
|
326 |
else { |
|
327 |
return SystemColor.text; |
|
328 |
} |
|
329 |
} |
|
330 |
||
331 |
/** |
|
332 |
* Returns the color that should be used to paint the list item text. |
|
333 |
*/ |
|
334 |
private Color getListForeground(Color[] colors) { |
|
335 |
if (fgColorSet) { |
|
336 |
return colors[FOREGROUND_COLOR]; |
|
337 |
} |
|
338 |
else { |
|
339 |
return SystemColor.textText; |
|
340 |
} |
|
341 |
} |
|
342 |
||
343 |
Rectangle getVScrollBarRec() { |
|
344 |
return new Rectangle(width - (SCROLLBAR_WIDTH), 0, SCROLLBAR_WIDTH+1, height); |
|
345 |
} |
|
346 |
||
347 |
Rectangle getHScrollBarRec() { |
|
348 |
return new Rectangle(0, height - SCROLLBAR_WIDTH, width, SCROLLBAR_WIDTH); |
|
349 |
} |
|
350 |
||
351 |
int getFirstVisibleItem() { |
|
352 |
if (vsbVis) { |
|
353 |
return vsb.getValue(); |
|
354 |
} else { |
|
355 |
return 0; |
|
356 |
} |
|
357 |
} |
|
358 |
||
359 |
int getLastVisibleItem() { |
|
360 |
if (vsbVis) { |
|
361 |
return Math.min(items.size()-1, vsb.getValue() + itemsInWindow() -1); |
|
362 |
} else { |
|
363 |
return Math.min(items.size()-1, itemsInWindow()-1); |
|
364 |
} |
|
365 |
} |
|
366 |
||
367 |
public void repaintScrollbarRequest(XScrollbar scrollbar) { |
|
368 |
Graphics g = getGraphics(); |
|
369 |
if (scrollbar == hsb) { |
|
370 |
repaint(PAINT_HSCROLL); |
|
371 |
} |
|
372 |
else if (scrollbar == vsb) { |
|
373 |
repaint(PAINT_VSCROLL); |
|
374 |
} |
|
375 |
} |
|
376 |
||
377 |
||
378 |
||
379 |
/** |
|
380 |
* Overridden for performance |
|
381 |
*/ |
|
382 |
public void repaint() { |
|
383 |
repaint(getFirstVisibleItem(), getLastVisibleItem(), PAINT_ALL); |
|
384 |
} |
|
385 |
||
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
386 |
private void repaint(int options) { |
2 | 387 |
repaint(getFirstVisibleItem(), getLastVisibleItem(), options); |
388 |
} |
|
389 |
||
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
390 |
private void repaint(int firstItem, int lastItem, int options) { |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
391 |
repaint(firstItem, lastItem, options, null, null); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
392 |
} |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
393 |
|
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
394 |
/** |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
395 |
* In most cases the entire area of the component doesn't have |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
396 |
* to be repainted. The method repaints the particular areas of |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
397 |
* the component. The areas to repaint is specified by the option |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
398 |
* parameter. The possible values of the option parameter are: |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
399 |
* PAINT_VSCROLL, PAINT_HSCROLL, PAINT_ITEMS, PAINT_FOCUS, |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
400 |
* PAINT_HIDEFOCUS, PAINT_BACKGROUND, PAINT_ALL, COPY_AREA. |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
401 |
* |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
402 |
* Note that the COPY_AREA value initiates copy of a source area |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
403 |
* of the component by a distance by means of the copyArea method |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
404 |
* of the Graphics class. |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
405 |
* |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
406 |
* @param firstItem the position of the first item of the range to repaint |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
407 |
* @param lastItem the position of the last item of the range to repaint |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
408 |
* @param options specifies the particular area of the component to repaint |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
409 |
* @param source the area of the component to copy |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
410 |
* @param distance the distance to copy the source area |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
411 |
*/ |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
412 |
private void repaint(int firstItem, int lastItem, int options, Rectangle source, Point distance) { |
2 | 413 |
Graphics g = getGraphics(); |
414 |
try { |
|
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
415 |
painter.paint(g, firstItem, lastItem, options, source, distance); |
2 | 416 |
} finally { |
417 |
g.dispose(); |
|
418 |
} |
|
419 |
} |
|
420 |
||
421 |
public void paint(Graphics g) { |
|
422 |
painter.paint(g, getFirstVisibleItem(), getLastVisibleItem(), PAINT_ALL); |
|
423 |
} |
|
424 |
||
425 |
public boolean isFocusable() { return true; } |
|
426 |
||
427 |
// TODO: share/promote the Focus methods? |
|
428 |
public void focusGained(FocusEvent e) { |
|
429 |
super.focusGained(e); |
|
430 |
repaint(PAINT_FOCUS); |
|
431 |
} |
|
432 |
||
433 |
public void focusLost(FocusEvent e) { |
|
434 |
super.focusLost(e); |
|
435 |
repaint(PAINT_FOCUS); |
|
436 |
} |
|
437 |
||
438 |
/** |
|
439 |
* Layout the sub-components of the List - that is, the scrollbars and the |
|
440 |
* list of items. |
|
441 |
*/ |
|
442 |
public void layout() { |
|
443 |
int vis, maximum; |
|
444 |
boolean vsbWasVisible; |
|
445 |
int origVSBVal; |
|
446 |
assert(target != null); |
|
447 |
||
448 |
// Start with assumption there is not a horizontal scrollbar, |
|
449 |
// see if we need a vertical scrollbar |
|
450 |
||
451 |
// Bug: If the list DOES have a horiz scrollbar and the value is set to |
|
452 |
// the very bottom value, value is reset in setValues() because it isn't |
|
453 |
// a valid value for cases when the list DOESN'T have a horiz scrollbar. |
|
454 |
// This is currently worked-around with origVSGVal. |
|
455 |
origVSBVal = vsb.getValue(); |
|
456 |
vis = itemsInWindow(false); |
|
457 |
maximum = items.size() < vis ? vis : items.size(); |
|
458 |
vsb.setValues(vsb.getValue(), vis, vsb.getMinimum(), maximum); |
|
459 |
vsbVis = vsbWasVisible = vsbIsVisible(false); |
|
460 |
listHeight = height; |
|
461 |
||
462 |
// now see if we need a horizontal scrollbar |
|
463 |
listWidth = getListWidth(); |
|
464 |
vis = listWidth - ((2 * SPACE) + (2 * MARGIN)); |
|
465 |
maximum = maxLength < vis ? vis : maxLength; |
|
466 |
hsb.setValues(hsb.getValue(), vis, hsb.getMinimum(), maximum); |
|
467 |
hsbVis = hsbIsVisible(vsbVis); |
|
468 |
||
469 |
if (hsbVis) { |
|
470 |
// do need a horizontal scrollbar, so recalculate height of |
|
471 |
// vertical s crollbar |
|
472 |
listHeight = height - SCROLLBAR_AREA; |
|
473 |
vis = itemsInWindow(true); |
|
474 |
maximum = items.size() < vis ? vis : items.size(); |
|
475 |
vsb.setValues(origVSBVal, vis, vsb.getMinimum(), maximum); |
|
476 |
vsbVis = vsbIsVisible(true); |
|
477 |
} |
|
478 |
||
479 |
// now check to make sure we haven't changed need for vertical |
|
480 |
// scrollbar - if we have, we need to |
|
481 |
// recalculate horizontal scrollbar width - then we're done... |
|
482 |
if (vsbWasVisible != vsbVis) { |
|
483 |
listWidth = getListWidth(); |
|
484 |
vis = listWidth - ((2 * SPACE) + (2 * MARGIN)); |
|
485 |
maximum = maxLength < vis ? 0 : maxLength; |
|
486 |
hsb.setValues(hsb.getValue(), vis, hsb.getMinimum(), maximum); |
|
487 |
hsbVis = hsbIsVisible(vsbVis); |
|
488 |
} |
|
489 |
||
490 |
vsb.setSize(SCROLLBAR_WIDTH, listHeight); |
|
491 |
hsb.setSize(listWidth, SCROLLBAR_WIDTH); |
|
492 |
||
493 |
vsb.setBlockIncrement(itemsInWindow()); |
|
494 |
hsb.setBlockIncrement(width - ((2 * SPACE) + (2 * MARGIN) + (vsbVis ? SCROLLBAR_AREA : 0))); |
|
495 |
} |
|
496 |
||
497 |
int getItemWidth() { |
|
498 |
return width - ((2 * MARGIN) + (vsbVis ? SCROLLBAR_AREA : 0)); |
|
499 |
} |
|
500 |
||
501 |
/* Returns height of an item in the list */ |
|
502 |
int getItemHeight() { |
|
503 |
return (fontHeight - fontLeading) + (2*SPACE); |
|
504 |
} |
|
505 |
||
506 |
int getItemX() { |
|
507 |
return MARGIN + SPACE; |
|
508 |
} |
|
509 |
||
510 |
int getItemY(int item) { |
|
511 |
return index2y(item); |
|
512 |
} |
|
513 |
||
514 |
int getFocusIndex() { |
|
515 |
return focusIndex; |
|
516 |
} |
|
517 |
||
518 |
void setFocusIndex(int value) { |
|
519 |
focusIndex = value; |
|
520 |
} |
|
521 |
||
522 |
/** |
|
523 |
* Update and return the focus rectangle. |
|
524 |
* Focus is around the focused item, if it is visible, or |
|
525 |
* around the border of the list if the focused item is scrolled off the top |
|
526 |
* or bottom of the list. |
|
527 |
*/ |
|
528 |
Rectangle getFocusRect() { |
|
529 |
Rectangle focusRect = new Rectangle(); |
|
530 |
// width is always only based on presence of vert sb |
|
531 |
focusRect.x = 1; |
|
532 |
focusRect.width = getListWidth() - 3; |
|
533 |
// if focused item is not currently displayed in the list, paint |
|
534 |
// focus around entire list (not including scrollbars) |
|
535 |
if (isIndexDisplayed(getFocusIndex())) { |
|
536 |
// focus rect is around the item |
|
537 |
focusRect.y = index2y(getFocusIndex()) - 2; |
|
538 |
focusRect.height = getItemHeight()+1; |
|
539 |
} else { |
|
540 |
// focus rect is around the list |
|
541 |
focusRect.y = 1; |
|
542 |
focusRect.height = hsbVis ? height - SCROLLBAR_AREA : height; |
|
543 |
focusRect.height -= 3; |
|
544 |
} |
|
545 |
return focusRect; |
|
546 |
} |
|
547 |
||
548 |
public void handleConfigureNotifyEvent(XEvent xev) { |
|
549 |
super.handleConfigureNotifyEvent(xev); |
|
550 |
||
551 |
// Update buffer |
|
552 |
painter.invalidate(); |
|
553 |
} |
|
554 |
public boolean handlesWheelScrolling() { return true; } |
|
555 |
||
556 |
// FIXME: need to support MouseWheel scrolling, too |
|
557 |
void handleJavaMouseEvent(MouseEvent e) { |
|
558 |
super.handleJavaMouseEvent(e); |
|
559 |
int i = e.getID(); |
|
560 |
switch (i) { |
|
561 |
case MouseEvent.MOUSE_PRESSED: |
|
562 |
mousePressed(e); |
|
563 |
break; |
|
564 |
case MouseEvent.MOUSE_RELEASED: |
|
565 |
mouseReleased(e); |
|
566 |
break; |
|
567 |
case MouseEvent.MOUSE_DRAGGED: |
|
568 |
mouseDragged(e); |
|
569 |
break; |
|
570 |
} |
|
571 |
} |
|
572 |
||
573 |
void handleJavaMouseWheelEvent(MouseWheelEvent e) { |
|
574 |
if (ListHelper.doWheelScroll(vsbVis ? vsb : null, |
|
575 |
hsbVis ? hsb : null, e)) { |
|
576 |
repaint(); |
|
577 |
} |
|
578 |
} |
|
579 |
||
580 |
void mousePressed(MouseEvent mouseEvent) { |
|
581 |
if (log.isLoggable(Level.FINER)) log.finer(mouseEvent.toString() + ", hsb " + hsbVis + ", vsb " + vsbVis); |
|
582 |
if (isEnabled() && mouseEvent.getButton() == MouseEvent.BUTTON1) { |
|
583 |
if (inWindow(mouseEvent.getX(), mouseEvent.getY())) { |
|
584 |
if (log.isLoggable(Level.FINE)) log.fine("Mouse press in items area"); |
|
585 |
active = WINDOW; |
|
586 |
int i = y2index(mouseEvent.getY()); |
|
587 |
if (i >= 0) { |
|
588 |
if (multipleSelections) { |
|
589 |
if (isSelected(i)) { |
|
590 |
// See 6243382 for more information |
|
591 |
deselectItem(i); |
|
592 |
eventIndex = i; |
|
593 |
eventType = ItemEvent.DESELECTED; |
|
594 |
} |
|
595 |
else { |
|
596 |
selectItem(i); |
|
597 |
eventIndex = i; |
|
598 |
eventType = ItemEvent.SELECTED; |
|
599 |
} |
|
600 |
} |
|
601 |
// Backward-compatible bug: even if a single-select |
|
602 |
// item is already selected, we send an ITEM_STATE_CHANGED/ |
|
603 |
// SELECTED event. Engineer's Toolbox appears to rely on |
|
604 |
// this. |
|
605 |
//else if (!isSelected(i)) { |
|
606 |
else { |
|
607 |
selectItem(i); |
|
608 |
eventIndex = i; |
|
609 |
eventType = ItemEvent.SELECTED; |
|
610 |
} |
|
611 |
// Restoring Windows behaviour |
|
612 |
// We should update focus index after "mouse pressed" event |
|
613 |
setFocusIndex(i); |
|
614 |
repaint(PAINT_FOCUS); |
|
615 |
} else { |
|
616 |
// 6426186: reset variable to prevent action event |
|
617 |
// if user clicks on unoccupied area of list |
|
618 |
currentIndex = -1; |
|
619 |
} |
|
620 |
} else if (inVerticalScrollbar(mouseEvent.getX(), mouseEvent.getY())) { |
|
621 |
if (log.isLoggable(Level.FINE)) log.fine("Mouse press in vertical scrollbar"); |
|
622 |
active = VERSCROLLBAR; |
|
623 |
vsb.handleMouseEvent(mouseEvent.getID(), |
|
624 |
mouseEvent.getModifiers(), |
|
625 |
mouseEvent.getX() - (width - SCROLLBAR_WIDTH), |
|
626 |
mouseEvent.getY()); |
|
627 |
} else if (inHorizontalScrollbar(mouseEvent.getX(), mouseEvent.getY())) { |
|
628 |
if (log.isLoggable(Level.FINE)) log.fine("Mouse press in horizontal scrollbar"); |
|
629 |
active = HORSCROLLBAR; |
|
630 |
hsb.handleMouseEvent(mouseEvent.getID(), |
|
631 |
mouseEvent.getModifiers(), |
|
632 |
mouseEvent.getX(), |
|
633 |
mouseEvent.getY() - (height - SCROLLBAR_WIDTH)); |
|
634 |
||
635 |
} |
|
636 |
isMousePressed = true; |
|
637 |
} |
|
638 |
} |
|
639 |
void mouseReleased(MouseEvent mouseEvent) { |
|
640 |
if (isEnabled() && mouseEvent.getButton() == MouseEvent.BUTTON1) { |
|
641 |
//winReleaseCursorFocus(); |
|
642 |
int clickCount = mouseEvent.getClickCount(); |
|
643 |
if (active == VERSCROLLBAR) { |
|
644 |
vsb.handleMouseEvent(mouseEvent.getID(), |
|
645 |
mouseEvent.getModifiers(), |
|
646 |
mouseEvent.getX()-(width-SCROLLBAR_WIDTH), |
|
647 |
mouseEvent.getY()); |
|
648 |
} else if(active == HORSCROLLBAR) { |
|
649 |
hsb.handleMouseEvent(mouseEvent.getID(), |
|
650 |
mouseEvent.getModifiers(), |
|
651 |
mouseEvent.getX(), |
|
652 |
mouseEvent.getY()-(height-SCROLLBAR_WIDTH)); |
|
653 |
} else if ( ( currentIndex >= 0 ) && ( clickCount >= 2 ) && |
|
654 |
( clickCount % 2 == 0 ) ) { |
|
655 |
postEvent(new ActionEvent(target, |
|
656 |
ActionEvent.ACTION_PERFORMED, |
|
657 |
(String)items.elementAt(currentIndex), |
|
658 |
mouseEvent.getWhen(), |
|
659 |
mouseEvent.getModifiers())); // No ext mods |
|
660 |
} else if (active == WINDOW) { |
|
661 |
// See 6243382 for more information |
|
662 |
trackMouseReleasedScroll(); |
|
663 |
||
664 |
if (eventType == ItemEvent.DESELECTED) { |
|
665 |
assert multipleSelections : "Shouldn't get a deselect for a single-select List"; |
|
666 |
// Paint deselection the release |
|
667 |
deselectItem(eventIndex); |
|
668 |
} |
|
669 |
if (eventType != NONE) { |
|
670 |
postEvent(new ItemEvent((List)target, |
|
671 |
ItemEvent.ITEM_STATE_CHANGED, |
|
672 |
Integer.valueOf(eventIndex), |
|
673 |
eventType)); |
|
674 |
} |
|
675 |
} |
|
676 |
active = NONE; |
|
677 |
eventIndex = -1; |
|
678 |
eventType = NONE; |
|
679 |
isMousePressed = false; |
|
680 |
} |
|
681 |
} |
|
682 |
||
683 |
void mouseDragged(MouseEvent mouseEvent) { |
|
684 |
// TODO: can you drag w/ any other buttons? what about multiple buttons? |
|
685 |
if (isEnabled() && |
|
686 |
(mouseEvent.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) != 0) { |
|
687 |
if ((active == VERSCROLLBAR)) { |
|
688 |
vsb.handleMouseEvent(mouseEvent.getID(), |
|
689 |
mouseEvent.getModifiers(), |
|
690 |
mouseEvent.getX()-(width-SCROLLBAR_WIDTH), |
|
691 |
mouseEvent.getY()); |
|
692 |
} else if ((active == HORSCROLLBAR)) { |
|
693 |
hsb.handleMouseEvent(mouseEvent.getID(), |
|
694 |
mouseEvent.getModifiers(), |
|
695 |
mouseEvent.getX(), |
|
696 |
mouseEvent.getY()-(height-SCROLLBAR_WIDTH)); |
|
697 |
} else if (active == WINDOW) { |
|
698 |
int i = y2index(mouseEvent.getY()); |
|
699 |
if (multipleSelections) { |
|
700 |
// Multi-select only: |
|
701 |
// If a selected item was pressed on and then dragged off |
|
702 |
// of, cancel the pending deselect. |
|
703 |
if (eventType == ItemEvent.DESELECTED) { |
|
704 |
if (i != eventIndex) { |
|
705 |
eventType = NONE; |
|
706 |
eventIndex = -1; |
|
707 |
} |
|
708 |
} |
|
709 |
} |
|
710 |
else if (eventType == ItemEvent.SELECTED) { |
|
711 |
// Single-select only: |
|
712 |
// If an unselected item was pressed on, track the drag |
|
713 |
// and select the item under the mouse |
|
714 |
||
715 |
// See 6243382 for more information |
|
716 |
trackMouseDraggedScroll(mouseEvent); |
|
717 |
||
718 |
if (i >= 0 && !isSelected(i)) { |
|
719 |
int oldSel = eventIndex; |
|
720 |
selectItem(i); |
|
721 |
eventIndex = i; |
|
722 |
repaint(oldSel, eventIndex, PAINT_ITEMS); |
|
723 |
} |
|
724 |
} |
|
725 |
// Restoring Windows behaviour |
|
726 |
// We should update focus index after "mouse dragged" event |
|
727 |
if (i >= 0) { |
|
728 |
setFocusIndex(i); |
|
729 |
repaint(PAINT_FOCUS); |
|
730 |
} |
|
731 |
} |
|
732 |
} |
|
733 |
} |
|
734 |
||
735 |
/* |
|
736 |
* Helper method for XListPeer with integrated vertical scrollbar. |
|
737 |
* Start or stop vertical scrolling when mouse dragged in / out the area of the list if it's required |
|
738 |
* Restoring Motif behavior |
|
739 |
* See 6243382 for more information |
|
740 |
*/ |
|
741 |
void trackMouseDraggedScroll(MouseEvent mouseEvent){ |
|
742 |
||
743 |
if (vsb.beforeThumb(mouseEvent.getX(), mouseEvent.getY())) { |
|
744 |
vsb.setMode(AdjustmentEvent.UNIT_DECREMENT); |
|
745 |
} else { |
|
746 |
vsb.setMode(AdjustmentEvent.UNIT_INCREMENT); |
|
747 |
} |
|
748 |
||
749 |
if(mouseEvent.getY() < 0 || mouseEvent.getY() >= listHeight){ |
|
750 |
if (!mouseDraggedOutVertically){ |
|
751 |
mouseDraggedOutVertically = true; |
|
752 |
vsb.startScrollingInstance(); |
|
753 |
} |
|
754 |
}else{ |
|
755 |
if (mouseDraggedOutVertically){ |
|
756 |
mouseDraggedOutVertically = false; |
|
757 |
vsb.stopScrollingInstance(); |
|
758 |
} |
|
759 |
} |
|
760 |
||
761 |
if (hsb.beforeThumb(mouseEvent.getX(), mouseEvent.getY())) { |
|
762 |
hsb.setMode(AdjustmentEvent.UNIT_DECREMENT); |
|
763 |
} else { |
|
764 |
hsb.setMode(AdjustmentEvent.UNIT_INCREMENT); |
|
765 |
} |
|
766 |
||
767 |
if (mouseEvent.getX() < 0 || mouseEvent.getX() >= listWidth) { |
|
768 |
if (!mouseDraggedOutHorizontally){ |
|
769 |
mouseDraggedOutHorizontally = true; |
|
770 |
hsb.startScrollingInstance(); |
|
771 |
} |
|
772 |
}else{ |
|
773 |
if (mouseDraggedOutHorizontally){ |
|
774 |
mouseDraggedOutHorizontally = false; |
|
775 |
hsb.stopScrollingInstance(); |
|
776 |
} |
|
777 |
} |
|
778 |
} |
|
779 |
||
780 |
/* |
|
781 |
* Helper method for XListPeer with integrated vertical scrollbar. |
|
782 |
* Stop vertical scrolling when mouse released in / out the area of the list if it's required |
|
783 |
* Restoring Motif behavior |
|
784 |
* see 6243382 for more information |
|
785 |
*/ |
|
786 |
void trackMouseReleasedScroll(){ |
|
787 |
||
788 |
if (mouseDraggedOutVertically){ |
|
789 |
mouseDraggedOutVertically = false; |
|
790 |
vsb.stopScrollingInstance(); |
|
791 |
} |
|
792 |
||
793 |
if (mouseDraggedOutHorizontally){ |
|
794 |
mouseDraggedOutHorizontally = false; |
|
795 |
hsb.stopScrollingInstance(); |
|
796 |
} |
|
797 |
} |
|
798 |
||
799 |
void handleJavaKeyEvent(KeyEvent e) { |
|
800 |
switch(e.getID()) { |
|
801 |
case KeyEvent.KEY_PRESSED: |
|
802 |
if (!isMousePressed){ |
|
803 |
keyPressed(e); |
|
804 |
} |
|
805 |
break; |
|
806 |
} |
|
807 |
} |
|
808 |
||
809 |
void keyPressed(KeyEvent e) { |
|
810 |
int keyCode = e.getKeyCode(); |
|
811 |
if (log.isLoggable(Level.FINE)) log.fine(e.toString()); |
|
812 |
switch(keyCode) { |
|
813 |
case KeyEvent.VK_UP: |
|
814 |
case KeyEvent.VK_KP_UP: // TODO: I assume we also want this, too |
|
815 |
if (getFocusIndex() > 0) { |
|
816 |
setFocusIndex(getFocusIndex()-1); |
|
817 |
repaint(PAINT_HIDEFOCUS); |
|
818 |
// If single-select, select the item |
|
819 |
if (!multipleSelections) { |
|
820 |
selectItem(getFocusIndex()); |
|
821 |
postEvent(new ItemEvent((List)target, |
|
822 |
ItemEvent.ITEM_STATE_CHANGED, |
|
823 |
Integer.valueOf(getFocusIndex()), |
|
824 |
ItemEvent.SELECTED)); |
|
825 |
} |
|
826 |
if (isItemHidden(getFocusIndex())) { |
|
827 |
makeVisible(getFocusIndex()); |
|
828 |
} |
|
829 |
else { |
|
830 |
repaint(PAINT_FOCUS); |
|
831 |
} |
|
832 |
} |
|
833 |
break; |
|
834 |
case KeyEvent.VK_DOWN: |
|
835 |
case KeyEvent.VK_KP_DOWN: // TODO: I assume we also want this, too |
|
836 |
if (getFocusIndex() < items.size() - 1) { |
|
837 |
setFocusIndex(getFocusIndex()+1); |
|
838 |
repaint(PAINT_HIDEFOCUS); |
|
839 |
// If single-select, select the item |
|
840 |
if (!multipleSelections) { |
|
841 |
selectItem(getFocusIndex()); |
|
842 |
postEvent(new ItemEvent((List)target, |
|
843 |
ItemEvent.ITEM_STATE_CHANGED, |
|
844 |
Integer.valueOf(getFocusIndex()), |
|
845 |
ItemEvent.SELECTED)); |
|
846 |
} |
|
847 |
if (isItemHidden(getFocusIndex())) { |
|
848 |
makeVisible(getFocusIndex()); |
|
849 |
} |
|
850 |
else { |
|
851 |
repaint(PAINT_FOCUS); |
|
852 |
} |
|
853 |
} |
|
854 |
break; |
|
855 |
case KeyEvent.VK_PAGE_UP: { |
|
856 |
// Assumes that scrollbar does its own bounds-checking |
|
857 |
int previousValue = vsb.getValue(); |
|
858 |
vsb.setValue(vsb.getValue() - vsb.getBlockIncrement()); |
|
859 |
int currentValue = vsb.getValue(); |
|
860 |
// 6190768 pressing pg-up on AWT multiple selection lists the items but no item event is triggered, on XToolkit |
|
861 |
// Restoring Motif behavior |
|
862 |
if (previousValue!=currentValue) { |
|
863 |
setFocusIndex(Math.max(getFocusIndex()-itemsInWindow(), 0)); |
|
864 |
if (!multipleSelections){ |
|
865 |
selectItem(getFocusIndex()); |
|
866 |
postEvent(new ItemEvent((List)target, |
|
867 |
ItemEvent.ITEM_STATE_CHANGED, |
|
868 |
Integer.valueOf(getFocusIndex()), |
|
869 |
ItemEvent.SELECTED)); |
|
870 |
} |
|
871 |
} |
|
872 |
repaint(); |
|
873 |
break; |
|
874 |
} |
|
875 |
case KeyEvent.VK_PAGE_DOWN: { |
|
876 |
// Assumes that scrollbar does its own bounds-checking |
|
877 |
int previousValue = vsb.getValue(); |
|
878 |
vsb.setValue(vsb.getValue() + vsb.getBlockIncrement()); |
|
879 |
int currentValue = vsb.getValue(); |
|
880 |
// 6190768 pressing pg-down on AWT multiple selection list selects the items but no item event is triggered, on XToolkit |
|
881 |
// Restoring Motif behavior |
|
882 |
if (previousValue!=currentValue) { |
|
883 |
setFocusIndex(Math.min(getFocusIndex() + itemsInWindow(), items.size()-1)); |
|
884 |
if (!multipleSelections){ |
|
885 |
selectItem(getFocusIndex()); |
|
886 |
postEvent(new ItemEvent((List)target, |
|
887 |
ItemEvent.ITEM_STATE_CHANGED, |
|
888 |
Integer.valueOf(getFocusIndex()), |
|
889 |
ItemEvent.SELECTED)); |
|
890 |
} |
|
891 |
} |
|
892 |
repaint(); |
|
893 |
break; |
|
894 |
} |
|
895 |
case KeyEvent.VK_LEFT: |
|
896 |
case KeyEvent.VK_KP_LEFT: |
|
897 |
if (hsbVis & hsb.getValue() > 0) { |
|
898 |
hsb.setValue(hsb.getValue() - HORIZ_SCROLL_AMT); |
|
899 |
repaint(); |
|
900 |
} |
|
901 |
break; |
|
902 |
case KeyEvent.VK_RIGHT: |
|
903 |
case KeyEvent.VK_KP_RIGHT: |
|
904 |
if (hsbVis) { // Should check if already at end |
|
905 |
hsb.setValue(hsb.getValue() + HORIZ_SCROLL_AMT); |
|
906 |
repaint(); |
|
907 |
} |
|
908 |
break; |
|
909 |
// 6190778 CTRL + HOME, CTRL + END keys do not work properly for list on XToolkit |
|
910 |
// Restoring Motif behavior |
|
911 |
case KeyEvent.VK_HOME: |
|
912 |
if (!e.isControlDown() || ((List)target).getItemCount() <= 0) |
|
913 |
break; |
|
914 |
if (vsbVis) { |
|
915 |
vsb.setValue(vsb.getMinimum()); |
|
916 |
} |
|
917 |
setFocusIndex(0); |
|
918 |
if (!multipleSelections) { |
|
919 |
selectItem(getFocusIndex()); |
|
920 |
postEvent(new ItemEvent((List)target, |
|
921 |
ItemEvent.ITEM_STATE_CHANGED, |
|
922 |
Integer.valueOf(getFocusIndex()), |
|
923 |
ItemEvent.SELECTED)); |
|
924 |
} |
|
925 |
repaint(); |
|
926 |
break; |
|
927 |
case KeyEvent.VK_END: |
|
928 |
if (!e.isControlDown() || ((List)target).getItemCount() <= 0) |
|
929 |
break; |
|
930 |
if (vsbVis) { |
|
931 |
vsb.setValue(vsb.getMaximum()); |
|
932 |
} |
|
933 |
setFocusIndex(items.size()-1); |
|
934 |
if (!multipleSelections) { |
|
935 |
selectItem(getFocusIndex()); |
|
936 |
postEvent(new ItemEvent((List)target, |
|
937 |
ItemEvent.ITEM_STATE_CHANGED, |
|
938 |
Integer.valueOf(getFocusIndex()), |
|
939 |
ItemEvent.SELECTED)); |
|
940 |
} |
|
941 |
repaint(); |
|
942 |
break; |
|
943 |
case KeyEvent.VK_SPACE: |
|
944 |
// Fixed 6299853: XToolkit: Pressing space triggers ItemStateChanged event after List.removeAll called |
|
945 |
// If getFocusIndex() is less than 0, the event will not be triggered when space pressed |
|
946 |
if (getFocusIndex() < 0 || ((List)target).getItemCount() <= 0) { |
|
947 |
break; |
|
948 |
} |
|
949 |
||
950 |
boolean isSelected = isSelected(getFocusIndex()); |
|
951 |
||
952 |
// Spacebar only deselects for multi-select Lists |
|
953 |
if (multipleSelections && isSelected) { |
|
954 |
deselectItem(getFocusIndex()); |
|
955 |
postEvent(new ItemEvent((List)target, |
|
956 |
ItemEvent.ITEM_STATE_CHANGED, |
|
957 |
Integer.valueOf(getFocusIndex()), |
|
958 |
ItemEvent.DESELECTED)); |
|
959 |
} |
|
960 |
else if (!isSelected) { // Note: this changes the Solaris/Linux |
|
961 |
// behavior to match that of win32. |
|
962 |
// That is, pressing space bar on a |
|
963 |
// single-select list when the focused |
|
964 |
// item is already selected does NOT |
|
965 |
// send an ItemEvent.SELECTED event. |
|
966 |
selectItem(getFocusIndex()); |
|
967 |
postEvent(new ItemEvent((List)target, |
|
968 |
ItemEvent.ITEM_STATE_CHANGED, |
|
969 |
Integer.valueOf(getFocusIndex()), |
|
970 |
ItemEvent.SELECTED)); |
|
971 |
} |
|
972 |
break; |
|
973 |
case KeyEvent.VK_ENTER: |
|
974 |
// It looks to me like there are bugs as well as inconsistencies |
|
975 |
// in the way the Enter key is handled by both Solaris and Windows. |
|
976 |
// So for now in XAWT, I'm going to simply go by what the List docs |
|
977 |
// say: "AWT also generates an action event when the user presses |
|
978 |
// the return key while an item in the list is selected." |
|
979 |
if (selected.length > 0) { |
|
980 |
postEvent(new ActionEvent((List)target, |
|
981 |
ActionEvent.ACTION_PERFORMED, |
|
982 |
(String)items.elementAt(getFocusIndex()), |
|
983 |
e.getWhen(), |
|
984 |
e.getModifiers())); // ActionEvent doesn't have |
|
985 |
// extended modifiers. |
|
986 |
} |
|
987 |
break; |
|
988 |
} |
|
989 |
} |
|
990 |
||
991 |
/** |
|
992 |
* return value from the scrollbar |
|
993 |
*/ |
|
994 |
public void notifyValue(XScrollbar obj, int type, int v, boolean isAdjusting) { |
|
995 |
||
996 |
if (log.isLoggable(Level.FINE)) log.fine("Notify value changed on " + obj + " to " + v); |
|
997 |
int value = obj.getValue(); |
|
998 |
if (obj == vsb) { |
|
999 |
scrollVertical(v - value); |
|
1000 |
||
1001 |
// See 6243382 for more information |
|
1002 |
int oldSel = eventIndex; |
|
1003 |
int newSel = eventIndex+v-value; |
|
1004 |
if (mouseDraggedOutVertically && !isSelected(newSel)){ |
|
1005 |
selectItem(newSel); |
|
1006 |
eventIndex = newSel; |
|
1007 |
repaint(oldSel, eventIndex, PAINT_ITEMS); |
|
1008 |
// Scrolling select() should also set the focus index |
|
1009 |
// Otherwise, the updating of the 'focusIndex' variable will be incorrect |
|
1010 |
// if user drag mouse out of the area of the list |
|
1011 |
setFocusIndex(newSel); |
|
1012 |
repaint(PAINT_FOCUS); |
|
1013 |
} |
|
1014 |
||
1015 |
} else if ((XHorizontalScrollbar)obj == hsb) { |
|
1016 |
scrollHorizontal(v - value); |
|
1017 |
} |
|
1018 |
||
1019 |
} |
|
1020 |
||
1021 |
/** |
|
1022 |
* deselect all items in List |
|
1023 |
*/ |
|
1024 |
private void deselectAllItems() { |
|
1025 |
selected = new int [0]; |
|
1026 |
repaint(PAINT_ITEMS); |
|
1027 |
} |
|
1028 |
||
1029 |
/** |
|
1030 |
* set multiple selections |
|
1031 |
*/ |
|
1032 |
public void setMultipleSelections(boolean v) { |
|
1033 |
if (multipleSelections != v) { |
|
1034 |
if ( !v) { |
|
1035 |
int selPos = ( isSelected( focusIndex )) ? focusIndex: -1; |
|
1036 |
deselectAllItems(); |
|
1037 |
if (selPos != -1){ |
|
1038 |
selectItem(selPos); |
|
1039 |
} |
|
1040 |
} |
|
1041 |
multipleSelections = v; |
|
1042 |
} |
|
1043 |
} |
|
1044 |
||
1045 |
/** |
|
1046 |
* add an item |
|
1047 |
* if the index of the item is < 0 or >= than items.size() |
|
1048 |
* then add the item to the end of the list |
|
1049 |
*/ |
|
1050 |
public void addItem(String item, int i) { |
|
1051 |
int oldMaxLength = maxLength; |
|
1052 |
boolean hsbWasVis = hsbVis; |
|
1053 |
boolean vsbWasVis = vsbVis; |
|
1054 |
||
1055 |
int addedIndex = 0; // Index where the new item ended up |
|
1056 |
if (i < 0 || i >= items.size()) { |
|
1057 |
i = -1; |
|
1058 |
} |
|
1059 |
||
1060 |
// Why we set this variable to -1 in spite of the fact that selected[] is changed in other way? |
|
1061 |
// It's not clear how to reproduce incorrect behaviour based on this assignment |
|
1062 |
// since before using this variable (mouseReleased) we certainly update it to correct value |
|
1063 |
// So we don't modify this behaviour now |
|
1064 |
currentIndex = -1; |
|
1065 |
||
1066 |
if (i == -1) { |
|
1067 |
items.addElement(item); |
|
1068 |
i = 0; // fix the math for the paintItems test |
|
1069 |
addedIndex = items.size() - 1; |
|
1070 |
} else { |
|
1071 |
items.insertElementAt(item, i); |
|
1072 |
addedIndex = i; |
|
1073 |
for (int j = 0 ; j < selected.length ; j++) { |
|
1074 |
if (selected[j] >= i) { |
|
1075 |
selected[j] += 1; |
|
1076 |
} |
|
1077 |
} |
|
1078 |
} |
|
1079 |
if (log.isLoggable(Level.FINER)) log.finer("Adding item '" + item + "' to " + addedIndex); |
|
1080 |
||
1081 |
// Update maxLength |
|
1082 |
boolean repaintItems = !isItemHidden(addedIndex); |
|
1083 |
maxLength = Math.max(maxLength, getItemWidth(addedIndex)); |
|
1084 |
layout(); |
|
1085 |
||
1086 |
int options = 0; |
|
1087 |
if (vsbVis != vsbWasVis || hsbVis != hsbWasVis) { |
|
1088 |
// Scrollbars are being added or removed, so we must repaint all |
|
1089 |
options = PAINT_ALL; |
|
1090 |
} |
|
1091 |
else { |
|
1092 |
options = (repaintItems ? (PAINT_ITEMS):0) |
|
1093 |
| ((maxLength != oldMaxLength || (hsbWasVis ^ hsbVis))?(PAINT_HSCROLL):0) |
|
1094 |
| ((vsb.needsRepaint())?(PAINT_VSCROLL):0); |
|
1095 |
||
1096 |
} |
|
1097 |
if (log.isLoggable(Level.FINEST)) log.finest("Last visible: " + getLastVisibleItem() + |
|
1098 |
", hsb changed : " + (hsbWasVis ^ hsbVis) + ", items changed " + repaintItems); |
|
1099 |
repaint(addedIndex, getLastVisibleItem(), options); |
|
1100 |
} |
|
1101 |
||
1102 |
/** |
|
1103 |
* delete items starting with s (start position) to e (end position) including s and e |
|
1104 |
* if s < 0 then s = 0 |
|
1105 |
* if e >= items.size() then e = items.size() - 1 |
|
1106 |
*/ |
|
1107 |
public void delItems(int s, int e) { |
|
1108 |
// save the current state of the scrollbars |
|
1109 |
boolean hsbWasVisible = hsbVis; |
|
1110 |
boolean vsbWasVisible = vsbVis; |
|
1111 |
int oldLastDisplayed = lastItemDisplayed(); |
|
1112 |
||
1113 |
if (log.isLoggable(Level.FINE)) log.fine("Deleting from " + s + " to " + e); |
|
1114 |
||
1115 |
if (log.isLoggable(Level.FINEST)) log.finest("Last displayed item: " + oldLastDisplayed + ", items in window " + itemsInWindow() + |
|
1116 |
", size " + items.size()); |
|
1117 |
||
1118 |
if (items.size() == 0) { |
|
1119 |
return; |
|
1120 |
} |
|
1121 |
||
1122 |
// if user passed in flipped args, reverse them |
|
1123 |
if (s > e) { |
|
1124 |
int tmp = s; |
|
1125 |
s = e; |
|
1126 |
e = tmp; |
|
1127 |
} |
|
1128 |
||
1129 |
// check for starting point less than zero |
|
1130 |
if (s < 0) { |
|
1131 |
s = 0; |
|
1132 |
} |
|
1133 |
||
1134 |
// check for end point greater than the size of the list |
|
1135 |
if (e >= items.size()) { |
|
1136 |
e = items.size() - 1; |
|
1137 |
} |
|
1138 |
||
1139 |
// determine whether we're going to delete any visible elements |
|
1140 |
// repaint must also be done if scrollbars appear/disappear, which |
|
1141 |
// can happen from removing a non-showing list item |
|
1142 |
/* |
|
1143 |
boolean repaintNeeded = |
|
1144 |
((s <= lastItemDisplayed()) && (e >= vsb.getValue())); |
|
1145 |
*/ |
|
1146 |
boolean repaintNeeded = (s >= getFirstVisibleItem() && s <= getLastVisibleItem()); |
|
1147 |
||
1148 |
// delete the items out of the items list and out of the selected list |
|
1149 |
for (int i = s ; i <= e ; i++) { |
|
1150 |
items.removeElementAt(s); |
|
1151 |
int j = posInSel(i); |
|
1152 |
if (j != -1) { |
|
1153 |
int newsel[] = new int[selected.length - 1]; |
|
1154 |
System.arraycopy(selected, 0, newsel, 0, j); |
|
1155 |
System.arraycopy(selected, j + 1, newsel, j, selected.length - (j + 1)); |
|
1156 |
selected = newsel; |
|
1157 |
} |
|
1158 |
||
1159 |
} |
|
1160 |
||
1161 |
// update the indexes in the selected array |
|
1162 |
int diff = (e - s) + 1; |
|
1163 |
for (int i = 0 ; i < selected.length ; i++) { |
|
1164 |
if (selected[i] > e) { |
|
1165 |
selected[i] -= diff; |
|
1166 |
} |
|
1167 |
} |
|
1168 |
||
1169 |
int options = PAINT_VSCROLL; |
|
1170 |
// focusedIndex updating according to native (Window, Motif) behaviour |
|
1171 |
if (getFocusIndex() > e) { |
|
1172 |
setFocusIndex(getFocusIndex() - (e - s + 1)); |
|
1173 |
options |= PAINT_FOCUS; |
|
1174 |
} else if (getFocusIndex() >= s && getFocusIndex() <= e) { |
|
1175 |
// Fixed 6299858: PIT. Focused border not shown on List if selected item is removed, XToolkit |
|
1176 |
// We should set focus to new first item if the current first item was removed |
|
1177 |
// except if the list is empty |
|
1178 |
int focusBound = (items.size() > 0) ? 0 : -1; |
|
1179 |
setFocusIndex(Math.max(s-1, focusBound)); |
|
1180 |
options |= PAINT_FOCUS; |
|
1181 |
} |
|
1182 |
||
1183 |
if (log.isLoggable(Level.FINEST)) log.finest("Multiple selections: " + multipleSelections); |
|
1184 |
||
1185 |
// update vsb.val |
|
1186 |
if (vsb.getValue() >= s) { |
|
1187 |
if (vsb.getValue() <= e) { |
|
1188 |
vsb.setValue(e+1 - diff); |
|
1189 |
} else { |
|
1190 |
vsb.setValue(vsb.getValue() - diff); |
|
1191 |
} |
|
1192 |
} |
|
1193 |
||
1194 |
int oldMaxLength = maxLength; |
|
1195 |
maxLength = maxLength(); |
|
1196 |
if (maxLength != oldMaxLength) { |
|
1197 |
// Width of the items changed affecting the range of |
|
1198 |
// horizontal scrollbar |
|
1199 |
options |= PAINT_HSCROLL; |
|
1200 |
} |
|
1201 |
layout(); |
|
1202 |
repaintNeeded |= (vsbWasVisible ^ vsbVis) || (hsbWasVisible ^ hsbVis); // If scrollbars visibility changed |
|
1203 |
if (repaintNeeded) { |
|
1204 |
options |= PAINT_ALL; |
|
1205 |
} |
|
1206 |
repaint(s, oldLastDisplayed, options); |
|
1207 |
} |
|
1208 |
||
1209 |
/** |
|
1210 |
* ListPeer method |
|
1211 |
*/ |
|
1212 |
public void select(int index) { |
|
1213 |
// Programmatic select() should also set the focus index |
|
1214 |
setFocusIndex(index); |
|
1215 |
repaint(PAINT_FOCUS); |
|
1216 |
selectItem(index); |
|
1217 |
} |
|
1218 |
||
1219 |
/** |
|
1220 |
* select the index |
|
1221 |
* redraw the list to the screen |
|
1222 |
*/ |
|
1223 |
void selectItem(int index) { |
|
1224 |
// NOTE: instead of recalculating and the calling repaint(), painting |
|
1225 |
// is done immediately |
|
1226 |
||
1227 |
// 6190746 List does not trigger ActionEvent when double clicking a programmatically selected item, XToolkit |
|
1228 |
// If we invoke select(int) before setVisible(boolean), then variable currentIndex will equals -1. At the same time isSelected may be true. |
|
1229 |
// Restoring Motif behavior |
|
1230 |
currentIndex = index; |
|
1231 |
||
1232 |
if (isSelected(index)) { |
|
1233 |
return; |
|
1234 |
} |
|
1235 |
if (!multipleSelections) { |
|
1236 |
if (selected.length == 0) { // No current selection |
|
1237 |
selected = new int[1]; |
|
1238 |
selected[0] = index; |
|
1239 |
} |
|
1240 |
else { |
|
1241 |
int oldSel = selected[0]; |
|
1242 |
selected[0] = index; |
|
1243 |
if (!isItemHidden(oldSel)) { |
|
1244 |
// Only bother painting if item is visible (4895367) |
|
1245 |
repaint(oldSel, oldSel, PAINT_ITEMS); |
|
1246 |
} |
|
1247 |
} |
|
1248 |
} else { |
|
1249 |
// insert "index" into the selection array |
|
1250 |
int newsel[] = new int[selected.length + 1]; |
|
1251 |
int i = 0; |
|
1252 |
while (i < selected.length && index > selected[i]) { |
|
1253 |
newsel[i] = selected[i]; |
|
1254 |
i++; |
|
1255 |
} |
|
1256 |
newsel[i] = index; |
|
1257 |
System.arraycopy(selected, i, newsel, i+1, selected.length - i); |
|
1258 |
selected = newsel; |
|
1259 |
} |
|
1260 |
if (!isItemHidden(index)) { |
|
1261 |
// Only bother painting if item is visible (4895367) |
|
1262 |
repaint(index, index, PAINT_ITEMS); |
|
1263 |
} |
|
1264 |
} |
|
1265 |
||
1266 |
/** |
|
1267 |
* ListPeer method |
|
1268 |
* focusedIndex isn't updated according to native (Window, Motif) behaviour |
|
1269 |
*/ |
|
1270 |
public void deselect(int index) { |
|
1271 |
deselectItem(index); |
|
1272 |
} |
|
1273 |
||
1274 |
/** |
|
1275 |
* deselect the index |
|
1276 |
* redraw the list to the screen |
|
1277 |
*/ |
|
1278 |
void deselectItem(int index) { |
|
1279 |
if (!isSelected(index)) { |
|
1280 |
return; |
|
1281 |
} |
|
1282 |
if (!multipleSelections) { |
|
1283 |
// TODO: keep an int[0] and int[1] around and just use them instead |
|
1284 |
// creating new ones all the time |
|
1285 |
selected = new int[0]; |
|
1286 |
} else { |
|
1287 |
int i = posInSel(index); |
|
1288 |
int newsel[] = new int[selected.length - 1]; |
|
1289 |
System.arraycopy(selected, 0, newsel, 0, i); |
|
1290 |
System.arraycopy(selected, i+1, newsel, i, selected.length - (i+1)); |
|
1291 |
selected = newsel; |
|
1292 |
} |
|
1293 |
currentIndex = index; |
|
1294 |
if (!isItemHidden(index)) { |
|
1295 |
// Only bother repainting if item is visible |
|
1296 |
repaint(index, index, PAINT_ITEMS); |
|
1297 |
} |
|
1298 |
} |
|
1299 |
||
1300 |
/** |
|
1301 |
* ensure that the given index is visible, scrolling the List |
|
1302 |
* if necessary, or doing nothing if the item is already visible. |
|
1303 |
* The List must be repainted for changes to be visible. |
|
1304 |
*/ |
|
1305 |
public void makeVisible(int index) { |
|
1306 |
if (index < 0 || index >= items.size()) { |
|
1307 |
return; |
|
1308 |
} |
|
1309 |
if (isItemHidden(index)) { // Do I really need to call this? |
|
1310 |
// If index is above the top, scroll up |
|
1311 |
if (index < vsb.getValue()) { |
|
1312 |
scrollVertical(index - vsb.getValue()); |
|
1313 |
} |
|
1314 |
// If index is below the bottom, scroll down |
|
1315 |
else if (index > lastItemDisplayed()) { |
|
1316 |
int val = index - lastItemDisplayed(); |
|
1317 |
scrollVertical(val); |
|
1318 |
} |
|
1319 |
} |
|
1320 |
} |
|
1321 |
||
1322 |
/** |
|
1323 |
* clear |
|
1324 |
*/ |
|
1325 |
public void clear() { |
|
1326 |
selected = new int[0]; |
|
1327 |
items = new Vector(); |
|
1328 |
currentIndex = -1; |
|
1329 |
// Fixed 6291736: ITEM_STATE_CHANGED triggered after List.removeAll(), XToolkit |
|
1330 |
// We should update 'focusIndex' variable more carefully |
|
1331 |
setFocusIndex(-1); |
|
1332 |
vsb.setValue(0); |
|
1333 |
maxLength = 0; |
|
1334 |
layout(); |
|
1335 |
repaint(); |
|
1336 |
} |
|
1337 |
||
1338 |
/** |
|
1339 |
* return the selected indexes |
|
1340 |
*/ |
|
1341 |
public int[] getSelectedIndexes() { |
|
1342 |
return selected; |
|
1343 |
} |
|
1344 |
||
1345 |
/** |
|
1346 |
* return the y value of the given index "i". |
|
1347 |
* the y value represents the top of the text |
|
1348 |
* NOTE: index can be larger than items.size as long |
|
1349 |
* as it can fit the window |
|
1350 |
*/ |
|
1351 |
int index2y(int index) { |
|
1352 |
int h = getItemHeight(); |
|
1353 |
||
1354 |
//if (index < vsb.getValue() || index > vsb.getValue() + itemsInWindow()) { |
|
1355 |
return MARGIN + ((index - vsb.getValue()) * h) + SPACE; |
|
1356 |
} |
|
1357 |
||
1358 |
/* return true if the y is a valid y coordinate for |
|
1359 |
* a VISIBLE list item, otherwise returns false |
|
1360 |
*/ |
|
1361 |
boolean validY(int y) { |
|
1362 |
||
1363 |
int shown = itemsDisplayed(); |
|
1364 |
int lastY = shown * getItemHeight() + MARGIN; |
|
1365 |
||
1366 |
if (shown == itemsInWindow()) { |
|
1367 |
lastY += MARGIN; |
|
1368 |
} |
|
1369 |
||
1370 |
if (y < 0 || y >= lastY) { |
|
1371 |
return false; |
|
1372 |
} |
|
1373 |
||
1374 |
return true; |
|
1375 |
} |
|
1376 |
||
1377 |
/** |
|
1378 |
* return the position of the index in the selected array |
|
1379 |
* if the index isn't in the array selected return -1; |
|
1380 |
*/ |
|
1381 |
int posInSel(int index) { |
|
1382 |
for (int i = 0 ; i < selected.length ; i++) { |
|
1383 |
if (index == selected[i]) { |
|
1384 |
return i; |
|
1385 |
} |
|
1386 |
} |
|
1387 |
return -1; |
|
1388 |
} |
|
1389 |
||
1390 |
boolean isIndexDisplayed(int idx) { |
|
1391 |
int lastDisplayed = lastItemDisplayed(); |
|
1392 |
||
1393 |
return idx <= lastDisplayed && |
|
1394 |
idx >= Math.max(0, lastDisplayed - itemsInWindow() + 1); |
|
1395 |
} |
|
1396 |
||
1397 |
/** |
|
1398 |
* returns index of last item displayed in the List |
|
1399 |
*/ |
|
1400 |
int lastItemDisplayed() { |
|
1401 |
int n = itemsInWindow(); |
|
1402 |
return (Math.min(items.size() - 1, (vsb.getValue() + n) - 1)); |
|
1403 |
} |
|
1404 |
||
1405 |
/** |
|
1406 |
* returns whether the given index is currently scrolled off the top or |
|
1407 |
* bottom of the List. |
|
1408 |
*/ |
|
1409 |
boolean isItemHidden(int index) { |
|
1410 |
return index < vsb.getValue() || |
|
1411 |
index >= vsb.getValue() + itemsInWindow(); |
|
1412 |
} |
|
1413 |
||
1414 |
/** |
|
1415 |
* returns the width of the list portion of the component (accounts for |
|
1416 |
* presence of vertical scrollbar) |
|
1417 |
*/ |
|
1418 |
int getListWidth() { |
|
1419 |
return vsbVis ? width - SCROLLBAR_AREA : width; |
|
1420 |
} |
|
1421 |
||
1422 |
/** |
|
1423 |
* returns number of items actually displayed in the List |
|
1424 |
*/ |
|
1425 |
int itemsDisplayed() { |
|
1426 |
||
1427 |
return (Math.min(items.size()-vsb.getValue(), itemsInWindow())); |
|
1428 |
||
1429 |
} |
|
1430 |
||
1431 |
/** |
|
1432 |
* scrollVertical |
|
1433 |
* y is the number of items to scroll |
|
1434 |
*/ |
|
1435 |
void scrollVertical(int y) { |
|
1436 |
if (log.isLoggable(Level.FINE)) log.fine("Scrolling vertically by " + y); |
|
1437 |
int itemsInWin = itemsInWindow(); |
|
1438 |
int h = getItemHeight(); |
|
1439 |
int pixelsToScroll = y * h; |
|
1440 |
||
1441 |
if (vsb.getValue() < -y) { |
|
1442 |
y = -vsb.getValue(); |
|
1443 |
} |
|
1444 |
vsb.setValue(vsb.getValue() + y); |
|
1445 |
||
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1446 |
Rectangle source = null; |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1447 |
Point distance = null; |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1448 |
int firstItem = 0, lastItem = 0; |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1449 |
int options = PAINT_HIDEFOCUS | PAINT_ITEMS | PAINT_VSCROLL | PAINT_FOCUS; |
2 | 1450 |
if (y > 0) { |
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1451 |
if (y < itemsInWin) { |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1452 |
source = new Rectangle(MARGIN, MARGIN + pixelsToScroll, width - SCROLLBAR_AREA, h * (itemsInWin - y - 1)-1); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1453 |
distance = new Point(0, -pixelsToScroll); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1454 |
options |= COPY_AREA; |
2 | 1455 |
} |
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1456 |
firstItem = vsb.getValue() + itemsInWin - y - 1; |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1457 |
lastItem = vsb.getValue() + itemsInWin - 1; |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1458 |
|
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1459 |
} else if (y < 0) { |
2 | 1460 |
if (y + itemsInWindow() > 0) { |
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1461 |
source = new Rectangle(MARGIN, MARGIN, width - SCROLLBAR_AREA, h * (itemsInWin + y)); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1462 |
distance = new Point(0, -pixelsToScroll); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1463 |
options |= COPY_AREA; |
2 | 1464 |
} |
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1465 |
firstItem = vsb.getValue(); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1466 |
lastItem = Math.min(getLastVisibleItem(), vsb.getValue() + -y); |
2 | 1467 |
} |
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1468 |
repaint(firstItem, lastItem, options, source, distance); |
2 | 1469 |
} |
1470 |
||
1471 |
/** |
|
1472 |
* scrollHorizontal |
|
1473 |
* x is the number of pixels to scroll |
|
1474 |
*/ |
|
1475 |
void scrollHorizontal(int x) { |
|
1476 |
if (log.isLoggable(Level.FINE)) log.fine("Scrolling horizontally by " + y); |
|
1477 |
int w = getListWidth(); |
|
1478 |
w -= ((2 * SPACE) + (2 * MARGIN)); |
|
1479 |
int h = height - (SCROLLBAR_AREA + (2 * MARGIN)); |
|
1480 |
hsb.setValue(hsb.getValue() + x); |
|
1481 |
||
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1482 |
Rectangle source = null; |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1483 |
Point distance = null; |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1484 |
if (x < 0) { |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1485 |
source = new Rectangle(MARGIN + SPACE, MARGIN, w + x, h); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1486 |
distance = new Point(-x, 0); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1487 |
} else if (x > 0) { |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1488 |
source = new Rectangle(MARGIN + SPACE + x, MARGIN, w - x, h); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1489 |
distance = new Point(-x, 0); |
2 | 1490 |
} |
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1491 |
int options = COPY_AREA | PAINT_ITEMS | PAINT_HSCROLL; |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1492 |
repaint(vsb.getValue(), lastItemDisplayed(), options, source, distance); |
2 | 1493 |
} |
1494 |
||
1495 |
/** |
|
1496 |
* return the index |
|
1497 |
*/ |
|
1498 |
int y2index(int y) { |
|
1499 |
if (!validY(y)) { |
|
1500 |
return -1; |
|
1501 |
} |
|
1502 |
||
1503 |
int i = (y - MARGIN) / getItemHeight() + vsb.getValue(); |
|
1504 |
int last = lastItemDisplayed(); |
|
1505 |
||
1506 |
if (i > last) { |
|
1507 |
i = last; |
|
1508 |
} |
|
1509 |
||
1510 |
return i; |
|
1511 |
||
1512 |
} |
|
1513 |
||
1514 |
/** |
|
1515 |
* is the index "index" selected |
|
1516 |
*/ |
|
1517 |
boolean isSelected(int index) { |
|
1518 |
if (eventType == ItemEvent.SELECTED && index == eventIndex) { |
|
1519 |
return true; |
|
1520 |
} |
|
1521 |
for (int i = 0 ; i < selected.length ; i++) { |
|
1522 |
if (selected[i] == index) { |
|
1523 |
return true; |
|
1524 |
} |
|
1525 |
} |
|
1526 |
return false; |
|
1527 |
} |
|
1528 |
||
1529 |
/** |
|
1530 |
* return the number of items that can fit |
|
1531 |
* in the current window |
|
1532 |
*/ |
|
1533 |
int itemsInWindow(boolean scrollbarVisible) { |
|
1534 |
int h; |
|
1535 |
if (scrollbarVisible) { |
|
1536 |
h = height - ((2 * MARGIN) + SCROLLBAR_AREA); |
|
1537 |
} else { |
|
1538 |
h = height - 2*MARGIN; |
|
1539 |
} |
|
1540 |
return (h / getItemHeight()); |
|
1541 |
} |
|
1542 |
||
1543 |
int itemsInWindow() { |
|
1544 |
return itemsInWindow(hsbVis); |
|
1545 |
} |
|
1546 |
||
1547 |
/** |
|
1548 |
* return true if the x and y position is in the horizontal scrollbar |
|
1549 |
*/ |
|
1550 |
boolean inHorizontalScrollbar(int x, int y) { |
|
1551 |
int w = getListWidth(); |
|
1552 |
int h = height - SCROLLBAR_WIDTH; |
|
1553 |
return (hsbVis && (x >= 0) && (x <= w) && (y > h)); |
|
1554 |
} |
|
1555 |
||
1556 |
/** |
|
1557 |
* return true if the x and y position is in the verticalscrollbar |
|
1558 |
*/ |
|
1559 |
boolean inVerticalScrollbar(int x, int y) { |
|
1560 |
int w = width - SCROLLBAR_WIDTH; |
|
1561 |
int h = hsbVis ? height - SCROLLBAR_AREA : height; |
|
1562 |
return (vsbVis && (x > w) && (y >= 0) && (y <= h)); |
|
1563 |
} |
|
1564 |
||
1565 |
/** |
|
1566 |
* return true if the x and y position is in the window |
|
1567 |
*/ |
|
1568 |
boolean inWindow(int x, int y) { |
|
1569 |
int w = getListWidth(); |
|
1570 |
int h = hsbVis ? height - SCROLLBAR_AREA : height; |
|
1571 |
return ((x >= 0) && (x <= w)) && ((y >= 0) && (y <= h)); |
|
1572 |
} |
|
1573 |
||
1574 |
/** |
|
1575 |
* return true if vertical scrollbar is visible and false otherwise; |
|
1576 |
* hsbVisible is the visibility of the horizontal scrollbar |
|
1577 |
*/ |
|
1578 |
boolean vsbIsVisible(boolean hsbVisible){ |
|
1579 |
return (items.size() > itemsInWindow(hsbVisible)); |
|
1580 |
} |
|
1581 |
||
1582 |
/** |
|
1583 |
* return true if horizontal scrollbar is visible and false otherwise; |
|
1584 |
* vsbVisible is the visibility of the vertical scrollbar |
|
1585 |
*/ |
|
1586 |
boolean hsbIsVisible(boolean vsbVisible){ |
|
1587 |
int w = width - ((2*SPACE) + (2*MARGIN) + (vsbVisible ? SCROLLBAR_AREA : 0)); |
|
1588 |
return (maxLength > w); |
|
1589 |
} |
|
1590 |
||
1591 |
/* |
|
1592 |
* Returns true if the event has been handled and should not be |
|
1593 |
* posted to Java |
|
1594 |
*/ |
|
1595 |
boolean prePostEvent(final AWTEvent e) { |
|
1596 |
if (e instanceof MouseEvent) { |
|
1597 |
return prePostMouseEvent((MouseEvent)e); |
|
1598 |
} |
|
1599 |
return super.prePostEvent(e); |
|
1600 |
} |
|
1601 |
||
1602 |
/* |
|
1603 |
* Fixed 6240151: XToolkit: Dragging the List scrollbar initiates DnD |
|
1604 |
* To be compatible with Motif, MouseEvent originated on the scrollbar |
|
1605 |
* should be sent into Java in this way: |
|
1606 |
* - post: MOUSE_ENTERED, MOUSE_EXITED, MOUSE_MOVED |
|
1607 |
* - don't post: MOUSE_PRESSED, MOUSE_RELEASED, MOUSE_CLICKED, MOUSE_DRAGGED |
|
1608 |
*/ |
|
1609 |
boolean prePostMouseEvent(final MouseEvent me){ |
|
1610 |
if (getToplevelXWindow().isModalBlocked()) { |
|
1611 |
return false; |
|
1612 |
} |
|
1613 |
||
1614 |
int eventId = me.getID(); |
|
1615 |
||
1616 |
if (eventId == MouseEvent.MOUSE_MOVED) |
|
1617 |
{ |
|
1618 |
// only for performance improvement |
|
1619 |
}else if((eventId == MouseEvent.MOUSE_DRAGGED || |
|
1620 |
eventId == MouseEvent.MOUSE_RELEASED) && |
|
1621 |
isScrollBarOriginated) |
|
1622 |
{ |
|
1623 |
if (eventId == MouseEvent.MOUSE_RELEASED) { |
|
1624 |
isScrollBarOriginated = false; |
|
1625 |
} |
|
1626 |
handleJavaMouseEventOnEDT(me); |
|
1627 |
return true; |
|
1628 |
}else if ((eventId == MouseEvent.MOUSE_PRESSED || |
|
1629 |
eventId == MouseEvent.MOUSE_CLICKED) && |
|
1630 |
(inVerticalScrollbar(me.getX(), me.getY()) || |
|
1631 |
inHorizontalScrollbar(me.getX(), me.getY()))) |
|
1632 |
{ |
|
1633 |
if (eventId == MouseEvent.MOUSE_PRESSED) { |
|
1634 |
isScrollBarOriginated = true; |
|
1635 |
} |
|
1636 |
handleJavaMouseEventOnEDT(me); |
|
1637 |
return true; |
|
1638 |
} |
|
1639 |
return false; |
|
1640 |
} |
|
1641 |
||
1642 |
/* |
|
1643 |
* Do handleJavaMouseEvent on EDT |
|
1644 |
*/ |
|
1645 |
void handleJavaMouseEventOnEDT(final MouseEvent me){ |
|
1646 |
EventQueue.invokeLater(new Runnable() { |
|
1647 |
public void run() { |
|
1648 |
handleJavaMouseEvent(me); |
|
1649 |
} |
|
1650 |
}); |
|
1651 |
} |
|
1652 |
||
1653 |
/* |
|
1654 |
* Fixed 5010944: List's rows overlap one another |
|
1655 |
* The bug is due to incorrent caching of the list item size |
|
1656 |
* So we should recalculate font metrics on setFont |
|
1657 |
*/ |
|
1658 |
public void setFont(Font f){ |
|
1659 |
super.setFont(f); |
|
1660 |
initFontMetrics(); |
|
1661 |
layout(); |
|
1662 |
repaint(); |
|
1663 |
} |
|
1664 |
||
1665 |
/** |
|
1666 |
* Sometimes painter is called on Toolkit thread, so the lock sequence is: |
|
1667 |
* awtLock -> Painter -> awtLock |
|
1668 |
* Sometimes it is called on other threads: |
|
1669 |
* Painter -> awtLock |
|
1670 |
* Since we can't guarantee the sequence, use awtLock. |
|
1671 |
*/ |
|
1672 |
class ListPainter { |
|
1673 |
VolatileImage buffer; |
|
1674 |
Color[] colors; |
|
1675 |
||
1676 |
private Color getListForeground() { |
|
1677 |
if (fgColorSet) { |
|
1678 |
return colors[FOREGROUND_COLOR]; |
|
1679 |
} |
|
1680 |
else { |
|
1681 |
return SystemColor.textText; |
|
1682 |
} |
|
1683 |
} |
|
1684 |
private Color getListBackground() { |
|
1685 |
if (bgColorSet) { |
|
1686 |
return colors[BACKGROUND_COLOR]; |
|
1687 |
} |
|
1688 |
else { |
|
1689 |
return SystemColor.text; |
|
1690 |
} |
|
1691 |
} |
|
1692 |
||
1693 |
private Color getDisabledColor() { |
|
1694 |
Color backgroundColor = getListBackground(); |
|
1695 |
Color foregroundColor = getListForeground(); |
|
1696 |
return (backgroundColor.equals(Color.BLACK)) ? foregroundColor.darker() : backgroundColor.darker(); |
|
1697 |
} |
|
1698 |
||
1699 |
private boolean createBuffer() { |
|
1700 |
VolatileImage localBuffer = null; |
|
1701 |
XToolkit.awtLock(); |
|
1702 |
try { |
|
1703 |
localBuffer = buffer; |
|
1704 |
} finally { |
|
1705 |
XToolkit.awtUnlock(); |
|
1706 |
} |
|
1707 |
||
1708 |
if (localBuffer == null) { |
|
1709 |
if (log.isLoggable(Level.FINE)) log.fine("Creating buffer " + width + "x" + height); |
|
1710 |
// use GraphicsConfig.cCVI() instead of Component.cVI(), |
|
1711 |
// because the latter may cause a deadlock with the tree lock |
|
1712 |
localBuffer = |
|
1713 |
graphicsConfig.createCompatibleVolatileImage(width+1, |
|
1714 |
height+1); |
|
1715 |
} |
|
1716 |
XToolkit.awtLock(); |
|
1717 |
try { |
|
1718 |
if (buffer == null) { |
|
1719 |
buffer = localBuffer; |
|
1720 |
return true; |
|
1721 |
} |
|
1722 |
} finally { |
|
1723 |
XToolkit.awtUnlock(); |
|
1724 |
} |
|
1725 |
return false; |
|
1726 |
} |
|
1727 |
||
1728 |
public void invalidate() { |
|
1729 |
XToolkit.awtLock(); |
|
1730 |
try { |
|
1731 |
if (buffer != null) { |
|
1732 |
buffer.flush(); |
|
1733 |
} |
|
1734 |
buffer = null; |
|
1735 |
} finally { |
|
1736 |
XToolkit.awtUnlock(); |
|
1737 |
} |
|
1738 |
} |
|
1739 |
||
1740 |
private void paint(Graphics listG, int firstItem, int lastItem, int options) { |
|
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1741 |
paint(listG, firstItem, lastItem, options, null, null); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1742 |
} |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1743 |
|
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1744 |
private void paint(Graphics listG, int firstItem, int lastItem, int options, |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1745 |
Rectangle source, Point distance) { |
2 | 1746 |
if (log.isLoggable(Level.FINER)) log.finer("Repaint from " + firstItem + " to " + lastItem + " options " + options); |
1747 |
if (firstItem > lastItem) { |
|
1748 |
int t = lastItem; |
|
1749 |
lastItem = firstItem; |
|
1750 |
firstItem = t; |
|
1751 |
} |
|
1752 |
if (firstItem < 0) { |
|
1753 |
firstItem = 0; |
|
1754 |
} |
|
1755 |
colors = getGUIcolors(); |
|
1756 |
VolatileImage localBuffer = null; |
|
1757 |
do { |
|
1758 |
XToolkit.awtLock(); |
|
1759 |
try { |
|
1760 |
if (createBuffer()) { |
|
1761 |
// First time created buffer should be painted over at full. |
|
1762 |
options = PAINT_ALL; |
|
1763 |
} |
|
1764 |
localBuffer = buffer; |
|
1765 |
} finally { |
|
1766 |
XToolkit.awtUnlock(); |
|
1767 |
} |
|
1768 |
switch (localBuffer.validate(getGraphicsConfiguration())) { |
|
1769 |
case VolatileImage.IMAGE_INCOMPATIBLE: |
|
1770 |
invalidate(); |
|
1771 |
options = PAINT_ALL; |
|
1772 |
continue; |
|
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1773 |
case VolatileImage.IMAGE_RESTORED: |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1774 |
options = PAINT_ALL; |
2 | 1775 |
} |
1776 |
Graphics g = localBuffer.createGraphics(); |
|
1777 |
||
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1778 |
// Note that the order of the following painting operations |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1779 |
// should not be modified |
2 | 1780 |
try { |
1781 |
g.setFont(getFont()); |
|
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1782 |
|
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1783 |
// hiding the focus rectangle must be done prior to copying |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1784 |
// area and so this is the first action to be performed |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1785 |
if ((options & (PAINT_HIDEFOCUS)) != 0) { |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1786 |
paintFocus(g, PAINT_HIDEFOCUS); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1787 |
} |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1788 |
/* |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1789 |
* The shift of the component contents occurs while someone |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1790 |
* scrolls the component, the only purpose of the shift is to |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1791 |
* increase the painting performance. The shift should be done |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1792 |
* prior to painting any area (except hiding focus) and actually |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1793 |
* it should never be done jointly with erase background. |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1794 |
*/ |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1795 |
if ((options & COPY_AREA) != 0) { |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1796 |
g.copyArea(source.x, source.y, source.width, source.height, |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1797 |
distance.x, distance.y); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1798 |
} |
2 | 1799 |
if ((options & PAINT_BACKGROUND) != 0) { |
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1800 |
paintBackground(g); |
2 | 1801 |
// Since we made full erase update items |
1802 |
firstItem = getFirstVisibleItem(); |
|
1803 |
lastItem = getLastVisibleItem(); |
|
1804 |
} |
|
1805 |
if ((options & PAINT_ITEMS) != 0) { |
|
1806 |
paintItems(g, firstItem, lastItem, options); |
|
1807 |
} |
|
1808 |
if ((options & PAINT_VSCROLL) != 0 && vsbVis) { |
|
1809 |
g.setClip(getVScrollBarRec()); |
|
1810 |
paintVerScrollbar(g, true); |
|
1811 |
} |
|
1812 |
if ((options & PAINT_HSCROLL) != 0 && hsbVis) { |
|
1813 |
g.setClip(getHScrollBarRec()); |
|
1814 |
paintHorScrollbar(g, true); |
|
1815 |
} |
|
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1816 |
if ((options & (PAINT_FOCUS)) != 0) { |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1817 |
paintFocus(g, PAINT_FOCUS); |
2 | 1818 |
} |
1819 |
} finally { |
|
1820 |
g.dispose(); |
|
1821 |
} |
|
1822 |
} while (localBuffer.contentsLost()); |
|
1823 |
listG.drawImage(localBuffer, 0, 0, null); |
|
1824 |
} |
|
1825 |
||
445
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1826 |
private void paintBackground(Graphics g) { |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1827 |
g.setColor(SystemColor.window); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1828 |
g.fillRect(0, 0, width, height); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1829 |
g.setColor(getListBackground()); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1830 |
g.fillRect(0, 0, listWidth, listHeight); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1831 |
draw3DRect(g, getSystemColors(), 0, 0, listWidth - 1, listHeight - 1, false); |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1832 |
} |
6f717a8cacfb
6471693: Moving the vertical scroll bar of List in FileDialog leads Flickering in solaris
dcherepanov
parents:
2
diff
changeset
|
1833 |
|
2 | 1834 |
private void paintItems(Graphics g, int firstItem, int lastItem, int options) { |
1835 |
if (log.isLoggable(Level.FINER)) log.finer("Painting items from " + firstItem + " to " + lastItem + ", focused " + focusIndex + ", first " + getFirstVisibleItem() + ", last " + getLastVisibleItem()); |
|
1836 |
||
1837 |
firstItem = Math.max(getFirstVisibleItem(), firstItem); |
|
1838 |
if (firstItem > lastItem) { |
|
1839 |
int t = lastItem; |
|
1840 |
lastItem = firstItem; |
|
1841 |
firstItem = t; |
|
1842 |
} |
|
1843 |
firstItem = Math.max(getFirstVisibleItem(), firstItem); |
|
1844 |
lastItem = Math.min(lastItem, items.size()-1); |
|
1845 |
||
1846 |
if (log.isLoggable(Level.FINER)) log.finer("Actually painting items from " + firstItem + " to " + lastItem + |
|
1847 |
", items in window " + itemsInWindow()); |
|
1848 |
for (int i = firstItem; i <= lastItem; i++) { |
|
1849 |
paintItem(g, i); |
|
1850 |
} |
|
1851 |
} |
|
1852 |
||
1853 |
private void paintItem(Graphics g, int index) { |
|
1854 |
if (log.isLoggable(Level.FINEST)) log.finest("Painting item " + index); |
|
1855 |
// 4895367 - only paint items which are visible |
|
1856 |
if (!isItemHidden(index)) { |
|
1857 |
Shape clip = g.getClip(); |
|
1858 |
int w = getItemWidth(); |
|
1859 |
int h = getItemHeight(); |
|
1860 |
int y = getItemY(index); |
|
1861 |
int x = getItemX(); |
|
1862 |
if (log.isLoggable(Level.FINEST)) log.finest("Setting clip " + new Rectangle(x, y, w - (SPACE*2), h-(SPACE*2))); |
|
1863 |
g.setClip(x, y, w - (SPACE*2), h-(SPACE*2)); |
|
1864 |
||
1865 |
// Always paint the background so that focus is unpainted in |
|
1866 |
// multiselect mode |
|
1867 |
if (isSelected(index)) { |
|
1868 |
if (log.isLoggable(Level.FINEST)) log.finest("Painted item is selected"); |
|
1869 |
g.setColor(getListForeground()); |
|
1870 |
} else { |
|
1871 |
g.setColor(getListBackground()); |
|
1872 |
} |
|
1873 |
if (log.isLoggable(Level.FINEST)) log.finest("Filling " + new Rectangle(x, y, w, h)); |
|
1874 |
g.fillRect(x, y, w, h); |
|
1875 |
||
1876 |
if (index <= getLastVisibleItem() && index < items.size()) { |
|
1877 |
if (!isEnabled()){ |
|
1878 |
g.setColor(getDisabledColor()); |
|
1879 |
} else if (isSelected(index)) { |
|
1880 |
g.setColor(getListBackground()); |
|
1881 |
} else { |
|
1882 |
g.setColor(getListForeground()); |
|
1883 |
} |
|
1884 |
String str = (String)items.elementAt(index); |
|
1885 |
g.drawString(str, x - hsb.getValue(), y + fontAscent); |
|
1886 |
} else { |
|
1887 |
// Clear the remaining area around the item - focus area and the rest of border |
|
1888 |
g.setClip(x, y, listWidth, h); |
|
1889 |
g.setColor(getListBackground()); |
|
1890 |
g.fillRect(x, y, listWidth, h); |
|
1891 |
} |
|
1892 |
g.setClip(clip); |
|
1893 |
} |
|
1894 |
} |
|
1895 |
||
1896 |
void paintScrollBar(XScrollbar scr, Graphics g, int x, int y, int width, int height, boolean paintAll) { |
|
1897 |
if (log.isLoggable(Level.FINEST)) log.finest("Painting scrollbar " + scr + " width " + |
|
1898 |
width + " height " + height + ", paintAll " + paintAll); |
|
1899 |
g.translate(x, y); |
|
1900 |
scr.paint(g, getSystemColors(), paintAll); |
|
1901 |
g.translate(-x, -y); |
|
1902 |
} |
|
1903 |
||
1904 |
/** |
|
1905 |
* Paint the horizontal scrollbar to the screen |
|
1906 |
* |
|
1907 |
* @param g the graphics context to draw into |
|
1908 |
* @param colors the colors used to draw the scrollbar |
|
1909 |
* @param paintAll paint the whole scrollbar if true, just the thumb if false |
|
1910 |
*/ |
|
1911 |
void paintHorScrollbar(Graphics g, boolean paintAll) { |
|
1912 |
int w = getListWidth(); |
|
1913 |
paintScrollBar(hsb, g, 0, height - (SCROLLBAR_WIDTH), w, SCROLLBAR_WIDTH, paintAll); |
|
1914 |
} |
|
1915 |
||
1916 |
/** |
|
1917 |
* Paint the vertical scrollbar to the screen |
|
1918 |
* |
|
1919 |
* @param g the graphics context to draw into |
|
1920 |
* @param colors the colors used to draw the scrollbar |
|
1921 |
* @param paintAll paint the whole scrollbar if true, just the thumb if false |
|
1922 |
*/ |
|
1923 |
void paintVerScrollbar(Graphics g, boolean paintAll) { |
|
1924 |
int h = height - (hsbVis ? (SCROLLBAR_AREA-2) : 0); |
|
1925 |
paintScrollBar(vsb, g, width - SCROLLBAR_WIDTH, 0, SCROLLBAR_WIDTH - 2, h, paintAll); |
|
1926 |
} |
|
1927 |
||
1928 |
||
1929 |
private Rectangle prevFocusRect; |
|
1930 |
private void paintFocus(Graphics g, int options) { |
|
1931 |
boolean paintFocus = (options & PAINT_FOCUS) != 0; |
|
1932 |
if (paintFocus && !hasFocus()) { |
|
1933 |
paintFocus = false; |
|
1934 |
} |
|
1935 |
if (log.isLoggable(Level.FINE)) log.fine("Painting focus, focus index " + getFocusIndex() + ", focus is " + |
|
1936 |
(isItemHidden(getFocusIndex())?("invisible"):("visible")) + ", paint focus is " + paintFocus); |
|
1937 |
Shape clip = g.getClip(); |
|
1938 |
g.setClip(0, 0, listWidth, listHeight); |
|
1939 |
if (log.isLoggable(Level.FINEST)) log.finest("Setting focus clip " + new Rectangle(0, 0, listWidth, listHeight)); |
|
1940 |
Rectangle rect = getFocusRect(); |
|
1941 |
if (prevFocusRect != null) { |
|
1942 |
// Erase focus rect |
|
1943 |
if (log.isLoggable(Level.FINEST)) log.finest("Erasing previous focus rect " + prevFocusRect); |
|
1944 |
g.setColor(getListBackground()); |
|
1945 |
g.drawRect(prevFocusRect.x, prevFocusRect.y, prevFocusRect.width, prevFocusRect.height); |
|
1946 |
prevFocusRect = null; |
|
1947 |
} |
|
1948 |
if (paintFocus) { |
|
1949 |
// Paint new |
|
1950 |
if (log.isLoggable(Level.FINEST)) log.finest("Painting focus rect " + rect); |
|
1951 |
g.setColor(getListForeground()); // Focus color is always black on Linux |
|
1952 |
g.drawRect(rect.x, rect.y, rect.width, rect.height); |
|
1953 |
prevFocusRect = rect; |
|
1954 |
} |
|
1955 |
g.setClip(clip); |
|
1956 |
} |
|
1957 |
} |
|
1958 |
} |