2 * Copyright (C) 2016 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
.ui
.dialog
.selection
;
13 import java
.text
.Collator
;
14 import java
.util
.Comparator
;
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
;
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
;
39 public class NomenclaturalAuthorSelectionDialog
extends AgentSelectionDialog
{
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
57 protected void initModel() {
58 Control control
= getPatternControl();
59 String pattern
= null;
61 pattern
= ((Text
)control
).getText();
63 if (pattern
== null || pattern
.equals("?")){
64 model
= CdmStore
.getService(IAgentService
.class).getUuidAndAbbrevTitleCache(null, null, null);
66 model
= CdmStore
.getService(IAgentService
.class).getUuidAndAbbrevTitleCache(limitOfInitialElements
, pattern
, null);
72 protected AbstractNewEntityWizard
getNewEntityWizard(String parameter
) {
73 if(TEAM
.equals(parameter
)){
74 return new NewTeamWizard(true);
76 else if(PERSON
.equals(parameter
)){
77 return new NewPersonWizard();
80 throw new IllegalArgumentException("Could not determine the desired wizard.");
88 protected ILabelProvider
createListLabelProvider() {
89 return new FilteredCdmResourceAbbrevLabelProvider();
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.
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
);
109 * @param cdmObject a T object.
110 * @return a {@link java.lang.String} object.
113 protected String
getTitle(AgentBase cdmObject
) {
114 if(cdmObject
== null){
118 if (cdmObject
instanceof TeamOrPersonBase
) {
119 return ((TeamOrPersonBase
) cdmObject
).getNomenclaturalTitle();
120 } else if (cdmObject
instanceof AgentBase
){
121 return ((TeamOrPersonBase
) cdmObject
).getTitleCache();
124 throw new IllegalArgumentException("Generic method only" +
125 " supports cdmObject of type IIdentifiableEntity." +
126 " Please implement specific method in subclass.");
131 public class FilteredCdmResourceAbbrevLabelProvider
extends LabelProvider
{
133 public String
getText(Object element
) {
134 if (element
== null) {
137 UuidAndTitleCache uuidAndTitleCache
= (UuidAndTitleCache
) element
;
138 String titleCache
= uuidAndTitleCache
.getTitleCache();
139 String abbrevTitleCache
= uuidAndTitleCache
.getAbbrevTitleCache();
140 if (!titleCache
.equals(abbrevTitleCache
)){
141 abbrevTitleCache
+= " - " + titleCache
;
143 if(PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG
)){
144 abbrevTitleCache
+= " ["+uuidAndTitleCache
.getId()+"]";
146 return abbrevTitleCache
;
152 protected ItemsFilter
createFilter() {
153 return new ItemsFilter() {
156 * Always returns false to enforce refiltering even if the pattern is equal
159 public boolean equalsFilter(ItemsFilter filter
) {
164 public boolean isConsistentItem(Object item
) {
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();
176 if (((UuidAndTitleCache
) item
).getTitleCache() != null ){
177 textTitleCache
= ((UuidAndTitleCache
) item
).getTitleCache();
179 }else if(item
instanceof String
){
180 textTitleCache
= (String
) item
;
183 return (matches(textTitleCache
) || matches(abbrevTitleCache
)) ;
191 protected Comparator
getItemsComparator() {
192 return new Comparator
<UuidAndTitleCache
>() {
194 public int compare(UuidAndTitleCache entity1
,
195 UuidAndTitleCache entity2
) {
196 Collator collator
= Collator
.getInstance();
197 String compareString1
= "";
198 if (entity1
.getAbbrevTitleCache() != null){
199 compareString1
= entity1
.getAbbrevTitleCache();
201 if (entity1
.getTitleCache() != null){
202 compareString1
+= entity1
.getTitleCache();
206 String compareString2
= "";
207 if (entity2
.getAbbrevTitleCache() != null){
208 compareString2
= entity2
.getAbbrevTitleCache();
210 if (entity2
.getTitleCache() != null){
211 compareString2
+= entity2
.getTitleCache();
215 return collator
.compare(compareString1
, compareString2
);