cleanup
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / matching / AbstractMatchingPreferences.java
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 package eu.etaxonomy.taxeditor.preference.matching;
10
11 import java.lang.reflect.Field;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17
18 import org.eclipse.jface.preference.ComboFieldEditor;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23
24 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
25 import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
26 import eu.etaxonomy.cdm.strategy.match.MatchException;
27 import eu.etaxonomy.cdm.strategy.match.MatchMode;
28 import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
29
30 /**
31 * @author n.hoffmann
32 * @created Jan 22, 2010
33 */
34 public abstract class AbstractMatchingPreferences<T extends IdentifiableEntity>
35 extends CdmPreferencePage {
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|CLOB_LENGTH|newEntityListener|.*SWITCH_TABLE.*";
42
43 protected IMatchStrategy matchStrategy;
44
45 protected Class<T> clazz;
46
47 private List<MatchMode> matchModeList;
48
49 protected Map<String, ComboFieldEditor> matchModeCombos = new HashMap<>();
50
51 @Override
52 protected Control createContents(Composite parent) {
53
54 Composite top = new Composite(parent, SWT.LEFT);
55 GridData layoutData = new GridData(SWT.FILL, SWT.TOP, true, false);
56
57 layoutData.heightHint = 300;
58
59 top.setLayoutData(layoutData);
60
61 // final GridLayout gridLayout = new GridLayout();
62 // gridLayout.numColumns = 2;
63 //
64 // top.setLayout(gridLayout);
65
66 for(String fieldName : getFieldNames()){
67 String[][] comboValues = new String[getMatchModeList().size()][2];
68 for (int i=0;i<getMatchModeList().size();i++) {
69 comboValues[i][0] = getMatchModeList().get(i).name();
70 comboValues[i][1] = getMatchModeList().get(i).name();
71 }
72
73 ComboFieldEditor editor = new ComboFieldEditor(this.getClass().getCanonicalName()+fieldName,
74 fieldName, comboValues,
75 top);
76
77 editor.setPreferenceStore(getPreferenceStore());
78 editor.load();
79 editor.fillIntoGrid(top, editor.getNumberOfControls());
80 matchModeCombos.put(fieldName, editor);
81 }
82
83 return top;
84 }
85
86 @Override
87 public void createControl(Composite parent){
88 super.createControl(parent);
89 if (getApplyButton() != null){
90 this.getApplyButton().setEnabled(true);
91 }
92 }
93
94 @Override
95 public boolean performOk() {
96 if (matchModeCombos.size() > 0){
97 for (ComboFieldEditor editor: matchModeCombos.values()){
98 editor.store();
99 }
100 }
101 return super.performOk();
102 }
103
104 @Override
105 protected void performDefaults() {
106 if (matchModeCombos.size() > 0){
107 for (ComboFieldEditor editor: matchModeCombos.values()){
108 editor.loadDefault();
109 }
110 }
111 super.performDefaults();
112 }
113
114 // @Override
115 // protected void createFieldEditors() {
116 // if(CdmStore.isActive()) {
117 // for(String fieldName : getFieldNames()){
118 // String[][] comboValues = new String[getMatchModeList().size()][2];
119 // for (int i=0;i<getMatchModeList().size();i++) {
120 // comboValues[i][0] = getMatchModeList().get(i).name();
121 // comboValues[i][1] = getMatchModeList().get(i).name();
122 // }
123 // addField(new ComboFieldEditor(this.getClass().getCanonicalName()+fieldName,
124 // fieldName, comboValues,
125 // getFieldEditorParent()));
126 // }
127 // }
128 // }
129
130 /**
131 * Transforms the MatchMode enum into a list.
132 *
133 * @return
134 */
135 private List<MatchMode> getMatchModeList(){
136 if(matchModeList == null){
137 matchModeList = Arrays.asList(MatchMode.values());
138 }
139 return matchModeList;
140 }
141
142 /**
143 * Get names of all declared fields
144 */
145 private List<String> getFieldNames(){
146 List<Field> fields = new ArrayList<>();
147
148 fields = getAllFields(fields, clazz);
149 List<String> fieldNames = new ArrayList<String>();
150
151 for(Field field : fields){
152 String fieldName = field.getName();
153 if(! fieldName.matches(ExcludePattern)){
154 fieldNames.add(fieldName);
155 }
156 }
157
158 return fieldNames;
159 }
160
161 /**
162 * Get all declared fields including fields of the superclasses.
163 *
164 * @param fields a {@link java.util.List} object.
165 * @param type a {@link java.lang.Class} object.
166 * @return a {@link java.util.List} object.
167 */
168 public static List<Field> getAllFields(List<Field> fields, Class<?> type) {
169 fields.addAll(Arrays.asList(type.getDeclaredFields()));
170
171 if (type.getSuperclass() != null) {
172 fields = getAllFields(fields, type.getSuperclass());
173 }
174
175 return fields;
176 }
177
178 /**
179 * Returns the default match strategy for the respective class
180 *
181 * @throws eu.etaxonomy.cdm.strategy.match.MatchException if any.
182 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
183 */
184 protected abstract IMatchStrategy getDefaultMatchStrategy() throws MatchException;
185 }