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