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: WizardUtil.java
13   * Created: 2009-04-29
14   * Author: bmilos
15   * $Id$
16   */
17  
18  package pl.edu.agh.cast.importer.wizard.util;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.apache.log4j.Logger;
24  import org.eclipse.core.runtime.CoreException;
25  import org.eclipse.core.runtime.IConfigurationElement;
26  import org.eclipse.core.runtime.Platform;
27  
28  import pl.edu.agh.cast.importer.base.util.IImporterPluginRegistryProvider;
29  import pl.edu.agh.cast.importer.wizard.Activator;
30  import pl.edu.agh.cast.importer.wizard.dialog.analyzer.AbstractDataTypeConfigDialog;
31  import pl.edu.agh.cast.importer.wizard.dialog.rule.AbstractConversionRuleConfigDialog;
32  import pl.edu.agh.cast.importer.wizard.page.AbstractConversionRulesSelectionPage;
33  
34  /**
35   * Utility methods for the import wizard.
36   * 
37   * @author AGH CAST Team
38   */
39  public class WizardUtil {
40  
41  	private static Logger log = Activator.getLogger();
42  
43  	/**
44  	 * The plugin registry provider.
45  	 */
46  	private IWizardPluginRegistryProvider pluginRegistryProvider;
47  
48  	/**
49  	 * The default constructor. Creates a new instance of {@link DefaultImporterPluginRegistryProvider}.
50  	 */
51  	public WizardUtil() {
52  		this.pluginRegistryProvider = new DefaultWizardPluginRegistryProvider();
53  	}
54  
55  	/**
56  	 * Constructor which allows to set custom {@link IWizardPluginRegistryProvider} instance. If null is passed, the
57  	 * {@link DefaultWizardPluginRegistryProvider} is used. This constructor should be used in tests.
58  	 * 
59  	 * @param provider
60  	 *            custom provider instance
61  	 */
62  	public WizardUtil(IWizardPluginRegistryProvider provider) {
63  		if (provider != null) {
64  			this.pluginRegistryProvider = provider;
65  		} else {
66  			this.pluginRegistryProvider = new DefaultWizardPluginRegistryProvider();
67  		}
68  	}
69  
70  	/**
71  	 * Returns infos about conversion rules selection pages read from
72  	 * <code>pl.edu.agh.cast.importer.wizard.rulespage</code> extension point.
73  	 * 
74  	 * @return list of infos about conversion rules selection pages
75  	 * @throws CoreException
76  	 *             when an error occurs during instantiating selection page
77  	 */
78  	public List<ConversionRulesPageInfo> getConversionRulesSelectionPageInfos() throws CoreException {
79  		IConfigurationElement[] elements = getConversionRulesSelectionPageConfigurations();
80  
81  		List<ConversionRulesPageInfo> res = new ArrayList<ConversionRulesPageInfo>(elements.length);
82  
83  		for (IConfigurationElement element : elements) {
84  			String id = element.getAttribute(Activator.WIZARD_CONVERSIONRULES_PAGE_ID);
85  			String name = element.getAttribute(Activator.WIZARD_CONVERSIONRULES_PAGE_NAME);
86  			String description = element.getAttribute(Activator.WIZARD_CONVERSIONRULES_PAGE_DESCRIPTION);
87  			AbstractConversionRulesSelectionPage page = (AbstractConversionRulesSelectionPage)element
88  			        .createExecutableExtension(Activator.WIZARD_CONVERSIONRULES_PAGE_CLASS);
89  			res.add(new ConversionRulesPageInfo(id, name, description, page));
90  
91  		}
92  
93  		return res;
94  	}
95  
96  	/**
97  	 * Returns configuration elements of extensions for <code>pl.edu.agh.cast.importer.wizard.rulespage</code> extension
98  	 * point.
99  	 * 
100 	 * @return array of conversion rules selection page configurations
101 	 */
102 	public IConfigurationElement[] getConversionRulesSelectionPageConfigurations() {
103 		return Platform.getExtensionRegistry().getConfigurationElementsFor(
104 		        Activator.WIZARD_CONVERSIONRULES_PAGE_EXTENSION_ID);
105 	}
106 
107 	/**
108 	 * Returns configuration elements of extensions for <code>pl.edu.agh.cast.importer.wizard.rulesconfig</code>
109 	 * extension point.
110 	 * 
111 	 * @return array of conversion rule configuration dialog configurations
112 	 */
113 	public IConfigurationElement[] getConversionRuleConfigDialogConfigurations() {
114 		return Platform.getExtensionRegistry().getConfigurationElementsFor(
115 		        Activator.WIZARD_CONVERSIONRULE_DIALOG_EXTENSION_ID);
116 	}
117 
118 	/**
119 	 * Returns configuration elements of extensions for <code>pl.edu.agh.cast.importer.wizard.datatypeconfig</code>
120 	 * extension point.
121 	 * 
122 	 * @return array of data type configuration dialog configurations
123 	 */
124 	public IConfigurationElement[] getDataTypeConfigDialogConfigurations() {
125 		return Platform.getExtensionRegistry().getConfigurationElementsFor(
126 		        Activator.WIZARD_DATATYPE_DIALOG_EXTENSION_ID);
127 	}
128 
129 	/*
130 	 * -----------------------------------------------------------------------------------------------------------
131 	 * --------------------------DELEGATED METHODS-------------------------------------------------------
132 	 * -----------------------------------------------------------------------------------------------------------
133 	 */
134 
135 	/**
136 	 * Delegated method.
137 	 * 
138 	 * {@inheritDoc}
139 	 * 
140 	 * @see pl.edu.agh.cast.importer.wizard.util.IWizardPluginRegistryProvider#
141 	 *      getConversionRulesSelectionPageInstance(java.lang.String)
142 	 */
143 	public AbstractConversionRulesSelectionPage getConversionRulesSelectionPageInstance(String pageId)
144 	        throws CoreException {
145 		return this.pluginRegistryProvider.getConversionRulesSelectionPageInstance(pageId);
146 	}
147 
148 	/**
149 	 * Delegated method.
150 	 * 
151 	 * {@inheritDoc}
152 	 * 
153 	 * @see pl.edu.agh.cast.importer.wizard.util.IWizardPluginRegistryProvider#
154 	 *      getConversionRuleConfigDialogInstance(String)
155 	 */
156 	public AbstractConversionRuleConfigDialog getConversionRuleConfigDialogInstance(String ruleId) throws CoreException {
157 		return this.pluginRegistryProvider.getConversionRuleConfigDialogInstance(ruleId);
158 	}
159 
160 	/**
161 	 * Delegated method.
162 	 * 
163 	 * {@inheritDoc}
164 	 * 
165 	 * @see pl.edu.agh.cast.importer.wizard.util.IWizardPluginRegistryProvider#
166 	 *      getConversionRuleConfigDialogInstance(String)
167 	 */
168 	public AbstractDataTypeConfigDialog getDataTypeConfigDialogInstance(String dataTypeId) throws CoreException {
169 		return this.pluginRegistryProvider.getDataTypeConfigDialogInstance(dataTypeId);
170 	}
171 
172 	/*----------------------------------------------------------------------------------------------------------*/
173 
174 	/*
175 	 * -----------------------------------------------------------------------------------------------------------
176 	 * --------------------------PRIVATE CLASSES------------------------------------------------------------
177 	 * -----------------------------------------------------------------------------------------------------------
178 	 */
179 
180 	/**
181 	 * Default implementation of the {@link IImporterPluginRegistryProvider}.
182 	 * 
183 	 * @author AGH CAST Team
184 	 */
185 	private class DefaultWizardPluginRegistryProvider implements IWizardPluginRegistryProvider {
186 
187 		/**
188 		 * Returns the conversion rule selection page configuration for given page identifier.
189 		 * 
190 		 * @param pageId
191 		 *            the id of the conversion rule selection page (defined in the plugin.xml file)
192 		 * @return the conversion rule selection page configuration
193 		 */
194 		private IConfigurationElement getConversionRulesSelectionPageConfiguration(String pageId) {
195 			IConfigurationElement[] rulePages = getConversionRulesSelectionPageConfigurations();
196 
197 			for (IConfigurationElement conf : rulePages) {
198 				String actualId = conf.getAttribute(Activator.WIZARD_CONVERSIONRULES_PAGE_ID);
199 
200 				if (actualId.equals(pageId)) {
201 					return conf;
202 				}
203 			}
204 			log.error("Cannot get conversion rules selection page instance. " //$NON-NLS-1$
205 			        + "No conversion rules page with ID = " + pageId); //$NON-NLS-1$
206 			return null;
207 		}
208 
209 		/**
210 		 * Returns the conversion rule configuration dialog configuration for given rule identifier.
211 		 * 
212 		 * @param ruleId
213 		 *            the id of the conversion rule (defined in the plugin.xml file)
214 		 * @return the conversion rule dialog configuration
215 		 */
216 		private IConfigurationElement getConversionRulesConfigDialogConfiguration(String ruleId) {
217 			IConfigurationElement[] ruleDialogs = getConversionRuleConfigDialogConfigurations();
218 
219 			for (IConfigurationElement conf : ruleDialogs) {
220 				String actualId = conf.getAttribute(Activator.WIZARD_CONVERSIONRULE_DIALOG_ID);
221 
222 				if (actualId.equals(ruleId)) {
223 					return conf;
224 				}
225 			}
226 			log.error("Cannot get conversion rule configuration dialog instance. " //$NON-NLS-1$
227 			        + "No conversion rules config dialog with ID = " + ruleId); //$NON-NLS-1$
228 			return null;
229 		}
230 
231 		/**
232 		 * Returns the data type configuration dialog configuration for given data type identifier.
233 		 * 
234 		 * @param dataTypeId
235 		 *            the id of the data type (defined in the plugin.xml file)
236 		 * @return the data type dialog configuration
237 		 */
238 		private IConfigurationElement getDataTypeConfigDialogConfiguration(String dataTypeId) {
239 			IConfigurationElement[] dataTypeDialogs = getDataTypeConfigDialogConfigurations();
240 
241 			for (IConfigurationElement conf : dataTypeDialogs) {
242 				String actualId = conf.getAttribute(Activator.WIZARD_DATATYPE_DIALOG_ID);
243 
244 				if (actualId.equals(dataTypeId)) {
245 					return conf;
246 				}
247 			}
248 			log.warn("Cannot get data type configuration dialog instance. " //$NON-NLS-1$
249 			        + "No data type config dialog with ID = " + dataTypeId); //$NON-NLS-1$
250 			return null;
251 		}
252 
253 		/**
254 		 * {@inheritDoc}
255 		 * 
256 		 * @see pl.edu.agh.cast.importer.wizard.util.IWizardPluginRegistryProvider#
257 		 *      getConversionRulesSelectionPageInstance(java.lang.String)
258 		 */
259 		public AbstractConversionRulesSelectionPage getConversionRulesSelectionPageInstance(String pageId)
260 		        throws CoreException {
261 			IConfigurationElement conf = getConversionRulesSelectionPageConfiguration(pageId);
262 			if (conf == null) {
263 				return null;
264 			}
265 			return (AbstractConversionRulesSelectionPage)conf
266 			        .createExecutableExtension(Activator.WIZARD_CONVERSIONRULES_PAGE_CLASS);
267 		}
268 
269 		/**
270 		 * {@inheritDoc}
271 		 * 
272 		 * @see pl.edu.agh.cast.importer.wizard.util.IWizardPluginRegistryProvider#
273 		 *      getConversionRuleConfigDialogInstance(java.lang.String)
274 		 */
275 		public AbstractConversionRuleConfigDialog getConversionRuleConfigDialogInstance(String ruleId)
276 		        throws CoreException {
277 			IConfigurationElement conf = getConversionRulesConfigDialogConfiguration(ruleId);
278 			if (conf == null) {
279 				return null;
280 			}
281 			return (AbstractConversionRuleConfigDialog)conf
282 			        .createExecutableExtension(Activator.WIZARD_CONVERSIONRULE_DIALOG_CLASS);
283 		}
284 
285 		/**
286 		 * {@inheritDoc}
287 		 * 
288 		 * @see pl.edu.agh.cast.importer.wizard.util.IWizardPluginRegistryProvider#
289 		 *      getDataTypeConfigDialogInstance(java.lang.String)
290 		 */
291 		@Override
292 		public AbstractDataTypeConfigDialog getDataTypeConfigDialogInstance(String dataTypeId) throws CoreException {
293 			IConfigurationElement conf = getDataTypeConfigDialogConfiguration(dataTypeId);
294 			if (conf == null) {
295 				return null;
296 			}
297 			return (AbstractDataTypeConfigDialog)conf.createExecutableExtension(Activator.WIZARD_DATATYPE_DIALOG_CLASS);
298 		}
299 	}
300 
301 	/*----------------------------------------------------------------------------------------------------------*/
302 }