KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > rcp > custom > ColorCombo


1 /**
2  * <p> Project: com.nightlabs.base </p>
3  * <p> Copyright: Copyright (c) 2005 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 29.07.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.rcp.custom;
9
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.SWTException;
12 import org.eclipse.swt.accessibility.ACC;
13 import org.eclipse.swt.accessibility.AccessibleAdapter;
14 import org.eclipse.swt.accessibility.AccessibleControlAdapter;
15 import org.eclipse.swt.accessibility.AccessibleControlEvent;
16 import org.eclipse.swt.accessibility.AccessibleEvent;
17 import org.eclipse.swt.accessibility.AccessibleTextAdapter;
18 import org.eclipse.swt.accessibility.AccessibleTextEvent;
19 import org.eclipse.swt.events.ModifyListener;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.events.SelectionListener;
22 import org.eclipse.swt.graphics.Color;
23 import org.eclipse.swt.graphics.Font;
24 import org.eclipse.swt.graphics.GC;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.graphics.Point;
27 import org.eclipse.swt.graphics.Rectangle;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Display;
32 import org.eclipse.swt.widgets.Event;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Layout;
35 import org.eclipse.swt.widgets.Listener;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.swt.widgets.Table;
38 import org.eclipse.swt.widgets.TableColumn;
39 import org.eclipse.swt.widgets.TableItem;
40 import org.eclipse.swt.widgets.Text;
41 import org.eclipse.swt.widgets.TypedListener;
42 import org.eclipse.swt.widgets.Widget;
43
44 /**
45  * A Custom Implementation of a ComboBox which is able not only to show Strings but also
46  * Images (@link org.eclipse.swt.graphics.Image)
47  *
48  * This Composite and its implementation (greate parts are adopted) is created more less like a CCombo
49  * (@link org.eclipse.swt.custom.CCombo) but
50  * internal it uses a Table (@link org.eclipse.swt.widgets.Table) instead of a
51  * List (@link org.eclipse.swt.widgets.List) for displaying the entries
52  *
53  * @author Daniel.Mazurek [AT] NightLabs [DOT] com
54  *
55  */

56 public class ColorCombo
57 extends Composite
58 {
59     Text text;
60     int visibleItemCount = 5;
61     Shell popup;
62     Button arrow;
63     boolean hasFocus;
64     Listener listener, filter;
65     Color foreground, background;
66     Font font;
67     Table table;
68
69     
70 /**
71  * Constructs a new instance of this class given its parent
72  * and a style value describing its behavior and appearance.
73  * <p>
74  * The style value is either one of the style constants defined in
75  * class <code>SWT</code> which is applicable to instances of this
76  * class, or must be built by <em>bitwise OR</em>'ing together
77  * (that is, using the <code>int</code> "|" operator) two or more
78  * of those <code>SWT</code> style constants. The class description
79  * lists the style constants that are applicable to the class.
80  * Style bits are also inherited from superclasses.
81  * </p>
82  *
83  * @param parent a widget which will be the parent of the new instance (cannot be null)
84  * @param style the style of widget to construct
85  *
86  * @exception IllegalArgumentException <ul>
87  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
88  * </ul>
89  * @exception SWTException <ul>
90  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
91  * </ul>
92  *
93  * @see SWT#BORDER
94  * @see SWT#READ_ONLY
95  * @see SWT#FLAT
96  * @see Widget#getStyle()
97  */

98 public ColorCombo (Composite parent, int style) {
99     super (parent, style = checkStyle (style));
100     
101     int textStyle = SWT.SINGLE;
102     if ((style & SWT.READ_ONLY) != 0) textStyle |= SWT.READ_ONLY;
103     if ((style & SWT.FLAT) != 0) textStyle |= SWT.FLAT;
104     text = new Text (this, textStyle);
105     int arrowStyle = SWT.ARROW | SWT.DOWN;
106     if ((style & SWT.FLAT) != 0) arrowStyle |= SWT.FLAT;
107     arrow = new Button (this, arrowStyle);
108
109     listener = new Listener () {
110         public void handleEvent (Event event) {
111             if (popup == event.widget) {
112                 popupEvent (event);
113                 return;
114             }
115             if (text == event.widget) {
116                 textEvent (event);
117                 return;
118             }
119             if (table == event.widget) {
120                 listEvent (event);
121                 return;
122             }
123             if (arrow == event.widget) {
124                 arrowEvent (event);
125                 return;
126             }
127             if (ColorCombo.this == event.widget) {
128                 comboEvent (event);
129                 return;
130             }
131             if (getShell () == event.widget) {
132                 handleFocus (SWT.FocusOut);
133             }
134         }
135     };
136     filter = new Listener() {
137         public void handleEvent(Event event) {
138             Shell shell = ((Control)event.widget).getShell ();
139             if (shell == ColorCombo.this.getShell ()) {
140                 handleFocus (SWT.FocusOut);
141             }
142         }
143     };
144     
145     int [] comboEvents = {SWT.Dispose, SWT.Move, SWT.Resize};
146     for (int i=0; i<comboEvents.length; i++) this.addListener (comboEvents [i], listener);
147     
148     int [] textEvents = {SWT.KeyDown, SWT.KeyUp, SWT.Modify, SWT.MouseDown, SWT.MouseUp, SWT.Traverse, SWT.FocusIn};
149     for (int i=0; i<textEvents.length; i++) text.addListener (textEvents [i], listener);
150     
151     int [] arrowEvents = {SWT.Selection, SWT.FocusIn};
152     for (int i=0; i<arrowEvents.length; i++) arrow.addListener (arrowEvents [i], listener);
153     
154     createPopup(null, -1);
155     initAccessible();
156 }
157
158 static int checkStyle (int style) {
159     int mask = SWT.BORDER | SWT.READ_ONLY | SWT.FLAT | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT;
160     return style & mask;
161 }
162
163 /**
164  * Adds the argument to the end of the receiver's table.
165  *
166  * @param string the new item
167  *
168  * @exception IllegalArgumentException <ul>
169  * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
170  * </ul>
171  * @exception SWTException <ul>
172  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
173  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
174  * </ul>
175  *
176  * @see #add(String,int)
177  */

178 public void add (Image image, String JavaDoc text) {
179     checkWidget();
180     if (text == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
181     TableItem item = new TableItem(table, getStyle());
182     item.setImage(image);
183     item.setText(text);
184 }
185
186 /**
187  * Adds the argument to the receiver's table at the given
188  * zero-relative index.
189  * <p>
190  * Note: To add an item at the end of the list, use the
191  * result of calling <code>getItemCount()</code> as the
192  * index or use <code>add(String)</code>.
193  * </p>
194  *
195  * @param string the new item name
196  * @param image the new item image
197  * @param index the index for the item
198  *
199  * @exception IllegalArgumentException <ul>
200  * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
201  * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list (inclusive)</li>
202  * </ul>
203  * @exception SWTException <ul>
204  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
205  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
206  * </ul>
207  *
208  * @see #add(String)
209  */

210 public void add (Image image, String JavaDoc text, int index) {
211     checkWidget();
212     if (text == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
213     TableItem item = new TableItem(table, getStyle(), index);
214     item.setImage(image);
215     item.setText(text);
216 }
217
218 /**
219  * Adds the listener to the collection of listeners who will
220  * be notified when the receiver's text is modified, by sending
221  * it one of the messages defined in the <code>ModifyListener</code>
222  * interface.
223  *
224  * @param listener the listener which should be notified
225  *
226  * @exception IllegalArgumentException <ul>
227  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
228  * </ul>
229  * @exception SWTException <ul>
230  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
231  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
232  * </ul>
233  *
234  * @see ModifyListener
235  * @see #removeModifyListener
236  */

237 public void addModifyListener (ModifyListener listener) {
238     checkWidget();
239     if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
240     TypedListener typedListener = new TypedListener (listener);
241     addListener (SWT.Modify, typedListener);
242 }
243 /**
244  * Adds the listener to the collection of listeners who will
245  * be notified when the receiver's selection changes, by sending
246  * it one of the messages defined in the <code>SelectionListener</code>
247  * interface.
248  * <p>
249  * <code>widgetSelected</code> is called when the combo's list selection changes.
250  * <code>widgetDefaultSelected</code> is typically called when ENTER is pressed the combo's text area.
251  * </p>
252  *
253  * @param listener the listener which should be notified
254  *
255  * @exception IllegalArgumentException <ul>
256  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
257  * </ul>
258  * @exception SWTException <ul>
259  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
260  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
261  * </ul>
262  *
263  * @see SelectionListener
264  * @see #removeSelectionListener
265  * @see SelectionEvent
266  */

267 public void addSelectionListener(SelectionListener listener) {
268     checkWidget();
269     if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
270     TypedListener typedListener = new TypedListener (listener);
271     addListener (SWT.Selection,typedListener);
272     addListener (SWT.DefaultSelection,typedListener);
273 }
274 void arrowEvent (Event event) {
275     switch (event.type) {
276         case SWT.FocusIn: {
277             handleFocus (SWT.FocusIn);
278             break;
279         }
280         case SWT.Selection: {
281             dropDown (!isDropped ());
282             break;
283         }
284     }
285 }
286 /**
287  * Sets the selection in the receiver's text field to an empty
288  * selection starting just before the first character. If the
289  * text field is editable, this has the effect of placing the
290  * i-beam at the start of the text.
291  * <p>
292  * Note: To clear the selected items in the receiver's list,
293  * use <code>deselectAll()</code>.
294  * </p>
295  *
296  * @exception SWTException <ul>
297  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
298  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
299  * </ul>
300  *
301  * @see #deselectAll
302  */

303 public void clearSelection () {
304     checkWidget ();
305     text.clearSelection ();
306     table.deselectAll ();
307 }
308
309 void comboEvent (Event event) {
310     switch (event.type) {
311         case SWT.Dispose:
312             if (popup != null && !popup.isDisposed ()) {
313                 table.removeListener (SWT.Dispose, listener);
314                 popup.dispose ();
315             }
316             Shell shell = getShell ();
317             shell.removeListener (SWT.Deactivate, listener);
318             Display display = getDisplay ();
319             display.removeFilter (SWT.FocusIn, filter);
320             popup = null;
321             text = null;
322             table = null;
323             arrow = null;
324             break;
325         case SWT.Move:
326             dropDown (false);
327             break;
328         case SWT.Resize:
329             internalLayout (false);
330             break;
331     }
332 }
333
334 //public Point computeSize (int wHint, int hHint, boolean changed) {
335
// checkWidget ();
336
// int width = 0, height = 0;
337
// TableItem[] items = list.getItems ();
338
// int textWidth = 0;
339
// GC gc = new GC (text);
340
// int spacer = gc.stringExtent (" ").x; //$NON-NLS-1$
341
// for (int i = 0; i < items.length; i++) {
342
// textWidth = Math.max (gc.stringExtent (items[i]).x, textWidth);
343
// }
344
// gc.dispose();
345
// Point textSize = text.computeSize (SWT.DEFAULT, SWT.DEFAULT, changed);
346
// Point arrowSize = arrow.computeSize (SWT.DEFAULT, SWT.DEFAULT, changed);
347
// Point listSize = list.computeSize (wHint, SWT.DEFAULT, changed);
348
// int borderWidth = getBorderWidth ();
349
//
350
// height = Math.max (hHint, Math.max (textSize.y, arrowSize.y) + 2*borderWidth);
351
// width = Math.max (wHint, Math.max (textWidth + 2*spacer + arrowSize.x + 2*borderWidth, listSize.x));
352
// return new Point (width, height);
353
//}
354
public Point computeSize (int wHint, int hHint, boolean changed)
355 {
356     checkWidget ();
357     int width = 0, height = 0;
358 // TableItem[] items = list.getItems();
359
int textWidth = 0;
360     GC gc = new GC (text);
361     int spacer = gc.stringExtent (" ").x; //$NON-NLS-1$
362
// for (int i = 0; i < items.length; i++) {
363
// TableItem item = items[i];
364
// item.getText();
365
// textWidth = Math.max (gc.stringExtent( (items[i]).x, textWidth));
366
// }
367
gc.dispose();
368     Point textSize = text.computeSize (SWT.DEFAULT, SWT.DEFAULT, changed);
369     Point arrowSize = arrow.computeSize (SWT.DEFAULT, SWT.DEFAULT, changed);
370     Point listSize = table.computeSize (wHint, SWT.DEFAULT, changed);
371     int borderWidth = getBorderWidth ();
372     
373     height = Math.max (hHint, Math.max (textSize.y, arrowSize.y) + 2*borderWidth);
374     width = Math.max (wHint, Math.max (textWidth + 2*spacer + arrowSize.x + 2*borderWidth, listSize.x));
375     return new Point (width, height);
376 }
377
378 void createPopup(TableItem[] items, int selectionIndex) {
379         // create shell and list
380
popup = new Shell (getShell (), SWT.NO_TRIM | SWT.ON_TOP);
381         int style = getStyle ();
382         int listStyle = SWT.SINGLE | SWT.V_SCROLL;
383         if ((style & SWT.FLAT) != 0) listStyle |= SWT.FLAT;
384         if ((style & SWT.RIGHT_TO_LEFT) != 0) listStyle |= SWT.RIGHT_TO_LEFT;
385         if ((style & SWT.LEFT_TO_RIGHT) != 0) listStyle |= SWT.LEFT_TO_RIGHT;
386         table = new Table (popup, listStyle);
387         if (font != null) table.setFont (font);
388         if (foreground != null) table.setForeground (foreground);
389         if (background != null) table.setBackground (background);
390         
391         int [] popupEvents = {SWT.Close, SWT.Paint, SWT.Deactivate};
392         for (int i=0; i<popupEvents.length; i++) popup.addListener (popupEvents [i], listener);
393         int [] listEvents = {SWT.MouseUp, SWT.Selection, SWT.Traverse, SWT.KeyDown, SWT.KeyUp, SWT.FocusIn, SWT.Dispose};
394         for (int i=0; i<listEvents.length; i++) table.addListener (listEvents [i], listener);
395         
396 // if (items != null) list.setItems (items);
397
if (selectionIndex != -1) table.setSelection (selectionIndex);
398 }
399 /**
400  * Deselects the item at the given zero-relative index in the receiver's
401  * table. If the item at the index was already deselected, it remains
402  * deselected. Indices that are out of range are ignored.
403  *
404  * @param index the index of the item to deselect
405  *
406  * @exception SWTException <ul>
407  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
408  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
409  * </ul>
410  */

411 public void deselect (int index) {
412     checkWidget ();
413     table.deselect (index);
414 }
415 /**
416  * Deselects all selected items in the receiver's table.
417  * <p>
418  * Note: To clear the selection in the receiver's text field,
419  * use <code>clearSelection()</code>.
420  * </p>
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  * @see #clearSelection
428  */

429 public void deselectAll () {
430     checkWidget ();
431     table.deselectAll ();
432 }
433
434 void dropDown (boolean drop) {
435     if (drop == isDropped ()) return;
436     if (!drop) {
437         popup.setVisible (false);
438         if (!isDisposed ()&& arrow.isFocusControl()) {
439             text.setFocus();
440         }
441         return;
442     }
443
444     if (getShell() != popup.getParent ()) {
445         TableItem[] items = table.getItems ();
446         int selectionIndex = table.getSelectionIndex ();
447         table.removeListener (SWT.Dispose, listener);
448         popup.dispose();
449         popup = null;
450         table = null;
451         createPopup (items, selectionIndex);
452     }
453     
454     Point size = getSize ();
455     int itemCount = table.getItemCount ();
456     itemCount = (itemCount == 0) ? visibleItemCount : Math.min(visibleItemCount, itemCount);
457     int itemHeight = table.getItemHeight () * itemCount;
458     Point listSize = table.computeSize (SWT.DEFAULT, itemHeight, false);
459     table.setBounds (1, 1, Math.max (size.x - 2, listSize.x), listSize.y);
460     
461     int index = table.getSelectionIndex ();
462     if (index != -1) table.setTopIndex (index);
463     Display display = getDisplay ();
464     Rectangle listRect = table.getBounds ();
465     Rectangle parentRect = display.map (getParent (), null, getBounds ());
466     Point comboSize = getSize ();
467     Rectangle displayRect = getMonitor ().getClientArea ();
468     int width = Math.max (comboSize.x, listRect.width + 2);
469     int height = listRect.height + 2;
470     int x = parentRect.x;
471     int y = parentRect.y + comboSize.y;
472     if (y + height > displayRect.y + displayRect.height) y = parentRect.y - height;
473     popup.setBounds (x, y, width, height);
474     popup.setVisible (true);
475     table.setFocus ();
476 }
477
478 /*
479  * Return the Label immediately preceding the receiver in the z-order,
480  * or null if none.
481  */

482 Label getAssociatedLabel () {
483     Control[] siblings = getParent ().getChildren ();
484     for (int i = 0; i < siblings.length; i++) {
485         if (siblings [i] == ColorCombo.this) {
486             if (i > 0 && siblings [i-1] instanceof Label) {
487                 return (Label) siblings [i-1];
488             }
489         }
490     }
491     return null;
492 }
493
494 public Control [] getChildren () {
495     checkWidget();
496     return new Control [0];
497 }
498
499 /**
500  * Gets the editable state.
501  *
502  * @return whether or not the reciever is editable
503  *
504  * @exception SWTException <ul>
505  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
506  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
507  * </ul>
508  *
509  * @since 3.0
510  */

511 public boolean getEditable () {
512     checkWidget ();
513     return text.getEditable();
514 }
515
516 /**
517  * Returns the item at the given, zero-relative index in the
518  * receiver's table. Throws an exception if the index is out
519  * of range.
520  *
521  * @param index the index of the item to return
522  * @return the item at the given index
523  *
524  * @exception IllegalArgumentException <ul>
525  * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
526  * </ul>
527  * @exception SWTException <ul>
528  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
529  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
530  * </ul>
531  */

532 public TableItem getItem (int index) {
533     checkWidget();
534     return table.getItem (index);
535 }
536
537 /**
538  * Returns the number of items contained in the receiver's table.
539  *
540  * @return the number of items
541  *
542  * @exception SWTException <ul>
543  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
544  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
545  * </ul>
546  */

547 public int getItemCount () {
548     checkWidget ();
549     return table.getItemCount ();
550 }
551
552 /**
553  * Returns the height of the area which would be used to
554  * display <em>one</em> of the items in the receiver's table.
555  *
556  * @return the height of one item
557  *
558  * @exception SWTException <ul>
559  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
560  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
561  * </ul>
562  */

563 public int getItemHeight () {
564     checkWidget ();
565     return table.getItemHeight ();
566 }
567
568 /**
569  * Returns an array of <code>TableItem</code>s which are the items
570  * in the receiver's table.
571  * <p>
572  * Note: This is not the actual structure used by the receiver
573  * to maintain its list of items, so modifying the array will
574  * not affect the receiver.
575  * </p>
576  *
577  * @return the items in the receiver's table
578  *
579  * @exception SWTException <ul>
580  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
581  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
582  * </ul>
583  */

584 public TableItem [] getItems () {
585     checkWidget ();
586     return table.getItems ();
587 }
588
589 char getMnemonic (String JavaDoc string) {
590     int index = 0;
591     int length = string.length ();
592     do {
593         while ((index < length) && (string.charAt (index) != '&')) index++;
594         if (++index >= length) return '\0';
595         if (string.charAt (index) != '&') return string.charAt (index);
596         index++;
597     } while (index < length);
598     return '\0';
599 }
600
601 /**
602  * Returns a <code>Point</code> whose x coordinate is the start
603  * of the selection in the receiver's text field, and whose y
604  * coordinate is the end of the selection. The returned values
605  * are zero-relative. An "empty" selection as indicated by
606  * the the x and y coordinates having the same value.
607  *
608  * @return a point representing the selection start and end
609  *
610  * @exception SWTException <ul>
611  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
612  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
613  * </ul>
614  */

615 public Point getSelection () {
616     checkWidget ();
617     return text.getSelection ();
618 }
619
620 /**
621  * Returns the zero-relative index of the item which is currently
622  * selected in the receiver's table, or -1 if no item is selected.
623  *
624  * @return the index of the selected item
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 int getSelectionIndex () {
632     checkWidget ();
633     return table.getSelectionIndex ();
634 }
635
636 public int getStyle () {
637     int style = super.getStyle ();
638     style &= ~SWT.READ_ONLY;
639     if (!text.getEditable()) style |= SWT.READ_ONLY;
640     return style;
641 }
642
643 /**
644  * Returns a string containing a copy of the contents of the
645  * receiver's text field.
646  *
647  * @return the receiver's text
648  *
649  * @exception SWTException <ul>
650  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
651  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
652  * </ul>
653  */

654 public String JavaDoc getText () {
655     checkWidget ();
656     return text.getText ();
657 }
658
659 /**
660  * Returns the height of the receivers's text field.
661  *
662  * @return the text height
663  *
664  * @exception SWTException <ul>
665  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
666  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
667  * </ul>
668  */

669 public int getTextHeight () {
670     checkWidget ();
671     return text.getLineHeight ();
672 }
673
674 /**
675  * Returns the maximum number of characters that the receiver's
676  * text field is capable of holding. If this has not been changed
677  * by <code>setTextLimit()</code>, it will be the constant
678  * <code>Combo.LIMIT</code>.
679  *
680  * @return the text limit
681  *
682  * @exception SWTException <ul>
683  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
684  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
685  * </ul>
686  */

687 public int getTextLimit () {
688     checkWidget ();
689     return text.getTextLimit ();
690 }
691
692 /**
693  * Gets the number of items that are visible in the drop
694  * down portion of the receiver's list.
695  *
696  * @return the number of items that are visible
697  *
698  * @exception SWTException <ul>
699  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
700  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
701  * </ul>
702  *
703  * @since 3.0
704  */

705 public int getVisibleItemCount () {
706     checkWidget ();
707     return visibleItemCount;
708 }
709
710 void handleFocus (int type) {
711     if (isDisposed ()) return;
712     switch (type) {
713         case SWT.FocusIn: {
714             if (hasFocus) return;
715             if (getEditable ()) text.selectAll ();
716             hasFocus = true;
717             Shell shell = getShell ();
718             shell.removeListener (SWT.Deactivate, listener);
719             shell.addListener (SWT.Deactivate, listener);
720             Display display = getDisplay ();
721             display.removeFilter (SWT.FocusIn, filter);
722             display.addFilter (SWT.FocusIn, filter);
723             Event e = new Event ();
724             notifyListeners (SWT.FocusIn, e);
725             break;
726         }
727         case SWT.FocusOut: {
728             if (!hasFocus) return;
729             Control focusControl = getDisplay ().getFocusControl ();
730             if (focusControl == arrow || focusControl == table || focusControl == text) return;
731             hasFocus = false;
732             Shell shell = getShell ();
733             shell.removeListener(SWT.Deactivate, listener);
734             Display display = getDisplay ();
735             display.removeFilter (SWT.FocusIn, filter);
736             Event e = new Event ();
737             notifyListeners (SWT.FocusOut, e);
738             break;
739         }
740     }
741 }
742
743 /**
744  * Searches the receiver's table starting at the first item
745  * (index 0) until an item is found that is equal to the
746  * argument, and returns the index of that item. If no item
747  * is found, returns -1.
748  *
749  * @param item the search item
750  * @return the index of the item
751  *
752  * @exception IllegalArgumentException <ul>
753  * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
754  * </ul>
755  * @exception SWTException <ul>
756  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
757  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
758  * </ul>
759  */

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

785 public int indexOf (TableColumn column) {
786     checkWidget ();
787     if (column == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
788     return table.indexOf (column);
789 }
790
791 void initAccessible() {
792     AccessibleAdapter accessibleAdapter = new AccessibleAdapter () {
793         public void getName (AccessibleEvent e) {
794             String JavaDoc name = null;
795             Label label = getAssociatedLabel ();
796             if (label != null) {
797                 name = stripMnemonic (label.getText());
798             }
799             e.result = name;
800         }
801         public void getKeyboardShortcut(AccessibleEvent e) {
802             String JavaDoc shortcut = null;
803             Label label = getAssociatedLabel ();
804             if (label != null) {
805                 String JavaDoc text = label.getText ();
806                 if (text != null) {
807                     char mnemonic = getMnemonic (text);
808                     if (mnemonic != '\0') {
809                         shortcut = "Alt+"+mnemonic; //$NON-NLS-1$
810
}
811                 }
812             }
813             e.result = shortcut;
814         }
815         public void getHelp (AccessibleEvent e) {
816             e.result = getToolTipText ();
817         }
818     };
819     getAccessible ().addAccessibleListener (accessibleAdapter);
820     text.getAccessible ().addAccessibleListener (accessibleAdapter);
821     table.getAccessible ().addAccessibleListener (accessibleAdapter);
822     
823     arrow.getAccessible ().addAccessibleListener (new AccessibleAdapter() {
824         public void getName (AccessibleEvent e) {
825             e.result = isDropped () ? SWT.getMessage ("SWT_Close") : SWT.getMessage ("SWT_Open"); //$NON-NLS-1$ //$NON-NLS-2$
826
}
827         public void getKeyboardShortcut (AccessibleEvent e) {
828             e.result = "Alt+Down Arrow"; //$NON-NLS-1$
829
}
830         public void getHelp (AccessibleEvent e) {
831             e.result = getToolTipText ();
832         }
833     });
834
835     getAccessible().addAccessibleTextListener (new AccessibleTextAdapter() {
836         public void getCaretOffset (AccessibleTextEvent e) {
837             e.offset = text.getCaretPosition ();
838         }
839     });
840     
841     getAccessible().addAccessibleControlListener (new AccessibleControlAdapter() {
842         public void getChildAtPoint (AccessibleControlEvent e) {
843             Point testPoint = toControl (e.x, e.y);
844             if (getBounds ().contains (testPoint)) {
845                 e.childID = ACC.CHILDID_SELF;
846             }
847         }
848         
849         public void getLocation (AccessibleControlEvent e) {
850             Rectangle location = getBounds ();
851             Point pt = toDisplay (location.x, location.y);
852             e.x = pt.x;
853             e.y = pt.y;
854             e.width = location.width;
855             e.height = location.height;
856         }
857         
858         public void getChildCount (AccessibleControlEvent e) {
859             e.detail = 0;
860         }
861         
862         public void getRole (AccessibleControlEvent e) {
863             e.detail = ACC.ROLE_COMBOBOX;
864         }
865         
866         public void getState (AccessibleControlEvent e) {
867             e.detail = ACC.STATE_NORMAL;
868         }
869
870         public void getValue (AccessibleControlEvent e) {
871             e.result = getText ();
872         }
873     });
874
875     text.getAccessible ().addAccessibleControlListener (new AccessibleControlAdapter () {
876         public void getRole (AccessibleControlEvent e) {
877             e.detail = text.getEditable () ? ACC.ROLE_TEXT : ACC.ROLE_LABEL;
878         }
879     });
880
881     arrow.getAccessible ().addAccessibleControlListener (new AccessibleControlAdapter() {
882         public void getDefaultAction (AccessibleControlEvent e) {
883             e.result = isDropped () ? SWT.getMessage ("SWT_Close") : SWT.getMessage ("SWT_Open"); //$NON-NLS-1$ //$NON-NLS-2$
884
}
885     });
886 }
887
888 boolean isDropped () {
889     return popup.getVisible ();
890 }
891
892 public boolean isFocusControl () {
893     checkWidget();
894     if (text.isFocusControl () || arrow.isFocusControl () || table.isFocusControl () || popup.isFocusControl ()) {
895         return true;
896     }
897     return super.isFocusControl ();
898 }
899
900 void internalLayout (boolean changed) {
901     if (isDropped ()) dropDown (false);
902     Rectangle rect = getClientArea ();
903     int width = rect.width;
904     int height = rect.height;
905     Point arrowSize = arrow.computeSize (SWT.DEFAULT, height, changed);
906     text.setBounds (0, 0, width - arrowSize.x, height);
907     arrow.setBounds (width - arrowSize.x, 0, arrowSize.x, arrowSize.y);
908 }
909
910 void listEvent (Event event) {
911     switch (event.type) {
912         case SWT.Dispose:
913             if (getShell () != popup.getParent ()) {
914                 TableItem[] items = table.getItems ();
915                 int selectionIndex = table.getSelectionIndex ();
916                 popup = null;
917                 table = null;
918                 createPopup (items, selectionIndex);
919             }
920             break;
921         case SWT.FocusIn: {
922             handleFocus (SWT.FocusIn);
923             break;
924         }
925         case SWT.MouseUp: {
926             if (event.button != 1) return;
927             dropDown (false);
928             break;
929         }
930         case SWT.Selection: {
931             int index = table.getSelectionIndex ();
932             if (index == -1) return;
933             text.setText (table.getItem (index).getText());
934             text.selectAll ();
935             table.setSelection (index);
936             Event e = new Event ();
937             e.time = event.time;
938             e.stateMask = event.stateMask;
939             e.doit = event.doit;
940             notifyListeners (SWT.Selection, e);
941             event.doit = e.doit;
942             break;
943         }
944         case SWT.Traverse: {
945             switch (event.detail) {
946                 case SWT.TRAVERSE_RETURN:
947                 case SWT.TRAVERSE_ESCAPE:
948                 case SWT.TRAVERSE_ARROW_PREVIOUS:
949                 case SWT.TRAVERSE_ARROW_NEXT:
950                     event.doit = false;
951                     break;
952             }
953             Event e = new Event ();
954             e.time = event.time;
955             e.detail = event.detail;
956             e.doit = event.doit;
957             e.character = event.character;
958             e.keyCode = event.keyCode;
959             notifyListeners (SWT.Traverse, e);
960             event.doit = e.doit;
961             event.detail = e.detail;
962             break;
963         }
964         case SWT.KeyUp: {
965             Event e = new Event ();
966             e.time = event.time;
967             e.character = event.character;
968             e.keyCode = event.keyCode;
969             e.stateMask = event.stateMask;
970             notifyListeners (SWT.KeyUp, e);
971             break;
972         }
973         case SWT.KeyDown: {
974             if (event.character == SWT.ESC) {
975                 // Escape key cancels popup list
976
dropDown (false);
977             }
978             if ((event.stateMask & SWT.ALT) != 0 && (event.keyCode == SWT.ARROW_UP || event.keyCode == SWT.ARROW_DOWN)) {
979                 dropDown (false);
980             }
981             if (event.character == SWT.CR) {
982                 // Enter causes default selection
983
dropDown (false);
984                 Event e = new Event ();
985                 e.time = event.time;
986                 e.stateMask = event.stateMask;
987                 notifyListeners (SWT.DefaultSelection, e);
988             }
989             // At this point the widget may have been disposed.
990
// If so, do not continue.
991
if (isDisposed ()) break;
992             Event e = new Event();
993             e.time = event.time;
994             e.character = event.character;
995             e.keyCode = event.keyCode;
996             e.stateMask = event.stateMask;
997             notifyListeners(SWT.KeyDown, e);
998             break;
999             
1000        }
1001    }
1002}
1003
1004void popupEvent(Event event) {
1005    switch (event.type) {
1006        case SWT.Paint:
1007            // draw black rectangle around list
1008
Rectangle listRect = table.getBounds();
1009            Color black = getDisplay().getSystemColor(SWT.COLOR_BLACK);
1010            event.gc.setForeground(black);
1011            event.gc.drawRectangle(0, 0, listRect.width + 1, listRect.height + 1);
1012            break;
1013        case SWT.Close:
1014            event.doit = false;
1015            dropDown (false);
1016            break;
1017        case SWT.Deactivate:
1018            dropDown (false);
1019            break;
1020    }
1021}
1022
1023public void redraw () {
1024    super.redraw();
1025    text.redraw();
1026    arrow.redraw();
1027    if (popup.isVisible()) table.redraw();
1028}
1029
1030public void redraw (int x, int y, int width, int height, boolean all) {
1031    super.redraw(x, y, width, height, true);
1032}
1033
1034/**
1035 * Removes the item from the receiver's table at the given
1036 * zero-relative index.
1037 *
1038 * @param index the index for the item
1039 *
1040 * @exception IllegalArgumentException <ul>
1041 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
1042 * </ul>
1043 * @exception SWTException <ul>
1044 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1045 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1046 * </ul>
1047 */

1048public void remove (int index) {
1049    checkWidget();
1050    table.remove (index);
1051}
1052
1053/**
1054 * Removes the items from the receiver's table which are
1055 * between the given zero-relative start and end
1056 * indices (inclusive).
1057 *
1058 * @param start the start of the range
1059 * @param end the end of the range
1060 *
1061 * @exception IllegalArgumentException <ul>
1062 * <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>
1063 * </ul>
1064 * @exception SWTException <ul>
1065 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1066 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1067 * </ul>
1068 */

1069public void remove (int start, int end) {
1070    checkWidget();
1071    table.remove (start, end);
1072}
1073
1074/**
1075 * Searches the receiver's table starting at the first item
1076 * until an item is found that text is equal to the argument,
1077 * and removes that item from the list.
1078 *
1079 * @param string the item to remove
1080 *
1081 * @exception IllegalArgumentException <ul>
1082 * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
1083 * <li>ERROR_INVALID_ARGUMENT - if the string is not found in the list</li>
1084 * </ul>
1085 * @exception SWTException <ul>
1086 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1087 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1088 * </ul>
1089 */

1090public void remove (String JavaDoc string) {
1091    checkWidget();
1092    if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
1093    table.remove (getTableItemIndex(string));
1094}
1095
1096protected TableItem getTableItem(String JavaDoc text)
1097{
1098    int itemCount = table.getItemCount();
1099    for (int i=0; i<itemCount-1; i++)
1100    {
1101        TableItem item = table.getItem(i);
1102        if (item.getText().equals(text))
1103            return item;
1104    }
1105    return null;
1106}
1107
1108protected int getTableItemIndex(String JavaDoc text)
1109{
1110    int itemCount = table.getItemCount();
1111    for (int i=0; i<itemCount-1; i++)
1112    {
1113        TableItem item = table.getItem(i);
1114        if (item.getText().equals(text))
1115            return i;
1116    }
1117    return -1;
1118}
1119
1120/**
1121 * Removes all of the items from the receiver's table and clear the
1122 * contents of receiver's text field.
1123 * <p>
1124 * @exception SWTException <ul>
1125 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1126 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1127 * </ul>
1128 */

1129public void removeAll () {
1130    checkWidget();
1131    text.setText (""); //$NON-NLS-1$
1132
table.removeAll ();
1133}
1134
1135/**
1136 * Removes the listener from the collection of listeners who will
1137 * be notified when the receiver's text is modified.
1138 *
1139 * @param listener the listener which should no longer be notified
1140 *
1141 * @exception IllegalArgumentException <ul>
1142 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
1143 * </ul>
1144 * @exception SWTException <ul>
1145 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1146 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1147 * </ul>
1148 *
1149 * @see ModifyListener
1150 * @see #addModifyListener
1151 */

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

1175public void removeSelectionListener (SelectionListener listener) {
1176    checkWidget();
1177    if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
1178    removeListener(SWT.Selection, listener);
1179    removeListener(SWT.DefaultSelection,listener);
1180}
1181
1182/**
1183 * Selects the item at the given zero-relative index in the receiver's
1184 * table. If the item at the index was already selected, it remains
1185 * selected. Indices that are out of range are ignored.
1186 *
1187 * @param index the index of the item to select
1188 *
1189 * @exception SWTException <ul>
1190 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1191 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1192 * </ul>
1193 */

1194public void select (int index) {
1195    checkWidget();
1196    if (index == -1) {
1197        table.deselectAll ();
1198        text.setText (""); //$NON-NLS-1$
1199
return;
1200    }
1201    if (0 <= index && index < table.getItemCount()) {
1202        if (index != getSelectionIndex()) {
1203            text.setText (table.getItem (index).getText());
1204            text.selectAll ();
1205            table.select (index);
1206            table.showSelection ();
1207        }
1208    }
1209}
1210
1211public void setBackground (Color color) {
1212    super.setBackground(color);
1213    background = color;
1214    if (text != null) text.setBackground(color);
1215    if (table != null) table.setBackground(color);
1216    if (arrow != null) arrow.setBackground(color);
1217}
1218
1219/**
1220 * Sets the editable state.
1221 *
1222 * @param editable the new editable state
1223 *
1224 * @exception SWTException <ul>
1225 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1226 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1227 * </ul>
1228 *
1229 * @since 3.0
1230 */

1231public void setEditable (boolean editable) {
1232    checkWidget ();
1233    text.setEditable(editable);
1234}
1235
1236public void setEnabled (boolean enabled) {
1237    super.setEnabled(enabled);
1238    if (popup != null) popup.setVisible (false);
1239    if (text != null) text.setEnabled(enabled);
1240    if (arrow != null) arrow.setEnabled(enabled);
1241}
1242public boolean setFocus () {
1243    checkWidget();
1244    return text.setFocus ();
1245}
1246
1247public void setFont (Font font) {
1248    super.setFont (font);
1249    this.font = font;
1250    text.setFont (font);
1251    table.setFont (font);
1252    internalLayout (true);
1253}
1254
1255public void setForeground (Color color) {
1256    super.setForeground(color);
1257    foreground = color;
1258    if (text != null) text.setForeground(color);
1259    if (table != null) table.setForeground(color);
1260    if (arrow != null) arrow.setForeground(color);
1261}
1262
1263/**
1264 * Sets the text and the image of the item in the receiver's table at the given
1265 * zero-relative index. This is equivalent
1266 * to <code>remove</code>'ing the old item at the index, and then
1267 * <code>add</code>'ing the new item at that index.
1268 *
1269 * @param index the index for the item
1270 * @param string the new text for the item
1271 * @param image the image for the item
1272 *
1273 * @exception IllegalArgumentException <ul>
1274 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
1275 * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
1276 * </ul>
1277 * @exception SWTException <ul>
1278 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1279 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1280 * </ul>
1281 */

1282public void setItem (int index, String JavaDoc string, Image image) {
1283    checkWidget();
1284// list.setItem (index, string);
1285
TableItem item = new TableItem(table, getStyle());
1286    item.setText(index, string);
1287    item.setImage(image);
1288}
1289
1290/**
1291 * Sets the layout which is associated with the receiver to be
1292 * the argument which may be null.
1293 * <p>
1294 * Note : No Layout can be set on this Control because it already
1295 * manages the size and position of its children.
1296 * </p>
1297 *
1298 * @param layout the receiver's new layout or null
1299 *
1300 * @exception SWTException <ul>
1301 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1302 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1303 * </ul>
1304 */

1305public void setLayout (Layout layout) {
1306    checkWidget ();
1307    return;
1308}
1309
1310/**
1311 * Sets the selection in the receiver's text field to the
1312 * range specified by the argument whose x coordinate is the
1313 * start of the selection and whose y coordinate is the end
1314 * of the selection.
1315 *
1316 * @param selection a point representing the new selection start and end
1317 *
1318 * @exception IllegalArgumentException <ul>
1319 * <li>ERROR_NULL_ARGUMENT - if the point is null</li>
1320 * </ul>
1321 * @exception SWTException <ul>
1322 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1323 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1324 * </ul>
1325 */

1326public void setSelection (Point selection) {
1327    checkWidget();
1328    if (selection == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
1329    text.setSelection (selection.x, selection.y);
1330}
1331
1332/**
1333 * Sets the contents of the receiver's text field to the
1334 * given string.
1335 * <p>
1336 * Note: The text field in a <code>Combo</code> is typically
1337 * only capable of displaying a single line of text. Thus,
1338 * setting the text to a string containing line breaks or
1339 * other special characters will probably cause it to
1340 * display incorrectly.
1341 * </p>
1342 *
1343 * @param string the new text
1344 *
1345 * @exception IllegalArgumentException <ul>
1346 * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
1347 * </ul>
1348 * @exception SWTException <ul>
1349 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1350 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1351 * </ul>
1352 */

1353public void setText (String JavaDoc string) {
1354    checkWidget();
1355    if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
1356    int index = table.indexOf (getTableItem(string));
1357    if (index == -1) {
1358        table.deselectAll ();
1359        text.setText (string);
1360        return;
1361    }
1362    text.setText (string);
1363    text.selectAll ();
1364    table.setSelection (index);
1365    table.showSelection ();
1366}
1367
1368/**
1369 * Sets the maximum number of characters that the receiver's
1370 * text field is capable of holding to be the argument.
1371 *
1372 * @param limit new text limit
1373 *
1374 * @exception IllegalArgumentException <ul>
1375 * <li>ERROR_CANNOT_BE_ZERO - if the limit is zero</li>
1376 * </ul>
1377 * @exception SWTException <ul>
1378 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1379 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1380 * </ul>
1381 */

1382public void setTextLimit (int limit) {
1383    checkWidget();
1384    text.setTextLimit (limit);
1385}
1386
1387public void setToolTipText (String JavaDoc string) {
1388    checkWidget();
1389    super.setToolTipText(string);
1390    arrow.setToolTipText (string);
1391    text.setToolTipText (string);
1392}
1393
1394public void setVisible (boolean visible) {
1395    super.setVisible(visible);
1396    if (!visible) popup.setVisible(false);
1397}
1398
1399/**
1400 * Sets the number of items that are visible in the drop
1401 * down portion of the receiver's table.
1402 *
1403 * @param count the new number of items to be visible
1404 *
1405 * @exception SWTException <ul>
1406 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1407 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1408 * </ul>
1409 *
1410 * @since 3.0
1411 */

1412public void setVisibleItemCount (int count) {
1413    checkWidget ();
1414    if (count < 0) return;
1415    visibleItemCount = count;
1416}
1417
1418String JavaDoc stripMnemonic (String JavaDoc string) {
1419    int index = 0;
1420    int length = string.length ();
1421    do {
1422        while ((index < length) && (string.charAt (index) != '&')) index++;
1423        if (++index >= length) return string;
1424        if (string.charAt (index) != '&') {
1425            return string.substring(0, index-1) + string.substring(index, length);
1426        }
1427        index++;
1428    } while (index < length);
1429    return string;
1430}
1431
1432void textEvent (Event event) {
1433    switch (event.type) {
1434        case SWT.FocusIn: {
1435            handleFocus (SWT.FocusIn);
1436            break;
1437        }
1438        case SWT.KeyDown: {
1439            if (event.character == SWT.CR) {
1440                dropDown (false);
1441                Event e = new Event ();
1442                e.time = event.time;
1443                e.stateMask = event.stateMask;
1444                notifyListeners (SWT.DefaultSelection, e);
1445            }
1446            //At this point the widget may have been disposed.
1447
// If so, do not continue.
1448
if (isDisposed ()) break;
1449            
1450            if (event.keyCode == SWT.ARROW_UP || event.keyCode == SWT.ARROW_DOWN) {
1451                event.doit = false;
1452                if ((event.stateMask & SWT.ALT) != 0) {
1453                    boolean dropped = isDropped ();
1454                    text.selectAll ();
1455                    if (!dropped) setFocus ();
1456                    dropDown (!dropped);
1457                    break;
1458                }
1459
1460                int oldIndex = getSelectionIndex ();
1461                if (event.keyCode == SWT.ARROW_UP) {
1462                    select (Math.max (oldIndex - 1, 0));
1463                } else {
1464                    select (Math.min (oldIndex + 1, getItemCount () - 1));
1465                }
1466                if (oldIndex != getSelectionIndex ()) {
1467                    Event e = new Event();
1468                    e.time = event.time;
1469                    e.stateMask = event.stateMask;
1470                    notifyListeners (SWT.Selection, e);
1471                }
1472                //At this point the widget may have been disposed.
1473
// If so, do not continue.
1474
if (isDisposed ()) break;
1475            }
1476            
1477            // Further work : Need to add support for incremental search in
1478
// pop up list as characters typed in text widget
1479

1480            Event e = new Event ();
1481            e.time = event.time;
1482            e.character = event.character;
1483            e.keyCode = event.keyCode;
1484            e.stateMask = event.stateMask;
1485            notifyListeners (SWT.KeyDown, e);
1486            break;
1487        }
1488        case SWT.KeyUp: {
1489            Event e = new Event ();
1490            e.time = event.time;
1491            e.character = event.character;
1492            e.keyCode = event.keyCode;
1493            e.stateMask = event.stateMask;
1494            notifyListeners (SWT.KeyUp, e);
1495            break;
1496        }
1497        case SWT.Modify: {
1498            table.deselectAll ();
1499            Event e = new Event ();
1500            e.time = event.time;
1501            notifyListeners (SWT.Modify, e);
1502            break;
1503        }
1504        case SWT.MouseDown: {
1505            if (event.button != 1) return;
1506            if (text.getEditable ()) return;
1507            boolean dropped = isDropped ();
1508            text.selectAll ();
1509            if (!dropped) setFocus ();
1510            dropDown (!dropped);
1511            break;
1512        }
1513        case SWT.MouseUp: {
1514            if (event.button != 1) return;
1515            if (text.getEditable ()) return;
1516            text.selectAll ();
1517            break;
1518        }
1519        case SWT.Traverse: {
1520            switch (event.detail) {
1521                case SWT.TRAVERSE_RETURN:
1522                case SWT.TRAVERSE_ARROW_PREVIOUS:
1523                case SWT.TRAVERSE_ARROW_NEXT:
1524                    // The enter causes default selection and
1525
// the arrow keys are used to manipulate the list contents so
1526
// do not use them for traversal.
1527
event.doit = false;
1528                    break;
1529            }
1530            
1531            Event e = new Event ();
1532            e.time = event.time;
1533            e.detail = event.detail;
1534            e.doit = event.doit;
1535            e.character = event.character;
1536            e.keyCode = event.keyCode;
1537            notifyListeners (SWT.Traverse, e);
1538            event.doit = e.doit;
1539            event.detail = e.detail;
1540            break;
1541        }
1542    }
1543}
1544}
1545
1546
Popular Tags