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
.core
.runtime
.Assert
;
17 import org
.eclipse
.core
.runtime
.ListenerList
;
18 import org
.eclipse
.jface
.viewers
.IColorProvider
;
19 import org
.eclipse
.jface
.viewers
.IFontProvider
;
20 import org
.eclipse
.jface
.viewers
.ILabelDecorator
;
21 import org
.eclipse
.jface
.viewers
.ILabelProvider
;
22 import org
.eclipse
.jface
.viewers
.ILabelProviderListener
;
23 import org
.eclipse
.jface
.viewers
.LabelProvider
;
24 import org
.eclipse
.jface
.viewers
.LabelProviderChangedEvent
;
25 import org
.eclipse
.jface
.viewers
.StyledCellLabelProvider
;
26 import org
.eclipse
.jface
.viewers
.StyledString
;
27 import org
.eclipse
.jface
.viewers
.ViewerCell
;
28 import org
.eclipse
.jface
.viewers
.DelegatingStyledCellLabelProvider
.IStyledLabelProvider
;
29 import org
.eclipse
.swt
.SWT
;
30 import org
.eclipse
.swt
.graphics
.Color
;
31 import org
.eclipse
.swt
.graphics
.Font
;
32 import org
.eclipse
.swt
.graphics
.GC
;
33 import org
.eclipse
.swt
.graphics
.Image
;
34 import org
.eclipse
.swt
.graphics
.Rectangle
;
35 import org
.eclipse
.swt
.widgets
.Control
;
36 import org
.eclipse
.swt
.widgets
.Display
;
37 import org
.eclipse
.swt
.widgets
.Shell
;
38 import org
.eclipse
.swt
.widgets
.Text
;
39 import org
.eclipse
.ui
.internal
.IWorkbenchGraphicConstants
;
40 import org
.eclipse
.ui
.internal
.WorkbenchImages
;
42 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
43 import eu
.etaxonomy
.cdm
.api
.service
.IAgentService
;
44 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
45 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
46 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
47 import eu
.etaxonomy
.taxeditor
.newWizard
.AbstractNewEntityWizard
;
48 import eu
.etaxonomy
.taxeditor
.newWizard
.NewPersonWizard
;
49 import eu
.etaxonomy
.taxeditor
.newWizard
.NewTeamWizard
;
50 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
51 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
52 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
53 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.AbstractFilteredCdmResourceSelectionDialog
.FilteredCdmResourceLabelProvider
;
54 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.CdmFilteredItemsSelectionDialog
.ItemsFilter
;
55 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.CdmFilteredItemsSelectionDialog
.ItemsListSeparator
;
62 public class NomenclaturalAuthorSelectionDialog
extends AgentSelectionDialog
{
73 protected NomenclaturalAuthorSelectionDialog(Shell shell
, ConversationHolder conversation
, String title
,
74 boolean multi
, String settings
, AgentBase agent
, boolean teamMemberSelection
) {
75 super(shell
, conversation
, title
, multi
, settings
, agent
, teamMemberSelection
);
76 // TODO Auto-generated constructor stub
80 protected void initModel() {
81 Control control
= getPatternControl();
82 String pattern
= null;
84 pattern
= ((Text
)control
).getText();
87 model
= CdmStore
.getService(IAgentService
.class).getUuidAndAbbrevTitleCache(limitOfInitialElements
, pattern
, null);
92 protected AbstractNewEntityWizard
getNewEntityWizard(String parameter
) {
93 if(TEAM
.equals(parameter
)){
94 return new NewTeamWizard(true);
96 else if(PERSON
.equals(parameter
)){
97 return new NewPersonWizard();
100 throw new IllegalArgumentException("Could not determine the desired wizard.");
108 protected ILabelProvider
createListLabelProvider() {
109 return new FilteredCdmResourceAbbrevLabelProvider();
115 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
116 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
117 * @param entity a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
118 * @return a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
120 public static AgentBase
select(Shell shell
, ConversationHolder conversation
, AgentBase entity
, boolean teamMemberSelection
) {
121 NomenclaturalAuthorSelectionDialog dialog
= new NomenclaturalAuthorSelectionDialog(shell
, conversation
,
122 "Choose Agent", false, NomenclaturalAuthorSelectionDialog
.class.getCanonicalName(), entity
, teamMemberSelection
);
123 return getSelectionFromDialog(dialog
);
129 * @param cdmObject a T object.
130 * @return a {@link java.lang.String} object.
133 protected String
getTitle(AgentBase cdmObject
) {
134 if(cdmObject
== null){
138 if (cdmObject
instanceof TeamOrPersonBase
) {
139 return ((TeamOrPersonBase
) cdmObject
).getNomenclaturalTitle();
140 } else if (cdmObject
instanceof AgentBase
){
141 return ((TeamOrPersonBase
) cdmObject
).getTitleCache();
144 throw new IllegalArgumentException("Generic method only" +
145 " supports cdmObject of type IIdentifiableEntity." +
146 " Please implement specific method in subclass.");
151 public class FilteredCdmResourceAbbrevLabelProvider
extends LabelProvider
{
153 public String
getText(Object element
) {
154 if (element
== null) {
157 UuidAndTitleCache uuidAndTitleCache
= (UuidAndTitleCache
) element
;
158 String titleCache
= uuidAndTitleCache
.getTitleCache();
159 String abbrevTitleCache
= uuidAndTitleCache
.getAbbrevTitleCache();
160 if (!titleCache
.equals(abbrevTitleCache
)){
161 abbrevTitleCache
+= " - " + titleCache
;
163 if(PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG
)){
164 abbrevTitleCache
+= " ["+uuidAndTitleCache
.getId()+"]";
166 return abbrevTitleCache
;
172 protected ItemsFilter
createFilter() {
173 return new ItemsFilter() {
176 * Always returns false to enforce refiltering even if the pattern is equal
179 public boolean equalsFilter(ItemsFilter filter
) {
184 public boolean isConsistentItem(Object item
) {
189 public boolean matchItem(Object item
) {
190 String textTitleCache
= null;
191 String abbrevTitleCache
= null;
192 if(item
instanceof UuidAndTitleCache
){
193 if (((UuidAndTitleCache
) item
).getAbbrevTitleCache() != null){
194 abbrevTitleCache
= ((UuidAndTitleCache
) item
).getAbbrevTitleCache();
196 if (((UuidAndTitleCache
) item
).getTitleCache() != null ){
197 textTitleCache
= ((UuidAndTitleCache
) item
).getTitleCache();
199 }else if(item
instanceof String
){
200 textTitleCache
= (String
) item
;
203 return (matches(textTitleCache
) || matches(abbrevTitleCache
)) ;
211 protected Comparator
getItemsComparator() {
212 return new Comparator
<UuidAndTitleCache
>() {
214 public int compare(UuidAndTitleCache entity1
,
215 UuidAndTitleCache entity2
) {
216 Collator collator
= Collator
.getInstance();
217 String compareString1
= "";
218 if (entity1
.getAbbrevTitleCache() != null){
219 compareString1
= entity1
.getAbbrevTitleCache();
221 if (entity1
.getTitleCache() != null){
222 compareString1
+= entity1
.getTitleCache();
226 String compareString2
= "";
227 if (entity2
.getAbbrevTitleCache() != null){
228 compareString2
= entity2
.getAbbrevTitleCache();
230 if (entity2
.getTitleCache() != null){
231 compareString2
+= entity2
.getTitleCache();
235 return collator
.compare(compareString1
, compareString2
);