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
;
17 import org
.eclipse
.jface
.wizard
.WizardPage
;
18 import org
.eclipse
.swt
.SWT
;
19 import org
.eclipse
.swt
.custom
.CLabel
;
20 import org
.eclipse
.swt
.events
.SelectionAdapter
;
21 import org
.eclipse
.swt
.events
.SelectionEvent
;
22 import org
.eclipse
.swt
.layout
.GridData
;
23 import org
.eclipse
.swt
.layout
.GridLayout
;
24 import org
.eclipse
.swt
.widgets
.Button
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
27 import eu
.etaxonomy
.cdm
.io
.common
.IIoConfigurator
;
28 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
31 * <p>GenericConfiguratorWizardPage class.</p>
37 public class GenericConfiguratorWizardPage
extends WizardPage
{
39 private IIoConfigurator configurator
;
45 private GenericConfiguratorWizardPage(String pageName
, IIoConfigurator configurator
, String title
, String description
) {
49 this.setDescription(description
);
51 this.configurator
= configurator
;
57 * @param pageName a {@link java.lang.String} object.
58 * @param configurator a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
59 * @return a {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage} object.
61 public static GenericConfiguratorWizardPage
Import(String pageName
, IIoConfigurator configurator
){
62 return new GenericConfiguratorWizardPage(pageName
, configurator
, "Import Configuration", "Configure the import mechanism.");
68 * @param pageName a {@link java.lang.String} object.
69 * @param configurator a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
70 * @return a {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage} object.
72 public static GenericConfiguratorWizardPage
Export(String pageName
, IIoConfigurator configurator
){
73 return new GenericConfiguratorWizardPage(pageName
, configurator
, "Export Configuration", "Configure the export mechanism.");
78 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
81 public void createControl(Composite parent
) {
83 // ScrolledComposite scrolledComposite = new ScrolledComposite(parent, SWT.H_SCROLL);
84 // scrolledComposite.setLayout(new GridLayout());
86 // TODO wrap this composite in a scrolled composite
87 Composite composite
= new Composite(parent
, SWT
.NULL
);
88 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
90 GridLayout gridLayout
= new GridLayout();
91 gridLayout
.numColumns
= 2;
92 composite
.setLayout(gridLayout
);
95 List
<Method
> methods
= getConfiguratorsBooleanSetMethods(configurator
);
97 for(Method method
: methods
){
98 createCheckbox(composite
, method
, configurator
);
101 // scrolledComposite.setContent(composite);
103 setControl(composite
);
106 private void createCheckbox(Composite parent
, Method method
, final IIoConfigurator configurator
){
108 String methodName
= method
.getName();
110 final String methodNameValue
= methodName
.substring(3);
112 CLabel label
= new CLabel(parent
, SWT
.NULL
);
113 label
.setText(methodNameValue
);
115 final Button checkBox
= new Button(parent
, SWT
.CHECK
);
116 // retrieve the default values and set the checkbox accordingly
117 boolean defaultSelection
= executeBooleanGetMethod(configurator
,"is" + methodNameValue
);
118 checkBox
.setSelection(defaultSelection
);
119 checkBox
.addSelectionListener(new SelectionAdapter(){
122 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
125 public void widgetSelected(SelectionEvent e
) {
126 executeBooleanSetMethod(configurator
, "set" + methodNameValue
, checkBox
.getSelection());
134 private boolean executeBooleanGetMethod(IIoConfigurator configurator
, String methodName
){
136 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
.getClass();
138 boolean result
= false;
140 Method
[] methods
= configuratorClass
.getMethods();
142 for(Method method
: methods
){
143 if(!method
.getName().equals(methodName
)){
148 Object returnType
= method
.invoke(configurator
, null);
149 if(returnType
.getClass().equals(Boolean
.class)){
150 result
= ((Boolean
) returnType
).booleanValue();
154 } catch (Exception e
) {
155 StoreUtil
.warn(this.getClass(), "Could not invoke method");
163 private void executeBooleanSetMethod(IIoConfigurator configurator
, String methodName
, boolean selected
){
165 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
.getClass();
168 Method
[] methods
= configuratorClass
.getMethods();
170 for(Method method
: methods
){
171 if(!method
.getName().equals(methodName
)){
176 method
.invoke(configurator
, selected
);
179 } catch (Exception e
) {
180 StoreUtil
.warn(this.getClass(), "Could not invoke method");
185 private List
<Method
> getConfiguratorsBooleanSetMethods(IIoConfigurator configurator
){
186 List
<Method
> booleanMethods
= new ArrayList
<Method
>();
188 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
.getClass();
190 Method
[] allMethods
= configuratorClass
.getMethods();
192 for(Method method
: allMethods
){
193 if(method
.getName().startsWith("set")){
195 Class
<?
>[] typeList
= method
.getParameterTypes();
197 if(typeList
.length
> 1){
198 new IllegalStateException("Found a setter with parameter count > 1");
201 if(typeList
[0].getName().equals("boolean")){
202 booleanMethods
.add(method
);
207 return booleanMethods
;