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: AbstractConversionRulesSelectionPage.java
13   * Created: 2009-04-29
14   * Author: bmilos
15   * $Id$
16   */
17  
18  package pl.edu.agh.cast.importer.wizard.page;
19  
20  import java.util.Collections;
21  import java.util.LinkedList;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.log4j.Logger;
26  import org.eclipse.core.runtime.CoreException;
27  import org.eclipse.swt.SWT;
28  import org.eclipse.swt.events.SelectionAdapter;
29  import org.eclipse.swt.events.SelectionEvent;
30  import org.eclipse.swt.layout.GridData;
31  import org.eclipse.swt.layout.GridLayout;
32  import org.eclipse.swt.widgets.Button;
33  import org.eclipse.swt.widgets.Combo;
34  import org.eclipse.swt.widgets.Composite;
35  import org.eclipse.swt.widgets.Label;
36  
37  import pl.edu.agh.cast.importer.base.converter.IImportConverter;
38  import pl.edu.agh.cast.importer.base.converter.rules.IConversionRule;
39  import pl.edu.agh.cast.importer.base.data.TabularData;
40  import pl.edu.agh.cast.importer.base.util.ConversionRuleInfo;
41  import pl.edu.agh.cast.importer.wizard.Activator;
42  import pl.edu.agh.cast.importer.wizard.dialog.rule.AbstractConversionRuleConfigDialog;
43  import pl.edu.agh.cast.importer.wizard.util.Messages;
44  
45  /**
46   * Abstract class for conversion rules selection pages of the importer wizard.
47   *
48   * @author AGH CAST Team
49   */
50  public abstract class AbstractConversionRulesSelectionPage extends AbstractImportWizardPage {
51  
52  	private final Logger log = Activator.getLogger();
53  
54  	/**
55  	 * The import converter responsible for the current domain model's data conversion.
56  	 */
57  	protected IImportConverter converter;
58  
59  	/**
60  	 * The map of existing conversion rules to the comboboxes that handle their configuration.
61  	 */
62  	protected Map<Class<? extends IConversionRule>, Combo> ruleCombosMap;
63  
64  	/**
65  	 * The conversion rules' information.
66  	 */
67  	protected List<ConversionRuleInfo> ruleInfos;
68  
69  	private Button saveTemplateBtn;
70  
71  	private boolean savePermamanetSelection;
72  
73  	protected AbstractConversionRulesSelectionPage(String pageName, String description) {
74  		super(pageName, description);
75  	}
76  
77  	/**
78  	 * {@inheritDoc}
79  	 *
80  	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
81  	 */
82  	@Override
83  	public void createControl(Composite parent) {
84  		final Composite composite = new Composite(parent, SWT.NULL);
85  		composite.setLayout(new GridLayout());
86  
87  		final Composite convRulesComposite = createConvRulesComposite(composite);
88  
89  		saveTemplateBtn = new Button(composite, SWT.CHECK);
90  		saveTemplateBtn.setText(Messages.AbstractConversionRulesSelectionPage_SaveAsTemplate);
91  		saveTemplateBtn.setLayoutData(new GridData(SWT.END, SWT.END, false, false, 0, 0));
92  		saveTemplateBtn.addSelectionListener(new SelectionAdapter() {
93  			@Override
94  			public void widgetSelected(SelectionEvent e) {
95  				widgetModified();
96  			}
97  		});
98  
99  		if (savePermamanetSelection) {
100 			saveTemplateBtn.setSelection(true);
101 			saveTemplateBtn.setEnabled(false);
102 		}
103 
104 		convRulesComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 0, 0));
105 		setControl(composite);
106 	}
107 
108 	public boolean isSaveTemplateSelected() {
109 		return saveTemplateBtn.getSelection();
110 	}
111 
112 	/**
113 	 * {@inheritDoc}
114 	 *
115 	 * @see pl.edu.agh.cast.importer.wizard.page.AbstractImportWizardPage#canFlipToNextPage()
116 	 */
117 	@Override
118 	public boolean canFlipToNextPage() {
119 		return super.canFlipToNextPage() && saveTemplateBtn.getSelection();
120 	}
121 
122 	/**
123 	 * This method marks flag that template from import process must be saved.
124 	 */
125 	public void setMustSaveTemplate() {
126 		savePermamanetSelection = true;
127 	}
128 
129 	protected abstract Composite createConvRulesComposite(Composite parent);
130 
131 	/**
132 	 *
133 	 * {@inheritDoc}
134 	 *
135 	 * @see pl.edu.agh.cast.importer.wizard.page.AbstractImportWizardPage#initPage()
136 	 */
137 	@Override
138 	public void initPage() {
139 		refreshSelectedData();
140 	}
141 
142 	/**
143 	 * Method is a delegate of a protected method <code>WizardPage.isCurrentPage</code>. Returns whether this page is
144 	 * the current one in the wizard's container.
145 	 *
146 	 * @return <code>true</code> if the page is active, and <code>false</code> otherwise
147 	 */
148 	public boolean isCurrentWizardPage() {
149 		return isCurrentPage();
150 	}
151 
152 	/**
153 	 * Checks whether the comboboxes in the curent rules selection page should be blocked or unblocked due to the change
154 	 * in rule selection.
155 	 *
156 	 * @param oldRule
157 	 *            the rule that had been selected previously
158 	 * @param newRule
159 	 *            the rule that had been selected recently
160 	 * @param ruleClazz
161 	 *            the class of the selected rule
162 	 */
163 	public void checkBlocking(IConversionRule oldRule, IConversionRule newRule,
164 	        Class<? extends IConversionRule> ruleClazz) {
165 		if (oldRule == null && newRule == null) {
166 			return;
167 		}
168 
169 		List<Combo> combosToBlock = new LinkedList<Combo>();
170 		List<Combo> combosToUnblock = new LinkedList<Combo>();
171 
172 		Class<? extends IConversionRule> newRuleClazz = null;
173 		if (newRule != null) {
174 			newRuleClazz = newRule.getClass();
175 		}
176 
177 		Class<? extends IConversionRule> oldRuleClazz = null;
178 		if (oldRule != null) {
179 			oldRuleClazz = oldRule.getClass();
180 		}
181 
182 		for (Class<? extends IConversionRule> ruleInterface : ruleCombosMap.keySet()) {
183 			if (ruleInterface.equals(ruleClazz)) {
184 				continue;
185 			}
186 
187 			if (newRuleClazz != null && ruleInterface.isAssignableFrom(newRuleClazz)) {
188 				combosToBlock.add(ruleCombosMap.get(ruleInterface));
189 			} else if (oldRuleClazz != null && ruleInterface.isAssignableFrom(oldRuleClazz)) {
190 				combosToUnblock.add(ruleCombosMap.get(ruleInterface));
191 			}
192 		}
193 
194 		refreshCombosBlocking(combosToBlock, combosToUnblock, newRule);
195 	}
196 
197 	/*
198 	 * -----------------------------------------------------------------------------------------------------------
199 	 * --------------------------ABSTRACT METHODS--------------------------------------------------------
200 	 * -----------------------------------------------------------------------------------------------------------
201 	 */
202 	/**
203 	 * Method called by the previous page to refresh the selected data.
204 	 */
205 	public abstract void refreshSelectedData();
206 
207 	/**
208 	 * Refreshes the conversion preview table.
209 	 */
210 	public abstract void refreshConversionPreview();
211 
212 	/**
213 	 * Refreshes the blocking of comboboxes on the current rules selection page.
214 	 *
215 	 * @param combosToBlock
216 	 *            the comboboxes to block
217 	 * @param combosToUnblock
218 	 *            the comboboxes to unblock
219 	 * @param newRule
220 	 *            the newly selected conversion rule
221 	 */
222 	public abstract void refreshCombosBlocking(List<Combo> combosToBlock, List<Combo> combosToUnblock,
223 	        IConversionRule newRule);
224 
225 	/**
226 	 * Checks whether the wizard can finish the import process (using the helper method
227 	 * {@link AbstractConversionRulesSelectionPage#isComplete()}), and when it is, creates an appropriate converter,
228 	 * sets its conversion rule values, and sets the converter for the parent wizard.
229 	 *
230 	 * @return <code>true</code> if the wizard can finish the import process, <code>false</code> otherwise
231 	 */
232 	public abstract boolean canFinish();
233 
234 	/**
235 	 * Displays a warning box before wizard finish with an appropriate message, if necessary.
236 	 *
237 	 * @return the message box result
238 	 */
239 	public abstract int showWarningMsgBox();
240 
241 	/**
242 	 * Returns whether the current conversion rule selection page is complete - whether all its obligatory conversion
243 	 * rules are complete (are entirely configured).
244 	 *
245 	 * @return <code>true</code> if the wizard can finish the import process, <code>false</code> otherwise
246 	 */
247 	protected abstract boolean isComplete();
248 
249 	/*----------------------------------------------------------------------------------------------------------*/
250 
251 	/*
252 	 * -----------------------------------------------------------------------------------------------------------
253 	 * --------------------------GETTERS AND SETTERS------------------------------------------------------
254 	 * -----------------------------------------------------------------------------------------------------------
255 	 */
256 
257 	/**
258 	 * Sets data and items to display for the specified ombo with conversion rule implementing a specified interface
259 	 * class.
260 	 *
261 	 * @param convRuleCombo
262 	 *            the combo for conversion rule
263 	 * @param convRuleClazz
264 	 *            the interface class for conversion rule
265 	 */
266 	public void setRuleComboData(Combo convRuleCombo, Class<?> convRuleClazz) {
267 		if (ruleInfos == null) {
268 			getRuleInfos();
269 		}
270 
271 		List<ConversionRuleInfo> convRuleInfos = new LinkedList<ConversionRuleInfo>();
272 		for (ConversionRuleInfo ruleInfo : ruleInfos) {
273 			Class<?> ruleClazz = ruleInfo.getRuleClass();
274 
275 			Class<?>[] ruleInterfaces = ruleClazz.getInterfaces();
276 			for (Class<?> ruleInterface : ruleInterfaces) {
277 				if (ruleInterface.isAssignableFrom(convRuleClazz)) {
278 					convRuleInfos.add(ruleInfo);
279 					break;
280 				}
281 			}
282 		}
283 
284 		String[] ruleNames = new String[convRuleInfos.size() + 1];
285 		String[] ruleIds = new String[convRuleInfos.size() + 1];
286 
287 		int i = 0;
288 		ruleNames[0] = ""; //$NON-NLS-1$
289 		ruleIds[0] = null;
290 		for (ConversionRuleInfo ruleInfo : convRuleInfos) {
291 			i++;
292 			ruleNames[i] = ruleInfo.getName();
293 			ruleIds[i] = ruleInfo.getRuleId();
294 		}
295 
296 		convRuleCombo.setItems(ruleNames);
297 		convRuleCombo.setData(ruleIds);
298 	}
299 
300 	/**
301 	 * Retrieves the conversion rule identifier selected in the specified combobox.
302 	 *
303 	 * @param combo
304 	 *            the combo, from which the selection is to be retrieved
305 	 * @return selected conversion rule id
306 	 */
307 	public String getSelectedRuleId(Combo combo) {
308 		String[] ruleIds = (String[])combo.getData();
309 		int selectedIndex = combo.getSelectionIndex();
310 		if (selectedIndex == -1) {
311 			return null;
312 		}
313 		return ruleIds[combo.getSelectionIndex()];
314 	}
315 
316 	/**
317 	 * Sets the specified description label for a conversion rule with specified identifier.
318 	 *
319 	 * @param ruleId
320 	 *            the rule identifier
321 	 * @param ruleDescriptionLbl
322 	 *            the rule description label
323 	 */
324 	public void setRuleDescriptionLabel(String ruleId, Label ruleDescriptionLbl) {
325 		// retrieve description
326 		try {
327 			String ruleDescription = (ruleId == null) ? "" : getConversionRuleDescription(ruleId); //$NON-NLS-1$
328 			if (ruleDescription != null && ruleDescription.length() > 0) {
329 				ruleDescriptionLbl.setText(ruleDescription);
330 			} else {
331 				ruleDescriptionLbl.setText(Messages.GeneralConversionRulesSelectionPageComposite_NoRuleDesc);
332 			}
333 		} catch (CoreException e) {
334 			String errorMsg = "CoreException while retrieving description of conversion rule with id: " //$NON-NLS-1$
335 			        + ruleId;
336 			log.error(errorMsg, e);
337 			throw new RuntimeException(errorMsg, e);
338 		}
339 	}
340 
341 	/**
342 	 * Retrieves conversion rule instance with specified identifier.
343 	 *
344 	 * @param ruleId
345 	 *            the rule id
346 	 * @return rule instance
347 	 */
348 	public IConversionRule getConversionRuleInstance(String ruleId) {
349 		try {
350 			return getImportWizard().getImporterUtil().getConversionRuleInstance(ruleId);
351 		} catch (CoreException e) {
352 			String errorMsg = "CoreException while retrieving conversion rule instance for conversion " //$NON-NLS-1$
353 			        + "rule with id: " + ruleId; //$NON-NLS-1$
354 			log.error(errorMsg, e);
355 			throw new RuntimeException(errorMsg, e);
356 		}
357 
358 	}
359 
360 	/**
361 	 * Retrieves conversion rule configuration dialog for conversion rule of the specified identifier.
362 	 *
363 	 * @param ruleId
364 	 *            the rule id
365 	 * @return rule description
366 	 */
367 	public AbstractConversionRuleConfigDialog getConversionRuleConfigDialog(String ruleId) {
368 		try {
369 			return getImportWizard().getWizardUtil().getConversionRuleConfigDialogInstance(ruleId);
370 		} catch (CoreException e) {
371 			String errorMsg = "CoreException while retrieving conversion rule configuration dialog for " //$NON-NLS-1$
372 			        + "conversion rule with id: " + ruleId; //$NON-NLS-1$
373 			log.error(errorMsg, e);
374 			throw new RuntimeException(errorMsg, e);
375 		}
376 	}
377 
378 	public void setRuleCombosMap(Map<Class<? extends IConversionRule>, Combo> ruleCombosMap) {
379 		this.ruleCombosMap = ruleCombosMap;
380 	}
381 
382 	/**
383 	 * Delegated method.
384 	 *
385 	 * {@inheritDoc}
386 	 *
387 	 * @see IImportConverter#preview(List)
388 	 */
389 	public List<TabularData> getConversionPreview(TabularData data) {
390 		List<TabularData> tabDatas = new LinkedList<TabularData>();
391 		tabDatas.add(data);
392 		return converter.preview(tabDatas);
393 	}
394 
395 	private void getRuleInfos() {
396 		try {
397 			ruleInfos = getImportWizard().getImporterUtil().getConversionRuleInfos();
398 			Collections.sort(ruleInfos);
399 		} catch (CoreException e) {
400 			String errorMsg = "CoreException while retrieving conversion rule infos from model util."; //$NON-NLS-1$
401 			log.error(errorMsg, e);
402 		}
403 	}
404 
405 	/**
406 	 * Retrieves description of a conversion rule of the specified identifier.
407 	 *
408 	 * @param ruleId
409 	 *            the rule id
410 	 * @return rule description
411 	 * @throws CoreException
412 	 */
413 	private String getConversionRuleDescription(String ruleId) throws CoreException {
414 		return getImportWizard().getImporterUtil().getConversionRuleDescription(ruleId);
415 	}
416 
417 	/*----------------------------------------------------------------------------------------------------------*/
418 }