1
|
/**
|
2
|
* Copyright (C) 2016 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.ui.dialog.selection;
|
10
|
|
11
|
|
12
|
|
13
|
import java.text.Collator;
|
14
|
import java.util.Comparator;
|
15
|
|
16
|
import org.eclipse.jface.viewers.ILabelProvider;
|
17
|
import org.eclipse.jface.viewers.LabelProvider;
|
18
|
import org.eclipse.swt.widgets.Control;
|
19
|
import org.eclipse.swt.widgets.Shell;
|
20
|
import org.eclipse.swt.widgets.Text;
|
21
|
|
22
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
23
|
import eu.etaxonomy.cdm.api.service.IAgentService;
|
24
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
25
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
26
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
27
|
import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
|
28
|
import eu.etaxonomy.taxeditor.newWizard.NewPersonWizard;
|
29
|
import eu.etaxonomy.taxeditor.newWizard.NewTeamWizard;
|
30
|
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
|
31
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
32
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
33
|
|
34
|
/**
|
35
|
* @author k.luther
|
36
|
* @date 25.05.2016
|
37
|
*
|
38
|
*/
|
39
|
public class NomenclaturalAuthorSelectionDialog extends AgentSelectionDialog {
|
40
|
|
41
|
|
42
|
/**
|
43
|
* @param shell
|
44
|
* @param conversation
|
45
|
* @param title
|
46
|
* @param multi
|
47
|
* @param settings
|
48
|
* @param agent
|
49
|
*/
|
50
|
protected NomenclaturalAuthorSelectionDialog(Shell shell, ConversationHolder conversation, String title,
|
51
|
boolean multi, String settings, AgentBase agent, boolean teamMemberSelection) {
|
52
|
super(shell, conversation, title, multi, settings, agent, teamMemberSelection);
|
53
|
// TODO Auto-generated constructor stub
|
54
|
}
|
55
|
|
56
|
@Override
|
57
|
protected void initModel() {
|
58
|
Control control = getPatternControl();
|
59
|
String pattern = null;
|
60
|
if (control != null){
|
61
|
pattern = ((Text)control).getText();
|
62
|
}
|
63
|
if (pattern == null || pattern.equals("?")){
|
64
|
model = CdmStore.getService(IAgentService.class).getUuidAndAbbrevTitleCache(null, null, null);
|
65
|
}else{
|
66
|
model = CdmStore.getService(IAgentService.class).getUuidAndAbbrevTitleCache(limitOfInitialElements, pattern, null);
|
67
|
}
|
68
|
}
|
69
|
|
70
|
/** {@inheritDoc} */
|
71
|
@Override
|
72
|
protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
|
73
|
if(TEAM.equals(parameter)){
|
74
|
return new NewTeamWizard(true);
|
75
|
}
|
76
|
else if(PERSON.equals(parameter)){
|
77
|
return new NewPersonWizard();
|
78
|
}
|
79
|
else{
|
80
|
throw new IllegalArgumentException("Could not determine the desired wizard.");
|
81
|
}
|
82
|
}
|
83
|
/**
|
84
|
*
|
85
|
* @return
|
86
|
*/
|
87
|
@Override
|
88
|
protected ILabelProvider createListLabelProvider() {
|
89
|
return new FilteredCdmResourceAbbrevLabelProvider();
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* <p>select</p>
|
94
|
*
|
95
|
* @param shell a {@link org.eclipse.swt.widgets.Shell} object.
|
96
|
* @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
|
97
|
* @param entity a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
|
98
|
* @return a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
|
99
|
*/
|
100
|
public static AgentBase select(Shell shell, ConversationHolder conversation, AgentBase entity, boolean teamMemberSelection) {
|
101
|
NomenclaturalAuthorSelectionDialog dialog = new NomenclaturalAuthorSelectionDialog(shell, conversation,
|
102
|
"Choose Agent", false, NomenclaturalAuthorSelectionDialog.class.getCanonicalName(), entity, teamMemberSelection);
|
103
|
return getSelectionFromDialog(dialog);
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* <p>getTitle</p>
|
108
|
*
|
109
|
* @param cdmObject a T object.
|
110
|
* @return a {@link java.lang.String} object.
|
111
|
*/
|
112
|
@Override
|
113
|
protected String getTitle(AgentBase cdmObject) {
|
114
|
if(cdmObject == null){
|
115
|
return "";
|
116
|
}
|
117
|
|
118
|
if (cdmObject instanceof TeamOrPersonBase) {
|
119
|
return ((TeamOrPersonBase) cdmObject).getNomenclaturalTitle();
|
120
|
} else if (cdmObject instanceof AgentBase){
|
121
|
return ((TeamOrPersonBase) cdmObject).getTitleCache();
|
122
|
}
|
123
|
|
124
|
throw new IllegalArgumentException("Generic method only" +
|
125
|
" supports cdmObject of type IIdentifiableEntity." +
|
126
|
" Please implement specific method in subclass.");
|
127
|
}
|
128
|
|
129
|
|
130
|
|
131
|
public class FilteredCdmResourceAbbrevLabelProvider extends LabelProvider {
|
132
|
@Override
|
133
|
public String getText(Object element) {
|
134
|
if (element == null) {
|
135
|
return null;
|
136
|
}
|
137
|
UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache) element;
|
138
|
String titleCache = uuidAndTitleCache.getTitleCache();
|
139
|
String abbrevTitleCache = uuidAndTitleCache.getAbbrevTitleCache();
|
140
|
if (!titleCache.equals(abbrevTitleCache)){
|
141
|
abbrevTitleCache += " - " + titleCache;
|
142
|
}
|
143
|
if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG)){
|
144
|
abbrevTitleCache += " ["+uuidAndTitleCache.getId()+"]";
|
145
|
}
|
146
|
return abbrevTitleCache;
|
147
|
}
|
148
|
};
|
149
|
|
150
|
/** {@inheritDoc} */
|
151
|
@Override
|
152
|
protected ItemsFilter createFilter() {
|
153
|
return new ItemsFilter() {
|
154
|
|
155
|
/**
|
156
|
* Always returns false to enforce refiltering even if the pattern is equal
|
157
|
*/
|
158
|
@Override
|
159
|
public boolean equalsFilter(ItemsFilter filter) {
|
160
|
return false;
|
161
|
}
|
162
|
|
163
|
@Override
|
164
|
public boolean isConsistentItem(Object item) {
|
165
|
return false;
|
166
|
}
|
167
|
|
168
|
@Override
|
169
|
public boolean matchItem(Object item) {
|
170
|
String textTitleCache = null;
|
171
|
String abbrevTitleCache = null;
|
172
|
if(item instanceof UuidAndTitleCache){
|
173
|
if (((UuidAndTitleCache) item).getAbbrevTitleCache() != null){
|
174
|
abbrevTitleCache = ((UuidAndTitleCache) item).getAbbrevTitleCache();
|
175
|
}
|
176
|
if (((UuidAndTitleCache) item).getTitleCache() != null ){
|
177
|
textTitleCache = ((UuidAndTitleCache) item).getTitleCache();
|
178
|
}
|
179
|
}else if(item instanceof String){
|
180
|
textTitleCache = (String) item;
|
181
|
}
|
182
|
|
183
|
return (matches(textTitleCache) || matches(abbrevTitleCache)) ;
|
184
|
|
185
|
}
|
186
|
|
187
|
};
|
188
|
}
|
189
|
|
190
|
@Override
|
191
|
protected Comparator getItemsComparator() {
|
192
|
return new Comparator<UuidAndTitleCache>() {
|
193
|
@Override
|
194
|
public int compare(UuidAndTitleCache entity1,
|
195
|
UuidAndTitleCache entity2) {
|
196
|
|
197
|
if (entity1.getUuid().equals(entity2.getUuid())){
|
198
|
return 0;
|
199
|
}
|
200
|
Collator collator = Collator.getInstance();
|
201
|
String compareString1 = "";
|
202
|
if (entity1.getAbbrevTitleCache() != null){
|
203
|
compareString1 = entity1.getAbbrevTitleCache();
|
204
|
}
|
205
|
if (entity1.getTitleCache() != null){
|
206
|
compareString1 += entity1.getTitleCache();
|
207
|
}
|
208
|
|
209
|
|
210
|
String compareString2 = "";
|
211
|
if (entity2.getAbbrevTitleCache() != null){
|
212
|
compareString2 = entity2.getAbbrevTitleCache();
|
213
|
}
|
214
|
if (entity2.getTitleCache() != null){
|
215
|
compareString2 += entity2.getTitleCache();
|
216
|
}
|
217
|
|
218
|
|
219
|
int result =collator.compare(compareString1, compareString2);
|
220
|
if (result == 0){
|
221
|
result = entity1.getUuid().compareTo(entity2.getUuid());
|
222
|
}
|
223
|
return result;
|
224
|
}
|
225
|
};
|
226
|
}
|
227
|
|
228
|
|
229
|
}
|