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
.e4
;
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 javax
.inject
.Inject
;
22 import org
.apache
.commons
.lang
.StringUtils
;
23 import org
.eclipse
.jface
.wizard
.WizardPage
;
24 import org
.eclipse
.swt
.SWT
;
25 import org
.eclipse
.swt
.events
.SelectionAdapter
;
26 import org
.eclipse
.swt
.events
.SelectionEvent
;
27 import org
.eclipse
.swt
.layout
.GridData
;
28 import org
.eclipse
.swt
.layout
.GridLayout
;
29 import org
.eclipse
.swt
.widgets
.Button
;
30 import org
.eclipse
.swt
.widgets
.Composite
;
32 import eu
.etaxonomy
.cdm
.io
.common
.IIoConfigurator
;
33 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
41 public class GenericConfiguratorWizardPageE4
extends WizardPage
{
43 private IIoConfigurator configurator
;
44 private ResourceBundle resourceBundle
;
45 private List
<String
> ignoreMethods
;
48 private GenericConfiguratorWizardPageE4() {
49 super("Configuration");
52 public void init(String title
, String description
, IIoConfigurator configurator
, List
<String
> ignoreMethods
){
54 this.setDescription(description
);
55 this.configurator
= configurator
;
56 this.ignoreMethods
= ignoreMethods
;
57 resourceBundle
= getResourceBundle(configurator
);
60 private ResourceBundle
getResourceBundle(IIoConfigurator configurator
){
62 if (configurator
!= null){
63 return ResourceBundle
.getBundle(configurator
.getClass().getName());
65 return ResourceBundle
.getBundle("Configuration");
67 }catch(MissingResourceException e
){
72 public void initImport(IIoConfigurator configurator
, List
<String
> ignoreMethods
, String label
) {
73 init("Import Configuration", label
, configurator
, ignoreMethods
);
76 public void initExport(IIoConfigurator configurator
, List
<String
> ignoreMethods
) {
77 init("Export Configuration", "Configure the export mechanism.", configurator
, ignoreMethods
);
82 public void createControl(Composite parent
) {
84 // TODO wrap this composite in a scrolled composite
85 Composite composite
= new Composite(parent
, SWT
.NULL
);
86 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
88 GridLayout gridLayout
= new GridLayout();
89 composite
.setLayout(gridLayout
);
91 List
<Method
> methods
= getConfiguratorsBooleanSetMethods(configurator
);
92 Collections
.sort(methods
, new Comparator
<Method
>() {
94 public int compare(Method o1
, Method o2
) {
98 if(o1
.getName()==null && o2
.getName()!=null){
101 if(o1
.getName()!=null && o2
.getName()==null){
104 if(o1
.getName()==null && o2
.getName()==null){
105 return o1
.toString().compareTo(o1
.toString());
107 int result
= o1
.getName().compareTo(o2
.getName());
109 return o1
.toString().compareTo(o1
.toString());
115 for (Method method
: methods
) {
116 if (ignoreMethods
!= null){
117 if (!ignoreMethods
.contains( method
.getName())) {
118 createCheckbox(composite
, method
, configurator
);
121 createCheckbox(composite
, method
, configurator
);
126 setControl(composite
);
129 private void createCheckbox(Composite parent
, Method method
,
130 final IIoConfigurator configurator
) {
132 final String methodName
= method
.getName();
133 final Button checkBox
= new Button(parent
, SWT
.CHECK
);
135 // if(configurator.getClass().equals(Abcd206ImportConfigurator.class)){
136 String
[] r
= methodName
.split("set")[1].split("(?=\\p{Upper})");
137 checkBox
.setText(getLabel(StringUtils
.join(r
," ")));
140 // checkBox.setText(getLabel(methodName));
142 // retrieve the default values and set the checkbox accordingly
143 boolean defaultSelection
= executeBooleanGetMethod(configurator
, "is"
144 + methodName
.substring(3));
145 checkBox
.setSelection(defaultSelection
);
146 checkBox
.addSelectionListener(new SelectionAdapter() {
149 public void widgetSelected(SelectionEvent e
) {
150 executeBooleanSetMethod(configurator
, methodName
,
151 checkBox
.getSelection());
158 private String
getLabel(String methodName
){
160 if(resourceBundle
== null){
165 return resourceBundle
.getString(methodName
);
166 }catch(MissingResourceException e
){
171 private boolean executeBooleanGetMethod(IIoConfigurator configurator
,
174 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
177 boolean result
= false;
179 Method
[] methods
= configuratorClass
.getMethods();
181 for (Method method
: methods
) {
182 if (!method
.getName().equals(methodName
)) {
187 Object returnType
= method
.invoke(configurator
, null);
188 if (returnType
.getClass().equals(Boolean
.class)) {
189 result
= ((Boolean
) returnType
).booleanValue();
193 } catch (Exception e
) {
194 MessagingUtils
.warn(this.getClass(), "Could not invoke method");
201 private void executeBooleanSetMethod(IIoConfigurator configurator
,
202 String methodName
, boolean selected
) {
204 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
207 Method
[] methods
= configuratorClass
.getMethods();
209 for (Method method
: methods
) {
210 if (!method
.getName().equals(methodName
)) {
215 method
.invoke(configurator
, selected
);
218 } catch (Exception e
) {
219 MessagingUtils
.warn(this.getClass(), "Could not invoke method");
224 private List
<Method
> getConfiguratorsBooleanSetMethods(
225 IIoConfigurator configurator
) {
226 List
<Method
> booleanMethods
= new ArrayList
<Method
>();
228 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
231 Method
[] allMethods
= configuratorClass
.getMethods();
233 for (Method method
: allMethods
) {
234 if (method
.getName().startsWith("set")) {
236 Class
<?
>[] typeList
= method
.getParameterTypes();
238 if (typeList
.length
> 1) {
239 new IllegalStateException(
240 "Found a setter with parameter count > 1");
243 if (typeList
[0].getName().equals("boolean")) {
244 booleanMethods
.add(method
);
249 return booleanMethods
;