1 /* 2 * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 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. 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 sun.util.logging.PlatformLogger; 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 PlatformLogger log = PlatformLogger.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 58 private final static int PAINT_VSCROLL = 2; 59 private final static int PAINT_HSCROLL = 4; 60 private final static int PAINT_ITEMS = 8; 61 private final static int PAINT_FOCUS = 16; 62 private final static int PAINT_BACKGROUND = 32; 63 private final static int PAINT_HIDEFOCUS = 64; 64 private final static int PAINT_ALL = 65 PAINT_VSCROLL | PAINT_HSCROLL | PAINT_ITEMS | PAINT_FOCUS | PAINT_BACKGROUND; 66 private final static int COPY_AREA = 128; 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 public void repaintScrollbarRequest(XScrollbar scrollbar) { 367 if (scrollbar == hsb) { 368 repaint(PAINT_HSCROLL); 369 } 370 else if (scrollbar == vsb) { 371 repaint(PAINT_VSCROLL); 372 } 373 } 374 /** 375 * Overridden for performance 376 */ 377 public void repaint() { 378 repaint(getFirstVisibleItem(), getLastVisibleItem(), PAINT_ALL); 379 } 380 381 private void repaint(int options) { 382 repaint(getFirstVisibleItem(), getLastVisibleItem(), options); 383 } 384 385 private void repaint(int firstItem, int lastItem, int options) { 386 repaint(firstItem, lastItem, options, null, null); 387 } 388 389 /** 390 * In most cases the entire area of the component doesn't have 391 * to be repainted. The method repaints the particular areas of 392 * the component. The areas to repaint is specified by the option 393 * parameter. The possible values of the option parameter are: 394 * PAINT_VSCROLL, PAINT_HSCROLL, PAINT_ITEMS, PAINT_FOCUS, 395 * PAINT_HIDEFOCUS, PAINT_BACKGROUND, PAINT_ALL, COPY_AREA. 396 * 397 * Note that the COPY_AREA value initiates copy of a source area 398 * of the component by a distance by means of the copyArea method 399 * of the Graphics class. 400 * 401 * @param firstItem the position of the first item of the range to repaint 402 * @param lastItem the position of the last item of the range to repaint 403 * @param options specifies the particular area of the component to repaint 404 * @param source the area of the component to copy 405 * @param distance the distance to copy the source area 406 */ 407 private void repaint(int firstItem, int lastItem, int options, Rectangle source, Point distance) { 408 final Graphics g = getGraphics(); 409 if (g != null) { 410 try { 411 painter.paint(g, firstItem, lastItem, options, source, distance); 412 target.paint(g); 413 } finally { 414 g.dispose(); 415 } 416 } 417 } 418 @Override 419 void paintPeer(final Graphics g) { 420 painter.paint(g, getFirstVisibleItem(), getLastVisibleItem(), PAINT_ALL); 421 } 422 public boolean isFocusable() { return true; } 423 424 // TODO: share/promote the Focus methods? 425 public void focusGained(FocusEvent e) { 426 super.focusGained(e); 427 repaint(PAINT_FOCUS); 428 } 429 430 public void focusLost(FocusEvent e) { 431 super.focusLost(e); 432 repaint(PAINT_FOCUS); 433 } 434 435 /** 436 * Layout the sub-components of the List - that is, the scrollbars and the 437 * list of items. 438 */ 439 public void layout() { 440 int vis, maximum; 441 boolean vsbWasVisible; 442 int origVSBVal; 443 assert(target != null); 444 445 // Start with assumption there is not a horizontal scrollbar, 446 // see if we need a vertical scrollbar 447 448 // Bug: If the list DOES have a horiz scrollbar and the value is set to 449 // the very bottom value, value is reset in setValues() because it isn't 450 // a valid value for cases when the list DOESN'T have a horiz scrollbar. 451 // This is currently worked-around with origVSGVal. 452 origVSBVal = vsb.getValue(); 453 vis = itemsInWindow(false); 454 maximum = items.size() < vis ? vis : items.size(); 455 vsb.setValues(vsb.getValue(), vis, vsb.getMinimum(), maximum); 456 vsbVis = vsbWasVisible = vsbIsVisible(false); 457 listHeight = height; 458 459 // now see if we need a horizontal scrollbar 460 listWidth = getListWidth(); 461 vis = listWidth - ((2 * SPACE) + (2 * MARGIN)); 462 maximum = maxLength < vis ? vis : maxLength; 463 hsb.setValues(hsb.getValue(), vis, hsb.getMinimum(), maximum); 464 hsbVis = hsbIsVisible(vsbVis); 465 466 if (hsbVis) { 467 // do need a horizontal scrollbar, so recalculate height of 468 // vertical s crollbar 469 listHeight = height - SCROLLBAR_AREA; 470 vis = itemsInWindow(true); 471 maximum = items.size() < vis ? vis : items.size(); 472 vsb.setValues(origVSBVal, vis, vsb.getMinimum(), maximum); 473 vsbVis = vsbIsVisible(true); 474 } 475 476 // now check to make sure we haven't changed need for vertical 477 // scrollbar - if we have, we need to 478 // recalculate horizontal scrollbar width - then we're done... 479 if (vsbWasVisible != vsbVis) { 480 listWidth = getListWidth(); 481 vis = listWidth - ((2 * SPACE) + (2 * MARGIN)); 482 maximum = maxLength < vis ? 0 : maxLength; 483 hsb.setValues(hsb.getValue(), vis, hsb.getMinimum(), maximum); 484 hsbVis = hsbIsVisible(vsbVis); 485 } 486 487 vsb.setSize(SCROLLBAR_WIDTH, listHeight); 488 hsb.setSize(listWidth, SCROLLBAR_WIDTH); 489 490 vsb.setBlockIncrement(itemsInWindow()); 491 hsb.setBlockIncrement(width - ((2 * SPACE) + (2 * MARGIN) + (vsbVis ? SCROLLBAR_AREA : 0))); 492 } 493 494 int getItemWidth() { 495 return width - ((2 * MARGIN) + (vsbVis ? SCROLLBAR_AREA : 0)); 496 } 497 498 /* Returns height of an item in the list */ 499 int getItemHeight() { 500 return (fontHeight - fontLeading) + (2*SPACE); 501 } 502 503 int getItemX() { 504 return MARGIN + SPACE; 505 } 506 507 int getItemY(int item) { 508 return index2y(item); 509 } 510 511 int getFocusIndex() { 512 return focusIndex; 513 } 514 515 void setFocusIndex(int value) { 516 focusIndex = value; 517 } 518 519 /** 520 * Update and return the focus rectangle. 521 * Focus is around the focused item, if it is visible, or 522 * around the border of the list if the focused item is scrolled off the top 523 * or bottom of the list. 524 */ 525 Rectangle getFocusRect() { 526 Rectangle focusRect = new Rectangle(); 527 // width is always only based on presence of vert sb 528 focusRect.x = 1; 529 focusRect.width = getListWidth() - 3; 530 // if focused item is not currently displayed in the list, paint 531 // focus around entire list (not including scrollbars) 532 if (isIndexDisplayed(getFocusIndex())) { 533 // focus rect is around the item 534 focusRect.y = index2y(getFocusIndex()) - 2; 535 focusRect.height = getItemHeight()+1; 536 } else { 537 // focus rect is around the list 538 focusRect.y = 1; 539 focusRect.height = hsbVis ? height - SCROLLBAR_AREA : height; 540 focusRect.height -= 3; 541 } 542 return focusRect; 543 } 544 545 public void handleConfigureNotifyEvent(XEvent xev) { 546 super.handleConfigureNotifyEvent(xev); 547 548 // Update buffer 549 painter.invalidate(); 550 } 551 public boolean handlesWheelScrolling() { return true; } 552 553 // FIXME: need to support MouseWheel scrolling, too 554 void handleJavaMouseEvent(MouseEvent e) { 555 super.handleJavaMouseEvent(e); 556 int i = e.getID(); 557 switch (i) { 558 case MouseEvent.MOUSE_PRESSED: 559 mousePressed(e); 560 break; 561 case MouseEvent.MOUSE_RELEASED: 562 mouseReleased(e); 563 break; 564 case MouseEvent.MOUSE_DRAGGED: 565 mouseDragged(e); 566 break; 567 } 568 } 569 570 void handleJavaMouseWheelEvent(MouseWheelEvent e) { 571 if (ListHelper.doWheelScroll(vsbVis ? vsb : null, 572 hsbVis ? hsb : null, e)) { 573 repaint(); 574 } 575 } 576 577 void mousePressed(MouseEvent mouseEvent) { 578 if (log.isLoggable(PlatformLogger.FINER)) log.finer(mouseEvent.toString() + ", hsb " + hsbVis + ", vsb " + vsbVis); 579 if (isEnabled() && mouseEvent.getButton() == MouseEvent.BUTTON1) { 580 if (inWindow(mouseEvent.getX(), mouseEvent.getY())) { 581 if (log.isLoggable(PlatformLogger.FINE)) log.fine("Mouse press in items area"); 582 active = WINDOW; 583 int i = y2index(mouseEvent.getY()); 584 if (i >= 0) { 585 if (multipleSelections) { 586 if (isSelected(i)) { 587 // See 6243382 for more information 588 deselectItem(i); 589 eventIndex = i; 590 eventType = ItemEvent.DESELECTED; 591 } 592 else { 593 selectItem(i); 594 eventIndex = i; 595 eventType = ItemEvent.SELECTED; 596 } 597 } 598 // Backward-compatible bug: even if a single-select 599 // item is already selected, we send an ITEM_STATE_CHANGED/ 600 // SELECTED event. Engineer's Toolbox appears to rely on 601 // this. 602 //else if (!isSelected(i)) { 603 else { 604 selectItem(i); 605 eventIndex = i; 606 eventType = ItemEvent.SELECTED; 607 } 608 // Restoring Windows behaviour 609 // We should update focus index after "mouse pressed" event 610 setFocusIndex(i); 611 repaint(PAINT_FOCUS); 612 } else { 613 // 6426186: reset variable to prevent action event 614 // if user clicks on unoccupied area of list 615 currentIndex = -1; 616 } 617 } else if (inVerticalScrollbar(mouseEvent.getX(), mouseEvent.getY())) { 618 if (log.isLoggable(PlatformLogger.FINE)) log.fine("Mouse press in vertical scrollbar"); 619 active = VERSCROLLBAR; 620 vsb.handleMouseEvent(mouseEvent.getID(), 621 mouseEvent.getModifiers(), 622 mouseEvent.getX() - (width - SCROLLBAR_WIDTH), 623 mouseEvent.getY()); 624 } else if (inHorizontalScrollbar(mouseEvent.getX(), mouseEvent.getY())) { 625 if (log.isLoggable(PlatformLogger.FINE)) log.fine("Mouse press in horizontal scrollbar"); 626 active = HORSCROLLBAR; 627 hsb.handleMouseEvent(mouseEvent.getID(), 628 mouseEvent.getModifiers(), 629 mouseEvent.getX(), 630 mouseEvent.getY() - (height - SCROLLBAR_WIDTH)); 631 632 } 633 isMousePressed = true; 634 } 635 } 636 void mouseReleased(MouseEvent mouseEvent) { 637 if (isEnabled() && mouseEvent.getButton() == MouseEvent.BUTTON1) { 638 //winReleaseCursorFocus(); 639 int clickCount = mouseEvent.getClickCount(); 640 if (active == VERSCROLLBAR) { 641 vsb.handleMouseEvent(mouseEvent.getID(), 642 mouseEvent.getModifiers(), 643 mouseEvent.getX()-(width-SCROLLBAR_WIDTH), 644 mouseEvent.getY()); 645 } else if(active == HORSCROLLBAR) { 646 hsb.handleMouseEvent(mouseEvent.getID(), 647 mouseEvent.getModifiers(), 648 mouseEvent.getX(), 649 mouseEvent.getY()-(height-SCROLLBAR_WIDTH)); 650 } else if ( ( currentIndex >= 0 ) && ( clickCount >= 2 ) && 651 ( clickCount % 2 == 0 ) ) { 652 postEvent(new ActionEvent(target, 653 ActionEvent.ACTION_PERFORMED, 654 (String)items.elementAt(currentIndex), 655 mouseEvent.getWhen(), 656 mouseEvent.getModifiers())); // No ext mods 657 } else if (active == WINDOW) { 658 // See 6243382 for more information 659 trackMouseReleasedScroll(); 660 661 if (eventType == ItemEvent.DESELECTED) { 662 assert multipleSelections : "Shouldn't get a deselect for a single-select List"; 663 // Paint deselection the release 664 deselectItem(eventIndex); 665 } 666 if (eventType != NONE) { 667 postEvent(new ItemEvent((List)target, 668 ItemEvent.ITEM_STATE_CHANGED, 669 Integer.valueOf(eventIndex), 670 eventType)); 671 } 672 } 673 active = NONE; 674 eventIndex = -1; 675 eventType = NONE; 676 isMousePressed = false; 677 } 678 } 679 680 void mouseDragged(MouseEvent mouseEvent) { 681 // TODO: can you drag w/ any other buttons? what about multiple buttons? 682 if (isEnabled() && 683 (mouseEvent.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) != 0) { 684 if ((active == VERSCROLLBAR)) { 685 vsb.handleMouseEvent(mouseEvent.getID(), 686 mouseEvent.getModifiers(), 687 mouseEvent.getX()-(width-SCROLLBAR_WIDTH), 688 mouseEvent.getY()); 689 } else if ((active == HORSCROLLBAR)) { 690 hsb.handleMouseEvent(mouseEvent.getID(), 691 mouseEvent.getModifiers(), 692 mouseEvent.getX(), 693 mouseEvent.getY()-(height-SCROLLBAR_WIDTH)); 694 } else if (active == WINDOW) { 695 int i = y2index(mouseEvent.getY()); 696 if (multipleSelections) { 697 // Multi-select only: 698 // If a selected item was pressed on and then dragged off 699 // of, cancel the pending deselect. 700 if (eventType == ItemEvent.DESELECTED) { 701 if (i != eventIndex) { 702 eventType = NONE; 703 eventIndex = -1; 704 } 705 } 706 } 707 else if (eventType == ItemEvent.SELECTED) { 708 // Single-select only: 709 // If an unselected item was pressed on, track the drag 710 // and select the item under the mouse 711 712 // See 6243382 for more information 713 trackMouseDraggedScroll(mouseEvent); 714 715 if (i >= 0 && !isSelected(i)) { 716 int oldSel = eventIndex; 717 selectItem(i); 718 eventIndex = i; 719 repaint(oldSel, eventIndex, PAINT_ITEMS); 720 } 721 } 722 // Restoring Windows behaviour 723 // We should update focus index after "mouse dragged" event 724 if (i >= 0) { 725 setFocusIndex(i); 726 repaint(PAINT_FOCUS); 727 } 728 } 729 } 730 } 731 732 /* 733 * Helper method for XListPeer with integrated vertical scrollbar. 734 * Start or stop vertical scrolling when mouse dragged in / out the area of the list if it's required 735 * Restoring Motif behavior 736 * See 6243382 for more information 737 */ 738 void trackMouseDraggedScroll(MouseEvent mouseEvent){ 739 740 if (vsb.beforeThumb(mouseEvent.getX(), mouseEvent.getY())) { 741 vsb.setMode(AdjustmentEvent.UNIT_DECREMENT); 742 } else { 743 vsb.setMode(AdjustmentEvent.UNIT_INCREMENT); 744 } 745 746 if(mouseEvent.getY() < 0 || mouseEvent.getY() >= listHeight){ 747 if (!mouseDraggedOutVertically){ 748 mouseDraggedOutVertically = true; 749 vsb.startScrollingInstance(); 750 } 751 }else{ 752 if (mouseDraggedOutVertically){ 753 mouseDraggedOutVertically = false; 754 vsb.stopScrollingInstance(); 755 } 756 } 757 758 if (hsb.beforeThumb(mouseEvent.getX(), mouseEvent.getY())) { 759 hsb.setMode(AdjustmentEvent.UNIT_DECREMENT); 760 } else { 761 hsb.setMode(AdjustmentEvent.UNIT_INCREMENT); 762 } 763 764 if (mouseEvent.getX() < 0 || mouseEvent.getX() >= listWidth) { 765 if (!mouseDraggedOutHorizontally){ 766 mouseDraggedOutHorizontally = true; 767 hsb.startScrollingInstance(); 768 } 769 }else{ 770 if (mouseDraggedOutHorizontally){ 771 mouseDraggedOutHorizontally = false; 772 hsb.stopScrollingInstance(); 773 } 774 } 775 } 776 777 /* 778 * Helper method for XListPeer with integrated vertical scrollbar. 779 * Stop vertical scrolling when mouse released in / out the area of the list if it's required 780 * Restoring Motif behavior 781 * see 6243382 for more information 782 */ 783 void trackMouseReleasedScroll(){ 784 785 if (mouseDraggedOutVertically){ 786 mouseDraggedOutVertically = false; 787 vsb.stopScrollingInstance(); 788 } 789 790 if (mouseDraggedOutHorizontally){ 791 mouseDraggedOutHorizontally = false; 792 hsb.stopScrollingInstance(); 793 } 794 } 795 796 void handleJavaKeyEvent(KeyEvent e) { 797 switch(e.getID()) { 798 case KeyEvent.KEY_PRESSED: 799 if (!isMousePressed){ 800 keyPressed(e); 801 } 802 break; 803 } 804 } 805 806 void keyPressed(KeyEvent e) { 807 int keyCode = e.getKeyCode(); 808 if (log.isLoggable(PlatformLogger.FINE)) log.fine(e.toString()); 809 switch(keyCode) { 810 case KeyEvent.VK_UP: 811 case KeyEvent.VK_KP_UP: // TODO: I assume we also want this, too 812 if (getFocusIndex() > 0) { 813 setFocusIndex(getFocusIndex()-1); 814 repaint(PAINT_HIDEFOCUS); 815 // If single-select, select the item 816 if (!multipleSelections) { 817 selectItem(getFocusIndex()); 818 postEvent(new ItemEvent((List)target, 819 ItemEvent.ITEM_STATE_CHANGED, 820 Integer.valueOf(getFocusIndex()), 821 ItemEvent.SELECTED)); 822 } 823 if (isItemHidden(getFocusIndex())) { 824 makeVisible(getFocusIndex()); 825 } 826 else { 827 repaint(PAINT_FOCUS); 828 } 829 } 830 break; 831 case KeyEvent.VK_DOWN: 832 case KeyEvent.VK_KP_DOWN: // TODO: I assume we also want this, too 833 if (getFocusIndex() < items.size() - 1) { 834 setFocusIndex(getFocusIndex()+1); 835 repaint(PAINT_HIDEFOCUS); 836 // If single-select, select the item 837 if (!multipleSelections) { 838 selectItem(getFocusIndex()); 839 postEvent(new ItemEvent((List)target, 840 ItemEvent.ITEM_STATE_CHANGED, 841 Integer.valueOf(getFocusIndex()), 842 ItemEvent.SELECTED)); 843 } 844 if (isItemHidden(getFocusIndex())) { 845 makeVisible(getFocusIndex()); 846 } 847 else { 848 repaint(PAINT_FOCUS); 849 } 850 } 851 break; 852 case KeyEvent.VK_PAGE_UP: { 853 // Assumes that scrollbar does its own bounds-checking 854 int previousValue = vsb.getValue(); 855 vsb.setValue(vsb.getValue() - vsb.getBlockIncrement()); 856 int currentValue = vsb.getValue(); 857 // 6190768 pressing pg-up on AWT multiple selection lists the items but no item event is triggered, on XToolkit 858 // Restoring Motif behavior 859 if (previousValue!=currentValue) { 860 setFocusIndex(Math.max(getFocusIndex()-itemsInWindow(), 0)); 861 if (!multipleSelections){ 862 selectItem(getFocusIndex()); 863 postEvent(new ItemEvent((List)target, 864 ItemEvent.ITEM_STATE_CHANGED, 865 Integer.valueOf(getFocusIndex()), 866 ItemEvent.SELECTED)); 867 } 868 } 869 repaint(); 870 break; 871 } 872 case KeyEvent.VK_PAGE_DOWN: { 873 // Assumes that scrollbar does its own bounds-checking 874 int previousValue = vsb.getValue(); 875 vsb.setValue(vsb.getValue() + vsb.getBlockIncrement()); 876 int currentValue = vsb.getValue(); 877 // 6190768 pressing pg-down on AWT multiple selection list selects the items but no item event is triggered, on XToolkit 878 // Restoring Motif behavior 879 if (previousValue!=currentValue) { 880 setFocusIndex(Math.min(getFocusIndex() + itemsInWindow(), items.size()-1)); 881 if (!multipleSelections){ 882 selectItem(getFocusIndex()); 883 postEvent(new ItemEvent((List)target, 884 ItemEvent.ITEM_STATE_CHANGED, 885 Integer.valueOf(getFocusIndex()), 886 ItemEvent.SELECTED)); 887 } 888 } 889 repaint(); 890 break; 891 } 892 case KeyEvent.VK_LEFT: 893 case KeyEvent.VK_KP_LEFT: 894 if (hsbVis & hsb.getValue() > 0) { 895 hsb.setValue(hsb.getValue() - HORIZ_SCROLL_AMT); 896 repaint(); 897 } 898 break; 899 case KeyEvent.VK_RIGHT: 900 case KeyEvent.VK_KP_RIGHT: 901 if (hsbVis) { // Should check if already at end 902 hsb.setValue(hsb.getValue() + HORIZ_SCROLL_AMT); 903 repaint(); 904 } 905 break; 906 // 6190778 CTRL + HOME, CTRL + END keys do not work properly for list on XToolkit 907 // Restoring Motif behavior 908 case KeyEvent.VK_HOME: 909 if (!e.isControlDown() || ((List)target).getItemCount() <= 0) 910 break; 911 if (vsbVis) { 912 vsb.setValue(vsb.getMinimum()); 913 } 914 setFocusIndex(0); 915 if (!multipleSelections) { 916 selectItem(getFocusIndex()); 917 postEvent(new ItemEvent((List)target, 918 ItemEvent.ITEM_STATE_CHANGED, 919 Integer.valueOf(getFocusIndex()), 920 ItemEvent.SELECTED)); 921 } 922 repaint(); 923 break; 924 case KeyEvent.VK_END: 925 if (!e.isControlDown() || ((List)target).getItemCount() <= 0) 926 break; 927 if (vsbVis) { 928 vsb.setValue(vsb.getMaximum()); 929 } 930 setFocusIndex(items.size()-1); 931 if (!multipleSelections) { 932 selectItem(getFocusIndex()); 933 postEvent(new ItemEvent((List)target, 934 ItemEvent.ITEM_STATE_CHANGED, 935 Integer.valueOf(getFocusIndex()), 936 ItemEvent.SELECTED)); 937 } 938 repaint(); 939 break; 940 case KeyEvent.VK_SPACE: 941 // Fixed 6299853: XToolkit: Pressing space triggers ItemStateChanged event after List.removeAll called 942 // If getFocusIndex() is less than 0, the event will not be triggered when space pressed 943 if (getFocusIndex() < 0 || ((List)target).getItemCount() <= 0) { 944 break; 945 } 946 947 boolean isSelected = isSelected(getFocusIndex()); 948 949 // Spacebar only deselects for multi-select Lists 950 if (multipleSelections && isSelected) { 951 deselectItem(getFocusIndex()); 952 postEvent(new ItemEvent((List)target, 953 ItemEvent.ITEM_STATE_CHANGED, 954 Integer.valueOf(getFocusIndex()), 955 ItemEvent.DESELECTED)); 956 } 957 else if (!isSelected) { // Note: this changes the Solaris/Linux 958 // behavior to match that of win32. 959 // That is, pressing space bar on a 960 // single-select list when the focused 961 // item is already selected does NOT 962 // send an ItemEvent.SELECTED event. 963 selectItem(getFocusIndex()); 964 postEvent(new ItemEvent((List)target, 965 ItemEvent.ITEM_STATE_CHANGED, 966 Integer.valueOf(getFocusIndex()), 967 ItemEvent.SELECTED)); 968 } 969 break; 970 case KeyEvent.VK_ENTER: 971 // It looks to me like there are bugs as well as inconsistencies 972 // in the way the Enter key is handled by both Solaris and Windows. 973 // So for now in XAWT, I'm going to simply go by what the List docs 974 // say: "AWT also generates an action event when the user presses 975 // the return key while an item in the list is selected." 976 if (selected.length > 0) { 977 postEvent(new ActionEvent((List)target, 978 ActionEvent.ACTION_PERFORMED, 979 (String)items.elementAt(getFocusIndex()), 980 e.getWhen(), 981 e.getModifiers())); // ActionEvent doesn't have 982 // extended modifiers. 983 } 984 break; 985 } 986 } 987 988 /** 989 * return value from the scrollbar 990 */ 991 public void notifyValue(XScrollbar obj, int type, int v, boolean isAdjusting) { 992 993 if (log.isLoggable(PlatformLogger.FINE)) log.fine("Notify value changed on " + obj + " to " + v); 994 int value = obj.getValue(); 995 if (obj == vsb) { 996 scrollVertical(v - value); 997 998 // See 6243382 for more information 999 int oldSel = eventIndex; 1000 int newSel = eventIndex+v-value; 1001 if (mouseDraggedOutVertically && !isSelected(newSel)){ 1002 selectItem(newSel); 1003 eventIndex = newSel; 1004 repaint(oldSel, eventIndex, PAINT_ITEMS); 1005 // Scrolling select() should also set the focus index 1006 // Otherwise, the updating of the 'focusIndex' variable will be incorrect 1007 // if user drag mouse out of the area of the list 1008 setFocusIndex(newSel); 1009 repaint(PAINT_FOCUS); 1010 } 1011 1012 } else if ((XHorizontalScrollbar)obj == hsb) { 1013 scrollHorizontal(v - value); 1014 } 1015 1016 } 1017 1018 /** 1019 * deselect all items in List 1020 */ 1021 private void deselectAllItems() { 1022 selected = new int [0]; 1023 repaint(PAINT_ITEMS); 1024 } 1025 1026 /** 1027 * set multiple selections 1028 */ 1029 public void setMultipleSelections(boolean v) { 1030 if (multipleSelections != v) { 1031 if ( !v) { 1032 int selPos = ( isSelected( focusIndex )) ? focusIndex: -1; 1033 deselectAllItems(); 1034 if (selPos != -1){ 1035 selectItem(selPos); 1036 } 1037 } 1038 multipleSelections = v; 1039 } 1040 } 1041 1042 /** 1043 * add an item 1044 * if the index of the item is < 0 or >= than items.size() 1045 * then add the item to the end of the list 1046 */ 1047 public void addItem(String item, int i) { 1048 int oldMaxLength = maxLength; 1049 boolean hsbWasVis = hsbVis; 1050 boolean vsbWasVis = vsbVis; 1051 1052 int addedIndex = 0; // Index where the new item ended up 1053 if (i < 0 || i >= items.size()) { 1054 i = -1; 1055 } 1056 1057 // Why we set this variable to -1 in spite of the fact that selected[] is changed in other way? 1058 // It's not clear how to reproduce incorrect behaviour based on this assignment 1059 // since before using this variable (mouseReleased) we certainly update it to correct value 1060 // So we don't modify this behaviour now 1061 currentIndex = -1; 1062 1063 if (i == -1) { 1064 items.addElement(item); 1065 i = 0; // fix the math for the paintItems test 1066 addedIndex = items.size() - 1; 1067 } else { 1068 items.insertElementAt(item, i); 1069 addedIndex = i; 1070 for (int j = 0 ; j < selected.length ; j++) { 1071 if (selected[j] >= i) { 1072 selected[j] += 1; 1073 } 1074 } 1075 } 1076 if (log.isLoggable(PlatformLogger.FINER)) log.finer("Adding item '" + item + "' to " + addedIndex); 1077 1078 // Update maxLength 1079 boolean repaintItems = !isItemHidden(addedIndex); 1080 maxLength = Math.max(maxLength, getItemWidth(addedIndex)); 1081 layout(); 1082 1083 int options = 0; 1084 if (vsbVis != vsbWasVis || hsbVis != hsbWasVis) { 1085 // Scrollbars are being added or removed, so we must repaint all 1086 options = PAINT_ALL; 1087 } 1088 else { 1089 options = (repaintItems ? (PAINT_ITEMS):0) 1090 | ((maxLength != oldMaxLength || (hsbWasVis ^ hsbVis))?(PAINT_HSCROLL):0) 1091 | ((vsb.needsRepaint())?(PAINT_VSCROLL):0); 1092 1093 } 1094 if (log.isLoggable(PlatformLogger.FINEST)) log.finest("Last visible: " + getLastVisibleItem() + 1095 ", hsb changed : " + (hsbWasVis ^ hsbVis) + ", items changed " + repaintItems); 1096 repaint(addedIndex, getLastVisibleItem(), options); 1097 } 1098 1099 /** 1100 * delete items starting with s (start position) to e (end position) including s and e 1101 * if s < 0 then s = 0 1102 * if e >= items.size() then e = items.size() - 1 1103 */ 1104 public void delItems(int s, int e) { 1105 // save the current state of the scrollbars 1106 boolean hsbWasVisible = hsbVis; 1107 boolean vsbWasVisible = vsbVis; 1108 int oldLastDisplayed = lastItemDisplayed(); 1109 1110 if (log.isLoggable(PlatformLogger.FINE)) log.fine("Deleting from " + s + " to " + e); 1111 1112 if (log.isLoggable(PlatformLogger.FINEST)) log.finest("Last displayed item: " + oldLastDisplayed + ", items in window " + itemsInWindow() + 1113 ", size " + items.size()); 1114 1115 if (items.size() == 0) { 1116 return; 1117 } 1118 1119 // if user passed in flipped args, reverse them 1120 if (s > e) { 1121 int tmp = s; 1122 s = e; 1123 e = tmp; 1124 } 1125 1126 // check for starting point less than zero 1127 if (s < 0) { 1128 s = 0; 1129 } 1130 1131 // check for end point greater than the size of the list 1132 if (e >= items.size()) { 1133 e = items.size() - 1; 1134 } 1135 1136 // determine whether we're going to delete any visible elements 1137 // repaint must also be done if scrollbars appear/disappear, which 1138 // can happen from removing a non-showing list item 1139 /* 1140 boolean repaintNeeded = 1141 ((s <= lastItemDisplayed()) && (e >= vsb.getValue())); 1142 */ 1143 boolean repaintNeeded = (s >= getFirstVisibleItem() && s <= getLastVisibleItem()); 1144 1145 // delete the items out of the items list and out of the selected list 1146 for (int i = s ; i <= e ; i++) { 1147 items.removeElementAt(s); 1148 int j = posInSel(i); 1149 if (j != -1) { 1150 int newsel[] = new int[selected.length - 1]; 1151 System.arraycopy(selected, 0, newsel, 0, j); 1152 System.arraycopy(selected, j + 1, newsel, j, selected.length - (j + 1)); 1153 selected = newsel; 1154 } 1155 1156 } 1157 1158 // update the indexes in the selected array 1159 int diff = (e - s) + 1; 1160 for (int i = 0 ; i < selected.length ; i++) { 1161 if (selected[i] > e) { 1162 selected[i] -= diff; 1163 } 1164 } 1165 1166 int options = PAINT_VSCROLL; 1167 // focusedIndex updating according to native (Window, Motif) behaviour 1168 if (getFocusIndex() > e) { 1169 setFocusIndex(getFocusIndex() - (e - s + 1)); 1170 options |= PAINT_FOCUS; 1171 } else if (getFocusIndex() >= s && getFocusIndex() <= e) { 1172 // Fixed 6299858: PIT. Focused border not shown on List if selected item is removed, XToolkit 1173 // We should set focus to new first item if the current first item was removed 1174 // except if the list is empty 1175 int focusBound = (items.size() > 0) ? 0 : -1; 1176 setFocusIndex(Math.max(s-1, focusBound)); 1177 options |= PAINT_FOCUS; 1178 } 1179 1180 if (log.isLoggable(PlatformLogger.FINEST)) log.finest("Multiple selections: " + multipleSelections); 1181 1182 // update vsb.val 1183 if (vsb.getValue() >= s) { 1184 if (vsb.getValue() <= e) { 1185 vsb.setValue(e+1 - diff); 1186 } else { 1187 vsb.setValue(vsb.getValue() - diff); 1188 } 1189 } 1190 1191 int oldMaxLength = maxLength; 1192 maxLength = maxLength(); 1193 if (maxLength != oldMaxLength) { 1194 // Width of the items changed affecting the range of 1195 // horizontal scrollbar 1196 options |= PAINT_HSCROLL; 1197 } 1198 layout(); 1199 repaintNeeded |= (vsbWasVisible ^ vsbVis) || (hsbWasVisible ^ hsbVis); // If scrollbars visibility changed 1200 if (repaintNeeded) { 1201 options |= PAINT_ALL; 1202 } 1203 repaint(s, oldLastDisplayed, options); 1204 } 1205 1206 /** 1207 * ListPeer method 1208 */ 1209 public void select(int index) { 1210 // Programmatic select() should also set the focus index 1211 setFocusIndex(index); 1212 repaint(PAINT_FOCUS); 1213 selectItem(index); 1214 } 1215 1216 /** 1217 * select the index 1218 * redraw the list to the screen 1219 */ 1220 void selectItem(int index) { 1221 // NOTE: instead of recalculating and the calling repaint(), painting 1222 // is done immediately 1223 1224 // 6190746 List does not trigger ActionEvent when double clicking a programmatically selected item, XToolkit 1225 // If we invoke select(int) before setVisible(boolean), then variable currentIndex will equals -1. At the same time isSelected may be true. 1226 // Restoring Motif behavior 1227 currentIndex = index; 1228 1229 if (isSelected(index)) { 1230 return; 1231 } 1232 if (!multipleSelections) { 1233 if (selected.length == 0) { // No current selection 1234 selected = new int[1]; 1235 selected[0] = index; 1236 } 1237 else { 1238 int oldSel = selected[0]; 1239 selected[0] = index; 1240 if (!isItemHidden(oldSel)) { 1241 // Only bother painting if item is visible (4895367) 1242 repaint(oldSel, oldSel, PAINT_ITEMS); 1243 } 1244 } 1245 } else { 1246 // insert "index" into the selection array 1247 int newsel[] = new int[selected.length + 1]; 1248 int i = 0; 1249 while (i < selected.length && index > selected[i]) { 1250 newsel[i] = selected[i]; 1251 i++; 1252 } 1253 newsel[i] = index; 1254 System.arraycopy(selected, i, newsel, i+1, selected.length - i); 1255 selected = newsel; 1256 } 1257 if (!isItemHidden(index)) { 1258 // Only bother painting if item is visible (4895367) 1259 repaint(index, index, PAINT_ITEMS); 1260 } 1261 } 1262 1263 /** 1264 * ListPeer method 1265 * focusedIndex isn't updated according to native (Window, Motif) behaviour 1266 */ 1267 public void deselect(int index) { 1268 deselectItem(index); 1269 } 1270 1271 /** 1272 * deselect the index 1273 * redraw the list to the screen 1274 */ 1275 void deselectItem(int index) { 1276 if (!isSelected(index)) { 1277 return; 1278 } 1279 if (!multipleSelections) { 1280 // TODO: keep an int[0] and int[1] around and just use them instead 1281 // creating new ones all the time 1282 selected = new int[0]; 1283 } else { 1284 int i = posInSel(index); 1285 int newsel[] = new int[selected.length - 1]; 1286 System.arraycopy(selected, 0, newsel, 0, i); 1287 System.arraycopy(selected, i+1, newsel, i, selected.length - (i+1)); 1288 selected = newsel; 1289 } 1290 currentIndex = index; 1291 if (!isItemHidden(index)) { 1292 // Only bother repainting if item is visible 1293 repaint(index, index, PAINT_ITEMS); 1294 } 1295 } 1296 1297 /** 1298 * ensure that the given index is visible, scrolling the List 1299 * if necessary, or doing nothing if the item is already visible. 1300 * The List must be repainted for changes to be visible. 1301 */ 1302 public void makeVisible(int index) { 1303 if (index < 0 || index >= items.size()) { 1304 return; 1305 } 1306 if (isItemHidden(index)) { // Do I really need to call this? 1307 // If index is above the top, scroll up 1308 if (index < vsb.getValue()) { 1309 scrollVertical(index - vsb.getValue()); 1310 } 1311 // If index is below the bottom, scroll down 1312 else if (index > lastItemDisplayed()) { 1313 int val = index - lastItemDisplayed(); 1314 scrollVertical(val); 1315 } 1316 } 1317 } 1318 1319 /** 1320 * clear 1321 */ 1322 public void clear() { 1323 selected = new int[0]; 1324 items = new Vector(); 1325 currentIndex = -1; 1326 // Fixed 6291736: ITEM_STATE_CHANGED triggered after List.removeAll(), XToolkit 1327 // We should update 'focusIndex' variable more carefully 1328 setFocusIndex(-1); 1329 vsb.setValue(0); 1330 maxLength = 0; 1331 layout(); 1332 repaint(); 1333 } 1334 1335 /** 1336 * return the selected indexes 1337 */ 1338 public int[] getSelectedIndexes() { 1339 return selected; 1340 } 1341 1342 /** 1343 * return the y value of the given index "i". 1344 * the y value represents the top of the text 1345 * NOTE: index can be larger than items.size as long 1346 * as it can fit the window 1347 */ 1348 int index2y(int index) { 1349 int h = getItemHeight(); 1350 1351 //if (index < vsb.getValue() || index > vsb.getValue() + itemsInWindow()) { 1352 return MARGIN + ((index - vsb.getValue()) * h) + SPACE; 1353 } 1354 1355 /* return true if the y is a valid y coordinate for 1356 * a VISIBLE list item, otherwise returns false 1357 */ 1358 boolean validY(int y) { 1359 1360 int shown = itemsDisplayed(); 1361 int lastY = shown * getItemHeight() + MARGIN; 1362 1363 if (shown == itemsInWindow()) { 1364 lastY += MARGIN; 1365 } 1366 1367 if (y < 0 || y >= lastY) { 1368 return false; 1369 } 1370 1371 return true; 1372 } 1373 1374 /** 1375 * return the position of the index in the selected array 1376 * if the index isn't in the array selected return -1; 1377 */ 1378 int posInSel(int index) { 1379 for (int i = 0 ; i < selected.length ; i++) { 1380 if (index == selected[i]) { 1381 return i; 1382 } 1383 } 1384 return -1; 1385 } 1386 1387 boolean isIndexDisplayed(int idx) { 1388 int lastDisplayed = lastItemDisplayed(); 1389 1390 return idx <= lastDisplayed && 1391 idx >= Math.max(0, lastDisplayed - itemsInWindow() + 1); 1392 } 1393 1394 /** 1395 * returns index of last item displayed in the List 1396 */ 1397 int lastItemDisplayed() { 1398 int n = itemsInWindow(); 1399 return (Math.min(items.size() - 1, (vsb.getValue() + n) - 1)); 1400 } 1401 1402 /** 1403 * returns whether the given index is currently scrolled off the top or 1404 * bottom of the List. 1405 */ 1406 boolean isItemHidden(int index) { 1407 return index < vsb.getValue() || 1408 index >= vsb.getValue() + itemsInWindow(); 1409 } 1410 1411 /** 1412 * returns the width of the list portion of the component (accounts for 1413 * presence of vertical scrollbar) 1414 */ 1415 int getListWidth() { 1416 return vsbVis ? width - SCROLLBAR_AREA : width; 1417 } 1418 1419 /** 1420 * returns number of items actually displayed in the List 1421 */ 1422 int itemsDisplayed() { 1423 1424 return (Math.min(items.size()-vsb.getValue(), itemsInWindow())); 1425 1426 } 1427 1428 /** 1429 * scrollVertical 1430 * y is the number of items to scroll 1431 */ 1432 void scrollVertical(int y) { 1433 if (log.isLoggable(PlatformLogger.FINE)) log.fine("Scrolling vertically by " + y); 1434 int itemsInWin = itemsInWindow(); 1435 int h = getItemHeight(); 1436 int pixelsToScroll = y * h; 1437 1438 if (vsb.getValue() < -y) { 1439 y = -vsb.getValue(); 1440 } 1441 vsb.setValue(vsb.getValue() + y); 1442 1443 Rectangle source = null; 1444 Point distance = null; 1445 int firstItem = 0, lastItem = 0; 1446 int options = PAINT_HIDEFOCUS | PAINT_ITEMS | PAINT_VSCROLL | PAINT_FOCUS; 1447 if (y > 0) { 1448 if (y < itemsInWin) { 1449 source = new Rectangle(MARGIN, MARGIN + pixelsToScroll, width - SCROLLBAR_AREA, h * (itemsInWin - y - 1)-1); 1450 distance = new Point(0, -pixelsToScroll); 1451 options |= COPY_AREA; 1452 } 1453 firstItem = vsb.getValue() + itemsInWin - y - 1; 1454 lastItem = vsb.getValue() + itemsInWin - 1; 1455 1456 } else if (y < 0) { 1457 if (y + itemsInWindow() > 0) { 1458 source = new Rectangle(MARGIN, MARGIN, width - SCROLLBAR_AREA, h * (itemsInWin + y)); 1459 distance = new Point(0, -pixelsToScroll); 1460 options |= COPY_AREA; 1461 } 1462 firstItem = vsb.getValue(); 1463 lastItem = Math.min(getLastVisibleItem(), vsb.getValue() + -y); 1464 } 1465 repaint(firstItem, lastItem, options, source, distance); 1466 } 1467 1468 /** 1469 * scrollHorizontal 1470 * x is the number of pixels to scroll 1471 */ 1472 void scrollHorizontal(int x) { 1473 if (log.isLoggable(PlatformLogger.FINE)) log.fine("Scrolling horizontally by " + y); 1474 int w = getListWidth(); 1475 w -= ((2 * SPACE) + (2 * MARGIN)); 1476 int h = height - (SCROLLBAR_AREA + (2 * MARGIN)); 1477 hsb.setValue(hsb.getValue() + x); 1478 1479 int options = PAINT_ITEMS | PAINT_HSCROLL; 1480 1481 Rectangle source = null; 1482 Point distance = null; 1483 if (x < 0) { 1484 source = new Rectangle(MARGIN + SPACE, MARGIN, w + x, h); 1485 distance = new Point(-x, 0); 1486 options |= COPY_AREA; 1487 } else if (x > 0) { 1488 source = new Rectangle(MARGIN + SPACE + x, MARGIN, w - x, h); 1489 distance = new Point(-x, 0); 1490 options |= COPY_AREA; 1491 } 1492 repaint(vsb.getValue(), lastItemDisplayed(), options, source, distance); 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(PlatformLogger.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) { 1741 paint(listG, firstItem, lastItem, options, null, null); 1742 } 1743 1744 private void paint(Graphics listG, int firstItem, int lastItem, int options, 1745 Rectangle source, Point distance) { 1746 if (log.isLoggable(PlatformLogger.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; 1773 case VolatileImage.IMAGE_RESTORED: 1774 options = PAINT_ALL; 1775 } 1776 Graphics g = localBuffer.createGraphics(); 1777 1778 // Note that the order of the following painting operations 1779 // should not be modified 1780 try { 1781 g.setFont(getFont()); 1782 1783 // hiding the focus rectangle must be done prior to copying 1784 // area and so this is the first action to be performed 1785 if ((options & (PAINT_HIDEFOCUS)) != 0) { 1786 paintFocus(g, PAINT_HIDEFOCUS); 1787 } 1788 /* 1789 * The shift of the component contents occurs while someone 1790 * scrolls the component, the only purpose of the shift is to 1791 * increase the painting performance. The shift should be done 1792 * prior to painting any area (except hiding focus) and actually 1793 * it should never be done jointly with erase background. 1794 */ 1795 if ((options & COPY_AREA) != 0) { 1796 g.copyArea(source.x, source.y, source.width, source.height, 1797 distance.x, distance.y); 1798 } 1799 if ((options & PAINT_BACKGROUND) != 0) { 1800 paintBackground(g); 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 } 1816 if ((options & (PAINT_FOCUS)) != 0) { 1817 paintFocus(g, PAINT_FOCUS); 1818 } 1819 } finally { 1820 g.dispose(); 1821 } 1822 } while (localBuffer.contentsLost()); 1823 listG.drawImage(localBuffer, 0, 0, null); 1824 } 1825 1826 private void paintBackground(Graphics g) { 1827 g.setColor(SystemColor.window); 1828 g.fillRect(0, 0, width, height); 1829 g.setColor(getListBackground()); 1830 g.fillRect(0, 0, listWidth, listHeight); 1831 draw3DRect(g, getSystemColors(), 0, 0, listWidth - 1, listHeight - 1, false); 1832 } 1833 1834 private void paintItems(Graphics g, int firstItem, int lastItem, int options) { 1835 if (log.isLoggable(PlatformLogger.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(PlatformLogger.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(PlatformLogger.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(PlatformLogger.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(PlatformLogger.FINEST)) log.finest("Painted item is selected"); 1869 g.setColor(getListForeground()); 1870 } else { 1871 g.setColor(getListBackground()); 1872 } 1873 if (log.isLoggable(PlatformLogger.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(PlatformLogger.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(PlatformLogger.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(PlatformLogger.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(PlatformLogger.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(PlatformLogger.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 }