Merge branch 'develop' into unify_derivative_views
[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.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;
25
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;
33
34 /**
35 * @author n.hoffmann
36 * @created Jan 22, 2010
37 * @version 1.0
38 */
39 public abstract class AbstractMatchingPreferences<T extends IdentifiableEntity> extends FieldEditorPreferencePage implements
40 IWorkbenchPreferencePage {
41
42 /**
43 * Fields that will be excluded from the display
44 */
45 private static final String ExcludePattern = "serialVersionUID|logger|allFields|ajc.*|id|updated|updatedBy|created|createdBy|uuid" +
46 "|parsingProblem|problemStarts|problemEnds|PROTECTED|NOT_PROTECTED|propertyChangeSupport";
47
48 protected IMatchStrategy matchStrategy;
49
50 protected Class<T> clazz;
51
52 private List<MatchMode> matchModeList;
53
54 protected Map<String, Combo> matchModeCombos = new HashMap<String, Combo>();
55
56 @Override
57 public void init(IWorkbench workbench) {
58 setPreferenceStore(PreferencesUtil.getPreferenceStore());
59 }
60
61 /**
62 * {@inheritDoc}
63 */
64 @Override
65 protected void createFieldEditors() {
66 if(!CdmStore.isActive()) {
67 MessagingUtils.noDataSourceWarningDialog(null);
68 }else{
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();
74 }
75 addField(new ComboFieldEditor(this.getClass().getCanonicalName()+fieldName,
76 fieldName, comboValues,
77 getFieldEditorParent()));
78 }
79 }
80 }
81
82 /**
83 * Transforms the MatchMode enum into a list.
84 *
85 * @return
86 */
87 private List<MatchMode> getMatchModeList(){
88 if(matchModeList == null){
89 matchModeList = Arrays.asList(MatchMode.values());
90 }
91 return matchModeList;
92 }
93
94 /**
95 * Get names of all declared fields
96 *
97 * @return
98 */
99 private List<String> getFieldNames(){
100 List<Field> fields = new ArrayList<Field>();
101
102 fields = getAllFields(fields, clazz);
103 List<String> fieldNames = new ArrayList<String>();
104
105 for(Field field : fields){
106 String fieldName = field.getName();
107 if(! fieldName.matches(ExcludePattern)){
108 fieldNames.add(fieldName);
109 }
110 }
111
112 return fieldNames;
113 }
114
115 /**
116 * Get all declared fields including fields of the superclasses.
117 *
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.
121 */
122 public static List<Field> getAllFields(List<Field> fields, Class<?> type) {
123 fields.addAll(Arrays.asList(type.getDeclaredFields()));
124
125 if (type.getSuperclass() != null) {
126 fields = getAllFields(fields, type.getSuperclass());
127 }
128
129 return fields;
130 }
131
132 /**
133 * Returns the default match strategy for the respective class
134 *
135 * @throws eu.etaxonomy.cdm.strategy.match.MatchException if any.
136 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
137 */
138 protected abstract IMatchStrategy getDefaultMatchStrategy() throws MatchException;
139 }