Project

General

Profile

Download (4.06 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.jface.preference.FieldEditorPreferencePage;
21
import org.eclipse.swt.widgets.Combo;
22
import org.eclipse.ui.IWorkbench;
23
import org.eclipse.ui.IWorkbenchPreferencePage;
24

    
25
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
26
import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
27
import eu.etaxonomy.cdm.strategy.match.MatchException;
28
import eu.etaxonomy.cdm.strategy.match.MatchMode;
29
import eu.etaxonomy.taxeditor.model.MessagingUtils;
30
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
31
import eu.etaxonomy.taxeditor.store.CdmStore;
32

    
33
/**
34
 * @author n.hoffmann
35
 * @created Jan 22, 2010
36
 * @version 1.0
37
 */
38
public abstract class AbstractMatchingPreferences<T extends IdentifiableEntity> extends FieldEditorPreferencePage implements
39
		IWorkbenchPreferencePage {
40

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

    
47
	protected IMatchStrategy matchStrategy;
48

    
49
	protected Class<T> clazz;
50

    
51
	private List<MatchMode> matchModeList;
52

    
53
	protected Map<String, Combo> matchModeCombos = new HashMap<String, Combo>();
54

    
55
    @Override
56
    public void init(IWorkbench workbench) {
57
        setPreferenceStore(PreferencesUtil.getPreferenceStore());
58
    }
59

    
60
	/**
61
	 * {@inheritDoc}
62
	 */
63
	@Override
64
	protected void createFieldEditors() {
65
	    if(!CdmStore.isActive()) {
66
            MessagingUtils.noDataSourceWarningDialog(null);
67
        }else{
68
            for(String fieldName : getFieldNames()){
69
                String[][] comboValues = new String[getMatchModeList().size()][2];
70
                for (int i=0;i<getMatchModeList().size();i++) {
71
                    comboValues[i][0] = getMatchModeList().get(i).name();
72
                    comboValues[i][1] = getMatchModeList().get(i).name();
73
                }
74
                addField(new ComboFieldEditor(this.getClass().getCanonicalName()+fieldName,
75
                        fieldName, comboValues,
76
                        getFieldEditorParent()));
77
            }
78
        }
79
	}
80

    
81
	/**
82
	 * Transforms the MatchMode enum into a list.
83
	 *
84
	 * @return
85
	 */
86
	private List<MatchMode> getMatchModeList(){
87
		if(matchModeList == null){
88
			matchModeList = Arrays.asList(MatchMode.values());
89
		}
90
		return matchModeList;
91
	}
92

    
93
	/**
94
	 * Get names of all declared fields
95
	 *
96
	 * @return
97
	 */
98
	private List<String> getFieldNames(){
99
		List<Field> fields = new ArrayList<Field>();
100

    
101
		fields = getAllFields(fields, clazz);
102
		List<String> fieldNames = new ArrayList<String>();
103

    
104
		for(Field field : fields){
105
			String fieldName = field.getName();
106
			if(! fieldName.matches(ExcludePattern)){
107
				fieldNames.add(fieldName);
108
			}
109
		}
110

    
111
		return fieldNames;
112
	}
113

    
114
	/**
115
	 * Get all declared fields including fields of the superclasses.
116
	 *
117
	 * @param fields a {@link java.util.List} object.
118
	 * @param type a {@link java.lang.Class} object.
119
	 * @return a {@link java.util.List} object.
120
	 */
121
	public static List<Field> getAllFields(List<Field> fields, Class<?> type) {
122
        fields.addAll(Arrays.asList(type.getDeclaredFields()));
123

    
124
	    if (type.getSuperclass() != null) {
125
	        fields = getAllFields(fields, type.getSuperclass());
126
	    }
127

    
128
	    return fields;
129
	}
130

    
131
	/**
132
	 * Returns the default match strategy for the respective class
133
	 *
134
	 * @throws eu.etaxonomy.cdm.strategy.match.MatchException if any.
135
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
136
	 */
137
	protected abstract IMatchStrategy getDefaultMatchStrategy() throws MatchException;
138
}
(1-1/4)