Mercurial > projects > dwt2
annotate org.eclipse.jface.snippets/EclipseJfaceSnippets/org/eclipse/jface/snippets/wizard/Snippet047WizardWithLongRunningOperation.d @ 34:a8a838017920
All JFace snippets build on windows
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 23 Mar 2009 15:06:06 +0100 |
parents | c4b36186a9bc |
children |
rev | line source |
---|---|
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; | |
34
a8a838017920
All JFace snippets build on windows
Frank Benoit <benoit@tionex.de>
parents:
32
diff
changeset
|
35 import org.eclipse.jface.operation.IRunnableWithProgress; |
32 | 36 import java.lang.all; |
34
a8a838017920
All JFace snippets build on windows
Frank Benoit <benoit@tionex.de>
parents:
32
diff
changeset
|
37 import java.util.ArrayList; |
32 | 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); | |
34
a8a838017920
All JFace snippets build on windows
Frank Benoit <benoit@tionex.de>
parents:
32
diff
changeset
|
150 dialog.run( true, true, dgIRunnableWithProgress(&mt.threadWork)); |
31 | 151 } |
152 class MyThread { | |
153 private Composite parent; | |
154 private Composite barContainer; | |
155 private Composite comp; | |
156 private ProgressBar bar; | |
157 | |
158 private void threadWork(){ | |
159 if (loadingType == 1) { | |
160 try { | |
32 | 161 Thread.sleep(10_000); |
31 | 162 ArrayList ms = new ArrayList(); |
163 for (int i = 0; i < 10; i++) { | |
164 ms.add(new MyModel(i)); | |
165 } | |
166 | |
167 if (v.getTable().isDisposed()) { | |
168 return; | |
169 } | |
170 | |
171 parent.getDisplay().asyncExec(dgRunnable((ArrayList ms_){ | |
172 v.setInput(ms_); | |
173 (cast(GridData) barContainer.getLayoutData()).exclude = true; | |
174 comp.layout(true); | |
175 }, ms )); | |
176 } catch (InterruptedException e) { | |
177 // TODO Auto-generated catch block | |
178 ExceptionPrintStackTrace(e); | |
179 } | |
180 } else { | |
181 ArrayList ms = new ArrayList(); | |
182 parent.getDisplay().syncExec( dgRunnable( (ArrayList ms_){ | |
183 v.setInput(ms_); | |
184 }, ms )); | |
185 | |
186 for (int i = 0; i < 10; i++) { | |
187 int j = i; | |
188 if (v.getTable().isDisposed()) { | |
189 return; | |
190 } | |
191 parent.getDisplay().asyncExec( dgRunnable( (int j_){ | |
192 MyModel tmp = new MyModel(j_); | |
193 v.add(tmp); | |
194 ms.add(tmp); | |
195 bar.setSelection(j_ + 1); | |
196 }, j )); | |
197 | |
198 try { | |
32 | 199 Thread.sleep(1_000); |
31 | 200 } catch (InterruptedException e) { |
201 // TODO Auto-generated catch block | |
202 ExceptionPrintStackTrace(e); | |
203 } | |
204 } | |
205 | |
206 parent.getDisplay().asyncExec(dgRunnable( { | |
207 | |
208 (cast(GridData) barContainer.getLayoutData()).exclude = true; | |
209 comp.layout(true); | |
210 })); | |
211 } | |
212 | |
213 parent.getDisplay().syncExec(dgRunnable( { | |
214 loading = false; | |
215 getWizard().getContainer().updateButtons(); | |
216 })); | |
217 } | |
218 } | |
219 public bool isPageComplete() { | |
220 return !loading && !v.getSelection().isEmpty(); | |
221 } | |
222 | |
223 } | |
224 | |
225 private static class MyModel { | |
226 private int index; | |
227 | |
228 public this(int index) { | |
229 this.index = index; | |
230 } | |
231 | |
232 public String toString() { | |
233 return Format("Item-{}", index); | |
234 } | |
235 } | |
236 | |
237 static Shell shell; | |
34
a8a838017920
All JFace snippets build on windows
Frank Benoit <benoit@tionex.de>
parents:
32
diff
changeset
|
238 static WizardDialog dialog; |
31 | 239 public static void main(String[] args) { |
240 Display display = new Display(); | |
241 | |
242 shell = new Shell(display); | |
243 shell.setLayout(new FillLayout()); | |
244 | |
32 | 245 Button b = new Button(shell, SWT.PUSH); |
31 | 246 b.setText("Load in one Chunk"); |
247 b.addSelectionListener(new class SelectionAdapter { | |
248 | |
249 public void widgetSelected(SelectionEvent e) { | |
34
a8a838017920
All JFace snippets build on windows
Frank Benoit <benoit@tionex.de>
parents:
32
diff
changeset
|
250 dialog = new WizardDialog(shell, new MyWizard(1)); |
31 | 251 dialog.open(); |
252 } | |
253 | |
254 }); | |
255 | |
32 | 256 b = new Button(shell, SWT.PUSH); |
31 | 257 b.setText("Load Item by Item"); |
258 b.addSelectionListener(new class SelectionAdapter { | |
259 | |
260 public void widgetSelected(SelectionEvent e) { | |
34
a8a838017920
All JFace snippets build on windows
Frank Benoit <benoit@tionex.de>
parents:
32
diff
changeset
|
261 dialog = new WizardDialog(shell, new MyWizard(2)); |
31 | 262 dialog.open(); |
263 } | |
264 | |
265 }); | |
266 | |
267 shell.open(); | |
268 | |
269 while (!shell.isDisposed()) { | |
270 if (!display.readAndDispatch()) | |
271 display.sleep(); | |
272 } | |
273 | |
274 display.dispose(); | |
275 } | |
276 } | |
277 | |
278 void main(){ | |
279 Snippet047WizardWithLongRunningOperation.main(null); | |
280 } |