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
.eclipse
.jface
.wizard
.WizardPage
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.events
.SelectionAdapter
;
22 import org
.eclipse
.swt
.events
.SelectionEvent
;
23 import org
.eclipse
.swt
.layout
.GridData
;
24 import org
.eclipse
.swt
.layout
.GridLayout
;
25 import org
.eclipse
.swt
.widgets
.Button
;
26 import org
.eclipse
.swt
.widgets
.Composite
;
28 import eu
.etaxonomy
.cdm
.io
.common
.IIoConfigurator
;
29 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
33 * GenericConfiguratorWizardPage class.
40 public class GenericConfiguratorWizardPage
extends WizardPage
{
42 private final IIoConfigurator configurator
;
43 private final ResourceBundle resourceBundle
;
49 private GenericConfiguratorWizardPage(String pageName
,
50 IIoConfigurator configurator
, String title
, String description
) {
54 this.setDescription(description
);
56 this.configurator
= configurator
;
59 resourceBundle
= getResourceBundle(configurator
);
62 private ResourceBundle
getResourceBundle(IIoConfigurator configurator
){
64 return ResourceBundle
.getBundle(configurator
.getClass().getName());
65 }catch(MissingResourceException e
){
76 * a {@link java.lang.String} object.
78 * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
80 * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
83 public static GenericConfiguratorWizardPage
Import(String pageName
,
84 IIoConfigurator configurator
) {
85 return new GenericConfiguratorWizardPage(pageName
, configurator
,
86 "Import Configuration", "Configure the import mechanism.");
95 * a {@link java.lang.String} object.
97 * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
99 * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
102 public static GenericConfiguratorWizardPage
Export(String pageName
,
103 IIoConfigurator configurator
) {
104 return new GenericConfiguratorWizardPage(pageName
, configurator
,
105 "Export Configuration", "Configure the export mechanism.");
112 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
116 public void createControl(Composite parent
) {
118 // ScrolledComposite scrolledComposite = new ScrolledComposite(parent,
120 // scrolledComposite.setLayout(new GridLayout());
122 // TODO wrap this composite in a scrolled composite
123 Composite composite
= new Composite(parent
, SWT
.NULL
);
124 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
126 GridLayout gridLayout
= new GridLayout();
127 composite
.setLayout(gridLayout
);
129 List
<Method
> methods
= getConfiguratorsBooleanSetMethods(configurator
);
131 for (Method method
: methods
) {
132 createCheckbox(composite
, method
, configurator
);
135 // scrolledComposite.setContent(composite);
137 setControl(composite
);
140 private void createCheckbox(Composite parent
, Method method
,
141 final IIoConfigurator configurator
) {
143 final String methodName
= method
.getName();
145 final Button checkBox
= new Button(parent
, SWT
.CHECK
);
147 checkBox
.setText(getLabel(methodName
));
148 // retrieve the default values and set the checkbox accordingly
149 boolean defaultSelection
= executeBooleanGetMethod(configurator
, "is"
150 + methodName
.substring(3));
151 checkBox
.setSelection(defaultSelection
);
152 checkBox
.addSelectionListener(new SelectionAdapter() {
158 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
159 * .swt.events.SelectionEvent)
162 public void widgetSelected(SelectionEvent e
) {
163 executeBooleanSetMethod(configurator
, methodName
,
164 checkBox
.getSelection());
171 private String
getLabel(String methodName
){
173 if(resourceBundle
== null){
178 return resourceBundle
.getString(methodName
);
179 }catch(MissingResourceException e
){
184 private boolean executeBooleanGetMethod(IIoConfigurator configurator
,
187 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
190 boolean result
= false;
192 Method
[] methods
= configuratorClass
.getMethods();
194 for (Method method
: methods
) {
195 if (!method
.getName().equals(methodName
)) {
200 Object returnType
= method
.invoke(configurator
, null);
201 if (returnType
.getClass().equals(Boolean
.class)) {
202 result
= ((Boolean
) returnType
).booleanValue();
206 } catch (Exception e
) {
207 StoreUtil
.warn(this.getClass(), "Could not invoke method");
214 private void executeBooleanSetMethod(IIoConfigurator configurator
,
215 String methodName
, boolean selected
) {
217 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
220 Method
[] methods
= configuratorClass
.getMethods();
222 for (Method method
: methods
) {
223 if (!method
.getName().equals(methodName
)) {
228 method
.invoke(configurator
, selected
);
231 } catch (Exception e
) {
232 StoreUtil
.warn(this.getClass(), "Could not invoke method");
237 private List
<Method
> getConfiguratorsBooleanSetMethods(
238 IIoConfigurator configurator
) {
239 List
<Method
> booleanMethods
= new ArrayList
<Method
>();
241 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
244 Method
[] allMethods
= configuratorClass
.getMethods();
246 for (Method method
: allMethods
) {
247 if (method
.getName().startsWith("set")) {
249 Class
<?
>[] typeList
= method
.getParameterTypes();
251 if (typeList
.length
> 1) {
252 new IllegalStateException(
253 "Found a setter with parameter count > 1");
256 if (typeList
[0].getName().equals("boolean")) {
257 booleanMethods
.add(method
);
262 return booleanMethods
;