View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: ImporterWizard.java
13   * Created: 2009-03-13
14   * Author: bmilos
15   * $Id$
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   * The wizard, which leads the users through an import data process, and which may also include a new template creation.
50   * 
51   * @author AGH CAST Team
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  	 * The importer utility class.
63  	 */
64  	private ImporterUtil importerUtil;
65  
66  	/**
67  	 * The wizard utility class.
68  	 */
69  	private WizardUtil wizardUtil;
70  
71  	/**
72  	 * The conversion rules page currently present in the import wizard.
73  	 */
74  	private AbstractConversionRulesSelectionPage currConversionRulesPage;
75  
76  	private ImportProcess importProcess;
77  
78  	private IImportWizardStrategy wizardStrategy;
79  
80  	/**
81  	 * The default constructor.
82  	 * 
83  	 * @param projectLocation
84  	 *            project location
85  	 */
86  	public ImporterWizard(String projectLocation) {
87  		this(null, null, null); // use default providers
88  		importProcess = new ImportProcess();
89  		importProcess.setProjectLocation(projectLocation);
90  
91  		setForcePreviousAndNextButtons(true);
92  	}
93  
94  	/**
95  	 * The constructor - for test use only.
96  	 * 
97  	 * @param pluginRegistryProvider
98  	 *            the importer plugin registry provider
99  	 * @param modelPluginRegistryProvider
100 	 *            the model plugin registry provider
101 	 * @param wizardPluginRegistryProvider
102 	 *            the wizard plugin registry provider
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 	 * Performs data tokenization with the data currently configured.
115 	 */
116 	public void performTokenization() {
117 		importProcess.tokenize(new NullProgressMonitor());
118 	}
119 
120 	/**
121 	 * Performs data parsing with the data currently configured and the analyzers specified.
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 	 * Performs data conversion with the data and converter previously configured.
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 	 * Saves converted data sets using persistence provider.
163 	 * 
164 	 * @return true if data sets have been properly saved; false if any error occurred or user cancel
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 	 * --------------------------OVERRIDED METHODS-------------------------------------------------------
204 	 * -----------------------------------------------------------------------------------------------------------
205 	 */
206 
207 	/**
208 	 * {@inheritDoc}
209 	 * 
210 	 * @see org.eclipse.jface.wizard.Wizard#addPages()
211 	 */
212 	@Override
213 	public void addPages() {
214 		addPage(new InitImportPage());
215 		getShell().setSize(WIZARD_SIZE_WIDTH, WIZARD_SIZE_HEIGHT);
216 	}
217 
218 	/**
219 	 * {@inheritDoc}
220 	 * 
221 	 * @see org.eclipse.jface.wizard.Wizard#canFinish()
222 	 */
223 	@Override
224 	public boolean canFinish() {
225 		return wizardStrategy != null && wizardStrategy.canFinishWizard();
226 	}
227 
228 	/**
229 	 * {@inheritDoc}
230 	 * 
231 	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
232 	 */
233 	@Override
234 	public boolean performFinish() {
235 		return wizardStrategy.performFinish();
236 	}
237 
238 	/*----------------------------------------------------------------------------------------------------------*/
239 
240 	/*
241 	 * -----------------------------------------------------------------------------------------------------------
242 	 * --------------------------HELPER METHODS------------------------------------------------------------
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 " //$NON-NLS-1$
253 				        + "page for model id: " + importProcess.getConversionRulesSelectionPageId(); //$NON-NLS-1$
254 				log.error(errorMsg, e);
255 				throw new RuntimeException(errorMsg, e);
256 			}
257 		}
258 	}
259 
260 	/*----------------------------------------------------------------------------------------------------------*/
261 
262 	/*
263 	 * -----------------------------------------------------------------------------------------------------------
264 	 * --------------------------GETTERS AND SETTERS------------------------------------------------------
265 	 * -----------------------------------------------------------------------------------------------------------
266 	 */
267 
268 	/**
269 	 * Sets new import strategy. When strategy is changed new import process instance is created.
270 	 * 
271 	 * @param strategy
272 	 *            import strategy to set
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 	 * Returns rules page for actual selected model.
300 	 * 
301 	 * @return current conversion rules page
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 }