cleanup
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / io / e4 / in / GenericConfiguratorWizardPageE4.java
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 package eu.etaxonomy.taxeditor.io.e4.in;
10
11 import java.lang.reflect.Method;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.MissingResourceException;
16 import java.util.ResourceBundle;
17
18 import javax.inject.Inject;
19
20 import org.eclipse.jface.wizard.WizardPage;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Composite;
28
29 import eu.etaxonomy.cdm.io.common.IIoConfigurator;
30 import eu.etaxonomy.taxeditor.model.MessagingUtils;
31
32 /**
33 * @author pplitzner
34 * @since Oct 5, 2017
35 */
36 public class GenericConfiguratorWizardPageE4 extends WizardPage {
37
38 private IIoConfigurator configurator;
39 private ResourceBundle resourceBundle;
40 private List<String> ignoreMethods;
41
42 @Inject
43 public GenericConfiguratorWizardPageE4() {
44 super("Configuration");
45 }
46
47 private void init(String title, String description, IIoConfigurator configurator, List<String> ignoreMethods){
48 this.setTitle(title);
49 this.setDescription(description);
50 this.configurator = configurator;
51 this.ignoreMethods = ignoreMethods;
52 resourceBundle = getResourceBundle(configurator);
53 }
54
55 private ResourceBundle getResourceBundle(IIoConfigurator configurator){
56 try{
57 if (configurator != null){
58 return ResourceBundle.getBundle(configurator.getClass().getName());
59 }else{
60 return ResourceBundle.getBundle("Configuration");
61 }
62 }catch(MissingResourceException e){
63 return null;
64 }
65 }
66
67 public void initImport(IIoConfigurator configurator, List<String> ignoreMethods, String label) {
68 init("Import Configuration", label, configurator, ignoreMethods);
69 }
70
71 public void initExport(IIoConfigurator configurator, List<String> ignoreMethods) {
72 init("Export Configuration", "Configure the export mechanism.", configurator, ignoreMethods);
73 }
74
75 @Override
76 public void createControl(Composite parent) {
77
78 // TODO wrap this composite in a scrolled composite
79 Composite composite = new Composite(parent, SWT.NULL);
80 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
81
82 GridLayout gridLayout = new GridLayout();
83 composite.setLayout(gridLayout);
84
85 List<Method> methods = getConfiguratorsBooleanSetMethods(configurator);
86 Collections.sort(methods, (o1,o2)-> {
87 if (o1.equals(o2)){
88 return 0;
89 }
90 if(o1.getName()==null && o2.getName()!=null){
91 return -1;
92 }
93 if(o1.getName()!=null && o2.getName()==null){
94 return 1;
95 }
96 if(o1.getName()==null && o2.getName()==null){
97 return o1.toString().compareTo(o1.toString());
98 }
99 int result = o1.getName().compareTo(o2.getName());
100 if (result == 0){
101 return o1.toString().compareTo(o1.toString());
102 }
103 return result;
104 });
105
106 for (Method method : methods) {
107 if (ignoreMethods != null){
108 if (!ignoreMethods.contains( method.getName())) {
109 createCheckbox(composite, method, configurator);
110 }
111 }else{
112 createCheckbox(composite, method, configurator);
113 }
114 }
115
116 setControl(composite);
117 }
118
119 private void createCheckbox(Composite parent, Method method,
120 final IIoConfigurator configurator) {
121
122 final String methodName = method.getName();
123 final Button checkBox = new Button(parent, SWT.CHECK);
124
125 // if(configurator.getClass().equals(Abcd206ImportConfigurator.class)){
126 checkBox.setText(getLabel(splitMethodName(methodName)));
127 // retrieve the default values and set the checkbox accordingly
128 boolean defaultSelection = executeBooleanGetMethod(configurator, "is"
129 + methodName.substring(3));
130 checkBox.setSelection(defaultSelection);
131 checkBox.addSelectionListener(new SelectionAdapter() {
132 @Override
133 public void widgetSelected(SelectionEvent e) {
134 executeBooleanSetMethod(configurator, methodName,
135 checkBox.getSelection());
136 }
137 });
138 }
139
140 private String getLabel(String methodName){
141
142 if(resourceBundle == null){
143 return methodName;
144 }
145
146 try{
147 return resourceBundle.getString(methodName);
148 }catch(MissingResourceException e){
149 return methodName;
150 }
151 }
152
153 private boolean executeBooleanGetMethod(IIoConfigurator configurator,
154 String methodName) {
155
156 Class<? extends IIoConfigurator> configuratorClass = configurator.getClass();
157
158 boolean result = false;
159
160 Method[] methods = configuratorClass.getMethods();
161
162 for (Method method : methods) {
163 if (!method.getName().equals(methodName)) {
164 continue;
165 }
166
167 try {
168 Object returnType = method.invoke(configurator, null);
169 if (returnType.getClass().equals(Boolean.class)) {
170 result = ((Boolean) returnType).booleanValue();
171 }
172
173 break;
174 } catch (Exception e) {
175 MessagingUtils.warn(this.getClass(), "Could not invoke method");
176 }
177 }
178
179 return result;
180 }
181
182 private void executeBooleanSetMethod(IIoConfigurator configurator,
183 String methodName, boolean selected) {
184
185 Class<? extends IIoConfigurator> configuratorClass = configurator
186 .getClass();
187
188 Method[] methods = configuratorClass.getMethods();
189
190 for (Method method : methods) {
191 if (!method.getName().equals(methodName)) {
192 continue;
193 }
194
195 try {
196 method.invoke(configurator, selected);
197
198 break;
199 } catch (Exception e) {
200 MessagingUtils.warn(this.getClass(), "Could not invoke method");
201 }
202 }
203 }
204
205 private List<Method> getConfiguratorsBooleanSetMethods(
206 IIoConfigurator configurator) {
207 List<Method> booleanMethods = new ArrayList<Method>();
208
209 Class<? extends IIoConfigurator> configuratorClass = configurator
210 .getClass();
211
212 Method[] allMethods = configuratorClass.getMethods();
213
214 for (Method method : allMethods) {
215 if (method.getName().startsWith("set")) {
216
217 Class<?>[] typeList = method.getParameterTypes();
218 //this is irrelevant for methods without boolean parameters
219 if (typeList.length > 1 && typeList[0].getName().equals("boolean")) {
220 throw new IllegalStateException(
221 "Found a setter with parameter count > 1");
222 }
223
224 if (typeList[0].getName().equals("boolean")) {
225 booleanMethods.add(method);
226 }
227 }
228 }
229
230 return booleanMethods;
231 }
232
233 private String splitMethodName(String s) {
234 s = s.replace("set", "");
235 return s.replaceAll(
236 String.format("%s|%s|%s",
237 "(?<=[A-Z])(?=[A-Z][a-z])",
238 "(?<=[^A-Z])(?=[A-Z])",
239 "(?<=[A-Za-z])(?=[^A-Za-z])"
240 ),
241 " "
242 );
243 }
244 }