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
.wizard
;
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 org
.apache
.commons
.lang
.StringUtils
;
21 import org
.eclipse
.jface
.wizard
.WizardPage
;
22 import org
.eclipse
.swt
.SWT
;
23 import org
.eclipse
.swt
.events
.SelectionAdapter
;
24 import org
.eclipse
.swt
.events
.SelectionEvent
;
25 import org
.eclipse
.swt
.layout
.GridData
;
26 import org
.eclipse
.swt
.layout
.GridLayout
;
27 import org
.eclipse
.swt
.widgets
.Button
;
28 import org
.eclipse
.swt
.widgets
.Composite
;
30 import eu
.etaxonomy
.cdm
.io
.common
.IIoConfigurator
;
31 import eu
.etaxonomy
.cdm
.io
.specimen
.abcd206
.in
.Abcd206ImportConfigurator
;
32 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
36 * GenericConfiguratorWizardPage class.
43 public class GenericConfiguratorWizardPage
extends WizardPage
{
45 private final IIoConfigurator configurator
;
46 private final ResourceBundle resourceBundle
;
47 private final List
<String
> ignoreMethods
;
53 private GenericConfiguratorWizardPage(String pageName
,
54 IIoConfigurator configurator
, String title
, String description
, List
<String
> ignoreMethods
) {
58 this.setDescription(description
);
60 this.configurator
= configurator
;
61 this.ignoreMethods
= ignoreMethods
;
64 resourceBundle
= getResourceBundle(configurator
);
67 private ResourceBundle
getResourceBundle(IIoConfigurator configurator
){
69 if (configurator
!= null){
70 return ResourceBundle
.getBundle(configurator
.getClass().getName());
72 return ResourceBundle
.getBundle("Configuration");
74 }catch(MissingResourceException e
){
85 * a {@link java.lang.String} object.
87 * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
89 * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
92 public static GenericConfiguratorWizardPage
Import(String pageName
,
93 IIoConfigurator configurator
, List
<String
> ignoreMethods
, String label
) {
94 return new GenericConfiguratorWizardPage(pageName
, configurator
,
95 "Import Configuration", label
, ignoreMethods
);
104 * a {@link java.lang.String} object.
105 * @param configurator
106 * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
108 * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
111 public static GenericConfiguratorWizardPage
Export(String pageName
,
112 IIoConfigurator configurator
, List
<String
> ignoreMethods
) {
113 return new GenericConfiguratorWizardPage(pageName
, configurator
,
114 "Export Configuration", "Configure the export mechanism.", ignoreMethods
);
121 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
126 public void createControl(Composite parent
) {
128 // ScrolledComposite scrolledComposite = new ScrolledComposite(parent,
130 // scrolledComposite.setLayout(new GridLayout());
132 // TODO wrap this composite in a scrolled composite
133 Composite composite
= new Composite(parent
, SWT
.NULL
);
134 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
136 GridLayout gridLayout
= new GridLayout();
137 composite
.setLayout(gridLayout
);
139 List
<Method
> methods
= getConfiguratorsBooleanSetMethods(configurator
);
140 Collections
.sort(methods
, new Comparator
<Method
>() {
142 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
145 public int compare(Method o1
, Method o2
) {
149 if(o1
.getName()==null && o2
.getName()!=null){
152 if(o1
.getName()!=null && o2
.getName()==null){
155 if(o1
.getName()==null && o2
.getName()==null){
156 return o1
.toString().compareTo(o1
.toString());
158 int result
= o1
.getName().compareTo(o2
.getName());
160 return o1
.toString().compareTo(o1
.toString());
166 for (Method method
: methods
) {
167 if (ignoreMethods
!= null){
168 if (!ignoreMethods
.contains( method
.getName())) {
169 createCheckbox(composite
, method
, configurator
);
172 createCheckbox(composite
, method
, configurator
);
177 // scrolledComposite.setContent(composite);
179 setControl(composite
);
182 private void createCheckbox(Composite parent
, Method method
,
183 final IIoConfigurator configurator
) {
185 final String methodName
= method
.getName();
186 final Button checkBox
= new Button(parent
, SWT
.CHECK
);
188 // if(configurator.getClass().equals(Abcd206ImportConfigurator.class)){
189 String
[] r
= methodName
.split("set")[1].split("(?=\\p{Upper})");
190 checkBox
.setText(getLabel(StringUtils
.join(r
," ")));
193 // checkBox.setText(getLabel(methodName));
195 // retrieve the default values and set the checkbox accordingly
196 boolean defaultSelection
= executeBooleanGetMethod(configurator
, "is"
197 + methodName
.substring(3));
198 checkBox
.setSelection(defaultSelection
);
199 checkBox
.addSelectionListener(new SelectionAdapter() {
205 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
206 * .swt.events.SelectionEvent)
209 public void widgetSelected(SelectionEvent e
) {
210 executeBooleanSetMethod(configurator
, methodName
,
211 checkBox
.getSelection());
218 private String
getLabel(String methodName
){
220 if(resourceBundle
== null){
225 return resourceBundle
.getString(methodName
);
226 }catch(MissingResourceException e
){
231 private boolean executeBooleanGetMethod(IIoConfigurator configurator
,
234 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
237 boolean result
= false;
239 Method
[] methods
= configuratorClass
.getMethods();
241 for (Method method
: methods
) {
242 if (!method
.getName().equals(methodName
)) {
247 Object returnType
= method
.invoke(configurator
, null);
248 if (returnType
.getClass().equals(Boolean
.class)) {
249 result
= ((Boolean
) returnType
).booleanValue();
253 } catch (Exception e
) {
254 MessagingUtils
.warn(this.getClass(), "Could not invoke method");
261 private void executeBooleanSetMethod(IIoConfigurator configurator
,
262 String methodName
, boolean selected
) {
264 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
267 Method
[] methods
= configuratorClass
.getMethods();
269 for (Method method
: methods
) {
270 if (!method
.getName().equals(methodName
)) {
275 method
.invoke(configurator
, selected
);
278 } catch (Exception e
) {
279 MessagingUtils
.warn(this.getClass(), "Could not invoke method");
284 private List
<Method
> getConfiguratorsBooleanSetMethods(
285 IIoConfigurator configurator
) {
286 List
<Method
> booleanMethods
= new ArrayList
<Method
>();
288 Class
<?
extends IIoConfigurator
> configuratorClass
= configurator
291 Method
[] allMethods
= configuratorClass
.getMethods();
293 for (Method method
: allMethods
) {
294 if (method
.getName().startsWith("set")) {
296 Class
<?
>[] typeList
= method
.getParameterTypes();
298 if (typeList
.length
> 1) {
299 new IllegalStateException(
300 "Found a setter with parameter count > 1");
303 if (typeList
[0].getName().equals("boolean")) {
304 booleanMethods
.add(method
);
309 return booleanMethods
;