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: InitImportPage.java
13   * Created: 2009-03-09
14   * Author: bmilos
15   * $Id$
16   */
17  
18  package pl.edu.agh.cast.importer.wizard.page;
19  
20  import java.io.File;
21  
22  import org.eclipse.jface.wizard.IWizardPage;
23  import org.eclipse.swt.SWT;
24  import org.eclipse.swt.widgets.Composite;
25  
26  import pl.edu.agh.cast.importer.base.data.DataRow;
27  import pl.edu.agh.cast.importer.base.data.ITabularData;
28  import pl.edu.agh.cast.importer.base.template.IImportTemplate;
29  import pl.edu.agh.cast.importer.base.util.ConversionPreviewGenerator;
30  import pl.edu.agh.cast.importer.wizard.strategy.ApplyTemplateWizardStrategy;
31  import pl.edu.agh.cast.importer.wizard.strategy.CreateTemplateWizardStrategy;
32  import pl.edu.agh.cast.importer.wizard.strategy.IImportWizardStrategy;
33  import pl.edu.agh.cast.importer.wizard.strategy.UseTemplateWizardStrategy;
34  import pl.edu.agh.cast.importer.wizard.strategy.WithoutTemplateWizardStrategy;
35  import pl.edu.agh.cast.importer.wizard.util.Messages;
36  
37  /**
38   * Page of the import wizard, which serves for selection of data to import, as well as selection of whether an existing
39   * template is to be used or applied or neither.
40   * 
41   * @author AGH CAST Team
42   */
43  public class InitImportPage extends AbstractImportWizardPage {
44  	private InitImportPageComposite composite;
45  
46  	private WithoutTemplateWizardStrategy withoutTemplateStrategy = new WithoutTemplateWizardStrategy();
47  
48  	private ApplyTemplateWizardStrategy applyTemplateStrategy = new ApplyTemplateWizardStrategy();
49  
50  	private UseTemplateWizardStrategy useTemplateStrategy = new UseTemplateWizardStrategy();
51  
52  	private CreateTemplateWizardStrategy createTemplateStrategy = new CreateTemplateWizardStrategy();
53  
54  	/**
55  	 * The default constructor.
56  	 */
57  	public InitImportPage() {
58  		super(Messages.ImporterWizard_InitImportPageTitle, Messages.ImporterWizard_InitImportPageDescription);
59  	}
60  
61  	/**
62  	 * {@inheritDoc}
63  	 * 
64  	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
65  	 */
66  	public void createControl(Composite parent) {
67  		composite = new InitImportPageComposite(parent, SWT.NULL, this);
68  		setControl(composite);
69  	}
70  
71  	protected boolean isComplete() {
72  		if (!isImportFileValid()) {
73  			return false;
74  		}
75  		if (getSelectedImportStrategy() == applyTemplateStrategy && getImportProcess().getTemplate() == null) {
76  			return false;
77  		}
78  		return true;
79  	}
80  
81  	/*
82  	 * -----------------------------------------------------------------------------------------------------------
83  	 * --------------------------OVERRIDED METHODS-------------------------------------------------------
84  	 * -----------------------------------------------------------------------------------------------------------
85  	 */
86  
87  	/**
88  	 * {@inheritDoc}
89  	 * 
90  	 * @see pl.edu.agh.cast.importer.wizard.page.AbstractImportWizardPage#getNextPage()
91  	 */
92  	@Override
93  	public IWizardPage getNextPage() {
94  		getImportWizard().setImportStrategy(getSelectedImportStrategy());
95  		return super.getNextPage();
96  	}
97  
98  	/*----------------------------------------------------------------------------------------------------------*/
99  
100 	/*
101 	 * -----------------------------------------------------------------------------------------------------------
102 	 * --------------------------HELPER METHODS------------------------------------------------------------
103 	 * -----------------------------------------------------------------------------------------------------------
104 	 */
105 
106 	/**
107 	 * Returns selected import strategy.
108 	 * 
109 	 * @return selected strategy
110 	 */
111 	public IImportWizardStrategy getSelectedImportStrategy() {
112 		IImportWizardStrategy strategy;
113 
114 		if (composite == null) {
115 			return null;
116 		}
117 
118 		if (composite.isImportWithoutTemplateSelected()) {
119 			strategy = withoutTemplateStrategy;
120 		} else if (composite.isImportUseTemplateSelected()) {
121 			strategy = useTemplateStrategy;
122 		} else if (composite.isImportApplyTemplateSelected()) {
123 			strategy = applyTemplateStrategy;
124 		} else if (composite.isCreateTemplateSelected()) {
125 			strategy = createTemplateStrategy;
126 		} else {
127 			throw new IllegalStateException("One of import strategies must be selected"); //$NON-NLS-1$
128 		}
129 
130 		return strategy;
131 	}
132 
133 	/**
134 	 * Method called by the <code>InitImportPageComposite</code> when a template is selected.
135 	 * 
136 	 * @param selectedTemplate
137 	 *            the selected template
138 	 */
139 	public void notifyTemplateSelection(IImportTemplate selectedTemplate) {
140 		getImportProcess().setTemplate(selectedTemplate);
141 		widgetModified();
142 	}
143 
144 	/**
145 	 * Method called by the <code>InitImportPageComposite</code> when a filePath has changed.
146 	 * 
147 	 * @param filePath
148 	 *            new file path value
149 	 */
150 	public void notifyFilePathChange(String filePath) {
151 		getImportProcess().setFilePath(filePath);
152 		widgetModified();
153 	}
154 
155 	/**
156 	 * Method called by the <code>InitImportPageComposite</code> when a wizard strategy has changed.
157 	 */
158 	public void notifyWizardStrategySelection() {
159 		getImportWizard().setImportStrategy(getSelectedImportStrategy());
160 		widgetModified();
161 	}
162 
163 	/**
164 	 * Creates and returns conversion preview.
165 	 * 
166 	 * @return conversion preview
167 	 */
168 	public ITabularData<DataRow> getTemplateConversionPreview() {
169 		if (getImportProcess().getTemplate() != null && getImportProcess().getFilePath() != null) {
170 			final ConversionPreviewGenerator previewGenerator = new ConversionPreviewGenerator();
171 			ITabularData<DataRow> preview = previewGenerator.generatePreview(getImportProcess().getTemplate(),
172 			        getImportProcess().getFilePath());
173 
174 			return preview;
175 		}
176 		return null;
177 	}
178 
179 	/**
180 	 * Checks whether a specified import file is valid.
181 	 * 
182 	 * @return <code>true</code> is the specified file is valid; <code>false</code> otherwise
183 	 */
184 	private boolean isImportFileValid() {
185 		if (getImportProcess().getFilePath() != null && new File(getImportProcess().getFilePath()).isFile()) {
186 			return true;
187 		}
188 		return false;
189 	}
190 
191 	/*----------------------------------------------------------------------------------------------------------*/
192 
193 }