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
;
52 private GenericConfiguratorWizardPage(String pageName
,
53 IIoConfigurator configurator
, String title
, String description
) {
57 this.setDescription(description
);
59 this.configurator
= configurator
;
62 resourceBundle
= getResourceBundle(configurator
);
65 private ResourceBundle
getResourceBundle(IIoConfigurator configurator
){
67 return ResourceBundle
.getBundle(configurator
.getClass().getName());
68 }catch(MissingResourceException e
){
79 * a {@link java.lang.String} object.
81 * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
83 * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
86 public static GenericConfiguratorWizardPage
Import(String pageName
,
87 IIoConfigurator configurator
) {
88 return new GenericConfiguratorWizardPage(pageName
, configurator
,
89 "Import Configuration", "Configure the import mechanism.");
98 * a {@link java.lang.String} object.
100 * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
102 * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
105 public static GenericConfiguratorWizardPage
Export(String pageName
,
106 IIoConfigurator configurator
) {
107 return new GenericConfiguratorWizardPage(pageName
, configurator
,
108 "Export Configuration", "Configure the export mechanism.");
115 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
120 public void createControl(Composite parent
) {
122 // ScrolledComposite scrolledComposite = new ScrolledComposite(parent,
124 // scrolledComposite.setLayout(new GridLayout());
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));
130 GridLayout gridLayout
= new GridLayout();
131 composite
.setLayout(gridLayout
);
133 List
<Method
> methods
= getConfiguratorsBooleanSetMethods(configurator
);
134 Collections
.sort(methods
, new Comparator
<Method
>() {
136 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
139 public int compare(Method o1
, Method o2
) {
140 if(o1
.getName()==null && o2
.getName()!=null){
143 if(o1
.getName()!=null && o2
.getName()==null){
146 if(o1
.getName()==null && o2
.getName()==null){
149 return o1
.getName().compareTo(o2
.getName());
153 for (Method method
: methods
) {
154 createCheckbox(composite
, method
, configurator
);
157 // scrolledComposite.setContent(composite);
159 setControl(composite
);
162 private void createCheckbox(Composite parent
, Method method
,
163 final IIoConfigurator configurator
) {
165 final String methodName
= method
.getName();
166 final Button checkBox
= new Button(parent
, SWT
.CHECK
);
168 if(configurator
.getClass().equals(Abcd206ImportConfigurator
.class)){
169 String
[] r
= methodName
.split("set")[1].split("(?=\\p{Upper})");
170 checkBox
.setText(getLabel(StringUtils
.join(r
," ")));
173 checkBox
.setText(getLabel(methodName
));
175 // retrieve the default values and set the checkbox accordingly
176 boolean defaultSelection
= executeBooleanGetMethod(configurator
, "is"
177 + methodName
.substring(3));
178 checkBox
.setSelection(defaultSelection
);
179 checkBox
.addSelectionListener(new SelectionAdapter() {
185 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
186 * .swt.events.SelectionEvent)
189 public void widgetSelected(SelectionEvent e
) {
190 executeBooleanSetMethod(configurator
, methodName
,
191 checkBox
.getSelection());
198 private String
getLabel(String methodName
){
200 if(resourceBundle
== null){
205 return resourceBundle
.getString(methodName
);
206 }catch(MissingResourceException e
){
211 private boolean executeBooleanGetMethod(IIoConfigurator configurator
,
214 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
217 boolean result
= false;
219 Method
[] methods
= configuratorClass
.getMethods();
221 for (Method method
: methods
) {
222 if (!method
.getName().equals(methodName
)) {
227 Object returnType
= method
.invoke(configurator
, null);
228 if (returnType
.getClass().equals(Boolean
.class)) {
229 result
= ((Boolean
) returnType
).booleanValue();
233 } catch (Exception e
) {
234 MessagingUtils
.warn(this.getClass(), "Could not invoke method");
241 private void executeBooleanSetMethod(IIoConfigurator configurator
,
242 String methodName
, boolean selected
) {
244 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
247 Method
[] methods
= configuratorClass
.getMethods();
249 for (Method method
: methods
) {
250 if (!method
.getName().equals(methodName
)) {
255 method
.invoke(configurator
, selected
);
258 } catch (Exception e
) {
259 MessagingUtils
.warn(this.getClass(), "Could not invoke method");
264 private List
<Method
> getConfiguratorsBooleanSetMethods(
265 IIoConfigurator configurator
) {
266 List
<Method
> booleanMethods
= new ArrayList
<Method
>();
268 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
271 Method
[] allMethods
= configuratorClass
.getMethods();
273 for (Method method
: allMethods
) {
274 if (method
.getName().startsWith("set")) {
276 Class
<?
>[] typeList
= method
.getParameterTypes();
278 if (typeList
.length
> 1) {
279 new IllegalStateException(
280 "Found a setter with parameter count > 1");
283 if (typeList
[0].getName().equals("boolean")) {
284 booleanMethods
.add(method
);
289 return booleanMethods
;