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
.model
.MessagingUtils
;
36 import eu
.etaxonomy
.taxeditor
.parser
.MatchStrategyConfigurator
;
37 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
40 * <p>Abstract AbstractMatchingPreferences class.</p>
43 * @created Jan 22, 2010
46 public abstract class AbstractMatchingPreferences
<T
extends IdentifiableEntity
> extends PreferencePage
implements
47 IWorkbenchPreferencePage
{
50 * Fields that will be excluded from the display
52 private static final String ExcludePattern
= "serialVersionUID|logger|allFields|ajc.*|id|updated|updatedBy|created|createdBy|uuid" +
53 "|parsingProblem|problemStarts|problemEnds|PROTECTED|NOT_PROTECTED|propertyChangeSupport";
55 protected IMatchStrategy matchStrategy
;
57 protected Class
<T
> clazz
;
59 private List
<MatchMode
> matchModeList
;
61 protected Map
<String
, Combo
> matchModeCombos
= new HashMap
<String
, Combo
>();
64 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
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
);
74 if(!CdmStore
.isActive()) {
75 MessagingUtils
.noDataSourceWarningDialog(null);
77 for(String fieldName
: getFieldNames()){
78 createFieldWidget(composite
, fieldName
);
86 * Creates a widget for a field consisting of the label and a combo
88 * @see {@link #createMatchModeCombo(Composite, String, MatchMode)}
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
);
97 createMatchModeCombo(parent
, fieldName
, matchMode
);
101 * Creates a combo for a field with the currently selected match mode for that field preselected
106 private void createMatchModeCombo(Composite parent
, String fieldName
, MatchMode selectedMatchMode
) {
107 Combo matchModeCombo
= new Combo(parent
, SWT
.NULL
);
109 for (MatchMode matchMode
: getMatchModeList()) {
110 matchModeCombo
.add(matchMode
.name());
113 int index
= getMatchModeList().indexOf(selectedMatchMode
);
115 matchModeCombo
.select(index
);
117 matchModeCombo
.addSelectionListener(new MatchModeComboSelectionListener(matchModeCombo
, fieldName
));
119 matchModeCombos
.put(fieldName
, matchModeCombo
);
123 * This listener updates the cache strategy when a value was changed in one of the combos
126 * @created Jan 28, 2010
129 private class MatchModeComboSelectionListener
extends SelectionAdapter
{
131 private final Combo matchModeCombo
;
132 private final String fieldName
;
134 MatchModeComboSelectionListener(Combo matchModeCombo
, String fieldName
){
135 this.matchModeCombo
= matchModeCombo
;
136 this.fieldName
= fieldName
;
140 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
143 public void widgetSelected(SelectionEvent event
) {
144 super.widgetSelected(event
);
145 MatchMode matchMode
= getMatchModeList().get(matchModeCombo
.getSelectionIndex());
147 matchStrategy
.setMatchMode(fieldName
, matchMode
);
148 } catch (MatchException e
) {
149 MessagingUtils
.error(this.getClass(), e
);
150 throw new RuntimeException(e
);
156 * Transforms the MatchMode enum into a list.
160 private List
<MatchMode
> getMatchModeList(){
161 if(matchModeList
== null){
162 matchModeList
= Arrays
.asList(MatchMode
.values());
164 return matchModeList
;
168 * Get names of all declared fields
172 private List
<String
> getFieldNames(){
173 List
<Field
> fields
= new ArrayList
<Field
>();
175 fields
= getAllFields(fields
, clazz
);
176 List
<String
> fieldNames
= new ArrayList
<String
>();
178 for(Field field
: fields
){
179 String fieldName
= field
.getName();
180 if(! fieldName
.matches(ExcludePattern
)){
181 fieldNames
.add(fieldName
);
189 * Get all declared fields including fields of the superclasses.
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.
195 public static List
<Field
> getAllFields(List
<Field
> fields
, Class
<?
> type
) {
196 fields
.addAll(Arrays
.asList(type
.getDeclaredFields()));
198 if (type
.getSuperclass() != null) {
199 fields
= getAllFields(fields
, type
.getSuperclass());
206 * @see org.eclipse.jface.preference.PreferencePage#performApply()
210 protected void performApply() {
211 MatchStrategyConfigurator
.setMatchStrategy(matchStrategy
);
212 super.performApply();
216 * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
220 protected void performDefaults() {
222 // set match strategy to default
223 matchStrategy
= getDefaultMatchStrategy();
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
));
232 } catch (MatchException e
) {
233 MessagingUtils
.error(this.getClass(), e
);
234 throw new RuntimeException(e
);
236 super.performDefaults();
240 * Returns the default match strategy for the respective class
242 * @throws eu.etaxonomy.cdm.strategy.match.MatchException if any.
243 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
245 protected abstract IMatchStrategy
getDefaultMatchStrategy() throws MatchException
;