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
|
/*******************************************************************************
* Copyright (c) 2005, 2018 IBM Corporation and others.
*
* 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 - initial API and implementation
* Jeanderson Candido <http://jeandersonbc.github.io> - Bug 414565
* Lars Vogel <Lars.Vogel@vogella.com> - Bug 475361
*******************************************************************************/
package org.eclipse.jface.snippets.wizard;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
/**
* Example how to load data from a background thread into a TableViewer
*
* @author Tom Schindl <tom.schindl@bestsolution.at>
* @since 1.0
*/
public class Snippet047WizardWithLongRunningOperation {
private static class MyWizard extends Wizard {
private int loadingType;
public MyWizard(int loadingType) {
this.loadingType = loadingType;
}
@Override
public void addPages() {
addPage(new MyWizardPage("Standard Page"));
addPage(new MyWizardPageThread("Thread Page", loadingType));
}
@Override
public boolean performFinish() {
return true;
}
@Override
public boolean canFinish() {
IWizardPage[] pages = getPages();
for (IWizardPage page : pages) {
if (!page.isPageComplete()) {
return false;
}
}
return true;
}
}
private static class MyWizardPage extends WizardPage {
protected MyWizardPage(String pageName) {
super(pageName);
setTitle(pageName);
}
@Override
public void createControl(Composite parent) {
Composite comp = new Composite(parent, SWT.NONE);
setControl(comp);
}
}
private static class MyWizardPageThread extends WizardPage {
private int loadingType;
private boolean loading = true;
private TableViewer v;
protected MyWizardPageThread(String pageName, int loadingType) {
super(pageName);
this.loadingType = loadingType;
setTitle(pageName);
}
@Override
public void createControl(final Composite parent) {
final Composite comp = new Composite(parent, SWT.NONE);
comp.setLayout(new GridLayout(1, false));
v = new TableViewer(comp, SWT.FULL_SELECTION);
v.setContentProvider(ArrayContentProvider.getInstance());
v.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
v.addSelectionChangedListener(event -> getWizard().getContainer().updateButtons());
final Composite barContainer = new Composite(comp, SWT.NONE);
barContainer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
barContainer.setLayout(new GridLayout(2, false));
Label l = new Label(barContainer, SWT.NONE);
l.setText("Loading Data");
final ProgressBar bar = new ProgressBar(barContainer,
(loadingType == 1) ? SWT.INDETERMINATE : SWT.NONE);
bar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
if (loadingType == 2) {
bar.setMaximum(10);
}
setControl(comp);
Thread t = new Thread() {
@Override
public void run() {
final List<MyModel> ms = new ArrayList<>();
if (loadingType == 1) {
try {
Thread.sleep(10000);
for (int i = 0; i < 10; i++) {
ms.add(new MyModel(i));
}
if (v.getTable().isDisposed()) {
return;
}
parent.getDisplay().asyncExec(() -> {
v.setInput(ms);
((GridData) barContainer.getLayoutData()).exclude = true;
comp.layout(true);
});
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
parent.getDisplay().syncExec(() -> v.setInput(ms));
for (int i = 0; i < 10; i++) {
final int j = i;
if (v.getTable().isDisposed()) {
return;
}
parent.getDisplay().asyncExec(() -> {
MyModel tmp = new MyModel(j);
v.add(tmp);
ms.add(tmp);
bar.setSelection(j + 1);
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
parent.getDisplay().asyncExec(() -> {
((GridData) barContainer.getLayoutData()).exclude = true;
comp.layout(true);
});
}
parent.getDisplay().syncExec(() -> {
loading = false;
getWizard().getContainer().updateButtons();
});
}
};
t.start();
}
@Override
public boolean isPageComplete() {
return !loading && !v.getSelection().isEmpty();
}
}
private static class MyModel {
private int index;
public MyModel(int index) {
this.index = index;
}
@Override
public String toString() {
return "Item-" + index;
}
}
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Button b = new Button(shell, SWT.PUSH);
b.setText("Load in one Chunk");
b.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
WizardDialog dialog = new WizardDialog(shell, new MyWizard(1));
dialog.open();
}
});
b = new Button(shell, SWT.PUSH);
b.setText("Load Item by Item");
b.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
WizardDialog dialog = new WizardDialog(shell, new MyWizard(2));
dialog.open();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
|