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.
9 package eu
.etaxonomy
.taxeditor
.io
.e4
.in
;
11 import java
.lang
.reflect
.Method
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collections
;
14 import java
.util
.List
;
15 import java
.util
.MissingResourceException
;
16 import java
.util
.ResourceBundle
;
18 import javax
.inject
.Inject
;
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
.taxeditor
.model
.MessagingUtils
;
36 public class GenericConfiguratorWizardPageE4
extends WizardPage
{
38 private IIoConfigurator configurator
;
39 private ResourceBundle resourceBundle
;
40 private List
<String
> ignoreMethods
;
43 public GenericConfiguratorWizardPageE4() {
44 super("Configuration");
47 private void init(String title
, String description
, IIoConfigurator configurator
, List
<String
> ignoreMethods
){
49 this.setDescription(description
);
50 this.configurator
= configurator
;
51 this.ignoreMethods
= ignoreMethods
;
52 resourceBundle
= getResourceBundle(configurator
);
55 private ResourceBundle
getResourceBundle(IIoConfigurator configurator
){
57 if (configurator
!= null){
58 return ResourceBundle
.getBundle(configurator
.getClass().getName());
60 return ResourceBundle
.getBundle("Configuration");
62 }catch(MissingResourceException e
){
67 public void initImport(IIoConfigurator configurator
, List
<String
> ignoreMethods
, String label
) {
68 init("Import Configuration", label
, configurator
, ignoreMethods
);
71 public void initExport(IIoConfigurator configurator
, List
<String
> ignoreMethods
) {
72 init("Export Configuration", "Configure the export mechanism.", configurator
, ignoreMethods
);
76 public void createControl(Composite parent
) {
78 // TODO wrap this composite in a scrolled composite
79 Composite composite
= new Composite(parent
, SWT
.NULL
);
80 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
82 GridLayout gridLayout
= new GridLayout();
83 composite
.setLayout(gridLayout
);
85 List
<Method
> methods
= getConfiguratorsBooleanSetMethods(configurator
);
86 Collections
.sort(methods
, (o1
,o2
)-> {
90 if(o1
.getName()==null && o2
.getName()!=null){
93 if(o1
.getName()!=null && o2
.getName()==null){
96 if(o1
.getName()==null && o2
.getName()==null){
97 return o1
.toString().compareTo(o1
.toString());
99 int result
= o1
.getName().compareTo(o2
.getName());
101 return o1
.toString().compareTo(o1
.toString());
106 for (Method method
: methods
) {
107 if (ignoreMethods
!= null){
108 if (!ignoreMethods
.contains( method
.getName())) {
109 createCheckbox(composite
, method
, configurator
);
112 createCheckbox(composite
, method
, configurator
);
116 setControl(composite
);
119 private void createCheckbox(Composite parent
, Method method
,
120 final IIoConfigurator configurator
) {
122 final String methodName
= method
.getName();
123 final Button checkBox
= new Button(parent
, SWT
.CHECK
);
125 // if(configurator.getClass().equals(Abcd206ImportConfigurator.class)){
126 checkBox
.setText(getLabel(splitMethodName(methodName
)));
127 // retrieve the default values and set the checkbox accordingly
128 boolean defaultSelection
= executeBooleanGetMethod(configurator
, "is"
129 + methodName
.substring(3));
130 checkBox
.setSelection(defaultSelection
);
131 checkBox
.addSelectionListener(new SelectionAdapter() {
133 public void widgetSelected(SelectionEvent e
) {
134 executeBooleanSetMethod(configurator
, methodName
,
135 checkBox
.getSelection());
140 private String
getLabel(String methodName
){
142 if(resourceBundle
== null){
147 return resourceBundle
.getString(methodName
);
148 }catch(MissingResourceException e
){
153 private boolean executeBooleanGetMethod(IIoConfigurator configurator
,
156 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
.getClass();
158 boolean result
= false;
160 Method
[] methods
= configuratorClass
.getMethods();
162 for (Method method
: methods
) {
163 if (!method
.getName().equals(methodName
)) {
168 Object returnType
= method
.invoke(configurator
, null);
169 if (returnType
.getClass().equals(Boolean
.class)) {
170 result
= ((Boolean
) returnType
).booleanValue();
174 } catch (Exception e
) {
175 MessagingUtils
.warn(this.getClass(), "Could not invoke method");
182 private void executeBooleanSetMethod(IIoConfigurator configurator
,
183 String methodName
, boolean selected
) {
185 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
188 Method
[] methods
= configuratorClass
.getMethods();
190 for (Method method
: methods
) {
191 if (!method
.getName().equals(methodName
)) {
196 method
.invoke(configurator
, selected
);
199 } catch (Exception e
) {
200 MessagingUtils
.warn(this.getClass(), "Could not invoke method");
205 private List
<Method
> getConfiguratorsBooleanSetMethods(
206 IIoConfigurator configurator
) {
207 List
<Method
> booleanMethods
= new ArrayList
<Method
>();
209 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
212 Method
[] allMethods
= configuratorClass
.getMethods();
214 for (Method method
: allMethods
) {
215 if (method
.getName().startsWith("set")) {
217 Class
<?
>[] typeList
= method
.getParameterTypes();
218 //this is irrelevant for methods without boolean parameters
219 if (typeList
.length
> 1 && typeList
[0].getName().equals("boolean")) {
220 throw new IllegalStateException(
221 "Found a setter with parameter count > 1");
224 if (typeList
[0].getName().equals("boolean")) {
225 booleanMethods
.add(method
);
230 return booleanMethods
;
233 private String
splitMethodName(String s
) {
234 s
= s
.replace("set", "");
236 String
.format("%s|%s|%s",
237 "(?<=[A-Z])(?=[A-Z][a-z])",
238 "(?<=[^A-Z])(?=[A-Z])",
239 "(?<=[A-Za-z])(?=[^A-Za-z])"