merge
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / matching / AbstractMatchingPreferences.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.preference.matching;
12
13 import java.lang.reflect.Field;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.jface.preference.PreferencePage;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.custom.CLabel;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Combo;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.ui.IWorkbenchPreferencePage;
30
31 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
32 import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
33 import eu.etaxonomy.cdm.strategy.match.MatchException;
34 import eu.etaxonomy.cdm.strategy.match.MatchMode;
35 import eu.etaxonomy.taxeditor.model.MessagingUtils;
36 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
37 import eu.etaxonomy.taxeditor.store.CdmStore;
38
39 /**
40 * <p>Abstract AbstractMatchingPreferences class.</p>
41 *
42 * @author n.hoffmann
43 * @created Jan 22, 2010
44 * @version 1.0
45 */
46 public abstract class AbstractMatchingPreferences<T extends IdentifiableEntity> extends PreferencePage implements
47 IWorkbenchPreferencePage {
48
49 /**
50 * Fields that will be excluded from the display
51 */
52 private static final String ExcludePattern = "serialVersionUID|logger|allFields|ajc.*|id|updated|updatedBy|created|createdBy|uuid" +
53 "|parsingProblem|problemStarts|problemEnds|PROTECTED|NOT_PROTECTED|propertyChangeSupport";
54
55 protected IMatchStrategy matchStrategy;
56
57 protected Class<T> clazz;
58
59 private List<MatchMode> matchModeList;
60
61 protected Map<String, Combo> matchModeCombos = new HashMap<String, Combo>();
62
63 /* (non-Javadoc)
64 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
65 */
66 /** {@inheritDoc} */
67 @Override
68 protected Control createContents(Composite parent) {
69 final Composite composite = new Composite(parent, SWT.NULL);
70 GridLayout gridLayout = new GridLayout();
71 gridLayout.numColumns = 2;
72 composite.setLayout(gridLayout);
73
74 if(!CdmStore.isActive()) {
75 MessagingUtils.noDataSourceWarningDialog(null);
76 }else{
77 for(String fieldName : getFieldNames()){
78 createFieldWidget(composite, fieldName);
79 }
80 }
81
82 return composite;
83 }
84
85 /**
86 * Creates a widget for a field consisting of the label and a combo
87 *
88 * @see {@link #createMatchModeCombo(Composite, String, MatchMode)}
89 * @param composite
90 * @param fieldName
91 */
92 private void createFieldWidget(Composite parent, String fieldName) {
93 CLabel label = new CLabel(parent, SWT.NONE);
94 label.setText(fieldName);
95 MatchMode matchMode = matchStrategy.getMatchMode(fieldName);
96
97 createMatchModeCombo(parent, fieldName, matchMode);
98 }
99
100 /**
101 * Creates a combo for a field with the currently selected match mode for that field preselected
102 *
103 * @param parent
104 * @param matchMode
105 */
106 private void createMatchModeCombo(Composite parent, String fieldName, MatchMode selectedMatchMode) {
107 Combo matchModeCombo = new Combo(parent, SWT.NULL);
108
109 for (MatchMode matchMode : getMatchModeList()) {
110 matchModeCombo.add(matchMode.name());
111 }
112
113 int index = getMatchModeList().indexOf(selectedMatchMode);
114
115 matchModeCombo.select(index);
116
117 matchModeCombo.addSelectionListener(new MatchModeComboSelectionListener(matchModeCombo, fieldName));
118
119 matchModeCombos.put(fieldName, matchModeCombo);
120 }
121
122 /**
123 * This listener updates the cache strategy when a value was changed in one of the combos
124 *
125 * @author n.hoffmann
126 * @created Jan 28, 2010
127 * @version 1.0
128 */
129 private class MatchModeComboSelectionListener extends SelectionAdapter{
130
131 private final Combo matchModeCombo;
132 private final String fieldName;
133
134 MatchModeComboSelectionListener(Combo matchModeCombo, String fieldName){
135 this.matchModeCombo = matchModeCombo;
136 this.fieldName = fieldName;
137 }
138
139 /* (non-Javadoc)
140 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
141 */
142 @Override
143 public void widgetSelected(SelectionEvent event) {
144 super.widgetSelected(event);
145 MatchMode matchMode = getMatchModeList().get(matchModeCombo.getSelectionIndex());
146 try {
147 matchStrategy.setMatchMode(fieldName, matchMode);
148 } catch (MatchException e) {
149 MessagingUtils.error(this.getClass(), e);
150 throw new RuntimeException(e);
151 }
152 }
153 }
154
155 /**
156 * Transforms the MatchMode enum into a list.
157 *
158 * @return
159 */
160 private List<MatchMode> getMatchModeList(){
161 if(matchModeList == null){
162 matchModeList = Arrays.asList(MatchMode.values());
163 }
164 return matchModeList;
165 }
166
167 /**
168 * Get names of all declared fields
169 *
170 * @return
171 */
172 private List<String> getFieldNames(){
173 List<Field> fields = new ArrayList<Field>();
174
175 fields = getAllFields(fields, clazz);
176 List<String> fieldNames = new ArrayList<String>();
177
178 for(Field field : fields){
179 String fieldName = field.getName();
180 if(! fieldName.matches(ExcludePattern)){
181 fieldNames.add(fieldName);
182 }
183 }
184
185 return fieldNames;
186 }
187
188 /**
189 * Get all declared fields including fields of the superclasses.
190 *
191 * @param fields a {@link java.util.List} object.
192 * @param type a {@link java.lang.Class} object.
193 * @return a {@link java.util.List} object.
194 */
195 public static List<Field> getAllFields(List<Field> fields, Class<?> type) {
196 fields.addAll(Arrays.asList(type.getDeclaredFields()));
197
198 if (type.getSuperclass() != null) {
199 fields = getAllFields(fields, type.getSuperclass());
200 }
201
202 return fields;
203 }
204
205 /* (non-Javadoc)
206 * @see org.eclipse.jface.preference.PreferencePage#performApply()
207 */
208 /** {@inheritDoc} */
209 @Override
210 protected void performApply() {
211 PreferencesUtil.setMatchStrategy(matchStrategy);
212 super.performApply();
213 }
214
215 /* (non-Javadoc)
216 * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
217 */
218 /** {@inheritDoc} */
219 @Override
220 protected void performDefaults() {
221 try {
222 // set match strategy to default
223 matchStrategy = getDefaultMatchStrategy();
224
225 // set combos to their default values
226 for(String fieldName : matchModeCombos.keySet()){
227 Combo combo = matchModeCombos.get(fieldName);
228 MatchMode matchMode = matchStrategy.getMatchMode(fieldName);
229 combo.select(matchModeList.indexOf(matchMode));
230 }
231
232 } catch (MatchException e) {
233 MessagingUtils.error(this.getClass(), e);
234 throw new RuntimeException(e);
235 }
236 super.performDefaults();
237 }
238
239 /**
240 * Returns the default match strategy for the respective class
241 *
242 * @throws eu.etaxonomy.cdm.strategy.match.MatchException if any.
243 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
244 */
245 protected abstract IMatchStrategy getDefaultMatchStrategy() throws MatchException;
246 }