2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.io
.wizard
;
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
;
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
;
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
;
36 * GenericConfiguratorWizardPage class.
43 public class GenericConfiguratorWizardPage
extends WizardPage
{
45 private final IIoConfigurator configurator
;
46 private final ResourceBundle resourceBundle
;
47 private final List
<String
> ignoreMethods
;
53 private GenericConfiguratorWizardPage(String pageName
,
54 IIoConfigurator configurator
, String title
, String description
, List
<String
> ignoreMethods
) {
58 this.setDescription(description
);
60 this.configurator
= configurator
;
61 this.ignoreMethods
= ignoreMethods
;
64 resourceBundle
= getResourceBundle(configurator
);
67 private ResourceBundle
getResourceBundle(IIoConfigurator configurator
){
69 if (configurator
!= null){
70 return ResourceBundle
.getBundle(configurator
.getClass().getName());
72 return ResourceBundle
.getBundle("Configuration");
74 }catch(MissingResourceException e
){
85 * a {@link java.lang.String} object.
87 * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
89 * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
92 public static GenericConfiguratorWizardPage
Import(String pageName
,
93 IIoConfigurator configurator
, List
<String
> ignoreMethods
, String label
) {
94 return new GenericConfiguratorWizardPage(pageName
, configurator
,
95 "Import Configuration", label
, ignoreMethods
);
104 * a {@link java.lang.String} object.
105 * @param configurator
106 * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
108 * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
111 public static GenericConfiguratorWizardPage
Export(String pageName
,
112 IIoConfigurator configurator
, List
<String
> ignoreMethods
) {
113 return new GenericConfiguratorWizardPage(pageName
, configurator
,
114 "Export Configuration", "Configure the export mechanism.", ignoreMethods
);
121 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
126 public void createControl(Composite parent
) {
128 // ScrolledComposite scrolledComposite = new ScrolledComposite(parent,
130 // scrolledComposite.setLayout(new GridLayout());
132 // TODO wrap this composite in a scrolled composite
133 Composite composite
= new Composite(parent
, SWT
.NULL
);
134 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
136 GridLayout gridLayout
= new GridLayout();
137 composite
.setLayout(gridLayout
);
139 List
<Method
> methods
= getConfiguratorsBooleanSetMethods(configurator
);
140 Collections
.sort(methods
, new Comparator
<Method
>() {
142 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
145 public int compare(Method o1
, Method o2
) {
149 if(o1
.getName()==null && o2
.getName()!=null){
152 if(o1
.getName()!=null && o2
.getName()==null){
155 if(o1
.getName()==null && o2
.getName()==null){
156 return o1
.toString().compareTo(o1
.toString());
158 int result
= o1
.getName().compareTo(o2
.getName());
160 return o1
.toString().compareTo(o1
.toString());
166 for (Method method
: methods
) {
167 if (!ignoreMethods
.contains( method
.getName())) {
168 createCheckbox(composite
, method
, configurator
);
172 // scrolledComposite.setContent(composite);
174 setControl(composite
);
177 private void createCheckbox(Composite parent
, Method method
,
178 final IIoConfigurator configurator
) {
180 final String methodName
= method
.getName();
181 final Button checkBox
= new Button(parent
, SWT
.CHECK
);
183 if(configurator
.getClass().equals(Abcd206ImportConfigurator
.class)){
184 String
[] r
= methodName
.split("set")[1].split("(?=\\p{Upper})");
185 checkBox
.setText(getLabel(StringUtils
.join(r
," ")));
188 checkBox
.setText(getLabel(methodName
));
190 // retrieve the default values and set the checkbox accordingly
191 boolean defaultSelection
= executeBooleanGetMethod(configurator
, "is"
192 + methodName
.substring(3));
193 checkBox
.setSelection(defaultSelection
);
194 checkBox
.addSelectionListener(new SelectionAdapter() {
200 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
201 * .swt.events.SelectionEvent)
204 public void widgetSelected(SelectionEvent e
) {
205 executeBooleanSetMethod(configurator
, methodName
,
206 checkBox
.getSelection());
213 private String
getLabel(String methodName
){
215 if(resourceBundle
== null){
220 return resourceBundle
.getString(methodName
);
221 }catch(MissingResourceException e
){
226 private boolean executeBooleanGetMethod(IIoConfigurator configurator
,
229 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
232 boolean result
= false;
234 Method
[] methods
= configuratorClass
.getMethods();
236 for (Method method
: methods
) {
237 if (!method
.getName().equals(methodName
)) {
242 Object returnType
= method
.invoke(configurator
, null);
243 if (returnType
.getClass().equals(Boolean
.class)) {
244 result
= ((Boolean
) returnType
).booleanValue();
248 } catch (Exception e
) {
249 MessagingUtils
.warn(this.getClass(), "Could not invoke method");
256 private void executeBooleanSetMethod(IIoConfigurator configurator
,
257 String methodName
, boolean selected
) {
259 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
262 Method
[] methods
= configuratorClass
.getMethods();
264 for (Method method
: methods
) {
265 if (!method
.getName().equals(methodName
)) {
270 method
.invoke(configurator
, selected
);
273 } catch (Exception e
) {
274 MessagingUtils
.warn(this.getClass(), "Could not invoke method");
279 private List
<Method
> getConfiguratorsBooleanSetMethods(
280 IIoConfigurator configurator
) {
281 List
<Method
> booleanMethods
= new ArrayList
<Method
>();
283 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
286 Method
[] allMethods
= configuratorClass
.getMethods();
288 for (Method method
: allMethods
) {
289 if (method
.getName().startsWith("set")) {
291 Class
<?
>[] typeList
= method
.getParameterTypes();
293 if (typeList
.length
> 1) {
294 new IllegalStateException(
295 "Found a setter with parameter count > 1");
298 if (typeList
[0].getName().equals("boolean")) {
299 booleanMethods
.add(method
);
304 return booleanMethods
;