1 /*******************************************************************************
2  * Copyright (c) 2005, 2018 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     Jeanderson Candido <http://jeandersonbc.github.io> - Bug 414565
14  *     Lars Vogel <Lars.Vogel@vogella.com> - Bug 475361
15  *******************************************************************************/
16 package org.eclipse.jface.snippets.wizard;
17 
18 import java.util.ArrayList;
19 import java.util.List;
20 
21 import org.eclipse.jface.viewers.ArrayContentProvider;
22 import org.eclipse.jface.viewers.TableViewer;
23 import org.eclipse.jface.wizard.IWizardPage;
24 import org.eclipse.jface.wizard.Wizard;
25 import org.eclipse.jface.wizard.WizardDialog;
26 import org.eclipse.jface.wizard.WizardPage;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.layout.FillLayout;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Display;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.ProgressBar;
38 import org.eclipse.swt.widgets.Shell;
39 
40 /**
41  * Example how to load data from a background thread into a TableViewer
42  *
43  * @author Tom Schindl &lt;tom.schindl@bestsolution.at&gt;
44  * @since 1.0
45  */
46 public class Snippet047WizardWithLongRunningOperation {
47 
48 	private static class MyWizard extends Wizard {
49 
50 		private int loadingType;
51 
MyWizard(int loadingType)52 		public MyWizard(int loadingType) {
53 			this.loadingType = loadingType;
54 		}
55 
56 		@Override
addPages()57 		public void addPages() {
58 			addPage(new MyWizardPage("Standard Page"));
59 			addPage(new MyWizardPageThread("Thread Page", loadingType));
60 		}
61 
62 		@Override
performFinish()63 		public boolean performFinish() {
64 			return true;
65 		}
66 
67 		@Override
canFinish()68 		public boolean canFinish() {
69 			IWizardPage[] pages = getPages();
70 			for (IWizardPage page : pages) {
71 				if (!page.isPageComplete()) {
72 					return false;
73 				}
74 			}
75 			return true;
76 		}
77 
78 	}
79 
80 	private static class MyWizardPage extends WizardPage {
81 
MyWizardPage(String pageName)82 		protected MyWizardPage(String pageName) {
83 			super(pageName);
84 			setTitle(pageName);
85 		}
86 
87 		@Override
createControl(Composite parent)88 		public void createControl(Composite parent) {
89 			Composite comp = new Composite(parent, SWT.NONE);
90 			setControl(comp);
91 		}
92 	}
93 
94 	private static class MyWizardPageThread extends WizardPage {
95 		private int loadingType;
96 		private boolean loading = true;
97 		private TableViewer v;
98 
MyWizardPageThread(String pageName, int loadingType)99 		protected MyWizardPageThread(String pageName, int loadingType) {
100 			super(pageName);
101 			this.loadingType = loadingType;
102 			setTitle(pageName);
103 		}
104 
105 		@Override
createControl(final Composite parent)106 		public void createControl(final Composite parent) {
107 			final Composite comp = new Composite(parent, SWT.NONE);
108 			comp.setLayout(new GridLayout(1, false));
109 
110 			v = new TableViewer(comp, SWT.FULL_SELECTION);
111 			v.setContentProvider(new ArrayContentProvider());
112 			v.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
113 			v.addSelectionChangedListener(event -> getWizard().getContainer().updateButtons());
114 
115 			final Composite barContainer = new Composite(comp, SWT.NONE);
116 			barContainer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
117 			barContainer.setLayout(new GridLayout(2, false));
118 
119 			Label l = new Label(barContainer, SWT.NONE);
120 			l.setText("Loading Data");
121 
122 			final ProgressBar bar = new ProgressBar(barContainer,
123 					(loadingType == 1) ? SWT.INDETERMINATE : SWT.NONE);
124 			bar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
125 
126 			if (loadingType == 2) {
127 				bar.setMaximum(10);
128 			}
129 
130 			setControl(comp);
131 
132 			Thread t = new Thread() {
133 
134 				@Override
135 				public void run() {
136 					final List<MyModel> ms = new ArrayList<>();
137 					if (loadingType == 1) {
138 						try {
139 							Thread.sleep(10000);
140 							for (int i = 0; i < 10; i++) {
141 								ms.add(new MyModel(i));
142 							}
143 							if (v.getTable().isDisposed()) {
144 								return;
145 							}
146 							parent.getDisplay().asyncExec(() -> {
147 								v.setInput(ms);
148 								((GridData) barContainer.getLayoutData()).exclude = true;
149 								comp.layout(true);
150 							});
151 						} catch (InterruptedException e) {
152 							e.printStackTrace();
153 						}
154 
155 					} else {
156 						parent.getDisplay().syncExec(() -> v.setInput(ms));
157 
158 						for (int i = 0; i < 10; i++) {
159 							final int j = i;
160 							if (v.getTable().isDisposed()) {
161 								return;
162 							}
163 							parent.getDisplay().asyncExec(() -> {
164 								MyModel tmp = new MyModel(j);
165 								v.add(tmp);
166 								ms.add(tmp);
167 								bar.setSelection(j + 1);
168 							});
169 
170 							try {
171 								Thread.sleep(1000);
172 							} catch (InterruptedException e) {
173 								e.printStackTrace();
174 							}
175 						}
176 
177 						parent.getDisplay().asyncExec(() -> {
178 							((GridData) barContainer.getLayoutData()).exclude = true;
179 							comp.layout(true);
180 						});
181 					}
182 
183 					parent.getDisplay().syncExec(() -> {
184 						loading = false;
185 						getWizard().getContainer().updateButtons();
186 					});
187 				}
188 
189 			};
190 			t.start();
191 		}
192 
193 		@Override
isPageComplete()194 		public boolean isPageComplete() {
195 			return !loading && !v.getSelection().isEmpty();
196 		}
197 
198 	}
199 
200 	private static class MyModel {
201 		private int index;
202 
MyModel(int index)203 		public MyModel(int index) {
204 			this.index = index;
205 		}
206 
207 		@Override
toString()208 		public String toString() {
209 			return "Item-" + index;
210 		}
211 	}
212 
main(String[] args)213 	public static void main(String[] args) {
214 		Display display = new Display();
215 
216 		final Shell shell = new Shell(display);
217 		shell.setLayout(new FillLayout());
218 
219 		Button b = new Button(shell, SWT.PUSH);
220 		b.setText("Load in one Chunk");
221 		b.addSelectionListener(new SelectionAdapter() {
222 
223 			@Override
224 			public void widgetSelected(SelectionEvent e) {
225 				WizardDialog dialog = new WizardDialog(shell, new MyWizard(1));
226 				dialog.open();
227 			}
228 
229 		});
230 
231 		b = new Button(shell, SWT.PUSH);
232 		b.setText("Load Item by Item");
233 		b.addSelectionListener(new SelectionAdapter() {
234 
235 			@Override
236 			public void widgetSelected(SelectionEvent e) {
237 				WizardDialog dialog = new WizardDialog(shell, new MyWizard(2));
238 				dialog.open();
239 			}
240 
241 		});
242 
243 		shell.open();
244 
245 		while (!shell.isDisposed()) {
246 			if (!display.readAndDispatch())
247 				display.sleep();
248 		}
249 		display.dispose();
250 	}
251 }
252