2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.preference
.matching
;
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
;
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
;
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
;
32 * @created Jan 22, 2010
34 public abstract class AbstractMatchingPreferences
<T
extends IdentifiableEntity
>
35 extends CdmPreferencePage
{
38 * Fields that will be excluded from the display
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.*";
43 protected IMatchStrategy matchStrategy
;
45 protected Class
<T
> clazz
;
47 private List
<MatchMode
> matchModeList
;
49 protected Map
<String
, ComboFieldEditor
> matchModeCombos
= new HashMap
<>();
52 protected Control
createContents(Composite parent
) {
54 Composite top
= new Composite(parent
, SWT
.LEFT
);
55 GridData layoutData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
57 layoutData
.heightHint
= 300;
59 top
.setLayoutData(layoutData
);
61 // final GridLayout gridLayout = new GridLayout();
62 // gridLayout.numColumns = 2;
64 // top.setLayout(gridLayout);
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();
73 ComboFieldEditor editor
= new ComboFieldEditor(this.getClass().getCanonicalName()+fieldName
,
74 fieldName
, comboValues
,
77 editor
.setPreferenceStore(getPreferenceStore());
79 editor
.fillIntoGrid(top
, editor
.getNumberOfControls());
80 matchModeCombos
.put(fieldName
, editor
);
87 public void createControl(Composite parent
){
88 super.createControl(parent
);
89 if (getApplyButton() != null){
90 this.getApplyButton().setEnabled(true);
95 public boolean performOk() {
96 if (matchModeCombos
.size() > 0){
97 for (ComboFieldEditor editor
: matchModeCombos
.values()){
101 return super.performOk();
105 protected void performDefaults() {
106 if (matchModeCombos
.size() > 0){
107 for (ComboFieldEditor editor
: matchModeCombos
.values()){
108 editor
.loadDefault();
111 super.performDefaults();
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();
123 // addField(new ComboFieldEditor(this.getClass().getCanonicalName()+fieldName,
124 // fieldName, comboValues,
125 // getFieldEditorParent()));
131 * Transforms the MatchMode enum into a list.
135 private List
<MatchMode
> getMatchModeList(){
136 if(matchModeList
== null){
137 matchModeList
= Arrays
.asList(MatchMode
.values());
139 return matchModeList
;
143 * Get names of all declared fields
145 private List
<String
> getFieldNames(){
146 List
<Field
> fields
= new ArrayList
<>();
148 fields
= getAllFields(fields
, clazz
);
149 List
<String
> fieldNames
= new ArrayList
<String
>();
151 for(Field field
: fields
){
152 String fieldName
= field
.getName();
153 if(! fieldName
.matches(ExcludePattern
)){
154 fieldNames
.add(fieldName
);
162 * Get all declared fields including fields of the superclasses.
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.
168 public static List
<Field
> getAllFields(List
<Field
> fields
, Class
<?
> type
) {
169 fields
.addAll(Arrays
.asList(type
.getDeclaredFields()));
171 if (type
.getSuperclass() != null) {
172 fields
= getAllFields(fields
, type
.getSuperclass());
179 * Returns the default match strategy for the respective class
181 * @throws eu.etaxonomy.cdm.strategy.match.MatchException if any.
182 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
184 protected abstract IMatchStrategy
getDefaultMatchStrategy() throws MatchException
;