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