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
.ComboFieldEditor
;
21 import org
.eclipse
.jface
.preference
.FieldEditorPreferencePage
;
22 import org
.eclipse
.swt
.widgets
.Combo
;
23 import org
.eclipse
.ui
.IWorkbench
;
24 import org
.eclipse
.ui
.IWorkbenchPreferencePage
;
26 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
27 import eu
.etaxonomy
.cdm
.strategy
.match
.IMatchStrategy
;
28 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchException
;
29 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchMode
;
30 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
31 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
32 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
36 * @created Jan 22, 2010
39 public abstract class AbstractMatchingPreferences
<T
extends IdentifiableEntity
> extends FieldEditorPreferencePage
implements
40 IWorkbenchPreferencePage
{
43 * Fields that will be excluded from the display
45 private static final String ExcludePattern
= "serialVersionUID|logger|allFields|ajc.*|id|updated|updatedBy|created|createdBy|uuid" +
46 "|parsingProblem|problemStarts|problemEnds|PROTECTED|NOT_PROTECTED|propertyChangeSupport";
48 protected IMatchStrategy matchStrategy
;
50 protected Class
<T
> clazz
;
52 private List
<MatchMode
> matchModeList
;
54 protected Map
<String
, Combo
> matchModeCombos
= new HashMap
<String
, Combo
>();
57 public void init(IWorkbench workbench
) {
58 setPreferenceStore(PreferencesUtil
.getPreferenceStore());
65 protected void createFieldEditors() {
66 if(!CdmStore
.isActive()) {
67 MessagingUtils
.noDataSourceWarningDialog(null);
69 for(String fieldName
: getFieldNames()){
70 String
[][] comboValues
= new String
[getMatchModeList().size()][2];
71 for (int i
=0;i
<getMatchModeList().size();i
++) {
72 comboValues
[i
][0] = getMatchModeList().get(i
).name();
73 comboValues
[i
][1] = getMatchModeList().get(i
).name();
75 addField(new ComboFieldEditor(this.getClass().getCanonicalName()+fieldName
,
76 fieldName
, comboValues
,
77 getFieldEditorParent()));
83 * Transforms the MatchMode enum into a list.
87 private List
<MatchMode
> getMatchModeList(){
88 if(matchModeList
== null){
89 matchModeList
= Arrays
.asList(MatchMode
.values());
95 * Get names of all declared fields
99 private List
<String
> getFieldNames(){
100 List
<Field
> fields
= new ArrayList
<Field
>();
102 fields
= getAllFields(fields
, clazz
);
103 List
<String
> fieldNames
= new ArrayList
<String
>();
105 for(Field field
: fields
){
106 String fieldName
= field
.getName();
107 if(! fieldName
.matches(ExcludePattern
)){
108 fieldNames
.add(fieldName
);
116 * Get all declared fields including fields of the superclasses.
118 * @param fields a {@link java.util.List} object.
119 * @param type a {@link java.lang.Class} object.
120 * @return a {@link java.util.List} object.
122 public static List
<Field
> getAllFields(List
<Field
> fields
, Class
<?
> type
) {
123 fields
.addAll(Arrays
.asList(type
.getDeclaredFields()));
125 if (type
.getSuperclass() != null) {
126 fields
= getAllFields(fields
, type
.getSuperclass());
133 * Returns the default match strategy for the respective class
135 * @throws eu.etaxonomy.cdm.strategy.match.MatchException if any.
136 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
138 protected abstract IMatchStrategy
getDefaultMatchStrategy() throws MatchException
;