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