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