3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.preference
.matching
;
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
;
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
;
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
.parser
.MatchStrategyConfigurator
;
36 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
39 * <p>Abstract AbstractMatchingPreferences class.</p>
42 * @created Jan 22, 2010
45 public abstract class AbstractMatchingPreferences
<T
extends IdentifiableEntity
> extends PreferencePage
implements
46 IWorkbenchPreferencePage
{
49 * Fields that will be excluded from the display
51 private static final String ExcludePattern
= "serialVersionUID|logger|allFields|ajc.*|id|updated|updatedBy|created|createdBy|uuid" +
52 "|parsingProblem|problemStarts|problemEnds|PROTECTED|NOT_PROTECTED|propertyChangeSupport";
54 protected IMatchStrategy matchStrategy
;
56 protected Class
<T
> clazz
;
58 private List
<MatchMode
> matchModeList
;
60 protected Map
<String
, Combo
> matchModeCombos
= new HashMap
<String
, Combo
>();
63 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
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
);
74 for(String fieldName
: getFieldNames()){
75 createFieldWidget(composite
, fieldName
);
82 * Creates a widget for a field consisting of the label and a combo
84 * @see {@link #createMatchModeCombo(Composite, String, MatchMode)}
88 private void createFieldWidget(Composite parent
, String fieldName
) {
89 CLabel label
= new CLabel(parent
, SWT
.NONE
);
90 label
.setText(fieldName
);
92 MatchMode matchMode
= matchStrategy
.getMatchMode(fieldName
);
94 createMatchModeCombo(parent
, fieldName
, matchMode
);
98 * Creates a combo for a field with the currently selected match mode for that field preselected
103 private void createMatchModeCombo(Composite parent
, String fieldName
, MatchMode selectedMatchMode
) {
104 Combo matchModeCombo
= new Combo(parent
, SWT
.NULL
);
106 for (MatchMode matchMode
: getMatchModeList()) {
107 matchModeCombo
.add(matchMode
.name());
110 int index
= getMatchModeList().indexOf(selectedMatchMode
);
112 matchModeCombo
.select(index
);
114 matchModeCombo
.addSelectionListener(new MatchModeComboSelectionListener(matchModeCombo
, fieldName
));
116 matchModeCombos
.put(fieldName
, matchModeCombo
);
120 * This listener updates the cache strategy when a value was changed in one of the combos
123 * @created Jan 28, 2010
126 private class MatchModeComboSelectionListener
extends SelectionAdapter
{
128 private Combo matchModeCombo
;
129 private String fieldName
;
131 MatchModeComboSelectionListener(Combo matchModeCombo
, String fieldName
){
132 this.matchModeCombo
= matchModeCombo
;
133 this.fieldName
= fieldName
;
137 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
140 public void widgetSelected(SelectionEvent event
) {
141 super.widgetSelected(event
);
142 MatchMode matchMode
= getMatchModeList().get(matchModeCombo
.getSelectionIndex());
144 matchStrategy
.setMatchMode(fieldName
, matchMode
);
145 } catch (MatchException e
) {
146 StoreUtil
.error(this.getClass(), e
);
147 throw new RuntimeException(e
);
153 * Transforms the MatchMode enum into a list.
157 private List
<MatchMode
> getMatchModeList(){
158 if(matchModeList
== null){
159 matchModeList
= Arrays
.asList(MatchMode
.values());
161 return matchModeList
;
165 * Get names of all declared fields
169 private List
<String
> getFieldNames(){
170 List
<Field
> fields
= new ArrayList
<Field
>();
172 fields
= getAllFields(fields
, clazz
);
173 List
<String
> fieldNames
= new ArrayList
<String
>();
175 for(Field field
: fields
){
176 String fieldName
= field
.getName();
177 if(! fieldName
.matches(ExcludePattern
)){
178 fieldNames
.add(fieldName
);
186 * Get all declared fields including fields of the superclasses.
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.
192 public static List
<Field
> getAllFields(List
<Field
> fields
, Class
<?
> type
) {
193 fields
.addAll(Arrays
.asList(type
.getDeclaredFields()));
195 if (type
.getSuperclass() != null) {
196 fields
= getAllFields(fields
, type
.getSuperclass());
203 * @see org.eclipse.jface.preference.PreferencePage#performApply()
207 protected void performApply() {
208 MatchStrategyConfigurator
.setMatchStrategy(matchStrategy
);
209 super.performApply();
213 * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
217 protected void performDefaults() {
219 // set match strategy to default
220 matchStrategy
= getDefaultMatchStrategy();
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
));
229 } catch (MatchException e
) {
230 StoreUtil
.error(this.getClass(), e
);
231 throw new RuntimeException(e
);
233 super.performDefaults();
237 * Returns the default match strategy for the respective class
239 * @throws eu.etaxonomy.cdm.strategy.match.MatchException if any.
240 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
242 protected abstract IMatchStrategy
getDefaultMatchStrategy() throws MatchException
;