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