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