31
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2005, 2007 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module jface.snippets.wizard.Snippet047WizardWithLongRunningOperation;
|
|
14
|
32
|
15 import org.eclipse.jface.viewers.ArrayContentProvider;
|
|
16 import org.eclipse.jface.viewers.ISelectionChangedListener;
|
|
17 import org.eclipse.jface.viewers.SelectionChangedEvent;
|
|
18 import org.eclipse.jface.viewers.TableViewer;
|
|
19 import org.eclipse.jface.wizard.IWizardPage;
|
|
20 import org.eclipse.jface.wizard.Wizard;
|
|
21 import org.eclipse.jface.wizard.WizardDialog;
|
|
22 import org.eclipse.jface.wizard.WizardPage;
|
|
23 import org.eclipse.swt.SWT;
|
|
24 import org.eclipse.swt.events.SelectionAdapter;
|
|
25 import org.eclipse.swt.events.SelectionEvent;
|
|
26 import org.eclipse.swt.layout.FillLayout;
|
|
27 import org.eclipse.swt.layout.GridData;
|
|
28 import org.eclipse.swt.layout.GridLayout;
|
|
29 import org.eclipse.swt.widgets.Button;
|
|
30 import org.eclipse.swt.widgets.Composite;
|
|
31 import org.eclipse.swt.widgets.Display;
|
|
32 import org.eclipse.swt.widgets.Label;
|
|
33 import org.eclipse.swt.widgets.ProgressBar;
|
|
34 import org.eclipse.swt.widgets.Shell;
|
31
|
35
|
32
|
36 import java.lang.all;
|
|
37 import java.utils.ArrayList;
|
|
38
|
|
39 import java.lang.Thread;
|
31
|
40 import tango.text.convert.Format;
|
|
41
|
32
|
42 import org.eclipse.jface.operation.ModalContext;
|
31
|
43
|
|
44 /**
|
|
45 * Example how to load data from a background thread into a TableViewer
|
|
46 *
|
|
47 * @author Tom Schindl <tom.schindl@bestsolution.at>
|
|
48 * @since 1.0
|
|
49 */
|
|
50 public class Snippet047WizardWithLongRunningOperation {
|
|
51
|
|
52 private static class MyWizard : Wizard {
|
|
53
|
|
54 private int loadingType;
|
|
55
|
|
56 public this(int loadingType) {
|
|
57 this.loadingType = loadingType;
|
|
58 }
|
|
59
|
|
60 /*
|
|
61 * (non-Javadoc)
|
|
62 *
|
32
|
63 * @see org.eclipse.jface.wizard.Wizard#addPages()
|
31
|
64 */
|
|
65 public void addPages() {
|
|
66 addPage(new MyWizardPageThread("Thread Page", loadingType));
|
|
67 addPage(new MyWizardPage("Standard Page"));
|
|
68 }
|
|
69
|
|
70 public bool performFinish() {
|
|
71 return true;
|
|
72 }
|
|
73
|
|
74 /*
|
|
75 * (non-Javadoc)
|
|
76 *
|
32
|
77 * @see org.eclipse.jface.wizard.Wizard#canFinish()
|
31
|
78 */
|
|
79 public bool canFinish() {
|
|
80 IWizardPage[] pages = getPages();
|
|
81 for (int i = 0; i < pages.length; i++) {
|
|
82 if (!pages[i].isPageComplete()) {
|
|
83 return false;
|
|
84 }
|
|
85 }
|
|
86
|
|
87 return true;
|
|
88 }
|
|
89
|
|
90 };
|
|
91
|
|
92 private static class MyWizardPage : WizardPage {
|
|
93
|
|
94 protected this(String pageName) {
|
|
95 super(pageName);
|
|
96 setTitle(pageName);
|
|
97 }
|
|
98
|
|
99 public /+override+/ void createControl(Composite parent) {
|
32
|
100 Composite comp = new Composite(parent, SWT.NONE);
|
31
|
101 setControl(comp);
|
|
102 }
|
|
103 }
|
|
104
|
|
105 private static class MyWizardPageThread : WizardPage {
|
|
106 private int loadingType;
|
|
107 private bool loading = true;
|
|
108 private TableViewer v;
|
|
109
|
|
110 protected this(String pageName, int loadingType) {
|
|
111 super(pageName);
|
|
112 this.loadingType = loadingType;
|
|
113 setTitle(pageName);
|
|
114 }
|
|
115
|
|
116 public /+override+/ void createControl(Composite parent) {
|
|
117 auto mt = new MyThread();
|
|
118 mt.parent = parent;
|
|
119
|
32
|
120 mt.comp = new Composite(parent, SWT.NONE);
|
31
|
121 mt.comp.setLayout(new GridLayout(1, false));
|
|
122
|
32
|
123 v = new TableViewer(mt.comp, SWT.FULL_SELECTION);
|
31
|
124 v.setContentProvider(new ArrayContentProvider!(Object)());
|
|
125 v.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
|
|
126 v.addSelectionChangedListener(new class ISelectionChangedListener {
|
|
127
|
|
128 public void selectionChanged(SelectionChangedEvent event) {
|
|
129 getWizard().getContainer().updateButtons();
|
|
130 }
|
|
131
|
|
132 });
|
|
133
|
32
|
134 mt.barContainer = new Composite(mt.comp, SWT.NONE);
|
31
|
135 mt.barContainer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
|
|
136 mt.barContainer.setLayout(new GridLayout(2, false));
|
|
137
|
32
|
138 Label l = new Label(mt.barContainer, SWT.NONE);
|
31
|
139 l.setText("Loading Data");
|
|
140
|
|
141 mt.bar = new ProgressBar(mt.barContainer,
|
32
|
142 (loadingType == 1) ? SWT.INDETERMINATE : SWT.NONE);
|
31
|
143 mt.bar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
|
|
144
|
|
145 if (loadingType == 2) {
|
|
146 mt.bar.setMaximum(10);
|
|
147 }
|
|
148
|
|
149 setControl(mt.comp);
|
|
150
|
|
151 ModalContext.run( dgRunnable(&mt.threadWork), true, getProgressMonitor(), getShell().getDisplay() );
|
|
152
|
|
153 //Thread t = new Thread(&mt.threadWork);
|
|
154
|
|
155 // t.start();
|
|
156 }
|
|
157 class MyThread {
|
|
158 private Composite parent;
|
|
159 private Composite barContainer;
|
|
160 private Composite comp;
|
|
161 private ProgressBar bar;
|
|
162
|
|
163 private void threadWork(){
|
|
164 if (loadingType == 1) {
|
|
165 try {
|
32
|
166 Thread.sleep(10_000);
|
31
|
167 ArrayList ms = new ArrayList();
|
|
168 for (int i = 0; i < 10; i++) {
|
|
169 ms.add(new MyModel(i));
|
|
170 }
|
|
171
|
|
172 if (v.getTable().isDisposed()) {
|
|
173 return;
|
|
174 }
|
|
175
|
|
176 parent.getDisplay().asyncExec(dgRunnable((ArrayList ms_){
|
|
177 v.setInput(ms_);
|
|
178 (cast(GridData) barContainer.getLayoutData()).exclude = true;
|
|
179 comp.layout(true);
|
|
180 }, ms ));
|
|
181 } catch (InterruptedException e) {
|
|
182 // TODO Auto-generated catch block
|
|
183 ExceptionPrintStackTrace(e);
|
|
184 }
|
|
185 } else {
|
|
186 ArrayList ms = new ArrayList();
|
|
187 parent.getDisplay().syncExec( dgRunnable( (ArrayList ms_){
|
|
188 v.setInput(ms_);
|
|
189 }, ms ));
|
|
190
|
|
191 for (int i = 0; i < 10; i++) {
|
|
192 int j = i;
|
|
193 if (v.getTable().isDisposed()) {
|
|
194 return;
|
|
195 }
|
|
196 parent.getDisplay().asyncExec( dgRunnable( (int j_){
|
|
197 MyModel tmp = new MyModel(j_);
|
|
198 v.add(tmp);
|
|
199 ms.add(tmp);
|
|
200 bar.setSelection(j_ + 1);
|
|
201 }, j ));
|
|
202
|
|
203 try {
|
32
|
204 Thread.sleep(1_000);
|
31
|
205 } catch (InterruptedException e) {
|
|
206 // TODO Auto-generated catch block
|
|
207 ExceptionPrintStackTrace(e);
|
|
208 }
|
|
209 }
|
|
210
|
|
211 parent.getDisplay().asyncExec(dgRunnable( {
|
|
212
|
|
213 (cast(GridData) barContainer.getLayoutData()).exclude = true;
|
|
214 comp.layout(true);
|
|
215 }));
|
|
216 }
|
|
217
|
|
218 parent.getDisplay().syncExec(dgRunnable( {
|
|
219 loading = false;
|
|
220 getWizard().getContainer().updateButtons();
|
|
221 }));
|
|
222 }
|
|
223 }
|
|
224 public bool isPageComplete() {
|
|
225 return !loading && !v.getSelection().isEmpty();
|
|
226 }
|
|
227
|
|
228 }
|
|
229
|
|
230 private static class MyModel {
|
|
231 private int index;
|
|
232
|
|
233 public this(int index) {
|
|
234 this.index = index;
|
|
235 }
|
|
236
|
|
237 public String toString() {
|
|
238 return Format("Item-{}", index);
|
|
239 }
|
|
240 }
|
|
241
|
|
242 static Shell shell;
|
|
243 public static void main(String[] args) {
|
|
244 Display display = new Display();
|
|
245
|
|
246 shell = new Shell(display);
|
|
247 shell.setLayout(new FillLayout());
|
|
248
|
32
|
249 Button b = new Button(shell, SWT.PUSH);
|
31
|
250 b.setText("Load in one Chunk");
|
|
251 b.addSelectionListener(new class SelectionAdapter {
|
|
252
|
|
253 public void widgetSelected(SelectionEvent e) {
|
|
254 WizardDialog dialog = new WizardDialog(shell, new MyWizard(1));
|
|
255 dialog.open();
|
|
256 }
|
|
257
|
|
258 });
|
|
259
|
32
|
260 b = new Button(shell, SWT.PUSH);
|
31
|
261 b.setText("Load Item by Item");
|
|
262 b.addSelectionListener(new class SelectionAdapter {
|
|
263
|
|
264 public void widgetSelected(SelectionEvent e) {
|
|
265 WizardDialog dialog = new WizardDialog(shell, new MyWizard(2));
|
|
266 dialog.open();
|
|
267 }
|
|
268
|
|
269 });
|
|
270
|
|
271 shell.open();
|
|
272
|
|
273 while (!shell.isDisposed()) {
|
|
274 if (!display.readAndDispatch())
|
|
275 display.sleep();
|
|
276 }
|
|
277
|
|
278 display.dispose();
|
|
279 }
|
|
280 }
|
|
281
|
|
282 void main(){
|
|
283 Snippet047WizardWithLongRunningOperation.main(null);
|
|
284 }
|