Project

General

Profile

« Previous | Next » 

Revision c6d09d18

Added by Patricia Kelbert almost 11 years ago

split method name for the display of the import-configurator (only for ABCD)

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/io/wizard/GenericConfiguratorWizardPage.java
1 1
// $Id$
2 2
/**
3 3
 * Copyright (C) 2007 EDIT
4
 * European Distributed Institute of Taxonomy 
4
 * European Distributed Institute of Taxonomy
5 5
 * http://www.e-taxonomy.eu
6
 * 
6
 *
7 7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 8
 * See LICENSE.TXT at the top of this package for the full license terms.
9 9
 */
......
16 16
import java.util.MissingResourceException;
17 17
import java.util.ResourceBundle;
18 18

  
19
import org.apache.commons.lang.StringUtils;
19 20
import org.eclipse.jface.wizard.WizardPage;
20 21
import org.eclipse.swt.SWT;
21 22
import org.eclipse.swt.events.SelectionAdapter;
......
26 27
import org.eclipse.swt.widgets.Composite;
27 28

  
28 29
import eu.etaxonomy.cdm.io.common.IIoConfigurator;
30
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
29 31
import eu.etaxonomy.taxeditor.store.StoreUtil;
30 32

  
31 33
/**
32 34
 * <p>
33 35
 * GenericConfiguratorWizardPage class.
34 36
 * </p>
35
 * 
37
 *
36 38
 * @author n.hoffmann
37 39
 * @created 23.06.2009
38 40
 * @version 1.0
39 41
 */
40 42
public class GenericConfiguratorWizardPage extends WizardPage {
41 43

  
42
	private final IIoConfigurator configurator;
43
	private final ResourceBundle resourceBundle;
44

  
45
	/**
46
	 * @param pageName
47
	 * @param configurator
48
	 */
49
	private GenericConfiguratorWizardPage(String pageName,
50
			IIoConfigurator configurator, String title, String description) {
51
		super(pageName);
52
		this.setTitle(title);
53

  
54
		this.setDescription(description);
55

  
56
		this.configurator = configurator;
57
		
58
		
59
		resourceBundle = getResourceBundle(configurator);
60
	}
61

  
62
	private ResourceBundle getResourceBundle(IIoConfigurator configurator){
63
		try{
64
			return ResourceBundle.getBundle(configurator.getClass().getName());
65
		}catch(MissingResourceException e){
66
			return null;
67
		}
68
	}
69
	
70
	/**
71
	 * <p>
72
	 * Import
73
	 * </p>
74
	 * 
75
	 * @param pageName
76
	 *            a {@link java.lang.String} object.
77
	 * @param configurator
78
	 *            a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
79
	 * @return a
80
	 *         {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
81
	 *         object.
82
	 */
83
	public static GenericConfiguratorWizardPage Import(String pageName,
84
			IIoConfigurator configurator) {
85
		return new GenericConfiguratorWizardPage(pageName, configurator,
86
				"Import Configuration", "Configure the import mechanism.");
87
	}
88

  
89
	/**
90
	 * <p>
91
	 * Export
92
	 * </p>
93
	 * 
94
	 * @param pageName
95
	 *            a {@link java.lang.String} object.
96
	 * @param configurator
97
	 *            a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
98
	 * @return a
99
	 *         {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
100
	 *         object.
101
	 */
102
	public static GenericConfiguratorWizardPage Export(String pageName,
103
			IIoConfigurator configurator) {
104
		return new GenericConfiguratorWizardPage(pageName, configurator,
105
				"Export Configuration", "Configure the export mechanism.");
106
	}
107

  
108
	/*
109
	 * (non-Javadoc)
110
	 * 
111
	 * @see
112
	 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
113
	 * .Composite)
114
	 */
115
	/** {@inheritDoc} */
116
	public void createControl(Composite parent) {
117

  
118
		// ScrolledComposite scrolledComposite = new ScrolledComposite(parent,
119
		// SWT.H_SCROLL);
120
		// scrolledComposite.setLayout(new GridLayout());
121

  
122
		// TODO wrap this composite in a scrolled composite
123
		Composite composite = new Composite(parent, SWT.NULL);
124
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
125

  
126
		GridLayout gridLayout = new GridLayout();
127
		composite.setLayout(gridLayout);
128

  
129
		List<Method> methods = getConfiguratorsBooleanSetMethods(configurator);
130

  
131
		for (Method method : methods) {
132
			createCheckbox(composite, method, configurator);
133
		}
134

  
135
		// scrolledComposite.setContent(composite);
136

  
137
		setControl(composite);
138
	}
139

  
140
	private void createCheckbox(Composite parent, Method method,
141
			final IIoConfigurator configurator) {
142

  
143
		final String methodName = method.getName();
144

  
145
		final Button checkBox = new Button(parent, SWT.CHECK);
146
		
147
		checkBox.setText(getLabel(methodName));
148
		// retrieve the default values and set the checkbox accordingly
149
		boolean defaultSelection = executeBooleanGetMethod(configurator, "is"
150
				+ methodName.substring(3));
151
		checkBox.setSelection(defaultSelection);
152
		checkBox.addSelectionListener(new SelectionAdapter() {
153

  
154
			/*
155
			 * (non-Javadoc)
156
			 * 
157
			 * @see
158
			 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
159
			 * .swt.events.SelectionEvent)
160
			 */
161
			@Override
162
			public void widgetSelected(SelectionEvent e) {
163
				executeBooleanSetMethod(configurator, methodName,
164
						checkBox.getSelection());
165
			}
166

  
167
		});
168

  
169
	}
170
	
171
	private String getLabel(String methodName){
172
		
173
		if(resourceBundle == null){
174
			return methodName;
175
		}
176
		
177
		try{
178
			return resourceBundle.getString(methodName);
179
		}catch(MissingResourceException e){
180
			return methodName;
181
		}
182
	}
183

  
184
	private boolean executeBooleanGetMethod(IIoConfigurator configurator,
185
			String methodName) {
186

  
187
		Class<? extends IIoConfigurator> configuratorClass = configurator
188
				.getClass();
189

  
190
		boolean result = false;
191

  
192
		Method[] methods = configuratorClass.getMethods();
193

  
194
		for (Method method : methods) {
195
			if (!method.getName().equals(methodName)) {
196
				continue;
197
			}
198

  
199
			try {
200
				Object returnType = method.invoke(configurator, null);
201
				if (returnType.getClass().equals(Boolean.class)) {
202
					result = ((Boolean) returnType).booleanValue();
203
				}
204

  
205
				break;
206
			} catch (Exception e) {
207
				StoreUtil.warn(this.getClass(), "Could not invoke method");
208
			}
209
		}
210

  
211
		return result;
212
	}
213

  
214
	private void executeBooleanSetMethod(IIoConfigurator configurator,
215
			String methodName, boolean selected) {
216

  
217
		Class<? extends IIoConfigurator> configuratorClass = configurator
218
				.getClass();
219

  
220
		Method[] methods = configuratorClass.getMethods();
221

  
222
		for (Method method : methods) {
223
			if (!method.getName().equals(methodName)) {
224
				continue;
225
			}
226

  
227
			try {
228
				method.invoke(configurator, selected);
229

  
230
				break;
231
			} catch (Exception e) {
232
				StoreUtil.warn(this.getClass(), "Could not invoke method");
233
			}
234
		}
235
	}
236

  
237
	private List<Method> getConfiguratorsBooleanSetMethods(
238
			IIoConfigurator configurator) {
239
		List<Method> booleanMethods = new ArrayList<Method>();
240

  
241
		Class<? extends IIoConfigurator> configuratorClass = configurator
242
				.getClass();
243

  
244
		Method[] allMethods = configuratorClass.getMethods();
245

  
246
		for (Method method : allMethods) {
247
			if (method.getName().startsWith("set")) {
248

  
249
				Class<?>[] typeList = method.getParameterTypes();
250

  
251
				if (typeList.length > 1) {
252
					new IllegalStateException(
253
							"Found a setter with parameter count > 1");
254
				}
255

  
256
				if (typeList[0].getName().equals("boolean")) {
257
					booleanMethods.add(method);
258
				}
259
			}
260
		}
261

  
262
		return booleanMethods;
263
	}
44
    private final IIoConfigurator configurator;
45
    private final ResourceBundle resourceBundle;
46

  
47
    /**
48
     * @param pageName
49
     * @param configurator
50
     */
51
    private GenericConfiguratorWizardPage(String pageName,
52
            IIoConfigurator configurator, String title, String description) {
53
        super(pageName);
54
        this.setTitle(title);
55

  
56
        this.setDescription(description);
57

  
58
        this.configurator = configurator;
59

  
60

  
61
        resourceBundle = getResourceBundle(configurator);
62
    }
63

  
64
    private ResourceBundle getResourceBundle(IIoConfigurator configurator){
65
        try{
66
            return ResourceBundle.getBundle(configurator.getClass().getName());
67
        }catch(MissingResourceException e){
68
            return null;
69
        }
70
    }
71

  
72
    /**
73
     * <p>
74
     * Import
75
     * </p>
76
     *
77
     * @param pageName
78
     *            a {@link java.lang.String} object.
79
     * @param configurator
80
     *            a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
81
     * @return a
82
     *         {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
83
     *         object.
84
     */
85
    public static GenericConfiguratorWizardPage Import(String pageName,
86
            IIoConfigurator configurator) {
87
        return new GenericConfiguratorWizardPage(pageName, configurator,
88
                "Import Configuration", "Configure the import mechanism.");
89
    }
90

  
91
    /**
92
     * <p>
93
     * Export
94
     * </p>
95
     *
96
     * @param pageName
97
     *            a {@link java.lang.String} object.
98
     * @param configurator
99
     *            a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
100
     * @return a
101
     *         {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
102
     *         object.
103
     */
104
    public static GenericConfiguratorWizardPage Export(String pageName,
105
            IIoConfigurator configurator) {
106
        return new GenericConfiguratorWizardPage(pageName, configurator,
107
                "Export Configuration", "Configure the export mechanism.");
108
    }
109

  
110
    /*
111
     * (non-Javadoc)
112
     *
113
     * @see
114
     * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
115
     * .Composite)
116
     */
117
    /** {@inheritDoc} */
118
    @Override
119
    public void createControl(Composite parent) {
120

  
121
        // ScrolledComposite scrolledComposite = new ScrolledComposite(parent,
122
        // SWT.H_SCROLL);
123
        // scrolledComposite.setLayout(new GridLayout());
124

  
125
        // TODO wrap this composite in a scrolled composite
126
        Composite composite = new Composite(parent, SWT.NULL);
127
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
128

  
129
        GridLayout gridLayout = new GridLayout();
130
        composite.setLayout(gridLayout);
131

  
132
        List<Method> methods = getConfiguratorsBooleanSetMethods(configurator);
133

  
134
        for (Method method : methods) {
135
            createCheckbox(composite, method, configurator);
136
        }
137

  
138
        // scrolledComposite.setContent(composite);
139

  
140
        setControl(composite);
141
    }
142

  
143
    private void createCheckbox(Composite parent, Method method,
144
            final IIoConfigurator configurator) {
145

  
146
        final String methodName = method.getName();
147
        final Button checkBox = new Button(parent, SWT.CHECK);
148

  
149
        if(configurator.getClass().equals(Abcd206ImportConfigurator.class)){
150
            String[] r = methodName.split("set")[1].split("(?=\\p{Upper})");
151
            checkBox.setText(getLabel(StringUtils.join(r," ")));
152
        }
153
        else{
154
            checkBox.setText(getLabel(methodName));
155
        }
156
        // retrieve the default values and set the checkbox accordingly
157
        boolean defaultSelection = executeBooleanGetMethod(configurator, "is"
158
                + methodName.substring(3));
159
        checkBox.setSelection(defaultSelection);
160
        checkBox.addSelectionListener(new SelectionAdapter() {
161

  
162
            /*
163
             * (non-Javadoc)
164
             *
165
             * @see
166
             * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
167
             * .swt.events.SelectionEvent)
168
             */
169
            @Override
170
            public void widgetSelected(SelectionEvent e) {
171
                executeBooleanSetMethod(configurator, methodName,
172
                        checkBox.getSelection());
173
            }
174

  
175
        });
176

  
177
    }
178

  
179
    private String getLabel(String methodName){
180

  
181
        if(resourceBundle == null){
182
            return methodName;
183
        }
184

  
185
        try{
186
            return resourceBundle.getString(methodName);
187
        }catch(MissingResourceException e){
188
            return methodName;
189
        }
190
    }
191

  
192
    private boolean executeBooleanGetMethod(IIoConfigurator configurator,
193
            String methodName) {
194

  
195
        Class<? extends IIoConfigurator> configuratorClass = configurator
196
                .getClass();
197

  
198
        boolean result = false;
199

  
200
        Method[] methods = configuratorClass.getMethods();
201

  
202
        for (Method method : methods) {
203
            if (!method.getName().equals(methodName)) {
204
                continue;
205
            }
206

  
207
            try {
208
                Object returnType = method.invoke(configurator, null);
209
                if (returnType.getClass().equals(Boolean.class)) {
210
                    result = ((Boolean) returnType).booleanValue();
211
                }
212

  
213
                break;
214
            } catch (Exception e) {
215
                StoreUtil.warn(this.getClass(), "Could not invoke method");
216
            }
217
        }
218

  
219
        return result;
220
    }
221

  
222
    private void executeBooleanSetMethod(IIoConfigurator configurator,
223
            String methodName, boolean selected) {
224

  
225
        Class<? extends IIoConfigurator> configuratorClass = configurator
226
                .getClass();
227

  
228
        Method[] methods = configuratorClass.getMethods();
229

  
230
        for (Method method : methods) {
231
            if (!method.getName().equals(methodName)) {
232
                continue;
233
            }
234

  
235
            try {
236
                method.invoke(configurator, selected);
237

  
238
                break;
239
            } catch (Exception e) {
240
                StoreUtil.warn(this.getClass(), "Could not invoke method");
241
            }
242
        }
243
    }
244

  
245
    private List<Method> getConfiguratorsBooleanSetMethods(
246
            IIoConfigurator configurator) {
247
        List<Method> booleanMethods = new ArrayList<Method>();
248

  
249
        Class<? extends IIoConfigurator> configuratorClass = configurator
250
                .getClass();
251

  
252
        Method[] allMethods = configuratorClass.getMethods();
253

  
254
        for (Method method : allMethods) {
255
            if (method.getName().startsWith("set")) {
256

  
257
                Class<?>[] typeList = method.getParameterTypes();
258

  
259
                if (typeList.length > 1) {
260
                    new IllegalStateException(
261
                            "Found a setter with parameter count > 1");
262
                }
263

  
264
                if (typeList[0].getName().equals("boolean")) {
265
                    booleanMethods.add(method);
266
                }
267
            }
268
        }
269

  
270
        return booleanMethods;
271
    }
264 272
}

Also available in: Unified diff