avoid NPE in check for dirty editors
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / io / wizard / GenericConfiguratorWizardPage.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
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 if (configurator != null){
70 return ResourceBundle.getBundle(configurator.getClass().getName());
71 }else{
72 return ResourceBundle.getBundle("Configuration");
73 }
74 }catch(MissingResourceException e){
75 return null;
76 }
77 }
78
79 /**
80 * <p>
81 * Import
82 * </p>
83 *
84 * @param pageName
85 * a {@link java.lang.String} object.
86 * @param configurator
87 * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
88 * @return a
89 * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
90 * object.
91 */
92 public static GenericConfiguratorWizardPage Import(String pageName,
93 IIoConfigurator configurator, List<String> ignoreMethods, String label) {
94 return new GenericConfiguratorWizardPage(pageName, configurator,
95 "Import Configuration", label, ignoreMethods);
96 }
97
98 /**
99 * <p>
100 * Export
101 * </p>
102 *
103 * @param pageName
104 * a {@link java.lang.String} object.
105 * @param configurator
106 * a {@link eu.etaxonomy.cdm.io.common.IIoConfigurator} object.
107 * @return a
108 * {@link eu.etaxonomy.taxeditor.io.wizard.GenericConfiguratorWizardPage}
109 * object.
110 */
111 public static GenericConfiguratorWizardPage Export(String pageName,
112 IIoConfigurator configurator, List<String> ignoreMethods) {
113 return new GenericConfiguratorWizardPage(pageName, configurator,
114 "Export Configuration", "Configure the export mechanism.", ignoreMethods);
115 }
116
117 /*
118 * (non-Javadoc)
119 *
120 * @see
121 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
122 * .Composite)
123 */
124 /** {@inheritDoc} */
125 @Override
126 public void createControl(Composite parent) {
127
128 // ScrolledComposite scrolledComposite = new ScrolledComposite(parent,
129 // SWT.H_SCROLL);
130 // scrolledComposite.setLayout(new GridLayout());
131
132 // TODO wrap this composite in a scrolled composite
133 Composite composite = new Composite(parent, SWT.NULL);
134 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
135
136 GridLayout gridLayout = new GridLayout();
137 composite.setLayout(gridLayout);
138
139 List<Method> methods = getConfiguratorsBooleanSetMethods(configurator);
140 Collections.sort(methods, new Comparator<Method>() {
141 /* (non-Javadoc)
142 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
143 */
144 @Override
145 public int compare(Method o1, Method o2) {
146 if (o1.equals(o2)){
147 return 0;
148 }
149 if(o1.getName()==null && o2.getName()!=null){
150 return -1;
151 }
152 if(o1.getName()!=null && o2.getName()==null){
153 return 1;
154 }
155 if(o1.getName()==null && o2.getName()==null){
156 return o1.toString().compareTo(o1.toString());
157 }
158 int result = o1.getName().compareTo(o2.getName());
159 if (result == 0){
160 return o1.toString().compareTo(o1.toString());
161 }
162 return result;
163 }
164 });
165
166 for (Method method : methods) {
167 if (ignoreMethods != null){
168 if (!ignoreMethods.contains( method.getName())) {
169 createCheckbox(composite, method, configurator);
170 }
171 }else{
172 createCheckbox(composite, method, configurator);
173 }
174
175 }
176
177 // scrolledComposite.setContent(composite);
178
179 setControl(composite);
180 }
181
182 private void createCheckbox(Composite parent, Method method,
183 final IIoConfigurator configurator) {
184
185 final String methodName = method.getName();
186 final Button checkBox = new Button(parent, SWT.CHECK);
187
188 // if(configurator.getClass().equals(Abcd206ImportConfigurator.class)){
189 String[] r = methodName.split("set")[1].split("(?=\\p{Upper})");
190 checkBox.setText(getLabel(StringUtils.join(r," ")));
191 // }
192 // else{
193 // checkBox.setText(getLabel(methodName));
194 // }
195 // retrieve the default values and set the checkbox accordingly
196 boolean defaultSelection = executeBooleanGetMethod(configurator, "is"
197 + methodName.substring(3));
198 checkBox.setSelection(defaultSelection);
199 checkBox.addSelectionListener(new SelectionAdapter() {
200
201 /*
202 * (non-Javadoc)
203 *
204 * @see
205 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
206 * .swt.events.SelectionEvent)
207 */
208 @Override
209 public void widgetSelected(SelectionEvent e) {
210 executeBooleanSetMethod(configurator, methodName,
211 checkBox.getSelection());
212 }
213
214 });
215
216 }
217
218 private String getLabel(String methodName){
219
220 if(resourceBundle == null){
221 return methodName;
222 }
223
224 try{
225 return resourceBundle.getString(methodName);
226 }catch(MissingResourceException e){
227 return methodName;
228 }
229 }
230
231 private boolean executeBooleanGetMethod(IIoConfigurator configurator,
232 String methodName) {
233
234 Class<? extends IIoConfigurator> configuratorClass = configurator
235 .getClass();
236
237 boolean result = false;
238
239 Method[] methods = configuratorClass.getMethods();
240
241 for (Method method : methods) {
242 if (!method.getName().equals(methodName)) {
243 continue;
244 }
245
246 try {
247 Object returnType = method.invoke(configurator, null);
248 if (returnType.getClass().equals(Boolean.class)) {
249 result = ((Boolean) returnType).booleanValue();
250 }
251
252 break;
253 } catch (Exception e) {
254 MessagingUtils.warn(this.getClass(), "Could not invoke method");
255 }
256 }
257
258 return result;
259 }
260
261 private void executeBooleanSetMethod(IIoConfigurator configurator,
262 String methodName, boolean selected) {
263
264 Class<? extends IIoConfigurator> configuratorClass = configurator
265 .getClass();
266
267 Method[] methods = configuratorClass.getMethods();
268
269 for (Method method : methods) {
270 if (!method.getName().equals(methodName)) {
271 continue;
272 }
273
274 try {
275 method.invoke(configurator, selected);
276
277 break;
278 } catch (Exception e) {
279 MessagingUtils.warn(this.getClass(), "Could not invoke method");
280 }
281 }
282 }
283
284 private List<Method> getConfiguratorsBooleanSetMethods(
285 IIoConfigurator configurator) {
286 List<Method> booleanMethods = new ArrayList<Method>();
287
288 Class<? extends IIoConfigurator> configuratorClass = configurator
289 .getClass();
290
291 Method[] allMethods = configuratorClass.getMethods();
292
293 for (Method method : allMethods) {
294 if (method.getName().startsWith("set")) {
295
296 Class<?>[] typeList = method.getParameterTypes();
297
298 if (typeList.length > 1) {
299 new IllegalStateException(
300 "Found a setter with parameter count > 1");
301 }
302
303 if (typeList[0].getName().equals("boolean")) {
304 booleanMethods.add(method);
305 }
306 }
307 }
308
309 return booleanMethods;
310 }
311 }