1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.importer.wizard;
19
20 import java.lang.reflect.InvocationTargetException;
21 import java.util.Collection;
22
23 import org.apache.log4j.Logger;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.NullProgressMonitor;
27 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
28 import org.eclipse.jface.operation.IRunnableWithProgress;
29 import org.eclipse.jface.window.Window;
30 import org.eclipse.jface.wizard.Wizard;
31
32 import pl.edu.agh.cast.CastApplication;
33 import pl.edu.agh.cast.CoreServiceLocator;
34 import pl.edu.agh.cast.data.model.IDataSet;
35 import pl.edu.agh.cast.data.model.IElement;
36 import pl.edu.agh.cast.importer.base.process.ImportProcess;
37 import pl.edu.agh.cast.importer.base.util.IImporterPluginRegistryProvider;
38 import pl.edu.agh.cast.importer.base.util.ImporterUtil;
39 import pl.edu.agh.cast.importer.wizard.dialog.DataSetNameDialog;
40 import pl.edu.agh.cast.importer.wizard.page.AbstractConversionRulesSelectionPage;
41 import pl.edu.agh.cast.importer.wizard.page.InitImportPage;
42 import pl.edu.agh.cast.importer.wizard.strategy.IImportWizardStrategy;
43 import pl.edu.agh.cast.importer.wizard.util.IWizardPluginRegistryProvider;
44 import pl.edu.agh.cast.importer.wizard.util.Messages;
45 import pl.edu.agh.cast.importer.wizard.util.WizardUtil;
46 import pl.edu.agh.cast.model.IModelPluginRegistryProvider;
47
48
49
50
51
52
53 public class ImporterWizard extends Wizard {
54
55 private static final int WIZARD_SIZE_HEIGHT = 700;
56
57 private static final int WIZARD_SIZE_WIDTH = 800;
58
59 private final Logger log = Activator.getLogger();
60
61
62
63
64 private ImporterUtil importerUtil;
65
66
67
68
69 private WizardUtil wizardUtil;
70
71
72
73
74 private AbstractConversionRulesSelectionPage currConversionRulesPage;
75
76 private ImportProcess importProcess;
77
78 private IImportWizardStrategy wizardStrategy;
79
80
81
82
83
84
85
86 public ImporterWizard(String projectLocation) {
87 this(null, null, null);
88 importProcess = new ImportProcess();
89 importProcess.setProjectLocation(projectLocation);
90
91 setForcePreviousAndNextButtons(true);
92 }
93
94
95
96
97
98
99
100
101
102
103
104 protected ImporterWizard(IImporterPluginRegistryProvider pluginRegistryProvider,
105 IModelPluginRegistryProvider modelPluginRegistryProvider,
106 IWizardPluginRegistryProvider wizardPluginRegistryProvider) {
107 super();
108 setWindowTitle(Messages.ImporterWizard_WindowTitle);
109 importerUtil = new ImporterUtil(pluginRegistryProvider);
110 wizardUtil = new WizardUtil(wizardPluginRegistryProvider);
111 }
112
113
114
115
116 public void performTokenization() {
117 importProcess.tokenize(new NullProgressMonitor());
118 }
119
120
121
122
123 public void performParsing() {
124 final IRunnableWithProgress rwp = new IRunnableWithProgress() {
125 @Override
126 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
127 importProcess.parse(monitor);
128 }
129 };
130
131 try {
132 new ProgressMonitorDialog(CastApplication.getDisplay().getActiveShell()).run(true, false, rwp);
133 } catch (InvocationTargetException e) {
134 log.error(e.getMessage(), e);
135 } catch (InterruptedException e) {
136 log.error(e.getMessage(), e);
137 }
138 }
139
140
141
142
143 public void performConversion() {
144 final IRunnableWithProgress rwp = new IRunnableWithProgress() {
145 @Override
146 public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
147 importProcess.convert(monitor);
148 }
149 };
150
151 try {
152 new ProgressMonitorDialog(CastApplication.getDisplay().getActiveShell()).run(true, false, rwp);
153 } catch (InvocationTargetException e) {
154 log.error(e.getMessage(), e);
155 } catch (InterruptedException e) {
156 log.error(e.getMessage(), e);
157 }
158
159 }
160
161
162
163
164
165
166 public boolean saveDataSet() {
167 final DataSetNameDialog dsNameDialog = new DataSetNameDialog(getShell(), importProcess.getConvertedData()
168 .get(0).getName());
169 if (dsNameDialog.open() != Window.OK) {
170 return false;
171 }
172
173 boolean res = true;
174 importProcess.getConvertedData().get(0).setName(dsNameDialog.getValue());
175
176 ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
177 try {
178 dialog.run(true, false, new IRunnableWithProgress() {
179
180 @Override
181 @SuppressWarnings("unchecked")
182 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
183 CoreServiceLocator.getPersistenceProvider().saveDataSets(
184 (Collection<IDataSet<? extends IElement>>)(Collection)importProcess.getConvertedData(),
185 monitor);
186
187 }
188
189 });
190 } catch (InvocationTargetException e) {
191 log.error(e.getMessage(), e);
192 res = false;
193 } catch (InterruptedException e) {
194 log.error(e.getMessage(), e);
195 res = false;
196 }
197 return res;
198
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212 @Override
213 public void addPages() {
214 addPage(new InitImportPage());
215 getShell().setSize(WIZARD_SIZE_WIDTH, WIZARD_SIZE_HEIGHT);
216 }
217
218
219
220
221
222
223 @Override
224 public boolean canFinish() {
225 return wizardStrategy != null && wizardStrategy.canFinishWizard();
226 }
227
228
229
230
231
232
233 @Override
234 public boolean performFinish() {
235 return wizardStrategy.performFinish();
236 }
237
238
239
240
241
242
243
244
245
246 private void updateCurrConversionRulesPage() {
247 if (importProcess.getConversionRulesSelectionPageId() != null) {
248 try {
249 currConversionRulesPage = wizardUtil.getConversionRulesSelectionPageInstance(importProcess
250 .getConversionRulesSelectionPageId());
251 } catch (CoreException e) {
252 String errorMsg = "CoreException while retrieving instance of conversion rule selection "
253 + "page for model id: " + importProcess.getConversionRulesSelectionPageId();
254 log.error(errorMsg, e);
255 throw new RuntimeException(errorMsg, e);
256 }
257 }
258 }
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274 public void setImportStrategy(IImportWizardStrategy strategy) {
275 if (strategy != wizardStrategy) {
276 final ImportProcess newProcess = new ImportProcess();
277 newProcess.setFilePath(importProcess.getFilePath());
278 newProcess.setProjectLocation(importProcess.getProjectLocation());
279 importProcess = newProcess;
280
281 this.wizardStrategy = strategy;
282 wizardStrategy.addPages(this);
283 }
284 }
285
286 public ImportProcess getImportProcess() {
287 return importProcess;
288 }
289
290 public ImporterUtil getImporterUtil() {
291 return this.importerUtil;
292 }
293
294 public WizardUtil getWizardUtil() {
295 return this.wizardUtil;
296 }
297
298
299
300
301
302
303 public AbstractConversionRulesSelectionPage getCurrConversionRulesPage() {
304 updateCurrConversionRulesPage();
305 return this.currConversionRulesPage;
306 }
307
308 public IImportWizardStrategy getWizardStrategy() {
309 return wizardStrategy;
310 }
311
312
313
314 }