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
;
13 import java
.lang
.reflect
.Method
;
14 import java
.util
.ArrayList
;
15 import java
.util
.List
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.eclipse
.jface
.wizard
.WizardPage
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.custom
.CLabel
;
21 import org
.eclipse
.swt
.events
.SelectionAdapter
;
22 import org
.eclipse
.swt
.events
.SelectionEvent
;
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
;
34 public class GenericConfiguratorWizardPage
extends WizardPage
{
37 private static final Logger logger
= Logger
38 .getLogger(GenericConfiguratorWizardPage
.class);
40 private IIoConfigurator configurator
;
46 protected GenericConfiguratorWizardPage(String pageName
, IIoConfigurator configurator
) {
48 this.setTitle("Export Configuration");
50 this.setDescription("Configure the export mechanism.");
52 this.configurator
= configurator
;
56 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
58 public void createControl(Composite parent
) {
60 Composite composite
= new Composite(parent
, SWT
.NULL
);
61 GridLayout gridLayout
= new GridLayout();
62 gridLayout
.numColumns
= 2;
63 composite
.setLayout(gridLayout
);
65 List
<Method
> methods
= getConfiguratorsBooleanSetMethods(configurator
);
67 for(Method method
: methods
){
68 createCheckbox(composite
, method
, configurator
);
71 setControl(composite
);
74 private void createCheckbox(Composite parent
, Method method
, final IIoConfigurator configurator
){
76 String methodName
= method
.getName();
78 final String methodNameValue
= methodName
.substring(3);
80 CLabel label
= new CLabel(parent
, SWT
.NULL
);
81 label
.setText(methodNameValue
);
83 final Button checkBox
= new Button(parent
, SWT
.CHECK
);
84 checkBox
.setSelection(executeBooleanGetMethod(configurator
,"is" + methodNameValue
));
85 checkBox
.addSelectionListener(new SelectionAdapter(){
88 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
91 public void widgetSelected(SelectionEvent e
) {
92 executeBooleanSetMethod(configurator
, "set" + methodNameValue
, checkBox
.getSelection());
100 private boolean executeBooleanGetMethod(IIoConfigurator configurator
, String methodName
){
102 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
.getClass();
104 boolean result
= false;
106 Method
[] methods
= configuratorClass
.getMethods();
108 for(Method method
: methods
){
109 if(!method
.getName().equals(methodName
)){
114 Object returnType
= method
.invoke(configurator
, null);
115 if(returnType
.getClass().equals(Boolean
.class)){
116 result
= ((Boolean
) returnType
).booleanValue();
120 } catch (Exception e
) {
121 logger
.warn("Could not invoke method");
129 private void executeBooleanSetMethod(IIoConfigurator configurator
, String methodName
, boolean selected
){
131 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
.getClass();
134 Method
[] methods
= configuratorClass
.getMethods();
136 for(Method method
: methods
){
137 if(!method
.getName().equals(methodName
)){
142 method
.invoke(configurator
, selected
);
145 } catch (Exception e
) {
146 logger
.warn("Could not invoke method");
151 private List
<Method
> getConfiguratorsBooleanSetMethods(IIoConfigurator configurator
){
152 List
<Method
> booleanMethods
= new ArrayList
<Method
>();
154 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
.getClass();
156 Method
[] allMethods
= configuratorClass
.getMethods();
158 for(Method method
: allMethods
){
159 if(method
.getName().startsWith("set")){
161 Class
<?
>[] typeList
= method
.getParameterTypes();
163 if(typeList
.length
> 1){
164 new IllegalStateException("Found a setter with parameter count > 1");
167 if(typeList
[0].getName().equals("boolean")){
168 booleanMethods
.add(method
);
173 return booleanMethods
;