Project

General

Profile

Download (5.71 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

    
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.swt.SWT;
22
import org.eclipse.swt.layout.GridData;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.swt.widgets.Control;
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.preference.menu.CdmPreferencePage;
31

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

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

    
45
	protected IMatchStrategy matchStrategy;
46

    
47
	protected Class<T> clazz;
48

    
49
	private List<MatchMode> matchModeList;
50

    
51
	protected Map<String, ComboFieldEditor> matchModeCombos = new HashMap();
52

    
53
	 @Override
54
	 protected Control createContents(Composite parent) {
55

    
56
	       Composite top = new Composite(parent, SWT.LEFT);
57
	       GridData layoutData = new GridData(SWT.FILL, SWT.TOP, true, false);
58

    
59
           layoutData.heightHint = 300;
60

    
61
           top.setLayoutData(layoutData);
62

    
63

    
64
//	       final GridLayout gridLayout = new GridLayout();
65
//	       gridLayout.numColumns = 2;
66
//
67
//	       top.setLayout(gridLayout);
68

    
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

    
76
               ComboFieldEditor editor = new ComboFieldEditor(this.getClass().getCanonicalName()+fieldName,
77
                       fieldName, comboValues,
78
                       top);
79

    
80
               editor.setPreferenceStore(getPreferenceStore());
81
               editor.load();
82
               editor.fillIntoGrid(top, editor.getNumberOfControls());
83
               matchModeCombos.put(fieldName, editor);
84
           }
85

    
86
	       return top;
87

    
88
	    }
89

    
90
	    @Override
91
	    public void createControl(Composite parent){
92
	        super.createControl(parent);
93
	        if (getApplyButton() != null){
94
	            this.getApplyButton().setEnabled(true);
95
	        }
96
	    }
97

    
98
	    @Override
99
	    public boolean performOk() {
100
	       if (matchModeCombos.size() >0){
101
	           for (ComboFieldEditor editor: matchModeCombos.values()){
102
	               editor.store();
103
	           }
104
	       }
105
	       return super.performOk();
106

    
107
	    }
108
	    @Override
109
	    protected void performDefaults() {
110
	        if (matchModeCombos.size() >0){
111
	               for (ComboFieldEditor editor: matchModeCombos.values()){
112
	                   editor.loadDefault();
113
	               }
114
	           }
115
	        super.performDefaults();
116
	    }
117

    
118

    
119

    
120

    
121
//	/**
122
//	 * {@inheritDoc}
123
//	 */
124
//	@Override
125
//	protected void createFieldEditors() {
126
//	    if(CdmStore.isActive()) {
127
//            for(String fieldName : getFieldNames()){
128
//                String[][] comboValues = new String[getMatchModeList().size()][2];
129
//                for (int i=0;i<getMatchModeList().size();i++) {
130
//                    comboValues[i][0] = getMatchModeList().get(i).name();
131
//                    comboValues[i][1] = getMatchModeList().get(i).name();
132
//                }
133
//                addField(new ComboFieldEditor(this.getClass().getCanonicalName()+fieldName,
134
//                        fieldName, comboValues,
135
//                        getFieldEditorParent()));
136
//            }
137
//        }
138
//	}
139

    
140
	/**
141
	 * Transforms the MatchMode enum into a list.
142
	 *
143
	 * @return
144
	 */
145
	private List<MatchMode> getMatchModeList(){
146
		if(matchModeList == null){
147
			matchModeList = Arrays.asList(MatchMode.values());
148
		}
149
		return matchModeList;
150
	}
151

    
152
	/**
153
	 * Get names of all declared fields
154
	 *
155
	 * @return
156
	 */
157
	private List<String> getFieldNames(){
158
		List<Field> fields = new ArrayList<Field>();
159

    
160
		fields = getAllFields(fields, clazz);
161
		List<String> fieldNames = new ArrayList<String>();
162

    
163
		for(Field field : fields){
164
			String fieldName = field.getName();
165
			if(! fieldName.matches(ExcludePattern)){
166
				fieldNames.add(fieldName);
167
			}
168
		}
169

    
170
		return fieldNames;
171
	}
172

    
173
	/**
174
	 * Get all declared fields including fields of the superclasses.
175
	 *
176
	 * @param fields a {@link java.util.List} object.
177
	 * @param type a {@link java.lang.Class} object.
178
	 * @return a {@link java.util.List} object.
179
	 */
180
	public static List<Field> getAllFields(List<Field> fields, Class<?> type) {
181
        fields.addAll(Arrays.asList(type.getDeclaredFields()));
182

    
183
	    if (type.getSuperclass() != null) {
184
	        fields = getAllFields(fields, type.getSuperclass());
185
	    }
186

    
187
	    return fields;
188
	}
189

    
190
	/**
191
	 * Returns the default match strategy for the respective class
192
	 *
193
	 * @throws eu.etaxonomy.cdm.strategy.match.MatchException if any.
194
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
195
	 */
196
	protected abstract IMatchStrategy getDefaultMatchStrategy() throws MatchException;
197
}
(1-1/4)