Added DwC-A export wizard. Configuration options are now read from l10n properties...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / io / wizard / GenericConfiguratorWizardPage.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.io.wizard;
12
13 import java.lang.reflect.Method;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.MissingResourceException;
17 import java.util.ResourceBundle;
18
19 import org.eclipse.jface.wizard.WizardPage;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27
28 import eu.etaxonomy.cdm.io.common.IIoConfigurator;
29 import eu.etaxonomy.taxeditor.store.StoreUtil;
30
31 /**
32 * <p>
33 * GenericConfiguratorWizardPage class.
34 * </p>
35 *
36 * @author n.hoffmann
37 * @created 23.06.2009
38 * @version 1.0
39 */
40 public class GenericConfiguratorWizardPage extends WizardPage {
41
42 private final IIoConfigurator configurator;
43 private final ResourceBundle resourceBundle;
44
45 /**
46 * @param pageName
47 * @param configurator
48 */
49 private GenericConfiguratorWizardPage(String pageName,
50 IIoConfigurator configurator, String title, String description) {
51 super(pageName);
52 this.setTitle(title);
53
54 this.setDescription(description);
55
56 this.configurator = configurator;
57
58
59 resourceBundle = getResourceBundle(configurator);
60 }
61
62 private ResourceBundle getResourceBundle(IIoConfigurator configurator){
63 try{
64 return ResourceBundle.getBundle(configurator.getClass().getName());
65 }catch(MissingResourceException e){
66 return null;
67 }
68 }
69
70 /**
71 * <p>
72 * Import
73 * </p>
74 *
75 * @param pageName
76 * a {@link java.lang.String} object.
77 * @param configurator
78 * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
79 * @return a
80 * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
81 * object.
82 */
83 public static GenericConfiguratorWizardPage Import(String pageName,
84 IIoConfigurator configurator) {
85 return new GenericConfiguratorWizardPage(pageName, configurator,
86 "Import Configuration", "Configure the import mechanism.");
87 }
88
89 /**
90 * <p>
91 * Export
92 * </p>
93 *
94 * @param pageName
95 * a {@link java.lang.String} object.
96 * @param configurator
97 * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
98 * @return a
99 * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
100 * object.
101 */
102 public static GenericConfiguratorWizardPage Export(String pageName,
103 IIoConfigurator configurator) {
104 return new GenericConfiguratorWizardPage(pageName, configurator,
105 "Export Configuration", "Configure the export mechanism.");
106 }
107
108 /*
109 * (non-Javadoc)
110 *
111 * @see
112 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
113 * .Composite)
114 */
115 /** {@inheritDoc} */
116 public void createControl(Composite parent) {
117
118 // ScrolledComposite scrolledComposite = new ScrolledComposite(parent,
119 // SWT.H_SCROLL);
120 // scrolledComposite.setLayout(new GridLayout());
121
122 // TODO wrap this composite in a scrolled composite
123 Composite composite = new Composite(parent, SWT.NULL);
124 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
125
126 GridLayout gridLayout = new GridLayout();
127 composite.setLayout(gridLayout);
128
129 List<Method> methods = getConfiguratorsBooleanSetMethods(configurator);
130
131 for (Method method : methods) {
132 createCheckbox(composite, method, configurator);
133 }
134
135 // scrolledComposite.setContent(composite);
136
137 setControl(composite);
138 }
139
140 private void createCheckbox(Composite parent, Method method,
141 final IIoConfigurator configurator) {
142
143 final String methodName = method.getName();
144
145 final Button checkBox = new Button(parent, SWT.CHECK);
146
147 checkBox.setText(getLabel(methodName));
148 // retrieve the default values and set the checkbox accordingly
149 boolean defaultSelection = executeBooleanGetMethod(configurator, "is"
150 + methodName.substring(3));
151 checkBox.setSelection(defaultSelection);
152 checkBox.addSelectionListener(new SelectionAdapter() {
153
154 /*
155 * (non-Javadoc)
156 *
157 * @see
158 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
159 * .swt.events.SelectionEvent)
160 */
161 @Override
162 public void widgetSelected(SelectionEvent e) {
163 executeBooleanSetMethod(configurator, methodName,
164 checkBox.getSelection());
165 }
166
167 });
168
169 }
170
171 private String getLabel(String methodName){
172
173 if(resourceBundle == null){
174 return methodName;
175 }
176
177 try{
178 return resourceBundle.getString(methodName);
179 }catch(MissingResourceException e){
180 return methodName;
181 }
182 }
183
184 private boolean executeBooleanGetMethod(IIoConfigurator configurator,
185 String methodName) {
186
187 Class<? extends IIoConfigurator> configuratorClass = configurator
188 .getClass();
189
190 boolean result = false;
191
192 Method[] methods = configuratorClass.getMethods();
193
194 for (Method method : methods) {
195 if (!method.getName().equals(methodName)) {
196 continue;
197 }
198
199 try {
200 Object returnType = method.invoke(configurator, null);
201 if (returnType.getClass().equals(Boolean.class)) {
202 result = ((Boolean) returnType).booleanValue();
203 }
204
205 break;
206 } catch (Exception e) {
207 StoreUtil.warn(this.getClass(), "Could not invoke method");
208 }
209 }
210
211 return result;
212 }
213
214 private void executeBooleanSetMethod(IIoConfigurator configurator,
215 String methodName, boolean selected) {
216
217 Class<? extends IIoConfigurator> configuratorClass = configurator
218 .getClass();
219
220 Method[] methods = configuratorClass.getMethods();
221
222 for (Method method : methods) {
223 if (!method.getName().equals(methodName)) {
224 continue;
225 }
226
227 try {
228 method.invoke(configurator, selected);
229
230 break;
231 } catch (Exception e) {
232 StoreUtil.warn(this.getClass(), "Could not invoke method");
233 }
234 }
235 }
236
237 private List<Method> getConfiguratorsBooleanSetMethods(
238 IIoConfigurator configurator) {
239 List<Method> booleanMethods = new ArrayList<Method>();
240
241 Class<? extends IIoConfigurator> configuratorClass = configurator
242 .getClass();
243
244 Method[] allMethods = configuratorClass.getMethods();
245
246 for (Method method : allMethods) {
247 if (method.getName().startsWith("set")) {
248
249 Class<?>[] typeList = method.getParameterTypes();
250
251 if (typeList.length > 1) {
252 new IllegalStateException(
253 "Found a setter with parameter count > 1");
254 }
255
256 if (typeList[0].getName().equals("boolean")) {
257 booleanMethods.add(method);
258 }
259 }
260 }
261
262 return booleanMethods;
263 }
264 }