fixes #862
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / io / 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;
12
13 import java.lang.reflect.Method;
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.apache.log4j.Logger;
18 import org.eclipse.jface.wizard.WizardPage;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.custom.CLabel;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Composite;
26
27 import eu.etaxonomy.cdm.io.common.IIoConfigurator;
28
29 /**
30 * @author n.hoffmann
31 * @created 23.06.2009
32 * @version 1.0
33 */
34 public class GenericConfiguratorWizardPage extends WizardPage {
35
36
37 private static final Logger logger = Logger
38 .getLogger(GenericConfiguratorWizardPage.class);
39
40 private IIoConfigurator configurator;
41
42 /**
43 * @param pageName
44 * @param configurator
45 */
46 private GenericConfiguratorWizardPage(String pageName, IIoConfigurator configurator, String title, String description) {
47 super(pageName);
48 this.setTitle(title);
49
50 this.setDescription(description);
51
52 this.configurator = configurator;
53 }
54
55 public static GenericConfiguratorWizardPage Import(String pageName, IIoConfigurator configurator){
56 return new GenericConfiguratorWizardPage(pageName, configurator, "Import Configuration", "Configure the import mechanism.");
57 }
58
59 public static GenericConfiguratorWizardPage Export(String pageName, IIoConfigurator configurator){
60 return new GenericConfiguratorWizardPage(pageName, configurator, "Export Configuration", "Configure the export mechanism.");
61 }
62
63
64 /* (non-Javadoc)
65 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
66 */
67 public void createControl(Composite parent) {
68
69 Composite composite = new Composite(parent, SWT.NULL);
70 GridLayout gridLayout = new GridLayout();
71 gridLayout.numColumns = 2;
72 composite.setLayout(gridLayout);
73
74 List<Method> methods = getConfiguratorsBooleanSetMethods(configurator);
75
76 for(Method method : methods){
77 createCheckbox(composite, method, configurator);
78 }
79
80 setControl(composite);
81 }
82
83 private void createCheckbox(Composite parent, Method method, final IIoConfigurator configurator){
84
85 String methodName = method.getName();
86
87 final String methodNameValue = methodName.substring(3);
88
89 CLabel label = new CLabel(parent, SWT.NULL);
90 label.setText(methodNameValue);
91
92 final Button checkBox = new Button(parent, SWT.CHECK);
93 checkBox.setSelection(executeBooleanGetMethod(configurator,"is" + methodNameValue));
94 checkBox.addSelectionListener(new SelectionAdapter(){
95
96 /* (non-Javadoc)
97 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
98 */
99 @Override
100 public void widgetSelected(SelectionEvent e) {
101 executeBooleanSetMethod(configurator, "set" + methodNameValue, checkBox.getSelection());
102 }
103
104 });
105
106 }
107
108
109 private boolean executeBooleanGetMethod(IIoConfigurator configurator, String methodName){
110
111 Class<? extends IIoConfigurator> configuratorClass = configurator.getClass();
112
113 boolean result = false;
114
115 Method[] methods = configuratorClass.getMethods();
116
117 for(Method method : methods){
118 if(!method.getName().equals(methodName)){
119 continue;
120 }
121
122 try {
123 Object returnType = method.invoke(configurator, null);
124 if(returnType.getClass().equals(Boolean.class)){
125 result = ((Boolean) returnType).booleanValue();
126 }
127
128 break;
129 } catch (Exception e) {
130 logger.warn("Could not invoke method");
131 }
132 }
133
134
135 return result;
136 }
137
138 private void executeBooleanSetMethod(IIoConfigurator configurator, String methodName, boolean selected){
139
140 Class<? extends IIoConfigurator> configuratorClass = configurator.getClass();
141
142
143 Method[] methods = configuratorClass.getMethods();
144
145 for(Method method : methods){
146 if(!method.getName().equals(methodName)){
147 continue;
148 }
149
150 try {
151 method.invoke(configurator, selected);
152
153 break;
154 } catch (Exception e) {
155 logger.warn("Could not invoke method");
156 }
157 }
158 }
159
160 private List<Method> getConfiguratorsBooleanSetMethods(IIoConfigurator configurator){
161 List<Method> booleanMethods = new ArrayList<Method>();
162
163 Class<? extends IIoConfigurator> configuratorClass = configurator.getClass();
164
165 Method[] allMethods = configuratorClass.getMethods();
166
167 for(Method method : allMethods){
168 if(method.getName().startsWith("set")){
169
170 Class<?>[] typeList = method.getParameterTypes();
171
172 if(typeList.length > 1){
173 new IllegalStateException("Found a setter with parameter count > 1");
174 }
175
176 if(typeList[0].getName().equals("boolean")){
177 booleanMethods.add(method);
178 }
179 }
180 }
181
182 return booleanMethods;
183 }
184 }