Project

General

Profile

Download (7.77 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.e4;
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 javax.inject.Inject;
21

    
22
import org.apache.commons.lang.StringUtils;
23
import org.eclipse.jface.wizard.WizardPage;
24
import org.eclipse.swt.SWT;
25
import org.eclipse.swt.events.SelectionAdapter;
26
import org.eclipse.swt.events.SelectionEvent;
27
import org.eclipse.swt.layout.GridData;
28
import org.eclipse.swt.layout.GridLayout;
29
import org.eclipse.swt.widgets.Button;
30
import org.eclipse.swt.widgets.Composite;
31

    
32
import eu.etaxonomy.cdm.io.common.IIoConfigurator;
33
import eu.etaxonomy.taxeditor.model.MessagingUtils;
34

    
35
/**
36
 *
37
 * @author pplitzner
38
 * @since Oct 5, 2017
39
 *
40
 */
41
public class GenericConfiguratorWizardPageE4 extends WizardPage {
42

    
43
    private IIoConfigurator configurator;
44
    private ResourceBundle resourceBundle;
45
    private List<String> ignoreMethods;
46

    
47
    @Inject
48
    public GenericConfiguratorWizardPageE4() {
49
        super("Configuration");
50
    }
51

    
52
    private void init(String title, String description, IIoConfigurator configurator, List<String> ignoreMethods){
53
        this.setTitle(title);
54
        this.setDescription(description);
55
        this.configurator = configurator;
56
        this.ignoreMethods = ignoreMethods;
57
        resourceBundle = getResourceBundle(configurator);
58
    }
59

    
60
    private ResourceBundle getResourceBundle(IIoConfigurator configurator){
61
        try{
62
            if (configurator != null){
63
                return ResourceBundle.getBundle(configurator.getClass().getName());
64
            }else{
65
                return ResourceBundle.getBundle("Configuration");
66
            }
67
        }catch(MissingResourceException e){
68
            return null;
69
        }
70
    }
71

    
72
    public void initImport(IIoConfigurator configurator, List<String> ignoreMethods, String label) {
73
        init("Import Configuration", label, configurator, ignoreMethods);
74
    }
75

    
76
    public void initExport(IIoConfigurator configurator, List<String> ignoreMethods) {
77
        init("Export Configuration", "Configure the export mechanism.", configurator, ignoreMethods);
78
    }
79

    
80
    /** {@inheritDoc} */
81
    @Override
82
    public void createControl(Composite parent) {
83

    
84
        // TODO wrap this composite in a scrolled composite
85
        Composite composite = new Composite(parent, SWT.NULL);
86
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
87

    
88
        GridLayout gridLayout = new GridLayout();
89
        composite.setLayout(gridLayout);
90

    
91
        List<Method> methods = getConfiguratorsBooleanSetMethods(configurator);
92
        Collections.sort(methods, new Comparator<Method>() {
93
            @Override
94
            public int compare(Method o1, Method o2) {
95
                if (o1.equals(o2)){
96
                    return 0;
97
                }
98
                if(o1.getName()==null && o2.getName()!=null){
99
                    return -1;
100
                }
101
                if(o1.getName()!=null && o2.getName()==null){
102
                    return 1;
103
                }
104
                if(o1.getName()==null && o2.getName()==null){
105
                    return o1.toString().compareTo(o1.toString());
106
                }
107
                int result = o1.getName().compareTo(o2.getName());
108
                if (result == 0){
109
                    return o1.toString().compareTo(o1.toString());
110
                }
111
                return result;
112
            }
113
        });
114

    
115
	        for (Method method : methods) {
116
	        	if (ignoreMethods != null){
117
		            if (!ignoreMethods.contains( method.getName())) {
118
		                createCheckbox(composite, method, configurator);
119
		            }
120
	        	}else{
121
	        		createCheckbox(composite, method, configurator);
122
	        	}
123

    
124
	        }
125

    
126
        setControl(composite);
127
    }
128

    
129
    private void createCheckbox(Composite parent, Method method,
130
            final IIoConfigurator configurator) {
131

    
132
        final String methodName = method.getName();
133
        final Button checkBox = new Button(parent, SWT.CHECK);
134

    
135
       // if(configurator.getClass().equals(Abcd206ImportConfigurator.class)){
136
            String[] r = methodName.split("set")[1].split("(?=\\p{Upper})");
137
            checkBox.setText(getLabel(StringUtils.join(r," ")));
138
//        }
139
//        else{
140
//            checkBox.setText(getLabel(methodName));
141
//        }
142
        // retrieve the default values and set the checkbox accordingly
143
        boolean defaultSelection = executeBooleanGetMethod(configurator, "is"
144
                + methodName.substring(3));
145
        checkBox.setSelection(defaultSelection);
146
        checkBox.addSelectionListener(new SelectionAdapter() {
147

    
148
            @Override
149
            public void widgetSelected(SelectionEvent e) {
150
                executeBooleanSetMethod(configurator, methodName,
151
                        checkBox.getSelection());
152
            }
153

    
154
        });
155

    
156
    }
157

    
158
    private String getLabel(String methodName){
159

    
160
        if(resourceBundle == null){
161
            return methodName;
162
        }
163

    
164
        try{
165
            return resourceBundle.getString(methodName);
166
        }catch(MissingResourceException e){
167
            return methodName;
168
        }
169
    }
170

    
171
    private boolean executeBooleanGetMethod(IIoConfigurator configurator,
172
            String methodName) {
173

    
174
        Class<? extends IIoConfigurator> configuratorClass = configurator
175
                .getClass();
176

    
177
        boolean result = false;
178

    
179
        Method[] methods = configuratorClass.getMethods();
180

    
181
        for (Method method : methods) {
182
            if (!method.getName().equals(methodName)) {
183
                continue;
184
            }
185

    
186
            try {
187
                Object returnType = method.invoke(configurator, null);
188
                if (returnType.getClass().equals(Boolean.class)) {
189
                    result = ((Boolean) returnType).booleanValue();
190
                }
191

    
192
                break;
193
            } catch (Exception e) {
194
                MessagingUtils.warn(this.getClass(), "Could not invoke method");
195
            }
196
        }
197

    
198
        return result;
199
    }
200

    
201
    private void executeBooleanSetMethod(IIoConfigurator configurator,
202
            String methodName, boolean selected) {
203

    
204
        Class<? extends IIoConfigurator> configuratorClass = configurator
205
                .getClass();
206

    
207
        Method[] methods = configuratorClass.getMethods();
208

    
209
        for (Method method : methods) {
210
            if (!method.getName().equals(methodName)) {
211
                continue;
212
            }
213

    
214
            try {
215
                method.invoke(configurator, selected);
216

    
217
                break;
218
            } catch (Exception e) {
219
                MessagingUtils.warn(this.getClass(), "Could not invoke method");
220
            }
221
        }
222
    }
223

    
224
    private List<Method> getConfiguratorsBooleanSetMethods(
225
            IIoConfigurator configurator) {
226
        List<Method> booleanMethods = new ArrayList<Method>();
227

    
228
        Class<? extends IIoConfigurator> configuratorClass = configurator
229
                .getClass();
230

    
231
        Method[] allMethods = configuratorClass.getMethods();
232

    
233
        for (Method method : allMethods) {
234
            if (method.getName().startsWith("set")) {
235

    
236
                Class<?>[] typeList = method.getParameterTypes();
237

    
238
                if (typeList.length > 1) {
239
                    new IllegalStateException(
240
                            "Found a setter with parameter count > 1");
241
                }
242

    
243
                if (typeList[0].getName().equals("boolean")) {
244
                    booleanMethods.add(method);
245
                }
246
            }
247
        }
248

    
249
        return booleanMethods;
250
    }
251
}
(3-3/6)