-
Notifications
You must be signed in to change notification settings - Fork 96
/
BaseCombo.java
1353 lines (1219 loc) · 35.6 KB
/
BaseCombo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
* Copyright (c) 2005-2009 Jeremy Dowdall
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - SWT's CCombo was relied upon _heavily_ for example and reference
* Jeremy Dowdall <jeremyd@aspencloud.com> - initial API and implementation
*****************************************************************************/
package org.eclipse.nebula.cwt.base;
import org.eclipse.nebula.cwt.animation.AnimationRunner;
import org.eclipse.nebula.cwt.animation.effects.Resize;
import org.eclipse.nebula.cwt.animation.movement.LinearInOut;
import org.eclipse.nebula.cwt.v.VButton;
import org.eclipse.nebula.cwt.v.VButtonPainter;
import org.eclipse.nebula.cwt.v.VControl;
import org.eclipse.nebula.cwt.v.VLayout;
import org.eclipse.nebula.cwt.v.VNative;
import org.eclipse.nebula.cwt.v.VPanel;
import org.eclipse.nebula.cwt.v.VSimpleLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
* The AbstractCombo is an abstract class which provides the basic functionality
* for a button with a DROP_DOWN, or "popup", shell component. When the user
* selects the button the shell is set visible and the SWT Components which have
* been placed on the "content" Composite will be shown.
*/
public abstract class BaseCombo extends Canvas {
/**
* Special layout implementation to position the combo's drop-down Button
* within its Text.
*/
protected class DropComboLayout extends VLayout {
protected Point computeSize(VPanel panel, int wHint, int hHint,
boolean flushCache) {
Point size = text.computeSize(SWT.DEFAULT, SWT.DEFAULT);
if (button.getVisible()) {
size.x += size.y;
}
size.y += textMarginHeight;
if (wHint != SWT.DEFAULT) {
size.x = Math.min(size.x, wHint);
}
if (hHint != SWT.DEFAULT) {
size.y = Math.min(size.y, hHint);
}
return size;
}
protected void layout(VPanel panel, boolean flushCache) {
Rectangle cRect = panel.getClientArea();
Point tSize = text.computeSize(SWT.DEFAULT, SWT.DEFAULT);
tSize.y += textMarginHeight;
Point bSize = button.getVisible() ? new Point(tSize.y, tSize.y)
: new Point(0, 0);
if (leftAlign) {
text.setBounds(cRect.x + bSize.x, cRect.y
+ (win32 ? getBorderWidth() : 0),
cRect.width - bSize.x, tSize.y);
button.setBounds(cRect.x, cRect.y, bSize.x, bSize.y);
} else {
text.setBounds(cRect.x + (win32 ? 1 : 0), cRect.y
+ (win32 ? 1 : 0), cRect.width - bSize.x
- (win32 ? 2 : 0), tSize.y - (win32 ? 2 : 0));
button.setBounds(
win32 ? (cRect.x + cRect.width - cRect.height + 1)
: (cRect.x + cRect.width - bSize.x), cRect.y,
win32 ? (cRect.height - 1) : bSize.x,
win32 ? cRect.height : bSize.y);
}
}
}
/**
* true if the platform is carbon, false otherwise
*/
protected static final boolean carbon = "carbon".equals(SWT.getPlatform()); //$NON-NLS-1$
/**
* true if the platform is gtk, false otherwise
*/
protected static final boolean gtk = "gtk".equals(SWT.getPlatform()); //$NON-NLS-1$
/**
* true if the platform is win32, false otherwise
*/
protected static final boolean win32 = "win32".equals(SWT.getPlatform()); //$NON-NLS-1$
/**
* A constant value used to pad the computed height for this widget, so that
* the combo's Button will fit without clipping its top and bottom borders.
*/
protected static final int textMarginHeight = win32 ? 4 : 0;
/**
* A style constant indicating that this combo will only have a drop down
* button, rather than a button and a text box.
*/
protected static final int BUTTON_ONLY = 0;
/**
* A constant indicating that the drop down button is always visible. Valid
* only when style is DROP_DOWN.
*
* @see #BUTTON_AUTO
* @see #BUTTON_NEVER
*/
protected static final int BUTTON_ALWAYS = 1;
/**
* A constant indicating that the drop down button is never visible. Valid
* only when style is DROP_DOWN.
* <p>
* This setting may be useful if the subclass of this BaseCombo allows
* programmatic opening and closing of the drop down shell via
* {@link #setOpen(boolean)}, or a similar method.
* </p>
*
* @see #BUTTON_AUTO
* @see #BUTTON_ALWAYS
* @see #setOpen(boolean)
* @see #setOpen(boolean, Runnable)
*/
protected static final int BUTTON_NEVER = 2;
/**
* A constant indicating that the drop down button is visible when the text
* box has the focus, and is hidden otherwise. Valid only when style is
* DROP_DOWN.
*
* @see #BUTTON_ALWAYS
* @see #BUTTON_NEVER
*/
protected static final int BUTTON_AUTO = 3;
/**
* boolean to disable button on open state.
*/
boolean buttonActive = true;
private static int checkStyle(int style) {
int rstyle = SWT.NONE;
if ((style & SWT.BORDER) != 0) {
if (win32 || (style & SWT.SIMPLE) != 0) {
rstyle |= SWT.BORDER;
}
}
if (win32) {
rstyle |= SWT.DOUBLE_BUFFERED;
}
return rstyle;
}
/**
* A recursive method to find out if a composite is an ancestor of a
* control.
*
* @param control
* @param composite
* @return true if the composite is an ancestor, false otherwise.
*/
protected static boolean containsControl(Control control,
Composite composite) {
if (composite != null && !composite.isDisposed()) {
Control[] children = composite.getChildren();
for (Control child : children) {
if (!child.isDisposed()) {
if (child == control) {
return true;
} else if (child instanceof Composite) {
return containsControl(control, (Composite) child);
}
}
}
}
return false;
}
/**
* The VPanel that is the base of this widget. If the style is SIMPLE, this
* panel will be the base of the content area, otherwise, it is the base of
* the text/button area.
*/
protected VPanel panel = null;
/**
* The Button widget of a DROP_DOWN style combo. This value may be null --
* protect all references to this field with the checkButton() method.
*/
protected VButton button = null;
/**
* True if a default image should be used for the button; false otherwise -
* as is the case when an image is set using {@link #setButtonImage(Image)}
*
* @see #setButtonImage(Image)
*/
protected boolean defaultButtonImage = true;
/**
* The Text widget of a DROP_DOWN style combo. This value may be null --
* protect all references to this field with the checkText() method.
*/
protected VNative<Text> text = null;
/**
* The popup Shell widget of a DROP_DOWN style combo. This value may be null
* -- protect all references to this field with the checkContentShell()
* method.
*/
protected Shell contentShell = null;
/**
* The widget contents of the popup Shell in a DROP_DOWN combo or the full
* contents of a SIMPLE combo. This value may be null -- protect all
* references to this field with the checkContent() method.
*/
protected Control content;
/**
* The style bits requested. NOTE: this may not match the value returned by
* {@link #getStyle()} if invalid bits were requested.
*/
protected int style;
/**
* Flag to indicate that this is a SIMPLE style combo.
*/
protected boolean simple;
/**
* Flag to indicate that this combo's BUTTON should be displayed on the left
* side of its Text.
*/
protected boolean leftAlign = false;
private int buttonVisibility;
private Listener buttonVisibilityListener;
private boolean dropDown;
private boolean open = false;
private boolean holdOpen = false;
private VControl positionControl;
private VControl stretchControl;
private Listener textListener;
private Listener shellListener;
private Listener comboListener = new Listener() {
public void handleEvent(Event event) {
switch (event.type) {
case SWT.Move:
if (isOpen()) {
setOpen(false);
}
break;
case SWT.Resize:
if (isOpen()) {
setOpen(false);
}
layout(true);
break;
}
}
};
private Listener disposeListener = new Listener() {
public void handleEvent(Event event) {
if (!isDisposed()) {
getShell().removeListener(SWT.Deactivate, comboListener);
if (checkContentShell()) {
contentShell.dispose();
}
}
}
};
/**
* Main constructor -- must be called by all subclasses in their own
* constructors.
* <p>
* SWT.TOGGLE, SWT.PUSH, SWT.ARROW, SWT.FLAT, SWT.TRAIL, SWT.LEAD,
* SWT.BORDER, SWT.SIMPLE, SWT.DROP_DOWN
* </p>
*
* @param parent
* the visual parent of this widget
* @param style
* the requested SWT style bitmask for this widget
*/
public BaseCombo(Composite parent, int style) {
super(parent, checkStyle(style));
panel = new VPanel(this, SWT.NONE);
panel.setWidget(this);
init(style);
}
/**
* Adds the listener to the collection of listeners who will
* be notified when keys are pressed and released on the system keyboard, by sending
* it one of the messages defined in the <code>KeyListener</code>
* interface.
* <p>
* When a key listener is added to a control, the control
* will take part in widget traversal. By default, all
* traversal keys (such as the tab key and so on) are
* delivered to the control. In order for a control to take
* part in traversal, it should listen for traversal events.
* Otherwise, the user can traverse into a control but not
* out. Note that native controls such as table and tree
* implement key traversal in the operating system. It is
* not necessary to add traversal listeners for these controls,
* unless you want to override the default traversal.
* </p>
* @param listener the listener which should be notified
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @see KeyListener
* @see #removeKeyListener
*/
public void addKeyListener (KeyListener listener) {
if (listener == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
if (checkText()) {
text.getControl().addKeyListener(listener);
}
}
/**
* Adds the listener to the collection of listeners who will be notified
* when the receiver's text is modified, by sending it one of the messages
* defined in the <code>ModifyListener</code> interface.<br/>
* Note that this is NOT the correct way to listen for changes in the
* underlying model for the combo. This should be provided by some other
* mechanism, such as a {@link SelectionListener}.
*
* @param listener
* the listener which should be notified
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException
* <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been
* disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
* thread that created the receiver</li>
* </ul>
*
* @see ModifyListener
* @see #removeModifyListener
*/
public void addModifyListener(ModifyListener listener) {
if (listener == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
if (checkText()) {
text.getControl().addModifyListener(listener);
}
}
private void addTextListener() {
text.getControl().addListener(SWT.KeyDown, textListener);
text.getControl().addListener(SWT.Modify, textListener);
}
/**
* Adds the listener to the collection of listeners who will
* be notified when traversal events occur, by sending it
* one of the messages defined in the <code>TraverseListener</code>
* interface.
*
* @param listener the listener which should be notified
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @see TraverseListener
* @see #removeTraverseListener
*/
public void addTraverseListener (TraverseListener listener) {
if (listener == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
if (checkText()) {
text.getControl().addTraverseListener(listener);
}
}
/**
* @return true if the {@link #button} field is in a fit state to be used
*/
protected boolean checkButton() {
return (button != null && !button.isDisposed());
}
/**
* @return true if the {@link #content} field is in a fit state to be used
*/
protected boolean checkContent() {
return (content != null && !content.isDisposed());
}
/**
* @return true if the {@link #contentShell} field is in a fit state to be
* used
*/
protected boolean checkContentShell() {
return (contentShell != null && !contentShell.isDisposed());
}
/**
* @return true if the {@link #text} field is in a fit state to be used
*/
protected boolean checkText() {
return (text != null && !text.isDisposed());
}
private void createButton(int style) {
int mask = BUTTON_ONLY | SWT.TOGGLE | SWT.PUSH | SWT.ARROW | SWT.FLAT;
int buttonStyle = style & mask;
button = new VButton(panel, buttonStyle | SWT.NO_FOCUS);
if ((style & BUTTON_ONLY) == 0) {
button.setMargins(0, 0);
}
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
if(buttonActive) {
setOpen(!isOpen());
}
}
});
if (win32) {
button.setPainter(new VButtonPainter() {
@Override
public void paintBackground(VControl control, Event e) {
VButton button = (VButton) control;
if (button.hasState(VControl.STATE_ACTIVE)) {
Rectangle r = button.getBounds();
e.gc.setBackground(e.display
.getSystemColor(SWT.COLOR_GRAY));
e.gc.fillRoundRectangle(r.x, r.y, r.width - 1,
r.height - 1, 2, 2);
e.gc.drawRoundRectangle(r.x, r.y, r.width - 1,
r.height - 1, 2, 2);
}
}
});
}
}
private void createContentShell() {
contentShell = new Shell(getShell(), SWT.NO_TRIM | SWT.ON_TOP);
contentShell.addListener(SWT.Close, shellListener);
contentShell.addListener(SWT.Deactivate, shellListener);
}
private void createText(int style) {
textListener = new Listener() {
public void handleEvent(Event event) {
switch (event.type) {
case SWT.KeyDown:
if (event.stateMask == SWT.CTRL && event.keyCode == ' ') {
event.doit = false;
setOpen(true);
}
break;
case SWT.Modify:
Event e = new Event();
e.time = event.time;
setModifyEventProperties(e);
notifyListeners(SWT.Modify, e);
break;
}
}
};
int mask = SWT.TRAIL | SWT.LEAD;
int textStyle = SWT.SINGLE | (style & mask);
if (!win32 && (style & SWT.BORDER) != 0) {
textStyle |= SWT.BORDER;
}
text = VNative.create(Text.class, panel, textStyle);
addTextListener();
}
/**
* @param image
*/
protected final void doSetButtonImage(Image image) {
if (checkButton()) {
button.setImage(image);
}
}
/**
* @return the Control that was set as this popup shell's content with
* setContent(Control)
*/
protected Control getContent() {
return content;
}
/**
* @return the content shell
*/
protected Shell getContentShell() {
if (contentShell == null) {
createContentShell();
}
return contentShell;
}
/**
* Returns the editable state.
*
* @return whether or not the receiver is editable
*
* @exception SWTException
* <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been
* disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
* thread that created the receiver</li>
* </ul>
*/
public boolean getEditable() {
return checkText() ? text.getControl().getEditable() : getEnabled();
}
/**
* Fixes bug 181442: [CDateTime] Incorrect getEnabled()
*/
public boolean getEnabled() {
return checkText() ? text.getEnabled() : super.getEnabled();
}
/**
* @return the state of the holdOpen flag
*/
protected boolean getHoldOpen() {
return holdOpen;
}
/**
* returns the menu for this combo
*/
public Menu getMenu() {
if (checkText()) {
return text.getMenu();
}
return super.getMenu();
}
/**
* @return the stretch control
*/
protected VControl getStretchControl() {
return stretchControl;
}
public int getStyle() {
return style;
}
/**
* Returns the text of this combo
*
* @return the combo's text
*/
public String getText() {
return checkText() ? text.getText() : ""; //$NON-NLS-1$
}
private void init(int style) {
this.style = style;
simple = (style & SWT.SIMPLE) != 0;
dropDown = (style & (BUTTON_ONLY | SWT.DROP_DOWN)) != 0;
leftAlign = (style & SWT.LEFT) != 0;
if (simple) {
panel.setLayout(new VSimpleLayout());
} else if (dropDown) {
createButton(style);
if ((style & BUTTON_ONLY) == 0) {
createText(style);
if (win32) {
setPositionControl(panel);
} else {
setPositionControl(text);
}
panel.setLayout(new DropComboLayout());
} else {
setPositionControl(button);
panel.setLayout(new VSimpleLayout());
}
shellListener = new Listener() {
public void handleEvent(Event event) {
switch (event.type) {
case SWT.Close:
event.doit = false;
setOpen(false);
break;
case SWT.Deactivate:
if (!checkContent() || content.getMenu() == null
|| !content.getMenu().isVisible()) {
setOpen(false);
}
break;
}
}
};
addListener(SWT.Move, comboListener);
addListener(SWT.Resize, comboListener);
} else {
panel.setLayout(new VSimpleLayout());
createText(style);
}
addListener(SWT.Dispose, disposeListener);
}
/**
* @return true if style is CDT.DROP_DOWN
*/
protected boolean isDropDown() {
return dropDown;
}
/**
* @return the state of the popup shell's visibility
*/
public boolean isOpen() {
return open;
}
/**
* @return if Shell style is SWT.RIGHT_TO_LEFT
*/
protected boolean isRTL() {
return (getShell().getStyle() & SWT.RIGHT_TO_LEFT) == SWT.RIGHT_TO_LEFT;
}
/**
* @return if style is CDT.SIMPLE
*/
protected boolean isSimple() {
return simple;
}
/**
* called just <i>after</i> the content shell is set not-visible and has
* "closed"
* <p>
* override if you want to do something with the shell just after becoming
* not visible
* </p>
*
* @param popup
* @see #preClose(Shell)
*/
protected void postClose(Shell popup) {
// subclasses to implement if necessary
}
/**
* called <i>after</i> the content shell is set visible and has "opened"
* <p>
* override if you want to do something with the shell just after becoming
* visible
* </p>
*
* @param popup
* @see #preOpen(Shell)
*/
protected void postOpen(Shell popup) {
// subclasses to implement if necessary
}
/**
* called just <i>before</i> the content shell is set not-visible and
* "closes"
* <p>
* override if you want to do something with the shell prior to it becoming
* not visible
* </p>
*
* @param popup
* @see #postClose(Shell)
*/
protected void preClose(Shell popup) {
// subclasses to implement if necessary
}
/**
* called just <i>before</i> the content shell is set visible and "opens"
* <p>
* override if you want to do something with the shell prior to it becoming
* visible
* </p>
*
* @param popup
* @see #postOpen(Shell)
*/
protected void preOpen(Shell popup) {
// subclasses to implement if necessary
}
/**
* Removes the listener from the collection of listeners who will
* be notified when keys are pressed and released on the system keyboard.
*
* @param listener the listener which should no longer be notified
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @see KeyListener
* @see #addKeyListener
*/
public void removeKeyListener(KeyListener listener) {
if (listener == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
if (checkText()) {
text.getControl().removeKeyListener(listener);
}
}
/**
* Removes the listener from the collection of listeners who will be
* notified when the receiver's text is modified.
*
* @param listener
* the listener which should no longer be notified
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException
* <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been
* disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
* thread that created the receiver</li>
* </ul>
*
* @see ModifyListener
* @see #addModifyListener
*/
public void removeModifyListener(ModifyListener listener) {
if (listener == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
if (checkText()) {
text.getControl().removeModifyListener(listener);
}
}
private void removeTextListener() {
text.getControl().removeListener(SWT.KeyDown, textListener);
text.getControl().removeListener(SWT.Modify, textListener);
}
/**
* Removes the listener from the collection of listeners who will
* be notified when traversal events occur.
*
* @param listener the listener which should no longer be notified
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @see TraverseListener
* @see #addTraverseListener
*/
public void removeTraverseListener(TraverseListener listener) {
if (listener == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
if (checkText()) {
text.getControl().removeTraverseListener(listener);
}
}
/**
* Set the alignment of the button in relation to the text box. Only valid
* if style is DROP_DOWN.
*
* @param alignment
* can be either SWT.LEFT or SWT.RIGHT. Other values have no
* effect.
*/
protected void setButtonAlignment(int alignment) {
if (SWT.LEFT == alignment) {
leftAlign = true;
} else if (SWT.RIGHT == alignment) {
leftAlign = false;
}
layout(true);
}
/**
* Set the custom image for the drop down button. Only valid if style is
* DROP_DOWN. Passing null in will set the image to its default value.
*
* @param image
*/
protected void setButtonImage(Image image) {
doSetButtonImage(image);
}
/**
* Set the text for the drop down button. Only valid if style is DROP_DOWN.
* Passing null will clear the text.
*
* @param text
*/
protected void setButtonText(String text) {
if (checkButton()) {
button.setText(text);
}
}
/**
* Set the visibility style of the drop button.
* <p>
* The style will be forced to NEVER if the contents are null
* </p>
* <dl>
* <dt><b>Styles:</b></dt>
* <dd>BUTTON_ALWAYS, BUTTON_AUTO, BUTTON_MANUAL, BUTTON_NEVER</dd>
* </dl>
* <dl>
* <dt><b>Style BUTTON_ALWAYS:</b></dt>
* <dd>Button will always be shown - standard SWT.DROP_DOWN behaviour. The
* method setButtonVisible(boolean) has no affect with this style set</dd>
* <dt><b>Style BUTTON_AUTO:</b></dt>
* <dd>Button visibility will be handled automatically through focus events,
* popup events, as well as programmatically</dd>
* <dt><b>Style BUTTON_MANUAL:</b></dt>
* <dd>Button visibility will only be handled programmatically</dd>
* <dt><b>Style BUTTON_NEVER:</b></dt>
* <dd>Button will never be shown - standard SWT.SIMPLE behaviour. The
* method setButtonVisible(boolean) has no affect with this style set</dd>
* </dl>
*
* @param visibility
* the visibility style constant
* @see #setButtonVisible(boolean)
*/
protected void setButtonVisibility(int visibility) {
buttonVisibility = visibility;
setButtonVisible(false);
if (buttonVisibility == BUTTON_AUTO) {
buttonVisibilityListener = new Listener() {
public void handleEvent(Event event) {
switch (event.type) {
case SWT.FocusIn:
setButtonVisible(true);
break;
case SWT.FocusOut:
setButtonVisible(false);
break;
}
}
};
addListener(SWT.FocusIn, buttonVisibilityListener);
addListener(SWT.FocusOut, buttonVisibilityListener);
} else {
if (buttonVisibilityListener != null) {
removeListener(SWT.FocusIn, buttonVisibilityListener);
removeListener(SWT.FocusOut, buttonVisibilityListener);
buttonVisibilityListener = null;
}
}
}
/**
* Set the visible state of the button
* <p>
* Note: This method is only useful when the button's visibility style is
* either AUTO or MANUAL.
* </p>
*
* @param visible
* @see #setButtonVisibility(int)
*/
protected void setButtonVisible(boolean visible) {
// bug 352689
if (!checkButton())
return;
if (BUTTON_ALWAYS == buttonVisibility) {
visible = true;
} else if (BUTTON_NEVER == buttonVisibility) {
visible = false;
}
button.setVisible(visible);
layout(true);
update();
}
/**
* set the content of the popup shell
* <p>
* Can be a single control, or a Composite consisting of many controls
* </p>
*
* @param content
*/
protected void setContent(Control content) {
this.content = content;
if (this.content != null) {
if (!simple) {
this.content.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
true, true));
}
}
}
/**
* Called when the popup shell has been open, this method provides a
* location for subclasses to set the focus to the content.