1 11 package org.eclipse.swt.custom; 12 13 14 import org.eclipse.swt.*; 15 import org.eclipse.swt.graphics.*; 16 import org.eclipse.swt.events.*; 17 import org.eclipse.swt.widgets.*; 18 import org.eclipse.swt.accessibility.*; 19 20 44 public final class CCombo extends Composite { 45 46 Text text; 47 List list; 48 int visibleItemCount = 5; 49 Shell popup; 50 Button arrow; 51 boolean hasFocus; 52 Listener listener, filter; 53 Color foreground, background; 54 Font font; 55 56 84 public CCombo (Composite parent, int style) { 85 super (parent, style = checkStyle (style)); 86 87 int textStyle = SWT.SINGLE; 88 if ((style & SWT.READ_ONLY) != 0) textStyle |= SWT.READ_ONLY; 89 if ((style & SWT.FLAT) != 0) textStyle |= SWT.FLAT; 90 text = new Text (this, textStyle); 91 int arrowStyle = SWT.ARROW | SWT.DOWN; 92 if ((style & SWT.FLAT) != 0) arrowStyle |= SWT.FLAT; 93 arrow = new Button (this, arrowStyle); 94 95 listener = new Listener () { 96 public void handleEvent (Event event) { 97 if (popup == event.widget) { 98 popupEvent (event); 99 return; 100 } 101 if (text == event.widget) { 102 textEvent (event); 103 return; 104 } 105 if (list == event.widget) { 106 listEvent (event); 107 return; 108 } 109 if (arrow == event.widget) { 110 arrowEvent (event); 111 return; 112 } 113 if (CCombo.this == event.widget) { 114 comboEvent (event); 115 return; 116 } 117 if (getShell () == event.widget) { 118 handleFocus (SWT.FocusOut); 119 } 120 } 121 }; 122 filter = new Listener() { 123 public void handleEvent(Event event) { 124 Shell shell = ((Control)event.widget).getShell (); 125 if (shell == CCombo.this.getShell ()) { 126 handleFocus (SWT.FocusOut); 127 } 128 } 129 }; 130 131 int [] comboEvents = {SWT.Dispose, SWT.Move, SWT.Resize}; 132 for (int i=0; i<comboEvents.length; i++) this.addListener (comboEvents [i], listener); 133 134 int [] textEvents = {SWT.KeyDown, SWT.KeyUp, SWT.MenuDetect, SWT.Modify, SWT.MouseDown, SWT.MouseUp, SWT.Traverse, SWT.FocusIn, SWT.Verify}; 135 for (int i=0; i<textEvents.length; i++) text.addListener (textEvents [i], listener); 136 137 int [] arrowEvents = {SWT.Selection, SWT.FocusIn}; 138 for (int i=0; i<arrowEvents.length; i++) arrow.addListener (arrowEvents [i], listener); 139 140 createPopup(null, -1); 141 initAccessible(); 142 } 143 static int checkStyle (int style) { 144 int mask = SWT.BORDER | SWT.READ_ONLY | SWT.FLAT | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT; 145 return style & mask; 146 } 147 162 public void add (String string) { 163 checkWidget(); 164 if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 165 list.add (string); 166 } 167 190 public void add (String string, int index) { 191 checkWidget(); 192 if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 193 list.add (string, index); 194 } 195 214 public void addModifyListener (ModifyListener listener) { 215 checkWidget(); 216 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 217 TypedListener typedListener = new TypedListener (listener); 218 addListener (SWT.Modify, typedListener); 219 } 220 244 public void addSelectionListener(SelectionListener listener) { 245 checkWidget(); 246 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 247 TypedListener typedListener = new TypedListener (listener); 248 addListener (SWT.Selection,typedListener); 249 addListener (SWT.DefaultSelection,typedListener); 250 } 251 272 public void addVerifyListener (VerifyListener listener) { 273 checkWidget(); 274 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 275 TypedListener typedListener = new TypedListener (listener); 276 addListener (SWT.Verify,typedListener); 277 } 278 void arrowEvent (Event event) { 279 switch (event.type) { 280 case SWT.FocusIn: { 281 handleFocus (SWT.FocusIn); 282 break; 283 } 284 case SWT.Selection: { 285 dropDown (!isDropped ()); 286 break; 287 } 288 } 289 } 290 307 public void clearSelection () { 308 checkWidget (); 309 text.clearSelection (); 310 list.deselectAll (); 311 } 312 void comboEvent (Event event) { 313 switch (event.type) { 314 case SWT.Dispose: 315 if (popup != null && !popup.isDisposed ()) { 316 list.removeListener (SWT.Dispose, listener); 317 popup.dispose (); 318 } 319 Shell shell = getShell (); 320 shell.removeListener (SWT.Deactivate, listener); 321 Display display = getDisplay (); 322 display.removeFilter (SWT.FocusIn, filter); 323 popup = null; 324 text = null; 325 list = null; 326 arrow = null; 327 break; 328 case SWT.Move: 329 dropDown (false); 330 break; 331 case SWT.Resize: 332 internalLayout (false); 333 break; 334 } 335 } 336 337 public Point computeSize (int wHint, int hHint, boolean changed) { 338 checkWidget (); 339 int width = 0, height = 0; 340 String [] items = list.getItems (); 341 GC gc = new GC (text); 342 int spacer = gc.stringExtent (" ").x; int textWidth = gc.stringExtent (text.getText ()).x; 344 for (int i = 0; i < items.length; i++) { 345 textWidth = Math.max (gc.stringExtent (items[i]).x, textWidth); 346 } 347 gc.dispose (); 348 Point textSize = text.computeSize (SWT.DEFAULT, SWT.DEFAULT, changed); 349 Point arrowSize = arrow.computeSize (SWT.DEFAULT, SWT.DEFAULT, changed); 350 Point listSize = list.computeSize (SWT.DEFAULT, SWT.DEFAULT, changed); 351 int borderWidth = getBorderWidth (); 352 353 height = Math.max (textSize.y, arrowSize.y); 354 width = Math.max (textWidth + 2*spacer + arrowSize.x + 2*borderWidth, listSize.x); 355 if (wHint != SWT.DEFAULT) width = wHint; 356 if (hHint != SWT.DEFAULT) height = hHint; 357 return new Point (width + 2*borderWidth, height + 2*borderWidth); 358 } 359 372 public void copy () { 373 checkWidget (); 374 text.copy (); 375 } 376 void createPopup(String [] items, int selectionIndex) { 377 popup = new Shell (getShell (), SWT.NO_TRIM | SWT.ON_TOP); 379 int style = getStyle (); 380 int listStyle = SWT.SINGLE | SWT.V_SCROLL; 381 if ((style & SWT.FLAT) != 0) listStyle |= SWT.FLAT; 382 if ((style & SWT.RIGHT_TO_LEFT) != 0) listStyle |= SWT.RIGHT_TO_LEFT; 383 if ((style & SWT.LEFT_TO_RIGHT) != 0) listStyle |= SWT.LEFT_TO_RIGHT; 384 list = new List (popup, listStyle); 385 if (font != null) list.setFont (font); 386 if (foreground != null) list.setForeground (foreground); 387 if (background != null) list.setBackground (background); 388 389 int [] popupEvents = {SWT.Close, SWT.Paint, SWT.Deactivate}; 390 for (int i=0; i<popupEvents.length; i++) popup.addListener (popupEvents [i], listener); 391 int [] listEvents = {SWT.MouseUp, SWT.Selection, SWT.Traverse, SWT.KeyDown, SWT.KeyUp, SWT.FocusIn, SWT.Dispose}; 392 for (int i=0; i<listEvents.length; i++) list.addListener (listEvents [i], listener); 393 394 if (items != null) list.setItems (items); 395 if (selectionIndex != -1) list.setSelection (selectionIndex); 396 } 397 411 public void cut () { 412 checkWidget (); 413 text.cut (); 414 } 415 427 public void deselect (int index) { 428 checkWidget (); 429 list.deselect (index); 430 } 431 445 public void deselectAll () { 446 checkWidget (); 447 list.deselectAll (); 448 } 449 void dropDown (boolean drop) { 450 if (drop == isDropped ()) return; 451 if (!drop) { 452 popup.setVisible (false); 453 if (!isDisposed ()&& arrow.isFocusControl()) { 454 text.setFocus(); 455 } 456 return; 457 } 458 459 if (getShell() != popup.getParent ()) { 460 String [] items = list.getItems (); 461 int selectionIndex = list.getSelectionIndex (); 462 list.removeListener (SWT.Dispose, listener); 463 popup.dispose(); 464 popup = null; 465 list = null; 466 createPopup (items, selectionIndex); 467 } 468 469 Point size = getSize (); 470 int itemCount = list.getItemCount (); 471 itemCount = (itemCount == 0) ? visibleItemCount : Math.min(visibleItemCount, itemCount); 472 int itemHeight = list.getItemHeight () * itemCount; 473 Point listSize = list.computeSize (SWT.DEFAULT, itemHeight, false); 474 list.setBounds (1, 1, Math.max (size.x - 2, listSize.x), listSize.y); 475 476 int index = list.getSelectionIndex (); 477 if (index != -1) list.setTopIndex (index); 478 Display display = getDisplay (); 479 Rectangle listRect = list.getBounds (); 480 Rectangle parentRect = display.map (getParent (), null, getBounds ()); 481 Point comboSize = getSize (); 482 Rectangle displayRect = getMonitor ().getClientArea (); 483 int width = Math.max (comboSize.x, listRect.width + 2); 484 int height = listRect.height + 2; 485 int x = parentRect.x; 486 int y = parentRect.y + comboSize.y; 487 if (y + height > displayRect.y + displayRect.height) y = parentRect.y - height; 488 if (x + width > displayRect.x + displayRect.width) x = displayRect.x + displayRect.width - listRect.width; 489 popup.setBounds (x, y, width, height); 490 popup.setVisible (true); 491 list.setFocus (); 492 } 493 498 char _findMnemonic (String string) { 499 if (string == null) return '\0'; 500 int index = 0; 501 int length = string.length (); 502 do { 503 while (index < length && string.charAt (index) != '&') index++; 504 if (++index >= length) return '\0'; 505 if (string.charAt (index) != '&') return Character.toLowerCase (string.charAt (index)); 506 index++; 507 } while (index < length); 508 return '\0'; 509 } 510 514 Label getAssociatedLabel () { 515 Control[] siblings = getParent ().getChildren (); 516 for (int i = 0; i < siblings.length; i++) { 517 if (siblings [i] == this) { 518 if (i > 0 && siblings [i-1] instanceof Label) { 519 return (Label) siblings [i-1]; 520 } 521 } 522 } 523 return null; 524 } 525 public Control [] getChildren () { 526 checkWidget(); 527 return new Control [0]; 528 } 529 541 public boolean getEditable () { 542 checkWidget (); 543 return text.getEditable(); 544 } 545 561 public String getItem (int index) { 562 checkWidget(); 563 return list.getItem (index); 564 } 565 575 public int getItemCount () { 576 checkWidget (); 577 return list.getItemCount (); 578 } 579 590 public int getItemHeight () { 591 checkWidget (); 592 return list.getItemHeight (); 593 } 594 610 public String [] getItems () { 611 checkWidget (); 612 return list.getItems (); 613 } 614 public Menu getMenu() { 615 return text.getMenu(); 616 } 617 631 public Point getSelection () { 632 checkWidget (); 633 return text.getSelection (); 634 } 635 646 public int getSelectionIndex () { 647 checkWidget (); 648 return list.getSelectionIndex (); 649 } 650 public int getStyle () { 651 int style = super.getStyle (); 652 style &= ~SWT.READ_ONLY; 653 if (!text.getEditable()) style |= SWT.READ_ONLY; 654 return style; 655 } 656 667 public String getText () { 668 checkWidget (); 669 return text.getText (); 670 } 671 681 public int getTextHeight () { 682 checkWidget (); 683 return text.getLineHeight (); 684 } 685 698 public int getTextLimit () { 699 checkWidget (); 700 return text.getTextLimit (); 701 } 702 715 public int getVisibleItemCount () { 716 checkWidget (); 717 return visibleItemCount; 718 } 719 void handleFocus (int type) { 720 if (isDisposed ()) return; 721 switch (type) { 722 case SWT.FocusIn: { 723 if (hasFocus) return; 724 if (getEditable ()) text.selectAll (); 725 hasFocus = true; 726 Shell shell = getShell (); 727 shell.removeListener (SWT.Deactivate, listener); 728 shell.addListener (SWT.Deactivate, listener); 729 Display display = getDisplay (); 730 display.removeFilter (SWT.FocusIn, filter); 731 display.addFilter (SWT.FocusIn, filter); 732 Event e = new Event (); 733 notifyListeners (SWT.FocusIn, e); 734 break; 735 } 736 case SWT.FocusOut: { 737 if (!hasFocus) return; 738 Control focusControl = getDisplay ().getFocusControl (); 739 if (focusControl == arrow || focusControl == list || focusControl == text) return; 740 hasFocus = false; 741 Shell shell = getShell (); 742 shell.removeListener(SWT.Deactivate, listener); 743 Display display = getDisplay (); 744 display.removeFilter (SWT.FocusIn, filter); 745 Event e = new Event (); 746 notifyListeners (SWT.FocusOut, e); 747 break; 748 } 749 } 750 } 751 768 public int indexOf (String string) { 769 checkWidget (); 770 if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 771 return list.indexOf (string); 772 } 773 792 public int indexOf (String string, int start) { 793 checkWidget (); 794 if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 795 return list.indexOf (string, start); 796 } 797 798 void initAccessible() { 799 AccessibleAdapter accessibleAdapter = new AccessibleAdapter () { 800 public void getName (AccessibleEvent e) { 801 String name = null; 802 Label label = getAssociatedLabel (); 803 if (label != null) { 804 name = stripMnemonic (label.getText()); 805 } 806 e.result = name; 807 } 808 public void getKeyboardShortcut(AccessibleEvent e) { 809 String shortcut = null; 810 Label label = getAssociatedLabel (); 811 if (label != null) { 812 String text = label.getText (); 813 if (text != null) { 814 char mnemonic = _findMnemonic (text); 815 if (mnemonic != '\0') { 816 shortcut = "Alt+"+mnemonic; } 818 } 819 } 820 e.result = shortcut; 821 } 822 public void getHelp (AccessibleEvent e) { 823 e.result = getToolTipText (); 824 } 825 }; 826 getAccessible ().addAccessibleListener (accessibleAdapter); 827 text.getAccessible ().addAccessibleListener (accessibleAdapter); 828 list.getAccessible ().addAccessibleListener (accessibleAdapter); 829 830 arrow.getAccessible ().addAccessibleListener (new AccessibleAdapter() { 831 public void getName (AccessibleEvent e) { 832 e.result = isDropped () ? SWT.getMessage ("SWT_Close") : SWT.getMessage ("SWT_Open"); } 834 public void getKeyboardShortcut (AccessibleEvent e) { 835 e.result = "Alt+Down Arrow"; } 837 public void getHelp (AccessibleEvent e) { 838 e.result = getToolTipText (); 839 } 840 }); 841 842 getAccessible().addAccessibleTextListener (new AccessibleTextAdapter() { 843 public void getCaretOffset (AccessibleTextEvent e) { 844 e.offset = text.getCaretPosition (); 845 } 846 public void getSelectionRange(AccessibleTextEvent e) { 847 Point sel = text.getSelection(); 848 e.offset = sel.x; 849 e.length = sel.y - sel.x; 850 } 851 }); 852 853 getAccessible().addAccessibleControlListener (new AccessibleControlAdapter() { 854 public void getChildAtPoint (AccessibleControlEvent e) { 855 Point testPoint = toControl (e.x, e.y); 856 if (getBounds ().contains (testPoint)) { 857 e.childID = ACC.CHILDID_SELF; 858 } 859 } 860 861 public void getLocation (AccessibleControlEvent e) { 862 Rectangle location = getBounds (); 863 Point pt = toDisplay (location.x, location.y); 864 e.x = pt.x; 865 e.y = pt.y; 866 e.width = location.width; 867 e.height = location.height; 868 } 869 870 public void getChildCount (AccessibleControlEvent e) { 871 e.detail = 0; 872 } 873 874 public void getRole (AccessibleControlEvent e) { 875 e.detail = ACC.ROLE_COMBOBOX; 876 } 877 878 public void getState (AccessibleControlEvent e) { 879 e.detail = ACC.STATE_NORMAL; 880 } 881 882 public void getValue (AccessibleControlEvent e) { 883 e.result = getText (); 884 } 885 }); 886 887 text.getAccessible ().addAccessibleControlListener (new AccessibleControlAdapter () { 888 public void getRole (AccessibleControlEvent e) { 889 e.detail = text.getEditable () ? ACC.ROLE_TEXT : ACC.ROLE_LABEL; 890 } 891 }); 892 893 arrow.getAccessible ().addAccessibleControlListener (new AccessibleControlAdapter() { 894 public void getDefaultAction (AccessibleControlEvent e) { 895 e.result = isDropped () ? SWT.getMessage ("SWT_Close") : SWT.getMessage ("SWT_Open"); } 897 }); 898 } 899 boolean isDropped () { 900 return popup.getVisible (); 901 } 902 public boolean isFocusControl () { 903 checkWidget(); 904 if (text.isFocusControl () || arrow.isFocusControl () || list.isFocusControl () || popup.isFocusControl ()) { 905 return true; 906 } 907 return super.isFocusControl (); 908 } 909 void internalLayout (boolean changed) { 910 if (isDropped ()) dropDown (false); 911 Rectangle rect = getClientArea (); 912 int width = rect.width; 913 int height = rect.height; 914 Point arrowSize = arrow.computeSize (SWT.DEFAULT, height, changed); 915 text.setBounds (0, 0, width - arrowSize.x, height); 916 arrow.setBounds (width - arrowSize.x, 0, arrowSize.x, arrowSize.y); 917 } 918 void listEvent (Event event) { 919 switch (event.type) { 920 case SWT.Dispose: 921 if (getShell () != popup.getParent ()) { 922 String [] items = list.getItems (); 923 int selectionIndex = list.getSelectionIndex (); 924 popup = null; 925 list = null; 926 createPopup (items, selectionIndex); 927 } 928 break; 929 case SWT.FocusIn: { 930 handleFocus (SWT.FocusIn); 931 break; 932 } 933 case SWT.MouseUp: { 934 if (event.button != 1) return; 935 dropDown (false); 936 break; 937 } 938 case SWT.Selection: { 939 int index = list.getSelectionIndex (); 940 if (index == -1) return; 941 text.setText (list.getItem (index)); 942 text.selectAll (); 943 list.setSelection (index); 944 Event e = new Event (); 945 e.time = event.time; 946 e.stateMask = event.stateMask; 947 e.doit = event.doit; 948 notifyListeners (SWT.Selection, e); 949 event.doit = e.doit; 950 break; 951 } 952 case SWT.Traverse: { 953 switch (event.detail) { 954 case SWT.TRAVERSE_RETURN: 955 case SWT.TRAVERSE_ESCAPE: 956 case SWT.TRAVERSE_ARROW_PREVIOUS: 957 case SWT.TRAVERSE_ARROW_NEXT: 958 event.doit = false; 959 break; 960 } 961 Event e = new Event (); 962 e.time = event.time; 963 e.detail = event.detail; 964 e.doit = event.doit; 965 e.character = event.character; 966 e.keyCode = event.keyCode; 967 notifyListeners (SWT.Traverse, e); 968 event.doit = e.doit; 969 event.detail = e.detail; 970 break; 971 } 972 case SWT.KeyUp: { 973 Event e = new Event (); 974 e.time = event.time; 975 e.character = event.character; 976 e.keyCode = event.keyCode; 977 e.stateMask = event.stateMask; 978 notifyListeners (SWT.KeyUp, e); 979 break; 980 } 981 case SWT.KeyDown: { 982 if (event.character == SWT.ESC) { 983 dropDown (false); 985 } 986 if ((event.stateMask & SWT.ALT) != 0 && (event.keyCode == SWT.ARROW_UP || event.keyCode == SWT.ARROW_DOWN)) { 987 dropDown (false); 988 } 989 if (event.character == SWT.CR) { 990 dropDown (false); 992 Event e = new Event (); 993 e.time = event.time; 994 e.stateMask = event.stateMask; 995 notifyListeners (SWT.DefaultSelection, e); 996 } 997 if (isDisposed ()) break; 1000 Event e = new Event(); 1001 e.time = event.time; 1002 e.character = event.character; 1003 e.keyCode = event.keyCode; 1004 e.stateMask = event.stateMask; 1005 notifyListeners(SWT.KeyDown, e); 1006 break; 1007 1008 } 1009 } 1010} 1011 1025public void paste () { 1026 checkWidget (); 1027 text.paste (); 1028} 1029void popupEvent(Event event) { 1030 switch (event.type) { 1031 case SWT.Paint: 1032 Rectangle listRect = list.getBounds(); 1034 Color black = getDisplay().getSystemColor(SWT.COLOR_BLACK); 1035 event.gc.setForeground(black); 1036 event.gc.drawRectangle(0, 0, listRect.width + 1, listRect.height + 1); 1037 break; 1038 case SWT.Close: 1039 event.doit = false; 1040 dropDown (false); 1041 break; 1042 case SWT.Deactivate: 1043 1050 if ("gtk".equals(SWT.getPlatform())) { 1051 Point point = arrow.toControl(getDisplay().getCursorLocation()); 1052 Point size = arrow.getSize(); 1053 Rectangle rect = new Rectangle(0, 0, size.x, size.y); 1054 if (!rect.contains(point)) dropDown (false); 1055 } else { 1056 dropDown(false); 1057 } 1058 break; 1059 } 1060} 1061public void redraw () { 1062 super.redraw(); 1063 text.redraw(); 1064 arrow.redraw(); 1065 if (popup.isVisible()) list.redraw(); 1066} 1067public void redraw (int x, int y, int width, int height, boolean all) { 1068 super.redraw(x, y, width, height, true); 1069} 1070 1071 1085public void remove (int index) { 1086 checkWidget(); 1087 list.remove (index); 1088} 1089 1105public void remove (int start, int end) { 1106 checkWidget(); 1107 list.remove (start, end); 1108} 1109 1125public void remove (String string) { 1126 checkWidget(); 1127 if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 1128 list.remove (string); 1129} 1130 1139public void removeAll () { 1140 checkWidget(); 1141 text.setText (""); list.removeAll (); 1143} 1144 1161public void removeModifyListener (ModifyListener listener) { 1162 checkWidget(); 1163 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 1164 removeListener(SWT.Modify, listener); 1165} 1166 1183public void removeSelectionListener (SelectionListener listener) { 1184 checkWidget(); 1185 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 1186 removeListener(SWT.Selection, listener); 1187 removeListener(SWT.DefaultSelection,listener); 1188} 1189 1208public void removeVerifyListener (VerifyListener listener) { 1209 checkWidget(); 1210 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 1211 removeListener(SWT.Verify, listener); 1212} 1213 1225public void select (int index) { 1226 checkWidget(); 1227 if (index == -1) { 1228 list.deselectAll (); 1229 text.setText (""); return; 1231 } 1232 if (0 <= index && index < list.getItemCount()) { 1233 if (index != getSelectionIndex()) { 1234 text.setText (list.getItem (index)); 1235 text.selectAll (); 1236 list.select (index); 1237 list.showSelection (); 1238 } 1239 } 1240} 1241public void setBackground (Color color) { 1242 super.setBackground(color); 1243 background = color; 1244 if (text != null) text.setBackground(color); 1245 if (list != null) list.setBackground(color); 1246 if (arrow != null) arrow.setBackground(color); 1247} 1248 1260public void setEditable (boolean editable) { 1261 checkWidget (); 1262 text.setEditable(editable); 1263} 1264public void setEnabled (boolean enabled) { 1265 super.setEnabled(enabled); 1266 if (popup != null) popup.setVisible (false); 1267 if (text != null) text.setEnabled(enabled); 1268 if (arrow != null) arrow.setEnabled(enabled); 1269} 1270public boolean setFocus () { 1271 checkWidget(); 1272 if (isFocusControl ()) return true; 1273 return text.setFocus (); 1274} 1275public void setFont (Font font) { 1276 super.setFont (font); 1277 this.font = font; 1278 text.setFont (font); 1279 list.setFont (font); 1280 internalLayout (true); 1281} 1282public void setForeground (Color color) { 1283 super.setForeground(color); 1284 foreground = color; 1285 if (text != null) text.setForeground(color); 1286 if (list != null) list.setForeground(color); 1287 if (arrow != null) arrow.setForeground(color); 1288} 1289 1307public void setItem (int index, String string) { 1308 checkWidget(); 1309 list.setItem (index, string); 1310} 1311 1325public void setItems (String [] items) { 1326 checkWidget (); 1327 list.setItems (items); 1328 if (!text.getEditable ()) text.setText (""); } 1330 1345public void setLayout (Layout layout) { 1346 checkWidget (); 1347 return; 1348} 1349public void setMenu(Menu menu) { 1350 text.setMenu(menu); 1351} 1352 1368public void setSelection (Point selection) { 1369 checkWidget(); 1370 if (selection == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 1371 text.setSelection (selection.x, selection.y); 1372} 1373 1374 1395public void setText (String string) { 1396 checkWidget(); 1397 if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 1398 int index = list.indexOf (string); 1399 if (index == -1) { 1400 list.deselectAll (); 1401 text.setText (string); 1402 return; 1403 } 1404 text.setText (string); 1405 text.selectAll (); 1406 list.setSelection (index); 1407 list.showSelection (); 1408} 1409 1423public void setTextLimit (int limit) { 1424 checkWidget(); 1425 text.setTextLimit (limit); 1426} 1427 1428public void setToolTipText (String string) { 1429 checkWidget(); 1430 super.setToolTipText(string); 1431 arrow.setToolTipText (string); 1432 text.setToolTipText (string); 1433} 1434 1435public void setVisible (boolean visible) { 1436 super.setVisible(visible); 1437 1441 if (isDisposed ()) return; 1442 if (!visible) popup.setVisible(false); 1443} 1444 1457public void setVisibleItemCount (int count) { 1458 checkWidget (); 1459 if (count < 0) return; 1460 visibleItemCount = count; 1461} 1462String stripMnemonic (String string) { 1463 int index = 0; 1464 int length = string.length (); 1465 do { 1466 while ((index < length) && (string.charAt (index) != '&')) index++; 1467 if (++index >= length) return string; 1468 if (string.charAt (index) != '&') { 1469 return string.substring(0, index-1) + string.substring(index, length); 1470 } 1471 index++; 1472 } while (index < length); 1473 return string; 1474} 1475void textEvent (Event event) { 1476 switch (event.type) { 1477 case SWT.FocusIn: { 1478 handleFocus (SWT.FocusIn); 1479 break; 1480 } 1481 case SWT.KeyDown: { 1482 Event keyEvent = new Event (); 1483 keyEvent.time = event.time; 1484 keyEvent.character = event.character; 1485 keyEvent.keyCode = event.keyCode; 1486 keyEvent.stateMask = event.stateMask; 1487 notifyListeners (SWT.KeyDown, keyEvent); 1488 if (isDisposed ()) break; 1489 event.doit = keyEvent.doit; 1490 if (!event.doit) break; 1491 1492 if (event.character == SWT.CR) { 1493 dropDown (false); 1494 Event selectionEvent = new Event (); 1495 selectionEvent.time = event.time; 1496 selectionEvent.stateMask = event.stateMask; 1497 notifyListeners (SWT.DefaultSelection, selectionEvent); 1498 if (isDisposed ()) break; 1499 } 1500 1501 if (event.keyCode == SWT.ARROW_UP || event.keyCode == SWT.ARROW_DOWN) { 1502 event.doit = false; 1503 if ((event.stateMask & SWT.ALT) != 0) { 1504 boolean dropped = isDropped (); 1505 text.selectAll (); 1506 if (!dropped) setFocus (); 1507 dropDown (!dropped); 1508 break; 1509 } 1510 1511 int oldIndex = getSelectionIndex (); 1512 if (event.keyCode == SWT.ARROW_UP) { 1513 select (Math.max (oldIndex - 1, 0)); 1514 } else { 1515 select (Math.min (oldIndex + 1, getItemCount () - 1)); 1516 } 1517 if (oldIndex != getSelectionIndex ()) { 1518 Event e = new Event(); 1519 e.time = event.time; 1520 e.stateMask = event.stateMask; 1521 notifyListeners (SWT.Selection, e); 1522 } 1523 if (isDisposed ()) break; 1524 } 1525 1526 break; 1529 } 1530 case SWT.KeyUp: { 1531 Event e = new Event (); 1532 e.time = event.time; 1533 e.character = event.character; 1534 e.keyCode = event.keyCode; 1535 e.stateMask = event.stateMask; 1536 notifyListeners (SWT.KeyUp, e); 1537 event.doit = e.doit; 1538 break; 1539 } 1540 case SWT.MenuDetect: { 1541 Event e = new Event (); 1542 e.time = event.time; 1543 notifyListeners (SWT.MenuDetect, e); 1544 break; 1545 } 1546 case SWT.Modify: { 1547 list.deselectAll (); 1548 Event e = new Event (); 1549 e.time = event.time; 1550 notifyListeners (SWT.Modify, e); 1551 break; 1552 } 1553 case SWT.MouseDown: { 1554 if (event.button != 1) return; 1555 if (text.getEditable ()) return; 1556 boolean dropped = isDropped (); 1557 text.selectAll (); 1558 if (!dropped) setFocus (); 1559 dropDown (!dropped); 1560 break; 1561 } 1562 case SWT.MouseUp: { 1563 if (event.button != 1) return; 1564 if (text.getEditable ()) return; 1565 text.selectAll (); 1566 break; 1567 } 1568 case SWT.Traverse: { 1569 switch (event.detail) { 1570 case SWT.TRAVERSE_RETURN: 1571 case SWT.TRAVERSE_ARROW_PREVIOUS: 1572 case SWT.TRAVERSE_ARROW_NEXT: 1573 event.doit = false; 1577 break; 1578 } 1579 1580 Event e = new Event (); 1581 e.time = event.time; 1582 e.detail = event.detail; 1583 e.doit = event.doit; 1584 e.character = event.character; 1585 e.keyCode = event.keyCode; 1586 notifyListeners (SWT.Traverse, e); 1587 event.doit = e.doit; 1588 event.detail = e.detail; 1589 break; 1590 } 1591 case SWT.Verify: { 1592 Event e = new Event (); 1593 e.text = event.text; 1594 e.start = event.start; 1595 e.end = event.end; 1596 e.character = event.character; 1597 e.keyCode = event.keyCode; 1598 e.stateMask = event.stateMask; 1599 notifyListeners (SWT.Verify, e); 1600 event.doit = e.doit; 1601 break; 1602 } 1603 } 1604} 1605} 1606 | Popular Tags |