// $Id$
/**
* Copyright (C) 2007 EDIT
- * European Distributed Institute of Taxonomy
+ * European Distributed Institute of Taxonomy
* http://www.e-taxonomy.eu
- *
+ *
* The contents of this file are subject to the Mozilla Public License Version 1.1
* See LICENSE.TXT at the top of this package for the full license terms.
*/
import java.util.MissingResourceException;
import java.util.ResourceBundle;
+import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.widgets.Composite;
import eu.etaxonomy.cdm.io.common.IIoConfigurator;
+import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
import eu.etaxonomy.taxeditor.store.StoreUtil;
/**
* <p>
* GenericConfiguratorWizardPage class.
* </p>
- *
+ *
* @author n.hoffmann
* @created 23.06.2009
* @version 1.0
*/
public class GenericConfiguratorWizardPage extends WizardPage {
- private final IIoConfigurator configurator;
- private final ResourceBundle resourceBundle;
-
- /**
- * @param pageName
- * @param configurator
- */
- private GenericConfiguratorWizardPage(String pageName,
- IIoConfigurator configurator, String title, String description) {
- super(pageName);
- this.setTitle(title);
-
- this.setDescription(description);
-
- this.configurator = configurator;
-
-
- resourceBundle = getResourceBundle(configurator);
- }
-
- private ResourceBundle getResourceBundle(IIoConfigurator configurator){
- try{
- return ResourceBundle.getBundle(configurator.getClass().getName());
- }catch(MissingResourceException e){
- return null;
- }
- }
-
- /**
- * <p>
- * Import
- * </p>
- *
- * @param pageName
- * a {@link java.lang.String} object.
- * @param configurator
- * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
- * @return a
- * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
- * object.
- */
- public static GenericConfiguratorWizardPage Import(String pageName,
- IIoConfigurator configurator) {
- return new GenericConfiguratorWizardPage(pageName, configurator,
- "Import Configuration", "Configure the import mechanism.");
- }
-
- /**
- * <p>
- * Export
- * </p>
- *
- * @param pageName
- * a {@link java.lang.String} object.
- * @param configurator
- * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
- * @return a
- * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
- * object.
- */
- public static GenericConfiguratorWizardPage Export(String pageName,
- IIoConfigurator configurator) {
- return new GenericConfiguratorWizardPage(pageName, configurator,
- "Export Configuration", "Configure the export mechanism.");
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
- * .Composite)
- */
- /** {@inheritDoc} */
- public void createControl(Composite parent) {
-
- // ScrolledComposite scrolledComposite = new ScrolledComposite(parent,
- // SWT.H_SCROLL);
- // scrolledComposite.setLayout(new GridLayout());
-
- // TODO wrap this composite in a scrolled composite
- Composite composite = new Composite(parent, SWT.NULL);
- composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
-
- GridLayout gridLayout = new GridLayout();
- composite.setLayout(gridLayout);
-
- List<Method> methods = getConfiguratorsBooleanSetMethods(configurator);
-
- for (Method method : methods) {
- createCheckbox(composite, method, configurator);
- }
-
- // scrolledComposite.setContent(composite);
-
- setControl(composite);
- }
-
- private void createCheckbox(Composite parent, Method method,
- final IIoConfigurator configurator) {
-
- final String methodName = method.getName();
-
- final Button checkBox = new Button(parent, SWT.CHECK);
-
- checkBox.setText(getLabel(methodName));
- // retrieve the default values and set the checkbox accordingly
- boolean defaultSelection = executeBooleanGetMethod(configurator, "is"
- + methodName.substring(3));
- checkBox.setSelection(defaultSelection);
- checkBox.addSelectionListener(new SelectionAdapter() {
-
- /*
- * (non-Javadoc)
- *
- * @see
- * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
- * .swt.events.SelectionEvent)
- */
- @Override
- public void widgetSelected(SelectionEvent e) {
- executeBooleanSetMethod(configurator, methodName,
- checkBox.getSelection());
- }
-
- });
-
- }
-
- private String getLabel(String methodName){
-
- if(resourceBundle == null){
- return methodName;
- }
-
- try{
- return resourceBundle.getString(methodName);
- }catch(MissingResourceException e){
- return methodName;
- }
- }
-
- private boolean executeBooleanGetMethod(IIoConfigurator configurator,
- String methodName) {
-
- Class<? extends IIoConfigurator> configuratorClass = configurator
- .getClass();
-
- boolean result = false;
-
- Method[] methods = configuratorClass.getMethods();
-
- for (Method method : methods) {
- if (!method.getName().equals(methodName)) {
- continue;
- }
-
- try {
- Object returnType = method.invoke(configurator, null);
- if (returnType.getClass().equals(Boolean.class)) {
- result = ((Boolean) returnType).booleanValue();
- }
-
- break;
- } catch (Exception e) {
- StoreUtil.warn(this.getClass(), "Could not invoke method");
- }
- }
-
- return result;
- }
-
- private void executeBooleanSetMethod(IIoConfigurator configurator,
- String methodName, boolean selected) {
-
- Class<? extends IIoConfigurator> configuratorClass = configurator
- .getClass();
-
- Method[] methods = configuratorClass.getMethods();
-
- for (Method method : methods) {
- if (!method.getName().equals(methodName)) {
- continue;
- }
-
- try {
- method.invoke(configurator, selected);
-
- break;
- } catch (Exception e) {
- StoreUtil.warn(this.getClass(), "Could not invoke method");
- }
- }
- }
-
- private List<Method> getConfiguratorsBooleanSetMethods(
- IIoConfigurator configurator) {
- List<Method> booleanMethods = new ArrayList<Method>();
-
- Class<? extends IIoConfigurator> configuratorClass = configurator
- .getClass();
-
- Method[] allMethods = configuratorClass.getMethods();
-
- for (Method method : allMethods) {
- if (method.getName().startsWith("set")) {
-
- Class<?>[] typeList = method.getParameterTypes();
-
- if (typeList.length > 1) {
- new IllegalStateException(
- "Found a setter with parameter count > 1");
- }
-
- if (typeList[0].getName().equals("boolean")) {
- booleanMethods.add(method);
- }
- }
- }
-
- return booleanMethods;
- }
+ private final IIoConfigurator configurator;
+ private final ResourceBundle resourceBundle;
+
+ /**
+ * @param pageName
+ * @param configurator
+ */
+ private GenericConfiguratorWizardPage(String pageName,
+ IIoConfigurator configurator, String title, String description) {
+ super(pageName);
+ this.setTitle(title);
+
+ this.setDescription(description);
+
+ this.configurator = configurator;
+
+
+ resourceBundle = getResourceBundle(configurator);
+ }
+
+ private ResourceBundle getResourceBundle(IIoConfigurator configurator){
+ try{
+ return ResourceBundle.getBundle(configurator.getClass().getName());
+ }catch(MissingResourceException e){
+ return null;
+ }
+ }
+
+ /**
+ * <p>
+ * Import
+ * </p>
+ *
+ * @param pageName
+ * a {@link java.lang.String} object.
+ * @param configurator
+ * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
+ * @return a
+ * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
+ * object.
+ */
+ public static GenericConfiguratorWizardPage Import(String pageName,
+ IIoConfigurator configurator) {
+ return new GenericConfiguratorWizardPage(pageName, configurator,
+ "Import Configuration", "Configure the import mechanism.");
+ }
+
+ /**
+ * <p>
+ * Export
+ * </p>
+ *
+ * @param pageName
+ * a {@link java.lang.String} object.
+ * @param configurator
+ * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
+ * @return a
+ * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
+ * object.
+ */
+ public static GenericConfiguratorWizardPage Export(String pageName,
+ IIoConfigurator configurator) {
+ return new GenericConfiguratorWizardPage(pageName, configurator,
+ "Export Configuration", "Configure the export mechanism.");
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
+ * .Composite)
+ */
+ /** {@inheritDoc} */
+ @Override
+ public void createControl(Composite parent) {
+
+ // ScrolledComposite scrolledComposite = new ScrolledComposite(parent,
+ // SWT.H_SCROLL);
+ // scrolledComposite.setLayout(new GridLayout());
+
+ // TODO wrap this composite in a scrolled composite
+ Composite composite = new Composite(parent, SWT.NULL);
+ composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+
+ GridLayout gridLayout = new GridLayout();
+ composite.setLayout(gridLayout);
+
+ List<Method> methods = getConfiguratorsBooleanSetMethods(configurator);
+
+ for (Method method : methods) {
+ createCheckbox(composite, method, configurator);
+ }
+
+ // scrolledComposite.setContent(composite);
+
+ setControl(composite);
+ }
+
+ private void createCheckbox(Composite parent, Method method,
+ final IIoConfigurator configurator) {
+
+ final String methodName = method.getName();
+ final Button checkBox = new Button(parent, SWT.CHECK);
+
+ if(configurator.getClass().equals(Abcd206ImportConfigurator.class)){
+ String[] r = methodName.split("set")[1].split("(?=\\p{Upper})");
+ checkBox.setText(getLabel(StringUtils.join(r," ")));
+ }
+ else{
+ checkBox.setText(getLabel(methodName));
+ }
+ // retrieve the default values and set the checkbox accordingly
+ boolean defaultSelection = executeBooleanGetMethod(configurator, "is"
+ + methodName.substring(3));
+ checkBox.setSelection(defaultSelection);
+ checkBox.addSelectionListener(new SelectionAdapter() {
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
+ * .swt.events.SelectionEvent)
+ */
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ executeBooleanSetMethod(configurator, methodName,
+ checkBox.getSelection());
+ }
+
+ });
+
+ }
+
+ private String getLabel(String methodName){
+
+ if(resourceBundle == null){
+ return methodName;
+ }
+
+ try{
+ return resourceBundle.getString(methodName);
+ }catch(MissingResourceException e){
+ return methodName;
+ }
+ }
+
+ private boolean executeBooleanGetMethod(IIoConfigurator configurator,
+ String methodName) {
+
+ Class<? extends IIoConfigurator> configuratorClass = configurator
+ .getClass();
+
+ boolean result = false;
+
+ Method[] methods = configuratorClass.getMethods();
+
+ for (Method method : methods) {
+ if (!method.getName().equals(methodName)) {
+ continue;
+ }
+
+ try {
+ Object returnType = method.invoke(configurator, null);
+ if (returnType.getClass().equals(Boolean.class)) {
+ result = ((Boolean) returnType).booleanValue();
+ }
+
+ break;
+ } catch (Exception e) {
+ StoreUtil.warn(this.getClass(), "Could not invoke method");
+ }
+ }
+
+ return result;
+ }
+
+ private void executeBooleanSetMethod(IIoConfigurator configurator,
+ String methodName, boolean selected) {
+
+ Class<? extends IIoConfigurator> configuratorClass = configurator
+ .getClass();
+
+ Method[] methods = configuratorClass.getMethods();
+
+ for (Method method : methods) {
+ if (!method.getName().equals(methodName)) {
+ continue;
+ }
+
+ try {
+ method.invoke(configurator, selected);
+
+ break;
+ } catch (Exception e) {
+ StoreUtil.warn(this.getClass(), "Could not invoke method");
+ }
+ }
+ }
+
+ private List<Method> getConfiguratorsBooleanSetMethods(
+ IIoConfigurator configurator) {
+ List<Method> booleanMethods = new ArrayList<Method>();
+
+ Class<? extends IIoConfigurator> configuratorClass = configurator
+ .getClass();
+
+ Method[] allMethods = configuratorClass.getMethods();
+
+ for (Method method : allMethods) {
+ if (method.getName().startsWith("set")) {
+
+ Class<?>[] typeList = method.getParameterTypes();
+
+ if (typeList.length > 1) {
+ new IllegalStateException(
+ "Found a setter with parameter count > 1");
+ }
+
+ if (typeList[0].getName().equals("boolean")) {
+ booleanMethods.add(method);
+ }
+ }
+ }
+
+ return booleanMethods;
+ }
}