-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAWizard.java
More file actions
320 lines (244 loc) · 8.96 KB
/
Copy pathAWizard.java
File metadata and controls
320 lines (244 loc) · 8.96 KB
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
package fr.com.app.ui.widgets.wizard;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import fr.com.app.ui.resources.PluginToolkit;
import fr.com.app.ui.widgets.wizard.BreadCrumbItem.EtatBreadCrumItemEnum;
public abstract class AWizard extends Dialog {
private String title;
private List<IStep> steps;
private IStep currentStep;
private int currentStepIndex;
private Composite mainContainer;
private Composite menuComposite;
private BreadCrumb breadCrumb;
private Composite buttonsComposite;
private Composite bodyComposite;
private StackLayout stackLayout;
private boolean readOnly = false;
// Wizard controls
private Button btPrevious;
private Button btNext;
private Button btCancel;
// Data
private Object dataContainer;
public AWizard(Shell parentShell, Object dataContainer, String title) {
super(parentShell);
this.title = title;
this.steps = new LinkedList<IStep>();
this.currentStepIndex = 0;
this.dataContainer = dataContainer;
}
public void addStep(IStep newStep) {
steps.add(newStep);
}
@Override
protected Control createDialogArea(Composite parent) {
Composite body = (Composite) super.createDialogArea(parent);
GridDataFactory.fillDefaults().grab(true, true).applyTo(body);
GridLayoutFactory.fillDefaults().applyTo(body);
this.mainContainer = PluginToolkit.getInstance().createComposite(body);
GridLayoutFactory.fillDefaults().numColumns(1).margins(10, 10).applyTo(mainContainer);
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(mainContainer);
// Menu
createMenu(mainContainer);
// Body
createBody(mainContainer);
// Buttons
createButtons(mainContainer);
return body;
}
private void createBody(Composite parent) {
this.bodyComposite = PluginToolkit.getInstance().createComposite(parent);
GridDataFactory.fillDefaults().grab(true, true).applyTo(bodyComposite);
GridLayoutFactory.fillDefaults().applyTo(bodyComposite);
stackLayout = new StackLayout();
bodyComposite.setLayout(stackLayout);
if (steps.size() == 0)
return;
currentStep = steps.get(currentStepIndex);
// On ne recrée pas l'UI si elle existe
if (currentStep.getBody() == null)
currentStep.createBody(bodyComposite, dataContainer);
stackLayout.topControl = currentStep.getBody();
}
private void createButtons(Composite parent) {
this.buttonsComposite = PluginToolkit.getInstance().createComposite(parent);
GridDataFactory.fillDefaults().grab(true, false).applyTo(buttonsComposite);
GridLayoutFactory.fillDefaults().equalWidth(true).applyTo(buttonsComposite);
buttonsComposite.setLayoutData(new GridData(SWT.CENTER, SWT.DEFAULT, false, false));
//TODO taille bouton pas belle
btPrevious = createButton(buttonsComposite, IDialogConstants.BACK_ID, "Précedent", true);
btPrevious.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
btCancel = createButton(buttonsComposite, IDialogConstants.CANCEL_ID, "Annuler", true);
btCancel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
btNext = createButton(buttonsComposite, IDialogConstants.OK_ID, "Suivant", true);
btNext.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
setupBtState();
}
private void setupBtState() {
if (steps.size() == 0)
return;
btPrevious.setEnabled( ! (steps.size() == 1 || currentStepIndex == 0));
btNext.setText(currentStep.getOkBtLabel());
buttonsComposite.getParent().layout(true);
buttonsComposite.redraw();
}
private void createMenu(Composite parent) {
this.menuComposite = PluginToolkit.getInstance().get(SWT.BORDER).createComposite(parent, SWT.BORDER);
GridDataFactory.fillDefaults().grab(false, false).applyTo(menuComposite);
GridLayoutFactory.fillDefaults().spacing(0, 0).applyTo(menuComposite);
menuComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false));
// Création de l'objet
breadCrumb = new BreadCrumb(menuComposite, SWT.BORDER);
breadCrumb.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, false, false));
for (int i=0; i < steps.size(); i++) {
final BreadCrumbItem item = new BreadCrumbItem(breadCrumb, SWT.CENTER | SWT.PUSH);
IStep step = steps.get(i);
item.setText(step.getLabel());
item.setImage(step.getImage());
item.setSelectionImage(step.getImage());
item.setDisabledImage(step.getImage());
item.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
BreadCrumbItem item = (BreadCrumbItem)e.item;
goToStep(breadCrumb.indexOf(item));
for (int i = currentStepIndex + 1; i < breadCrumb.getItemCount(); i++)
breadCrumb.getItem(i).setEtat(EtatBreadCrumItemEnum.TODO);
breadCrumb.selectedItem(currentStepIndex);
}
});
}
breadCrumb.selectedItem(0);
}
@Override
protected void okPressed() {
Collection<IStatus> errors = currentStep.validate();
if (errors.size() > 0) {
StringBuilder message = new StringBuilder();
for (IStatus error : errors) {
message.append(error.getMessage());
message.append("\n");
}
MessageDialog.openError(getShell(), "Erreur", message.toString());
return;
}
currentStep.save(dataContainer);
if (currentStepIndex == steps.size() - 1) {
save();
super.okPressed();
return;
}
goToStep(currentStepIndex + 1);
}
protected abstract void save();
protected void previousPressed() {
// Should not happen
if (currentStepIndex == 0)
return;
goToStep(currentStepIndex - 1);
}
private void goToStep(int index) {
IStep newStep = steps.get(index);
// FIXME Known issue :
// si la derniere step est disable, on va avoir des ennuis
if( ! newStep.isEnable()) {
breadCrumb.disableNextItem();
if (index == currentStepIndex + 1) {
currentStepIndex = index;
goToStep(index + 1);
} else {
currentStepIndex = index;
goToStep(index - 1);
}
return;
} else {
breadCrumb.getItem(index).setEnabled(true);
}
// On ne recrée pas l'UI si elle existe
if (newStep.getBody() == null || newStep.getBody().isDisposed())
newStep.createBody(bodyComposite, dataContainer);
// Si la step précendante était avant dans le fil d'execution, on reset les
// valeurs par defaut de la nouvelle step
if (currentStepIndex > index)
currentStep.resetUI();
currentStep = newStep;
stackLayout.topControl = currentStep.getBody();
bodyComposite.layout();
if (index == currentStepIndex + 1)
breadCrumb.goNextItem();
else if (index == currentStepIndex - 1)
breadCrumb.goPrevItem();
else
breadCrumb.goTo(index);
currentStepIndex = index;
menuComposite.layout();
setupBtState();
}
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@Override
protected void cancelPressed() {
if ( ! MessageDialog.openQuestion(this.getShell(), "Attention", "Vous êtes sur de fermer cette fenêtre. "
+ "Toutes les modifications non sauvegardées seront perdues. Vous vraiment continuer ?"))
return;
super.cancelPressed();
}
@Override
protected void buttonPressed(int buttonId) {
if (buttonId == IDialogConstants.BACK_ID) {
previousPressed();
}
super.buttonPressed(buttonId);
}
@Override
protected void createButtonsForButtonBar(Composite parent) {
// pas de bouton par defaut
parent.dispose();
}
@Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText(this.title);
}
@Override
protected Point getInitialSize() {
return new Point(1200, 850);
}
@Override
protected boolean isResizable() {
return true;
}
public boolean isReadOnly() {
return readOnly;
}
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
}
public Button getBtPrevious() {
return btPrevious;
}
public Button getBtNext() {
return btNext;
}
public Button getBtCancel() {
return btCancel;
}
}