KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > custom > CCombo


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
21  * The CCombo class represents a selectable user interface object
22  * that combines a text field and a list and issues notification
23  * when an item is selected from the list.
24  * <p>
25  * CCombo was written to work around certain limitations in the native
26  * combo box. Specifically, on win32, the height of a CCombo can be set;
27  * attempts to set the height of a Combo are ignored. CCombo can be used
28  * anywhere that having the increased flexibility is more important than
29  * getting native L&F, but the decision should not be taken lightly.
30  * There is no is no strict requirement that CCombo look or behave
31  * the same as the native combo box.
32  * </p>
33  * <p>
34  * Note that although this class is a subclass of <code>Composite</code>,
35  * it does not make sense to add children to it, or set a layout on it.
36  * </p>
37  * <dl>
38  * <dt><b>Styles:</b>
39  * <dd>BORDER, READ_ONLY, FLAT</dd>
40  * <dt><b>Events:</b>
41  * <dd>DefaultSelection, Modify, Selection, Verify</dd>
42  * </dl>
43  */

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 /**
57  * Constructs a new instance of this class given its parent
58  * and a style value describing its behavior and appearance.
59  * <p>
60  * The style value is either one of the style constants defined in
61  * class <code>SWT</code> which is applicable to instances of this
62  * class, or must be built by <em>bitwise OR</em>'ing together
63  * (that is, using the <code>int</code> "|" operator) two or more
64  * of those <code>SWT</code> style constants. The class description
65  * lists the style constants that are applicable to the class.
66  * Style bits are also inherited from superclasses.
67  * </p>
68  *
69  * @param parent a widget which will be the parent of the new instance (cannot be null)
70  * @param style the style of widget to construct
71  *
72  * @exception IllegalArgumentException <ul>
73  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
74  * </ul>
75  * @exception SWTException <ul>
76  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
77  * </ul>
78  *
79  * @see SWT#BORDER
80  * @see SWT#READ_ONLY
81  * @see SWT#FLAT
82  * @see Widget#getStyle()
83  */

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 /**
148  * Adds the argument to the end of the receiver's list.
149  *
150  * @param string the new item
151  *
152  * @exception IllegalArgumentException <ul>
153  * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
154  * </ul>
155  * @exception SWTException <ul>
156  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
157  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
158  * </ul>
159  *
160  * @see #add(String,int)
161  */

162 public void add (String JavaDoc string) {
163     checkWidget();
164     if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
165     list.add (string);
166 }
167 /**
168  * Adds the argument to the receiver's list at the given
169  * zero-relative index.
170  * <p>
171  * Note: To add an item at the end of the list, use the
172  * result of calling <code>getItemCount()</code> as the
173  * index or use <code>add(String)</code>.
174  * </p>
175  *
176  * @param string the new item
177  * @param index the index for the item
178  *
179  * @exception IllegalArgumentException <ul>
180  * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
181  * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list (inclusive)</li>
182  * </ul>
183  * @exception SWTException <ul>
184  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
185  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
186  * </ul>
187  *
188  * @see #add(String)
189  */

190 public void add (String JavaDoc string, int index) {
191     checkWidget();
192     if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
193     list.add (string, index);
194 }
195 /**
196  * Adds the listener to the collection of listeners who will
197  * be notified when the receiver's text is modified, by sending
198  * it one of the messages defined in the <code>ModifyListener</code>
199  * interface.
200  *
201  * @param listener the listener which should be notified
202  *
203  * @exception IllegalArgumentException <ul>
204  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
205  * </ul>
206  * @exception SWTException <ul>
207  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
208  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
209  * </ul>
210  *
211  * @see ModifyListener
212  * @see #removeModifyListener
213  */

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 /**
221  * Adds the listener to the collection of listeners who will
222  * be notified when the user changes the receiver's selection, by sending
223  * it one of the messages defined in the <code>SelectionListener</code>
224  * interface.
225  * <p>
226  * <code>widgetSelected</code> is called when the combo's list selection changes.
227  * <code>widgetDefaultSelected</code> is typically called when ENTER is pressed the combo's text area.
228  * </p>
229  *
230  * @param listener the listener which should be notified when the user changes the receiver's selection
231  *
232  * @exception IllegalArgumentException <ul>
233  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
234  * </ul>
235  * @exception SWTException <ul>
236  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
237  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
238  * </ul>
239  *
240  * @see SelectionListener
241  * @see #removeSelectionListener
242  * @see SelectionEvent
243  */

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 /**
252  * Adds the listener to the collection of listeners who will
253  * be notified when the receiver's text is verified, by sending
254  * it one of the messages defined in the <code>VerifyListener</code>
255  * interface.
256  *
257  * @param listener the listener which should be notified
258  *
259  * @exception IllegalArgumentException <ul>
260  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
261  * </ul>
262  * @exception SWTException <ul>
263  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
264  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
265  * </ul>
266  *
267  * @see VerifyListener
268  * @see #removeVerifyListener
269  *
270  * @since 3.3
271  */

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 /**
291  * Sets the selection in the receiver's text field to an empty
292  * selection starting just before the first character. If the
293  * text field is editable, this has the effect of placing the
294  * i-beam at the start of the text.
295  * <p>
296  * Note: To clear the selected items in the receiver's list,
297  * use <code>deselectAll()</code>.
298  * </p>
299  *
300  * @exception SWTException <ul>
301  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
302  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
303  * </ul>
304  *
305  * @see #deselectAll
306  */

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 JavaDoc[] items = list.getItems ();
341     GC gc = new GC (text);
342     int spacer = gc.stringExtent (" ").x; //$NON-NLS-1$
343
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 /**
360  * Copies the selected text.
361  * <p>
362  * The current selection is copied to the clipboard.
363  * </p>
364  *
365  * @exception SWTException <ul>
366  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
367  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
368  * </ul>
369  *
370  * @since 3.3
371  */

372 public void copy () {
373     checkWidget ();
374     text.copy ();
375 }
376 void createPopup(String JavaDoc[] items, int selectionIndex) {
377     // create shell and list
378
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 /**
398  * Cuts the selected text.
399  * <p>
400  * The current selection is first copied to the
401  * clipboard and then deleted from the widget.
402  * </p>
403  *
404  * @exception SWTException <ul>
405  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
406  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
407  * </ul>
408  *
409  * @since 3.3
410  */

411 public void cut () {
412     checkWidget ();
413     text.cut ();
414 }
415 /**
416  * Deselects the item at the given zero-relative index in the receiver's
417  * list. If the item at the index was already deselected, it remains
418  * deselected. Indices that are out of range are ignored.
419  *
420  * @param index the index of the item to deselect
421  *
422  * @exception SWTException <ul>
423  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
424  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
425  * </ul>
426  */

427 public void deselect (int index) {
428     checkWidget ();
429     list.deselect (index);
430 }
431 /**
432  * Deselects all selected items in the receiver's list.
433  * <p>
434  * Note: To clear the selection in the receiver's text field,
435  * use <code>clearSelection()</code>.
436  * </p>
437  *
438  * @exception SWTException <ul>
439  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
440  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
441  * </ul>
442  *
443  * @see #clearSelection
444  */

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 JavaDoc[] 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 /*
494  * Return the lowercase of the first non-'&' character following
495  * an '&' character in the given string. If there are no '&'
496  * characters in the given string, return '\0'.
497  */

498 char _findMnemonic (String JavaDoc 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 /*
511  * Return the Label immediately preceding the receiver in the z-order,
512  * or null if none.
513  */

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 /**
530  * Gets the editable state.
531  *
532  * @return whether or not the receiver is editable
533  *
534  * @exception SWTException <ul>
535  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
536  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
537  * </ul>
538  *
539  * @since 3.0
540  */

541 public boolean getEditable () {
542     checkWidget ();
543     return text.getEditable();
544 }
545 /**
546  * Returns the item at the given, zero-relative index in the
547  * receiver's list. Throws an exception if the index is out
548  * of range.
549  *
550  * @param index the index of the item to return
551  * @return the item at the given index
552  *
553  * @exception IllegalArgumentException <ul>
554  * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
555  * </ul>
556  * @exception SWTException <ul>
557  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
558  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
559  * </ul>
560  */

561 public String JavaDoc getItem (int index) {
562     checkWidget();
563     return list.getItem (index);
564 }
565 /**
566  * Returns the number of items contained in the receiver's list.
567  *
568  * @return the number of items
569  *
570  * @exception SWTException <ul>
571  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
572  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
573  * </ul>
574  */

575 public int getItemCount () {
576     checkWidget ();
577     return list.getItemCount ();
578 }
579 /**
580  * Returns the height of the area which would be used to
581  * display <em>one</em> of the items in the receiver's list.
582  *
583  * @return the height of one item
584  *
585  * @exception SWTException <ul>
586  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
587  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
588  * </ul>
589  */

590 public int getItemHeight () {
591     checkWidget ();
592     return list.getItemHeight ();
593 }
594 /**
595  * Returns an array of <code>String</code>s which are the items
596  * in the receiver's list.
597  * <p>
598  * Note: This is not the actual structure used by the receiver
599  * to maintain its list of items, so modifying the array will
600  * not affect the receiver.
601  * </p>
602  *
603  * @return the items in the receiver's list
604  *
605  * @exception SWTException <ul>
606  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
607  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
608  * </ul>
609  */

610 public String JavaDoc [] getItems () {
611     checkWidget ();
612     return list.getItems ();
613 }
614 public Menu getMenu() {
615     return text.getMenu();
616 }
617 /**
618  * Returns a <code>Point</code> whose x coordinate is the start
619  * of the selection in the receiver's text field, and whose y
620  * coordinate is the end of the selection. The returned values
621  * are zero-relative. An "empty" selection as indicated by
622  * the the x and y coordinates having the same value.
623  *
624  * @return a point representing the selection start and end
625  *
626  * @exception SWTException <ul>
627  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
628  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
629  * </ul>
630  */

631 public Point getSelection () {
632     checkWidget ();
633     return text.getSelection ();
634 }
635 /**
636  * Returns the zero-relative index of the item which is currently
637  * selected in the receiver's list, or -1 if no item is selected.
638  *
639  * @return the index of the selected item
640  *
641  * @exception SWTException <ul>
642  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
643  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
644  * </ul>
645  */

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 /**
657  * Returns a string containing a copy of the contents of the
658  * receiver's text field.
659  *
660  * @return the receiver's text
661  *
662  * @exception SWTException <ul>
663  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
664  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
665  * </ul>
666  */

667 public String JavaDoc getText () {
668     checkWidget ();
669     return text.getText ();
670 }
671 /**
672  * Returns the height of the receivers's text field.
673  *
674  * @return the text height
675  *
676  * @exception SWTException <ul>
677  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
678  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
679  * </ul>
680  */

681 public int getTextHeight () {
682     checkWidget ();
683     return text.getLineHeight ();
684 }
685 /**
686  * Returns the maximum number of characters that the receiver's
687  * text field is capable of holding. If this has not been changed
688  * by <code>setTextLimit()</code>, it will be the constant
689  * <code>Combo.LIMIT</code>.
690  *
691  * @return the text limit
692  *
693  * @exception SWTException <ul>
694  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
695  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
696  * </ul>
697  */

698 public int getTextLimit () {
699     checkWidget ();
700     return text.getTextLimit ();
701 }
702 /**
703  * Gets the number of items that are visible in the drop
704  * down portion of the receiver's list.
705  *
706  * @return the number of items that are visible
707  *
708  * @exception SWTException <ul>
709  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
710  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
711  * </ul>
712  *
713  * @since 3.0
714  */

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 /**
752  * Searches the receiver's list starting at the first item
753  * (index 0) until an item is found that is equal to the
754  * argument, and returns the index of that item. If no item
755  * is found, returns -1.
756  *
757  * @param string the search item
758  * @return the index of the item
759  *
760  * @exception IllegalArgumentException <ul>
761  * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
762  * </ul>
763  * @exception SWTException <ul>
764  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
765  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
766  * </ul>
767  */

768 public int indexOf (String JavaDoc string) {
769     checkWidget ();
770     if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
771     return list.indexOf (string);
772 }
773 /**
774  * Searches the receiver's list starting at the given,
775  * zero-relative index until an item is found that is equal
776  * to the argument, and returns the index of that item. If
777  * no item is found or the starting index is out of range,
778  * returns -1.
779  *
780  * @param string the search item
781  * @param start the zero-relative index at which to begin the search
782  * @return the index of the item
783  *
784  * @exception IllegalArgumentException <ul>
785  * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
786  * </ul>
787  * @exception SWTException <ul>
788  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
789  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
790  * </ul>
791  */

792 public int indexOf (String JavaDoc 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 JavaDoc 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 JavaDoc shortcut = null;
810             Label label = getAssociatedLabel ();
811             if (label != null) {
812                 String JavaDoc text = label.getText ();
813                 if (text != null) {
814                     char mnemonic = _findMnemonic (text);
815                     if (mnemonic != '\0') {
816                         shortcut = "Alt+"+mnemonic; //$NON-NLS-1$
817
}
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"); //$NON-NLS-1$ //$NON-NLS-2$
833
}
834         public void getKeyboardShortcut (AccessibleEvent e) {
835             e.result = "Alt+Down Arrow"; //$NON-NLS-1$
836
}
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"); //$NON-NLS-1$ //$NON-NLS-2$
896
}
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 JavaDoc[] 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                 // Escape key cancels popup list
984
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                 // Enter causes default selection
991
dropDown (false);
992                 Event e = new Event ();
993                 e.time = event.time;
994                 e.stateMask = event.stateMask;
995                 notifyListeners (SWT.DefaultSelection, e);
996             }
997             // At this point the widget may have been disposed.
998
// If so, do not continue.
999
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/**
1012 * Pastes text from clipboard.
1013 * <p>
1014 * The selected text is deleted from the widget
1015 * and new text inserted from the clipboard.
1016 * </p>
1017 *
1018 * @exception SWTException <ul>
1019 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1020 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1021 * </ul>
1022 *
1023 * @since 3.3
1024 */

1025public void paste () {
1026    checkWidget ();
1027    text.paste ();
1028}
1029void popupEvent(Event event) {
1030    switch (event.type) {
1031        case SWT.Paint:
1032            // draw black rectangle around list
1033
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            /*
1044             * Bug in GTK. When the arrow button is pressed the popup control receives a
1045             * deactivate event and then the arrow button receives a selection event. If
1046             * we hide the popup in the deactivate event, the selection event will show
1047             * it again. To prevent the popup from showing again, we will let the selection
1048             * event of the arrow button hide the popup.
1049             */

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/**
1072 * Removes the item from the receiver's list at the given
1073 * zero-relative index.
1074 *
1075 * @param index the index for the item
1076 *
1077 * @exception IllegalArgumentException <ul>
1078 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
1079 * </ul>
1080 * @exception SWTException <ul>
1081 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1082 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1083 * </ul>
1084 */

1085public void remove (int index) {
1086    checkWidget();
1087    list.remove (index);
1088}
1089/**
1090 * Removes the items from the receiver's list which are
1091 * between the given zero-relative start and end
1092 * indices (inclusive).
1093 *
1094 * @param start the start of the range
1095 * @param end the end of the range
1096 *
1097 * @exception IllegalArgumentException <ul>
1098 * <li>ERROR_INVALID_RANGE - if either the start or end are not between 0 and the number of elements in the list minus 1 (inclusive)</li>
1099 * </ul>
1100 * @exception SWTException <ul>
1101 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1102 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1103 * </ul>
1104 */

1105public void remove (int start, int end) {
1106    checkWidget();
1107    list.remove (start, end);
1108}
1109/**
1110 * Searches the receiver's list starting at the first item
1111 * until an item is found that is equal to the argument,
1112 * and removes that item from the list.
1113 *
1114 * @param string the item to remove
1115 *
1116 * @exception IllegalArgumentException <ul>
1117 * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
1118 * <li>ERROR_INVALID_ARGUMENT - if the string is not found in the list</li>
1119 * </ul>
1120 * @exception SWTException <ul>
1121 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1122 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1123 * </ul>
1124 */

1125public void remove (String JavaDoc string) {
1126    checkWidget();
1127    if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
1128    list.remove (string);
1129}
1130/**
1131 * Removes all of the items from the receiver's list and clear the
1132 * contents of receiver's text field.
1133 * <p>
1134 * @exception SWTException <ul>
1135 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1136 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1137 * </ul>
1138 */

1139public void removeAll () {
1140    checkWidget();
1141    text.setText (""); //$NON-NLS-1$
1142
list.removeAll ();
1143}
1144/**
1145 * Removes the listener from the collection of listeners who will
1146 * be notified when the receiver's text is modified.
1147 *
1148 * @param listener the listener which should no longer be notified
1149 *
1150 * @exception IllegalArgumentException <ul>
1151 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
1152 * </ul>
1153 * @exception SWTException <ul>
1154 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1155 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1156 * </ul>
1157 *
1158 * @see ModifyListener
1159 * @see #addModifyListener
1160 */

1161public void removeModifyListener (ModifyListener listener) {
1162    checkWidget();
1163    if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
1164    removeListener(SWT.Modify, listener);
1165}
1166/**
1167 * Removes the listener from the collection of listeners who will
1168 * be notified when the user changes the receiver's selection.
1169 *
1170 * @param listener the listener which should no longer be notified
1171 *
1172 * @exception IllegalArgumentException <ul>
1173 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
1174 * </ul>
1175 * @exception SWTException <ul>
1176 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1177 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1178 * </ul>
1179 *
1180 * @see SelectionListener
1181 * @see #addSelectionListener
1182 */

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/**
1190 * Removes the listener from the collection of listeners who will
1191 * be notified when the control is verified.
1192 *
1193 * @param listener the listener which should no longer be notified
1194 *
1195 * @exception IllegalArgumentException <ul>
1196 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
1197 * </ul>
1198 * @exception SWTException <ul>
1199 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1200 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1201 * </ul>
1202 *
1203 * @see VerifyListener
1204 * @see #addVerifyListener
1205 *
1206 * @since 3.3
1207 */

1208public void removeVerifyListener (VerifyListener listener) {
1209    checkWidget();
1210    if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
1211    removeListener(SWT.Verify, listener);
1212}
1213/**
1214 * Selects the item at the given zero-relative index in the receiver's
1215 * list. If the item at the index was already selected, it remains
1216 * selected. Indices that are out of range are ignored.
1217 *
1218 * @param index the index of the item to select
1219 *
1220 * @exception SWTException <ul>
1221 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1222 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1223 * </ul>
1224 */

1225public void select (int index) {
1226    checkWidget();
1227    if (index == -1) {
1228        list.deselectAll ();
1229        text.setText (""); //$NON-NLS-1$
1230
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/**
1249 * Sets the editable state.
1250 *
1251 * @param editable the new editable state
1252 *
1253 * @exception SWTException <ul>
1254 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1255 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1256 * </ul>
1257 *
1258 * @since 3.0
1259 */

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/**
1290 * Sets the text of the item in the receiver's list at the given
1291 * zero-relative index to the string argument. This is equivalent
1292 * to <code>remove</code>'ing the old item at the index, and then
1293 * <code>add</code>'ing the new item at that index.
1294 *
1295 * @param index the index for the item
1296 * @param string the new text for the item
1297 *
1298 * @exception IllegalArgumentException <ul>
1299 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
1300 * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
1301 * </ul>
1302 * @exception SWTException <ul>
1303 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1304 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1305 * </ul>
1306 */

1307public void setItem (int index, String JavaDoc string) {
1308    checkWidget();
1309    list.setItem (index, string);
1310}
1311/**
1312 * Sets the receiver's list to be the given array of items.
1313 *
1314 * @param items the array of items
1315 *
1316 * @exception IllegalArgumentException <ul>
1317 * <li>ERROR_NULL_ARGUMENT - if the items array is null</li>
1318 * <li>ERROR_INVALID_ARGUMENT - if an item in the items array is null</li>
1319 * </ul>
1320 * @exception SWTException <ul>
1321 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1322 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1323 * </ul>
1324 */

1325public void setItems (String JavaDoc [] items) {
1326    checkWidget ();
1327    list.setItems (items);
1328    if (!text.getEditable ()) text.setText (""); //$NON-NLS-1$
1329
}
1330/**
1331 * Sets the layout which is associated with the receiver to be
1332 * the argument which may be null.
1333 * <p>
1334 * Note: No Layout can be set on this Control because it already
1335 * manages the size and position of its children.
1336 * </p>
1337 *
1338 * @param layout the receiver's new layout or null
1339 *
1340 * @exception SWTException <ul>
1341 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1342 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1343 * </ul>
1344 */

1345public void setLayout (Layout layout) {
1346    checkWidget ();
1347    return;
1348}
1349public void setMenu(Menu menu) {
1350    text.setMenu(menu);
1351}
1352/**
1353 * Sets the selection in the receiver's text field to the
1354 * range specified by the argument whose x coordinate is the
1355 * start of the selection and whose y coordinate is the end
1356 * of the selection.
1357 *
1358 * @param selection a point representing the new selection start and end
1359 *
1360 * @exception IllegalArgumentException <ul>
1361 * <li>ERROR_NULL_ARGUMENT - if the point is null</li>
1362 * </ul>
1363 * @exception SWTException <ul>
1364 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1365 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1366 * </ul>
1367 */

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/**
1375 * Sets the contents of the receiver's text field to the
1376 * given string.
1377 * <p>
1378 * Note: The text field in a <code>Combo</code> is typically
1379 * only capable of displaying a single line of text. Thus,
1380 * setting the text to a string containing line breaks or
1381 * other special characters will probably cause it to
1382 * display incorrectly.
1383 * </p>
1384 *
1385 * @param string the new text
1386 *
1387 * @exception IllegalArgumentException <ul>
1388 * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
1389 * </ul>
1390 * @exception SWTException <ul>
1391 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1392 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1393 * </ul>
1394 */

1395public void setText (String JavaDoc 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/**
1410 * Sets the maximum number of characters that the receiver's
1411 * text field is capable of holding to be the argument.
1412 *
1413 * @param limit new text limit
1414 *
1415 * @exception IllegalArgumentException <ul>
1416 * <li>ERROR_CANNOT_BE_ZERO - if the limit is zero</li>
1417 * </ul>
1418 * @exception SWTException <ul>
1419 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1420 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1421 * </ul>
1422 */

1423public void setTextLimit (int limit) {
1424    checkWidget();
1425    text.setTextLimit (limit);
1426}
1427
1428public void setToolTipText (String JavaDoc 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    /*
1438     * At this point the widget may have been disposed in a FocusOut event.
1439     * If so then do not continue.
1440     */

1441    if (isDisposed ()) return;
1442    if (!visible) popup.setVisible(false);
1443}
1444/**
1445 * Sets the number of items that are visible in the drop
1446 * down portion of the receiver's list.
1447 *
1448 * @param count the new number of items to be visible
1449 *
1450 * @exception SWTException <ul>
1451 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1452 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1453 * </ul>
1454 *
1455 * @since 3.0
1456 */

1457public void setVisibleItemCount (int count) {
1458    checkWidget ();
1459    if (count < 0) return;
1460    visibleItemCount = count;
1461}
1462String JavaDoc stripMnemonic (String JavaDoc 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            // Further work : Need to add support for incremental search in
1527
// pop up list as characters typed in text widget
1528
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                    // The enter causes default selection and
1574
// the arrow keys are used to manipulate the list contents so
1575
// do not use them for traversal.
1576
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