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