Project

General

Profile

Download (3.77 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.model.MessagingUtils;
27
import eu.etaxonomy.taxeditor.preference.menu.FieldEditorPreferencePageE4;
28
import eu.etaxonomy.taxeditor.store.CdmStore;
29

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

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

    
43
	protected IMatchStrategy matchStrategy;
44

    
45
	protected Class<T> clazz;
46

    
47
	private List<MatchMode> matchModeList;
48

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

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

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

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

    
92
		fields = getAllFields(fields, clazz);
93
		List<String> fieldNames = new ArrayList<String>();
94

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

    
102
		return fieldNames;
103
	}
104

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

    
115
	    if (type.getSuperclass() != null) {
116
	        fields = getAllFields(fields, type.getSuperclass());
117
	    }
118

    
119
	    return fields;
120
	}
121

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