11
11
* a class containing a label and a combobox in one. This makes it easier to make both visible together
12
12
*/
13
13
public class LabelCombo {
14
- private GridData myComboGriddata ;
15
- private GridData myLabelGriddata ;
16
- private String myID = new String ();
17
14
private Label myLabel ;
18
15
private Combo myCombo ;
19
- private String myValue = "" ; //$NON-NLS-1$
20
16
private String myMenuName ;
21
- private Listener myListener = null ;
22
17
23
18
/**
24
19
* Create a combo box with a label in front of it.
@@ -28,11 +23,10 @@ public class LabelCombo {
28
23
* @param horSpan
29
24
* @param fixedList if true only items of the list can be selected. If false you can type any text you want
30
25
*/
31
- public LabelCombo (Composite composite , String menuName , String ID , int horSpan , boolean fixedList ) {
32
- myID = ID ;
26
+ public LabelCombo (Composite composite , String menuName , int horSpan , boolean fixedList ) {
33
27
myLabel = new Label (composite , SWT .NONE );
34
28
myLabel .setText (menuName + " :" ); //$NON-NLS-1$
35
- myLabelGriddata = new GridData ();
29
+ GridData myLabelGriddata = new GridData ();
36
30
myLabelGriddata .horizontalSpan = 1 ;
37
31
myLabelGriddata .horizontalAlignment = SWT .FILL ;
38
32
myLabel .setLayoutData (myLabelGriddata );
@@ -41,41 +35,26 @@ public LabelCombo(Composite composite, String menuName, String ID, int horSpan,
41
35
} else {
42
36
myCombo = new Combo (composite , SWT .BORDER );
43
37
}
44
- myComboGriddata = new GridData ();
38
+ GridData myComboGriddata = new GridData ();
45
39
myComboGriddata .horizontalSpan = horSpan ;
46
40
myComboGriddata .horizontalAlignment = SWT .FILL ;
47
41
myCombo .setLayoutData (myComboGriddata );
48
42
myMenuName = menuName ;
43
+ myCombo .layout ();
49
44
50
45
}
51
46
52
47
public void addListener (Listener listener ) {
53
48
myCombo .addListener (SWT .Modify , listener );
54
- myListener = listener ;
55
- }
56
-
57
-
58
-
59
- public String getValue () {
60
- myValue = myCombo .getText ().trim ();
61
- return myValue ;
62
49
}
63
50
64
51
public String getMenuName () {
65
52
return myMenuName .trim ();
66
53
}
67
54
68
- public void setValue (String value ) {
69
- myValue = value ;
70
- myCombo .setText (value );
71
- }
72
-
73
- public void setVisible (boolean visible ) {
74
- boolean newvisible = visible && (myCombo .getItemCount () > 0 );
75
- myLabel .setVisible (newvisible );
76
- myCombo .setVisible (newvisible );
77
- myComboGriddata .exclude = !newvisible ;
78
- myLabelGriddata .exclude = !newvisible ;
55
+ public void dispose () {
56
+ myLabel .dispose ();
57
+ myCombo .dispose ();
79
58
}
80
59
81
60
public boolean isValid () {
@@ -87,26 +66,23 @@ public void setEnabled(boolean enabled) {
87
66
}
88
67
89
68
public void setItems (String [] items ) {
90
- if (myListener != null )
91
- myCombo .removeListener (SWT .Modify , myListener );
69
+ Listener [] listeners = myCombo .getListeners (SWT .Modify );
70
+ for (Listener curListener : listeners ) {
71
+ myCombo .removeListener (SWT .Modify , curListener );
72
+ }
73
+ String curValue = getText ();
92
74
myCombo .setItems (items );
93
- myCombo .setText (myValue );
94
- if (myListener != null )
95
- myCombo .addListener (SWT .Modify , myListener );
75
+ myCombo .setText (curValue );
76
+ for (Listener curListener : listeners ) {
77
+ myCombo .addListener (SWT .Modify , curListener );
78
+ }
96
79
97
80
}
98
81
99
82
public void add (String item ) {
100
83
myCombo .add (item );
101
84
}
102
85
103
- public String getID () {
104
- return myID ;
105
- }
106
-
107
- public boolean isVisible () {
108
- return (myCombo .getItemCount () > 0 );
109
- }
110
86
111
87
public void setLabel (String newLabel ) {
112
88
myLabel .setText (newLabel );
@@ -122,7 +98,7 @@ public void select(int ordinal) {
122
98
}
123
99
124
100
public String getText () {
125
- return myCombo .getText ();
101
+ return myCombo .getText (). trim () ;
126
102
}
127
103
128
104
public void setText (String text ) {
@@ -134,4 +110,5 @@ public void addListener(int event, Listener comboListener) {
134
110
myCombo .addListener ( event , comboListener );
135
111
136
112
}
113
+
137
114
}
0 commit comments