1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.fosstrak.llrp.commander.dialogs;
23
24 import org.eclipse.jface.window.Window;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Listener;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.swt.widgets.Text;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public abstract class ConnectDialog extends org.eclipse.jface.dialogs.Dialog {
52
53
54 public String [] FIELDS;
55
56
57 public String [] DEFAULTS;
58
59
60 public String [] values;
61
62
63 protected Text []txts;
64
65
66 protected final String caption;
67
68
69 protected GridData gridLabel = new GridData(GridData.FILL_BOTH);
70
71
72 protected GridData gridText = new GridData(GridData.FILL_BOTH);
73
74
75 protected GridData gridAll = new GridData(GridData.FILL_BOTH);
76
77
78
79
80
81
82 public ConnectDialog(Shell shell, String caption) {
83 super(shell);
84 this.caption = caption;
85 }
86
87
88
89
90
91 protected void setLayout(Composite parent) {
92 GridLayout layout = new GridLayout();
93 layout.numColumns = 3;
94
95 gridLabel.verticalSpan = 1;
96 gridLabel.horizontalSpan = 1;
97 gridLabel.widthHint=100;
98 gridLabel.heightHint = 20;
99
100 gridText.verticalSpan = 1;
101 gridText.horizontalSpan = 2;
102 gridText.widthHint=200;
103 gridText.heightHint = 20;
104
105 gridAll.verticalSpan = 1;
106 gridAll.horizontalSpan = 3;
107 gridAll.heightHint = 20;
108
109 parent.getShell().setLayout(layout);
110 parent.getShell().setText(caption);
111 }
112
113
114
115
116
117 protected void registerTextFieldListeners(Button btnOK) {
118
119 for (int i=0; i<DEFAULTS.length; i++) {
120 Listener listener = getListener(txts[i], i, btnOK);
121 if (null != listener) {
122
123 txts[i].addListener(SWT.Modify, listener);
124 }
125 }
126 }
127
128
129
130
131
132 protected void addTextFields(Composite parent) {
133 values = new String[DEFAULTS.length];
134 txts = new Text[DEFAULTS.length];
135 for (int i=0; i<FIELDS.length; i++) {
136 Label label = new Label(parent, SWT.NONE);
137 label.setText(FIELDS[i]);
138 label.setLayoutData(gridLabel);
139
140 txts[i] = new Text(parent, SWT.BORDER);
141 txts[i].setText(DEFAULTS[i]);
142 txts[i].setLayoutData(gridText);
143 }
144 }
145
146
147
148
149
150 protected void addCancelButton(Composite parent) {
151 final Button btnCancel = new Button(parent, SWT.PUSH);
152 btnCancel.setText("Cancel");
153 btnCancel.setLayoutData(gridLabel);
154 btnCancel.addSelectionListener(new SelectionAdapter() {
155 public void widgetSelected(SelectionEvent e) {
156 setReturnCode(Window.CANCEL);
157 close();
158 }
159 });
160 }
161
162
163
164
165
166 protected void addOKButton(Composite parent) {
167 final Button btnOK = new Button(parent, SWT.PUSH);
168 btnOK.setText("OK");
169 btnOK.setLayoutData(gridLabel);
170
171 btnOK.addSelectionListener(new SelectionAdapter() {
172 public void widgetSelected(SelectionEvent e) {
173 for (int i=0; i<DEFAULTS.length; i++) {
174 values[i] = txts[i].getText();
175 }
176
177 setReturnCode(Window.OK);
178 close();
179 }
180 });
181
182
183 for (int i=0; i<DEFAULTS.length; i++) {
184 Listener listener = getListener(txts[i], i, btnOK);
185 if (null != listener) {
186
187 txts[i].addListener(SWT.Modify, listener);
188 }
189 }
190 }
191
192
193
194
195
196 protected void addInvisibleButton(Composite parent) {
197
198 final Button none = new Button(parent, SWT.NONE);
199 none.setVisible(false);
200 none.setLayoutData(gridLabel);
201 }
202
203
204
205
206 protected Control createContents(Composite parent) {
207
208 setLayout(parent);
209
210 addTextFields(parent);
211 addInvisibleButton(parent);
212 addOKButton(parent);
213 addCancelButton(parent);
214
215 parent.pack();
216 return parent;
217 }
218
219
220
221
222
223
224
225
226
227
228 public abstract Listener getListener(final Text txt, int offset, final Button ok);
229 }