3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.io
.wizard
;
13 import java
.lang
.reflect
.Method
;
14 import java
.util
.ArrayList
;
15 import java
.util
.List
;
16 import java
.util
.MissingResourceException
;
17 import java
.util
.ResourceBundle
;
19 import org
.apache
.commons
.lang
.StringUtils
;
20 import org
.eclipse
.jface
.wizard
.WizardPage
;
21 import org
.eclipse
.swt
.SWT
;
22 import org
.eclipse
.swt
.events
.SelectionAdapter
;
23 import org
.eclipse
.swt
.events
.SelectionEvent
;
24 import org
.eclipse
.swt
.layout
.GridData
;
25 import org
.eclipse
.swt
.layout
.GridLayout
;
26 import org
.eclipse
.swt
.widgets
.Button
;
27 import org
.eclipse
.swt
.widgets
.Composite
;
29 import eu
.etaxonomy
.cdm
.io
.common
.IIoConfigurator
;
30 import eu
.etaxonomy
.cdm
.io
.specimen
.abcd206
.in
.Abcd206ImportConfigurator
;
31 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
35 * GenericConfiguratorWizardPage class.
42 public class GenericConfiguratorWizardPage
extends WizardPage
{
44 private final IIoConfigurator configurator
;
45 private final ResourceBundle resourceBundle
;
51 private GenericConfiguratorWizardPage(String pageName
,
52 IIoConfigurator configurator
, String title
, String description
) {
56 this.setDescription(description
);
58 this.configurator
= configurator
;
61 resourceBundle
= getResourceBundle(configurator
);
64 private ResourceBundle
getResourceBundle(IIoConfigurator configurator
){
66 return ResourceBundle
.getBundle(configurator
.getClass().getName());
67 }catch(MissingResourceException e
){
78 * a {@link java.lang.String} object.
80 * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
82 * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
85 public static GenericConfiguratorWizardPage
Import(String pageName
,
86 IIoConfigurator configurator
) {
87 return new GenericConfiguratorWizardPage(pageName
, configurator
,
88 "Import Configuration", "Configure the import mechanism.");
97 * a {@link java.lang.String} object.
99 * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
101 * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
104 public static GenericConfiguratorWizardPage
Export(String pageName
,
105 IIoConfigurator configurator
) {
106 return new GenericConfiguratorWizardPage(pageName
, configurator
,
107 "Export Configuration", "Configure the export mechanism.");
114 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
119 public void createControl(Composite parent
) {
121 // ScrolledComposite scrolledComposite = new ScrolledComposite(parent,
123 // scrolledComposite.setLayout(new GridLayout());
125 // TODO wrap this composite in a scrolled composite
126 Composite composite
= new Composite(parent
, SWT
.NULL
);
127 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
129 GridLayout gridLayout
= new GridLayout();
130 composite
.setLayout(gridLayout
);
132 List
<Method
> methods
= getConfiguratorsBooleanSetMethods(configurator
);
134 for (Method method
: methods
) {
135 createCheckbox(composite
, method
, configurator
);
138 // scrolledComposite.setContent(composite);
140 setControl(composite
);
143 private void createCheckbox(Composite parent
, Method method
,
144 final IIoConfigurator configurator
) {
146 final String methodName
= method
.getName();
147 final Button checkBox
= new Button(parent
, SWT
.CHECK
);
149 if(configurator
.getClass().equals(Abcd206ImportConfigurator
.class)){
150 String
[] r
= methodName
.split("set")[1].split("(?=\\p{Upper})");
151 checkBox
.setText(getLabel(StringUtils
.join(r
," ")));
154 checkBox
.setText(getLabel(methodName
));
156 // retrieve the default values and set the checkbox accordingly
157 boolean defaultSelection
= executeBooleanGetMethod(configurator
, "is"
158 + methodName
.substring(3));
159 checkBox
.setSelection(defaultSelection
);
160 checkBox
.addSelectionListener(new SelectionAdapter() {
166 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
167 * .swt.events.SelectionEvent)
170 public void widgetSelected(SelectionEvent e
) {
171 executeBooleanSetMethod(configurator
, methodName
,
172 checkBox
.getSelection());
179 private String
getLabel(String methodName
){
181 if(resourceBundle
== null){
186 return resourceBundle
.getString(methodName
);
187 }catch(MissingResourceException e
){
192 private boolean executeBooleanGetMethod(IIoConfigurator configurator
,
195 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
198 boolean result
= false;
200 Method
[] methods
= configuratorClass
.getMethods();
202 for (Method method
: methods
) {
203 if (!method
.getName().equals(methodName
)) {
208 Object returnType
= method
.invoke(configurator
, null);
209 if (returnType
.getClass().equals(Boolean
.class)) {
210 result
= ((Boolean
) returnType
).booleanValue();
214 } catch (Exception e
) {
215 StoreUtil
.warn(this.getClass(), "Could not invoke method");
222 private void executeBooleanSetMethod(IIoConfigurator configurator
,
223 String methodName
, boolean selected
) {
225 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
228 Method
[] methods
= configuratorClass
.getMethods();
230 for (Method method
: methods
) {
231 if (!method
.getName().equals(methodName
)) {
236 method
.invoke(configurator
, selected
);
239 } catch (Exception e
) {
240 StoreUtil
.warn(this.getClass(), "Could not invoke method");
245 private List
<Method
> getConfiguratorsBooleanSetMethods(
246 IIoConfigurator configurator
) {
247 List
<Method
> booleanMethods
= new ArrayList
<Method
>();
249 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
252 Method
[] allMethods
= configuratorClass
.getMethods();
254 for (Method method
: allMethods
) {
255 if (method
.getName().startsWith("set")) {
257 Class
<?
>[] typeList
= method
.getParameterTypes();
259 if (typeList
.length
> 1) {
260 new IllegalStateException(
261 "Found a setter with parameter count > 1");
264 if (typeList
[0].getName().equals("boolean")) {
265 booleanMethods
.add(method
);
270 return booleanMethods
;