3 * Copyright (C) 2016 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
;
14 import java
.text
.Collator
;
15 import java
.util
.Comparator
;
17 import org
.eclipse
.core
.runtime
.Assert
;
18 import org
.eclipse
.core
.runtime
.ListenerList
;
19 import org
.eclipse
.jface
.viewers
.IColorProvider
;
20 import org
.eclipse
.jface
.viewers
.IFontProvider
;
21 import org
.eclipse
.jface
.viewers
.ILabelDecorator
;
22 import org
.eclipse
.jface
.viewers
.ILabelProvider
;
23 import org
.eclipse
.jface
.viewers
.ILabelProviderListener
;
24 import org
.eclipse
.jface
.viewers
.LabelProvider
;
25 import org
.eclipse
.jface
.viewers
.LabelProviderChangedEvent
;
26 import org
.eclipse
.jface
.viewers
.StyledCellLabelProvider
;
27 import org
.eclipse
.jface
.viewers
.StyledString
;
28 import org
.eclipse
.jface
.viewers
.ViewerCell
;
29 import org
.eclipse
.jface
.viewers
.DelegatingStyledCellLabelProvider
.IStyledLabelProvider
;
30 import org
.eclipse
.swt
.SWT
;
31 import org
.eclipse
.swt
.graphics
.Color
;
32 import org
.eclipse
.swt
.graphics
.Font
;
33 import org
.eclipse
.swt
.graphics
.GC
;
34 import org
.eclipse
.swt
.graphics
.Image
;
35 import org
.eclipse
.swt
.graphics
.Rectangle
;
36 import org
.eclipse
.swt
.widgets
.Control
;
37 import org
.eclipse
.swt
.widgets
.Display
;
38 import org
.eclipse
.swt
.widgets
.Shell
;
39 import org
.eclipse
.swt
.widgets
.Text
;
40 import org
.eclipse
.ui
.internal
.IWorkbenchGraphicConstants
;
41 import org
.eclipse
.ui
.internal
.WorkbenchImages
;
43 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
44 import eu
.etaxonomy
.cdm
.api
.service
.IAgentService
;
45 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
46 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
47 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
48 import eu
.etaxonomy
.taxeditor
.newWizard
.AbstractNewEntityWizard
;
49 import eu
.etaxonomy
.taxeditor
.newWizard
.NewPersonWizard
;
50 import eu
.etaxonomy
.taxeditor
.newWizard
.NewTeamWizard
;
51 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
52 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
53 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
54 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.AbstractFilteredCdmResourceSelectionDialog
.FilteredCdmResourceLabelProvider
;
55 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.CdmFilteredItemsSelectionDialog
.ItemsFilter
;
56 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.CdmFilteredItemsSelectionDialog
.ItemsListSeparator
;
63 public class NomenclaturalAuthorSelectionDialog
extends AgentSelectionDialog
{
74 protected NomenclaturalAuthorSelectionDialog(Shell shell
, ConversationHolder conversation
, String title
,
75 boolean multi
, String settings
, AgentBase agent
, boolean teamMemberSelection
) {
76 super(shell
, conversation
, title
, multi
, settings
, agent
, teamMemberSelection
);
77 // TODO Auto-generated constructor stub
81 protected void initModel() {
82 Control control
= getPatternControl();
83 String pattern
= null;
85 pattern
= ((Text
)control
).getText();
88 model
= CdmStore
.getService(IAgentService
.class).getUuidAndAbbrevTitleCache(limitOfInitialElements
, pattern
, null);
93 protected AbstractNewEntityWizard
getNewEntityWizard(String parameter
) {
94 if(TEAM
.equals(parameter
)){
95 return new NewTeamWizard(true);
97 else if(PERSON
.equals(parameter
)){
98 return new NewPersonWizard();
101 throw new IllegalArgumentException("Could not determine the desired wizard.");
109 protected ILabelProvider
createListLabelProvider() {
110 return new FilteredCdmResourceAbbrevLabelProvider();
116 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
117 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
118 * @param entity a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
119 * @return a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
121 public static AgentBase
select(Shell shell
, ConversationHolder conversation
, AgentBase entity
, boolean teamMemberSelection
) {
122 NomenclaturalAuthorSelectionDialog dialog
= new NomenclaturalAuthorSelectionDialog(shell
, conversation
,
123 "Choose Agent", false, NomenclaturalAuthorSelectionDialog
.class.getCanonicalName(), entity
, teamMemberSelection
);
124 return getSelectionFromDialog(dialog
);
130 * @param cdmObject a T object.
131 * @return a {@link java.lang.String} object.
134 protected String
getTitle(AgentBase cdmObject
) {
135 if(cdmObject
== null){
139 if (cdmObject
instanceof TeamOrPersonBase
) {
140 return ((TeamOrPersonBase
) cdmObject
).getNomenclaturalTitle();
141 } else if (cdmObject
instanceof AgentBase
){
142 return ((TeamOrPersonBase
) cdmObject
).getTitleCache();
145 throw new IllegalArgumentException("Generic method only" +
146 " supports cdmObject of type IIdentifiableEntity." +
147 " Please implement specific method in subclass.");
152 public class FilteredCdmResourceAbbrevLabelProvider
extends LabelProvider
{
154 public String
getText(Object element
) {
155 if (element
== null) {
158 UuidAndTitleCache uuidAndTitleCache
= (UuidAndTitleCache
) element
;
159 String titleCache
= uuidAndTitleCache
.getTitleCache();
160 String abbrevTitleCache
= uuidAndTitleCache
.getAbbrevTitleCache();
161 if (!titleCache
.equals(abbrevTitleCache
)){
162 abbrevTitleCache
+= " - " + titleCache
;
164 if(PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG
)){
165 abbrevTitleCache
+= " ["+uuidAndTitleCache
.getId()+"]";
167 return abbrevTitleCache
;
173 protected ItemsFilter
createFilter() {
174 return new ItemsFilter() {
177 * Always returns false to enforce refiltering even if the pattern is equal
180 public boolean equalsFilter(ItemsFilter filter
) {
185 public boolean isConsistentItem(Object item
) {
190 public boolean matchItem(Object item
) {
191 String textTitleCache
= null;
192 String abbrevTitleCache
= null;
193 if(item
instanceof UuidAndTitleCache
){
194 if (((UuidAndTitleCache
) item
).getAbbrevTitleCache() != null){
195 abbrevTitleCache
= ((UuidAndTitleCache
) item
).getAbbrevTitleCache();
197 if (((UuidAndTitleCache
) item
).getTitleCache() != null ){
198 textTitleCache
= ((UuidAndTitleCache
) item
).getTitleCache();
200 }else if(item
instanceof String
){
201 textTitleCache
= (String
) item
;
204 return (matches(textTitleCache
) || matches(abbrevTitleCache
)) ;
212 protected Comparator
getItemsComparator() {
213 return new Comparator
<UuidAndTitleCache
>() {
215 public int compare(UuidAndTitleCache entity1
,
216 UuidAndTitleCache entity2
) {
217 Collator collator
= Collator
.getInstance();
218 String compareString1
= "";
219 if (entity1
.getAbbrevTitleCache() != null){
220 compareString1
= entity1
.getAbbrevTitleCache();
222 if (entity1
.getTitleCache() != null){
223 compareString1
+= entity1
.getTitleCache();
227 String compareString2
= "";
228 if (entity2
.getAbbrevTitleCache() != null){
229 compareString2
= entity2
.getAbbrevTitleCache();
231 if (entity2
.getTitleCache() != null){
232 compareString2
+= entity2
.getTitleCache();
236 return collator
.compare(compareString1
, compareString2
);