Project

General

Profile

Download (9.33 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
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.
8
 */
9

    
10
package eu.etaxonomy.taxeditor.io.wizard;
11

    
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;
19

    
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;
29

    
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;
33

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

    
45
    private final IIoConfigurator configurator;
46
    private final ResourceBundle resourceBundle;
47
    private final List<String> ignoreMethods;
48

    
49
    /**
50
     * @param pageName
51
     * @param configurator
52
     */
53
    private GenericConfiguratorWizardPage(String pageName,
54
            IIoConfigurator configurator, String title, String description, List<String> ignoreMethods) {
55
        super(pageName);
56
        this.setTitle(title);
57

    
58
        this.setDescription(description);
59

    
60
        this.configurator = configurator;
61
        this.ignoreMethods = ignoreMethods;
62

    
63

    
64
        resourceBundle = getResourceBundle(configurator);
65
    }
66

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

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

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

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

    
124
        // ScrolledComposite scrolledComposite = new ScrolledComposite(parent,
125
        // SWT.H_SCROLL);
126
        // scrolledComposite.setLayout(new GridLayout());
127

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

    
132
        GridLayout gridLayout = new GridLayout();
133
        composite.setLayout(gridLayout);
134

    
135
        List<Method> methods = getConfiguratorsBooleanSetMethods(configurator);
136
        Collections.sort(methods, new Comparator<Method>() {
137
            /* (non-Javadoc)
138
             * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
139
             */
140
            @Override
141
            public int compare(Method o1, Method o2) {
142
                if (o1.equals(o2)){
143
                    return 0;
144
                }
145
                if(o1.getName()==null && o2.getName()!=null){
146
                    return -1;
147
                }
148
                if(o1.getName()!=null && o2.getName()==null){
149
                    return 1;
150
                }
151
                if(o1.getName()==null && o2.getName()==null){
152
                    return o1.toString().compareTo(o1.toString());
153
                }
154
                int result = o1.getName().compareTo(o2.getName());
155
                if (result == 0){
156
                    return o1.toString().compareTo(o1.toString());
157
                }
158
                return result;
159
            }
160
        });
161

    
162
        for (Method method : methods) {
163
            if (!ignoreMethods.contains( method.getName())) {
164
                createCheckbox(composite, method, configurator);
165
            }
166
        }
167

    
168
        // scrolledComposite.setContent(composite);
169

    
170
        setControl(composite);
171
    }
172

    
173
    private void createCheckbox(Composite parent, Method method,
174
            final IIoConfigurator configurator) {
175

    
176
        final String methodName = method.getName();
177
        final Button checkBox = new Button(parent, SWT.CHECK);
178

    
179
        if(configurator.getClass().equals(Abcd206ImportConfigurator.class)){
180
            String[] r = methodName.split("set")[1].split("(?=\\p{Upper})");
181
            checkBox.setText(getLabel(StringUtils.join(r," ")));
182
        }
183
        else{
184
            checkBox.setText(getLabel(methodName));
185
        }
186
        // retrieve the default values and set the checkbox accordingly
187
        boolean defaultSelection = executeBooleanGetMethod(configurator, "is"
188
                + methodName.substring(3));
189
        checkBox.setSelection(defaultSelection);
190
        checkBox.addSelectionListener(new SelectionAdapter() {
191

    
192
            /*
193
             * (non-Javadoc)
194
             *
195
             * @see
196
             * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
197
             * .swt.events.SelectionEvent)
198
             */
199
            @Override
200
            public void widgetSelected(SelectionEvent e) {
201
                executeBooleanSetMethod(configurator, methodName,
202
                        checkBox.getSelection());
203
            }
204

    
205
        });
206

    
207
    }
208

    
209
    private String getLabel(String methodName){
210

    
211
        if(resourceBundle == null){
212
            return methodName;
213
        }
214

    
215
        try{
216
            return resourceBundle.getString(methodName);
217
        }catch(MissingResourceException e){
218
            return methodName;
219
        }
220
    }
221

    
222
    private boolean executeBooleanGetMethod(IIoConfigurator configurator,
223
            String methodName) {
224

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

    
228
        boolean result = false;
229

    
230
        Method[] methods = configuratorClass.getMethods();
231

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

    
237
            try {
238
                Object returnType = method.invoke(configurator, null);
239
                if (returnType.getClass().equals(Boolean.class)) {
240
                    result = ((Boolean) returnType).booleanValue();
241
                }
242

    
243
                break;
244
            } catch (Exception e) {
245
                MessagingUtils.warn(this.getClass(), "Could not invoke method");
246
            }
247
        }
248

    
249
        return result;
250
    }
251

    
252
    private void executeBooleanSetMethod(IIoConfigurator configurator,
253
            String methodName, boolean selected) {
254

    
255
        Class<? extends IIoConfigurator> configuratorClass = configurator
256
                .getClass();
257

    
258
        Method[] methods = configuratorClass.getMethods();
259

    
260
        for (Method method : methods) {
261
            if (!method.getName().equals(methodName)) {
262
                continue;
263
            }
264

    
265
            try {
266
                method.invoke(configurator, selected);
267

    
268
                break;
269
            } catch (Exception e) {
270
                MessagingUtils.warn(this.getClass(), "Could not invoke method");
271
            }
272
        }
273
    }
274

    
275
    private List<Method> getConfiguratorsBooleanSetMethods(
276
            IIoConfigurator configurator) {
277
        List<Method> booleanMethods = new ArrayList<Method>();
278

    
279
        Class<? extends IIoConfigurator> configuratorClass = configurator
280
                .getClass();
281

    
282
        Method[] allMethods = configuratorClass.getMethods();
283

    
284
        for (Method method : allMethods) {
285
            if (method.getName().startsWith("set")) {
286

    
287
                Class<?>[] typeList = method.getParameterTypes();
288

    
289
                if (typeList.length > 1) {
290
                    new IllegalStateException(
291
                            "Found a setter with parameter count > 1");
292
                }
293

    
294
                if (typeList[0].getName().equals("boolean")) {
295
                    booleanMethods.add(method);
296
                }
297
            }
298
        }
299

    
300
        return booleanMethods;
301
    }
302
}
(17-17/29)