Implemented some of the import/export functionality. Major refactoring of datasource...
[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 protected GenericConfiguratorWizardPage(String pageName, IIoConfigurator configurator) {
47 super(pageName);
48 this.setTitle("Export Configuration");
49
50 this.setDescription("Configure the export mechanism.");
51
52 this.configurator = configurator;
53 }
54
55 /* (non-Javadoc)
56 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
57 */
58 public void createControl(Composite parent) {
59
60 Composite composite = new Composite(parent, SWT.NULL);
61 GridLayout gridLayout = new GridLayout();
62 gridLayout.numColumns = 2;
63 composite.setLayout(gridLayout);
64
65 List<Method> methods = getConfiguratorsBooleanSetMethods(configurator);
66
67 for(Method method : methods){
68 createCheckbox(composite, method, configurator);
69 }
70
71 setControl(composite);
72 }
73
74 private void createCheckbox(Composite parent, Method method, final IIoConfigurator configurator){
75
76 String methodName = method.getName();
77
78 final String methodNameValue = methodName.substring(3);
79
80 CLabel label = new CLabel(parent, SWT.NULL);
81 label.setText(methodNameValue);
82
83 final Button checkBox = new Button(parent, SWT.CHECK);
84 checkBox.setSelection(executeBooleanGetMethod(configurator,"is" + methodNameValue));
85 checkBox.addSelectionListener(new SelectionAdapter(){
86
87 /* (non-Javadoc)
88 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
89 */
90 @Override
91 public void widgetSelected(SelectionEvent e) {
92 executeBooleanSetMethod(configurator, "set" + methodNameValue, checkBox.getSelection());
93 }
94
95 });
96
97 }
98
99
100 private boolean executeBooleanGetMethod(IIoConfigurator configurator, String methodName){
101
102 Class<? extends IIoConfigurator> configuratorClass = configurator.getClass();
103
104 boolean result = false;
105
106 Method[] methods = configuratorClass.getMethods();
107
108 for(Method method : methods){
109 if(!method.getName().equals(methodName)){
110 continue;
111 }
112
113 try {
114 Object returnType = method.invoke(configurator, null);
115 if(returnType.getClass().equals(Boolean.class)){
116 result = ((Boolean) returnType).booleanValue();
117 }
118
119 break;
120 } catch (Exception e) {
121 logger.warn("Could not invoke method");
122 }
123 }
124
125
126 return result;
127 }
128
129 private void executeBooleanSetMethod(IIoConfigurator configurator, String methodName, boolean selected){
130
131 Class<? extends IIoConfigurator> configuratorClass = configurator.getClass();
132
133
134 Method[] methods = configuratorClass.getMethods();
135
136 for(Method method : methods){
137 if(!method.getName().equals(methodName)){
138 continue;
139 }
140
141 try {
142 method.invoke(configurator, selected);
143
144 break;
145 } catch (Exception e) {
146 logger.warn("Could not invoke method");
147 }
148 }
149 }
150
151 private List<Method> getConfiguratorsBooleanSetMethods(IIoConfigurator configurator){
152 List<Method> booleanMethods = new ArrayList<Method>();
153
154 Class<? extends IIoConfigurator> configuratorClass = configurator.getClass();
155
156 Method[] allMethods = configuratorClass.getMethods();
157
158 for(Method method : allMethods){
159 if(method.getName().startsWith("set")){
160
161 Class<?>[] typeList = method.getParameterTypes();
162
163 if(typeList.length > 1){
164 new IllegalStateException("Found a setter with parameter count > 1");
165 }
166
167 if(typeList[0].getName().equals("boolean")){
168 booleanMethods.add(method);
169 }
170 }
171 }
172
173 return booleanMethods;
174 }
175 }