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: ApplyTemplateWizardStrategy.java
13   * Created: 2009-09-10
14   * Author: entrop
15   * $Id$
16   */
17  
18  package pl.edu.agh.cast.importer.wizard.strategy;
19  
20  import java.util.List;
21  
22  import org.apache.log4j.Logger;
23  import org.eclipse.jface.wizard.Wizard;
24  
25  import pl.edu.agh.cast.data.model.IDataSet;
26  import pl.edu.agh.cast.data.model.domain.IDomainElement;
27  import pl.edu.agh.cast.importer.wizard.Activator;
28  import pl.edu.agh.cast.importer.wizard.ImporterWizard;
29  import pl.edu.agh.cast.importer.wizard.page.AbstractImportWizardPage;
30  import pl.edu.agh.cast.importer.wizard.page.ConversionPreviewPage;
31  import pl.edu.agh.cast.importer.wizard.page.InitImportPage;
32  import pl.edu.agh.cast.importer.wizard.page.ValidationPage;
33  import pl.edu.agh.cast.importer.wizard.util.Messages;
34  import pl.edu.agh.cast.ui.util.MsgBoxHelper;
35  
36  /**
37   * Wizard strategy used for importing data with using chosen template.
38   * 
39   * @author AGH CAST Team
40   */
41  public class ApplyTemplateWizardStrategy implements IImportWizardStrategy {
42  
43  	private ValidationPage validationPage;
44  
45  	private ConversionPreviewPage conversionPreviewPage;
46  
47  	private ImporterWizard importerWizard;
48  
49  	private static final Logger LOG = Activator.getLogger();
50  
51  	/**
52  	 * {@inheritDoc}
53  	 * 
54  	 * @see pl.edu.agh.cast.importer.wizard.strategy.IImportWizardStrategy#addPages(org.eclipse.jface.wizard.Wizard)
55  	 */
56  	@Override
57  	public void addPages(Wizard wizard) {
58  		if (!(wizard instanceof ImporterWizard)) {
59  			throw new IllegalArgumentException("Cannot apply strategy to wizard different " + //$NON-NLS-1$
60  			        "than ImporterWizard instance"); //$NON-NLS-1$
61  		}
62  		importerWizard = (ImporterWizard)wizard;
63  		validationPage = new ValidationPage();
64  		conversionPreviewPage = new ConversionPreviewPage();
65  
66  		wizard.addPage(validationPage);
67  		wizard.addPage(conversionPreviewPage);
68  	}
69  
70  	/**
71  	 * {@inheritDoc}
72  	 * 
73  	 * @see pl.edu.agh.cast.importer.wizard.strategy.IImportWizardStrategy
74  	 *      #getNextPage(pl.edu.agh.cast.importer.wizard.page.AbstractImportWizardPage)
75  	 */
76  	@Override
77  	public AbstractImportWizardPage getNextPage(AbstractImportWizardPage current) {
78  		AbstractImportWizardPage nextPage;
79  
80  		current.refreshImportData();
81  
82  		if (current instanceof InitImportPage) {
83  			nextPage = validationPage;
84  		} else if (current == validationPage) {
85  			nextPage = conversionPreviewPage;
86  		} else {
87  			throw new IllegalArgumentException("Not expected wizard (next) page:" + current.getName()); //$NON-NLS-1$
88  		}
89  		nextPage.initPage();
90  
91  		return nextPage;
92  	}
93  
94  	/**
95  	 * {@inheritDoc}
96  	 * 
97  	 * @see pl.edu.agh.cast.importer.wizard.strategy.IImportWizardStrategy#canFinishWizard()
98  	 */
99  	@Override
100 	public boolean canFinishWizard() {
101 		return conversionPreviewPage.isCurrentPage();
102 	}
103 
104 	/**
105 	 * {@inheritDoc}
106 	 * 
107 	 * @see pl.edu.agh.cast.importer.wizard.strategy.IImportWizardStrategy#performFinish()
108 	 */
109 	@Override
110 	public boolean performFinish() {
111 		boolean result = false;
112 
113 		// TODO copy paste from WithoutTemplateWizardStrategy
114 		importerWizard.performConversion();
115 
116 		final List<IDataSet<IDomainElement>> dataSets = importerWizard.getImportProcess().getConvertedData();
117 
118 		if (dataSets != null) {
119 			// save data set
120 			if (importerWizard.saveDataSet()) {
121 				MsgBoxHelper.showInfoBox(conversionPreviewPage.getShell(),
122 				        Messages.ImporterWizard_ImportSuccessfulTitle, Messages.ImporterWizard_ImportSuccessfulMsg);
123 				LOG.debug("Import successful."); //$NON-NLS-1$
124 			}
125 			result = true;
126 
127 		} else {
128 			MsgBoxHelper.showErrorBox(conversionPreviewPage.getShell(), Messages.ImporterWizard_ImportFailedTitle,
129 			        Messages.ImporterWizard_ImportFailedMsg);
130 			LOG.debug("Import failed."); //$NON-NLS-1$
131 			result = false;
132 		}
133 
134 		return result;
135 	}
136 }