Project

General

Profile

Download (8.79 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

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

    
57
        this.setDescription(description);
58

    
59
        this.configurator = configurator;
60

    
61

    
62
        resourceBundle = getResourceBundle(configurator);
63
    }
64

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

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

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

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

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

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

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

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

    
153
        for (Method method : methods) {
154
            createCheckbox(composite, method, configurator);
155
        }
156

    
157
        // scrolledComposite.setContent(composite);
158

    
159
        setControl(composite);
160
    }
161

    
162
    private void createCheckbox(Composite parent, Method method,
163
            final IIoConfigurator configurator) {
164

    
165
        final String methodName = method.getName();
166
        final Button checkBox = new Button(parent, SWT.CHECK);
167

    
168
        if(configurator.getClass().equals(Abcd206ImportConfigurator.class)){
169
            String[] r = methodName.split("set")[1].split("(?=\\p{Upper})");
170
            checkBox.setText(getLabel(StringUtils.join(r," ")));
171
        }
172
        else{
173
            checkBox.setText(getLabel(methodName));
174
        }
175
        // retrieve the default values and set the checkbox accordingly
176
        boolean defaultSelection = executeBooleanGetMethod(configurator, "is"
177
                + methodName.substring(3));
178
        checkBox.setSelection(defaultSelection);
179
        checkBox.addSelectionListener(new SelectionAdapter() {
180

    
181
            /*
182
             * (non-Javadoc)
183
             *
184
             * @see
185
             * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
186
             * .swt.events.SelectionEvent)
187
             */
188
            @Override
189
            public void widgetSelected(SelectionEvent e) {
190
                executeBooleanSetMethod(configurator, methodName,
191
                        checkBox.getSelection());
192
            }
193

    
194
        });
195

    
196
    }
197

    
198
    private String getLabel(String methodName){
199

    
200
        if(resourceBundle == null){
201
            return methodName;
202
        }
203

    
204
        try{
205
            return resourceBundle.getString(methodName);
206
        }catch(MissingResourceException e){
207
            return methodName;
208
        }
209
    }
210

    
211
    private boolean executeBooleanGetMethod(IIoConfigurator configurator,
212
            String methodName) {
213

    
214
        Class<? extends IIoConfigurator> configuratorClass = configurator
215
                .getClass();
216

    
217
        boolean result = false;
218

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

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

    
226
            try {
227
                Object returnType = method.invoke(configurator, null);
228
                if (returnType.getClass().equals(Boolean.class)) {
229
                    result = ((Boolean) returnType).booleanValue();
230
                }
231

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

    
238
        return result;
239
    }
240

    
241
    private void executeBooleanSetMethod(IIoConfigurator configurator,
242
            String methodName, boolean selected) {
243

    
244
        Class<? extends IIoConfigurator> configuratorClass = configurator
245
                .getClass();
246

    
247
        Method[] methods = configuratorClass.getMethods();
248

    
249
        for (Method method : methods) {
250
            if (!method.getName().equals(methodName)) {
251
                continue;
252
            }
253

    
254
            try {
255
                method.invoke(configurator, selected);
256

    
257
                break;
258
            } catch (Exception e) {
259
                MessagingUtils.warn(this.getClass(), "Could not invoke method");
260
            }
261
        }
262
    }
263

    
264
    private List<Method> getConfiguratorsBooleanSetMethods(
265
            IIoConfigurator configurator) {
266
        List<Method> booleanMethods = new ArrayList<Method>();
267

    
268
        Class<? extends IIoConfigurator> configuratorClass = configurator
269
                .getClass();
270

    
271
        Method[] allMethods = configuratorClass.getMethods();
272

    
273
        for (Method method : allMethods) {
274
            if (method.getName().startsWith("set")) {
275

    
276
                Class<?>[] typeList = method.getParameterTypes();
277

    
278
                if (typeList.length > 1) {
279
                    new IllegalStateException(
280
                            "Found a setter with parameter count > 1");
281
                }
282

    
283
                if (typeList[0].getName().equals("boolean")) {
284
                    booleanMethods.add(method);
285
                }
286
            }
287
        }
288

    
289
        return booleanMethods;
290
    }
291
}
(16-16/26)