Project

General

Profile

Download (3.65 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.preference.matching;
11

    
12
import java.lang.reflect.Field;
13
import java.util.ArrayList;
14
import java.util.Arrays;
15
import java.util.HashMap;
16
import java.util.List;
17
import java.util.Map;
18

    
19
import org.eclipse.jface.preference.ComboFieldEditor;
20
import org.eclipse.swt.widgets.Combo;
21

    
22
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
23
import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
24
import eu.etaxonomy.cdm.strategy.match.MatchException;
25
import eu.etaxonomy.cdm.strategy.match.MatchMode;
26
import eu.etaxonomy.taxeditor.preference.menu.FieldEditorPreferencePageE4;
27
import eu.etaxonomy.taxeditor.store.CdmStore;
28

    
29
/**
30
 * @author n.hoffmann
31
 * @created Jan 22, 2010
32
 * @version 1.0
33
 */
34
public abstract class AbstractMatchingPreferences<T extends IdentifiableEntity> extends FieldEditorPreferencePageE4 {
35

    
36
	/**
37
	 * Fields that will be excluded from the display
38
	 */
39
	private static final String ExcludePattern = "serialVersionUID|logger|allFields|ajc.*|id|updated|updatedBy|created|createdBy|uuid" +
40
				"|parsingProblem|problemStarts|problemEnds|PROTECTED|NOT_PROTECTED|propertyChangeSupport";
41

    
42
	protected IMatchStrategy matchStrategy;
43

    
44
	protected Class<T> clazz;
45

    
46
	private List<MatchMode> matchModeList;
47

    
48
	protected Map<String, Combo> matchModeCombos = new HashMap<String, Combo>();
49

    
50
	/**
51
	 * {@inheritDoc}
52
	 */
53
	@Override
54
	protected void createFieldEditors() {
55
	    if(CdmStore.isActive()) {
56
            for(String fieldName : getFieldNames()){
57
                String[][] comboValues = new String[getMatchModeList().size()][2];
58
                for (int i=0;i<getMatchModeList().size();i++) {
59
                    comboValues[i][0] = getMatchModeList().get(i).name();
60
                    comboValues[i][1] = getMatchModeList().get(i).name();
61
                }
62
                addField(new ComboFieldEditor(this.getClass().getCanonicalName()+fieldName,
63
                        fieldName, comboValues,
64
                        getFieldEditorParent()));
65
            }
66
        }
67
	}
68

    
69
	/**
70
	 * Transforms the MatchMode enum into a list.
71
	 *
72
	 * @return
73
	 */
74
	private List<MatchMode> getMatchModeList(){
75
		if(matchModeList == null){
76
			matchModeList = Arrays.asList(MatchMode.values());
77
		}
78
		return matchModeList;
79
	}
80

    
81
	/**
82
	 * Get names of all declared fields
83
	 *
84
	 * @return
85
	 */
86
	private List<String> getFieldNames(){
87
		List<Field> fields = new ArrayList<Field>();
88

    
89
		fields = getAllFields(fields, clazz);
90
		List<String> fieldNames = new ArrayList<String>();
91

    
92
		for(Field field : fields){
93
			String fieldName = field.getName();
94
			if(! fieldName.matches(ExcludePattern)){
95
				fieldNames.add(fieldName);
96
			}
97
		}
98

    
99
		return fieldNames;
100
	}
101

    
102
	/**
103
	 * Get all declared fields including fields of the superclasses.
104
	 *
105
	 * @param fields a {@link java.util.List} object.
106
	 * @param type a {@link java.lang.Class} object.
107
	 * @return a {@link java.util.List} object.
108
	 */
109
	public static List<Field> getAllFields(List<Field> fields, Class<?> type) {
110
        fields.addAll(Arrays.asList(type.getDeclaredFields()));
111

    
112
	    if (type.getSuperclass() != null) {
113
	        fields = getAllFields(fields, type.getSuperclass());
114
	    }
115

    
116
	    return fields;
117
	}
118

    
119
	/**
120
	 * Returns the default match strategy for the respective class
121
	 *
122
	 * @throws eu.etaxonomy.cdm.strategy.match.MatchException if any.
123
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
124
	 */
125
	protected abstract IMatchStrategy getDefaultMatchStrategy() throws MatchException;
126
}
(1-1/4)