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: FileParamsSelectionPage.java
13   * Created: 2009-03-19
14   * Author: bmilos
15   * $Id$
16   */
17  
18  package pl.edu.agh.cast.importer.wizard.page;
19  
20  import java.io.IOException;
21  import java.nio.charset.Charset;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.log4j.Logger;
27  import org.eclipse.core.runtime.CoreException;
28  import org.eclipse.jface.viewers.ArrayContentProvider;
29  import org.eclipse.jface.viewers.ComboViewer;
30  import org.eclipse.jface.viewers.LabelProvider;
31  import org.eclipse.swt.SWT;
32  import org.eclipse.swt.widgets.Combo;
33  import org.eclipse.swt.widgets.Composite;
34  
35  import pl.edu.agh.cast.Activator;
36  import pl.edu.agh.cast.importer.base.data.RawTabularData;
37  import pl.edu.agh.cast.importer.base.tokenizer.IImportTokenizer;
38  import pl.edu.agh.cast.importer.base.tokenizer.ITokenizerOption;
39  import pl.edu.agh.cast.importer.base.util.ImporterUtil;
40  import pl.edu.agh.cast.importer.base.util.TokenizerInfo;
41  import pl.edu.agh.cast.importer.wizard.util.ConversionRulesPageInfo;
42  import pl.edu.agh.cast.importer.wizard.util.Messages;
43  
44  /**
45   * Page of the import wizard, which serves for initial selection of all basic information that are required by the
46   * import process, such as selection of the data file format, domain model and encoding.
47   * 
48   * @author AGH CAST Team
49   */
50  public class FileParamsSelectionPage extends AbstractImportWizardPage {
51  
52  	private FileParamsSelectionPageComposite composite;
53  
54  	private List<TokenizerInfo> tokenizerInfos;
55  
56  	private List<ConversionRulesPageInfo> conversionRulesPageInfos;
57  
58  	private Logger log = Activator.getLogger();
59  
60  	private IImportTokenizer importTokenizer;
61  
62  	/**
63  	 * The default constructor.
64  	 */
65  	public FileParamsSelectionPage() {
66  		super(Messages.ImporterWizard_FileParamsSelectionPageTitle,
67  		        Messages.ImporterWizard_FileParamsSelectionPageDescription);
68  	}
69  
70  	/**
71  	 * {@inheritDoc}
72  	 * 
73  	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
74  	 */
75  	public void createControl(Composite parent) {
76  		composite = new FileParamsSelectionPageComposite(parent, SWT.NULL, this);
77  		setControl(composite);
78  
79  		refreshSelectedData();
80  	}
81  
82  	protected boolean isComplete() {
83  		return composite.getSelectedConversionRulesSelectionPageInfo() != null
84  		        && composite.getSelectedFormatId() != null;
85  	}
86  
87  	/*
88  	 * -----------------------------------------------------------------------------------------------------------
89  	 * --------------------------OVERRIDED METHODS-------------------------------------------------------
90  	 * -----------------------------------------------------------------------------------------------------------
91  	 */
92  
93  	/**
94  	 * {@inheritDoc}
95  	 * 
96  	 * @see pl.edu.agh.cast.importer.wizard.page.AbstractImportWizardPage#initPage()
97  	 */
98  	@Override
99  	public void initPage() {
100 		refreshSelectedData();
101 	}
102 
103 	/**
104 	 * {@inheritDoc}
105 	 * 
106 	 * @see pl.edu.agh.cast.importer.wizard.page.AbstractImportWizardPage#refreshImportData()
107 	 */
108 	@Override
109 	public void refreshImportData() {
110 		if (!isComplete()) {
111 			return;
112 		}
113 		final String formatId = composite.getSelectedFormatId();
114 
115 		getImportProcess().setConversionRulesSelectionPageId(
116 		        composite.getSelectedConversionRulesSelectionPageInfo().getId());
117 
118 		if (formatId != null) {
119 			importTokenizer = null;
120 			try {
121 				importTokenizer = getImporterUtil().getTokenizerInstance(formatId);
122 			} catch (CoreException e) {
123 				final String errorMsg = "CoreException while retrieving instance of tokenizer with id: " //$NON-NLS-1$
124 				        + formatId;
125 				log.error(errorMsg, e);
126 				throw new RuntimeException(errorMsg, e);
127 			}
128 
129 			importTokenizer.setEncoding(composite.getSelectedEncoding());
130 			getImportProcess().setTokenizer(importTokenizer);
131 		}
132 
133 	}
134 
135 	/*----------------------------------------------------------------------------------------------------------*/
136 
137 	/*
138 	 * -----------------------------------------------------------------------------------------------------------
139 	 * --------------------------HELPER METHODS------------------------------------------------------------
140 	 * -----------------------------------------------------------------------------------------------------------
141 	 */
142 
143 	/**
144 	 * Loads a data preview by importing a given amount of rows (<code>PREVIEW_ROWS_LIMIT</code>) of a specified data
145 	 * file, with use of a tokenizer of a specified identifier and its options.
146 	 * 
147 	 * <p>
148 	 * TODO: now the method loads a preview of only a single tabular data; future versions must include tabs for every
149 	 * tokenized tabular data
150 	 * 
151 	 *@param fileEncoding
152 	 *            the file selected file encoding
153 	 * @param tokenizerOptions
154 	 *            the options for the tokenizer
155 	 * @param previewRowsLimit
156 	 *            the limit of rows previewed
157 	 * 
158 	 * @return the raw tabular data ready for preview
159 	 */
160 	public RawTabularData loadPreview(Charset fileEncoding, List<ITokenizerOption> tokenizerOptions,
161 	        int previewRowsLimit) {
162 		try {
163 			importTokenizer = getImporterUtil().getTokenizerInstance(composite.getSelectedFormatId());
164 			importTokenizer.setEncoding(fileEncoding);
165 			importTokenizer.setTokenizerOptions(tokenizerOptions);
166 			getImportProcess().setTokenizer(importTokenizer);
167 			return getImportProcess().getTokenizePreview();
168 		} catch (CoreException e) {
169 			log.debug("Exception while loading file preview", e); //$NON-NLS-1$
170 		}
171 		return null;
172 	}
173 
174 	/*----------------------------------------------------------------------------------------------------------*/
175 
176 	/*
177 	 * -----------------------------------------------------------------------------------------------------------
178 	 * --------------------------GETTERS AND SETTERS------------------------------------------------------
179 	 * -----------------------------------------------------------------------------------------------------------
180 	 */
181 
182 	/**
183 	 * Sets data and items to display for the domain model combo on <code>InitImportPageComposite</code>.
184 	 * 
185 	 * @param domainModelCombo
186 	 *            the domain model combo
187 	 */
188 	public void setDomainModelComboData(Combo domainModelCombo) {
189 		if (conversionRulesPageInfos == null) {
190 			try {
191 				conversionRulesPageInfos = getImportWizard().getWizardUtil().getConversionRulesSelectionPageInfos();
192 				Collections.sort(conversionRulesPageInfos);
193 			} catch (CoreException e) {
194 				String errorMsg = "CoreException while retrieving model infos from model util."; //$NON-NLS-1$
195 				log.error(errorMsg, e);
196 			}
197 		}
198 
199 		String[] modelNames = new String[conversionRulesPageInfos.size()];
200 		ConversionRulesPageInfo[] pageInfos = new ConversionRulesPageInfo[conversionRulesPageInfos.size()];
201 
202 		int i = 0;
203 		for (ConversionRulesPageInfo pageInfo : conversionRulesPageInfos) {
204 			modelNames[i] = pageInfo.getName();
205 			pageInfos[i] = pageInfo;
206 			i++;
207 		}
208 
209 		domainModelCombo.setItems(modelNames);
210 		domainModelCombo.setData(pageInfos);
211 	}
212 
213 	/**
214 	 * Sets data and items to display for the file format combo on <code>InitImportPageComposite</code>.
215 	 * 
216 	 * @param fileFormatCombo
217 	 *            the file format combo
218 	 */
219 	public void setFormatComboData(Combo fileFormatCombo) {
220 		if (tokenizerInfos == null) {
221 			try {
222 				tokenizerInfos = getImporterUtil().getTokenizerInfos();
223 				Collections.sort(tokenizerInfos);
224 			} catch (CoreException e) {
225 				String errorMsg = "CoreException while retrieving tokenizer infos from importer util."; //$NON-NLS-1$
226 				log.error(errorMsg, e);
227 			}
228 		}
229 
230 		String[] tokenzierIds = new String[tokenizerInfos.size()];
231 
232 		int i = 0;
233 		for (TokenizerInfo tokenizerInfo : tokenizerInfos) {
234 			tokenzierIds[i++] = tokenizerInfo.getTokenizerId();
235 		}
236 
237 		ComboViewer viewer = new ComboViewer(fileFormatCombo);
238 		viewer.setContentProvider(new ArrayContentProvider());
239 		viewer.setLabelProvider(new LabelProvider() {
240 			@Override
241 			public String getText(Object element) {
242 				return ((TokenizerInfo)element).getName();
243 			}
244 
245 		});
246 		viewer.setInput(tokenizerInfos);
247 
248 		// fileFormatCombo.setItems(tokenizerNames);
249 		// TODO use tokenizer ids retrieved from combo viewer
250 		fileFormatCombo.setData(tokenzierIds);
251 	}
252 
253 	/**
254 	 * Retrieves description of a tokenizer of the specified identifier.
255 	 * 
256 	 * @param tokenizerId
257 	 *            the tokenizer id
258 	 * @return tokenizer description
259 	 */
260 	public String getTokenizerDescription(String tokenizerId) {
261 		try {
262 			return getImporterUtil().getTokenizerDescription(tokenizerId);
263 		} catch (CoreException e) {
264 			String errorMsg = "CoreException while retrieving description of tokenizer with id: " //$NON-NLS-1$
265 			        + tokenizerId;
266 			log.error(errorMsg, e);
267 			throw new RuntimeException(errorMsg, e);
268 		}
269 	}
270 
271 	/**
272 	 * Retrieves all file extensions supported by all tokenizers, mapped to their tokenizer's identifiers.
273 	 * 
274 	 * @return all supported file extensions mappings
275 	 */
276 	public Map<String, String> getAllSupportedFileExtensions() {
277 		try {
278 			return getImporterUtil().getSupportedFileExtensions();
279 		} catch (CoreException e) {
280 			String errorMsg = "CoreException while retrieving all supported file extensions."; //$NON-NLS-1$
281 			log.error(errorMsg, e);
282 			throw new RuntimeException(errorMsg, e);
283 		}
284 	}
285 
286 	/**
287 	 * Retrieves the charset for selected file, on the basis of selected file format (tokenizer).
288 	 * 
289 	 * @return the file charset
290 	 */
291 	public Charset getFileCharset() {
292 		try {
293 			return getImporterUtil().getFileCharset(getImportProcess().getFilePath(), composite.getSelectedFormatId());
294 		} catch (IOException e) {
295 			// do nothing
296 			return null;
297 		} catch (CoreException e) {
298 			// do nothing
299 			return null;
300 		}
301 	}
302 
303 	private void refreshSelectedData() {
304 		if (composite != null && getImportProcess().shouldChangeTokenizePreview()) {
305 			composite.refreshPage(getImportProcess().getFilePath());
306 		}
307 	}
308 
309 	/**
310 	 * Sets the selected encoding.
311 	 * 
312 	 * @param charset
313 	 *            the selected encoding
314 	 */
315 	public void setSelectedEncoding(Charset charset) {
316 		this.composite.setSelectedEncoding(charset);
317 	}
318 
319 	public IImportTokenizer getTokenizer() {
320 		return importTokenizer;
321 	}
322 
323 	private ImporterUtil getImporterUtil() {
324 		return getImportWizard().getImporterUtil();
325 	}
326 
327 	/*----------------------------------------------------------------------------------------------------------*/
328 
329 }