1 package eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
;
3 import java
.text
.Collator
;
4 import java
.util
.Comparator
;
5 import java
.util
.HashMap
;
8 import org
.apache
.commons
.lang
.StringUtils
;
9 import org
.eclipse
.jface
.viewers
.ILabelProvider
;
10 import org
.eclipse
.jface
.viewers
.LabelProvider
;
11 import org
.eclipse
.swt
.SWT
;
12 import org
.eclipse
.swt
.events
.SelectionAdapter
;
13 import org
.eclipse
.swt
.events
.SelectionEvent
;
14 import org
.eclipse
.swt
.widgets
.Button
;
15 import org
.eclipse
.swt
.widgets
.Composite
;
16 import org
.eclipse
.swt
.widgets
.Control
;
17 import org
.eclipse
.swt
.widgets
.Shell
;
18 import org
.eclipse
.swt
.widgets
.Text
;
20 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
21 import eu
.etaxonomy
.cdm
.api
.service
.dto
.EntityDTOBase
.CdmEntity
;
22 import eu
.etaxonomy
.cdm
.api
.service
.dto
.IdentifiedEntityDTO
;
23 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
24 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
25 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
26 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
27 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
28 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
29 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
31 public class NomenclaturalReferenceSelectionDialog
extends
32 ReferenceSelectionDialog
{
34 private TeamOrPersonBase author
;
35 private boolean firstCall
= true;
37 protected NomenclaturalReferenceSelectionDialog(Shell shell
,//ConversationHolder conversation,
38 String title
, boolean multi
,
39 Reference reference
, boolean inReference
, TeamOrPersonBase author
) {
40 super(shell
, //conversation,
41 title
, multi
, reference
);
48 * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#search
52 protected void callService(String pattern
) {
53 Control control
=getSearchField();
55 if (firstCall
&& author
!= null){
56 pattern
= author
.getNomenclaturalTitle();
59 model
= CdmStore
.getService(IReferenceService
.class).getUuidAndAbbrevTitleCacheForAuthor(null, pattern
, null);
65 if (isUseIdentifier() && PreferencesUtil
.getBooleanValue(IPreferenceKeys
.SEARCH_FOR_IDENTIFIER_AND_TITLECACHE
)){
66 List
<IdentifiedEntityDTO
<Reference
>> list
= CdmStore
.getService(IReferenceService
.class).listByIdentifierAndTitleCacheAbbrev(pattern
, null, MatchMode
.BEGINNING
, limitOfInitialElements
);
70 identifierMap
= new HashMap();
71 for (IdentifiedEntityDTO dto
: list
){
72 if (dto
.getIdentifier() != null){
73 identifierMap
.put(dto
.getCdmEntity().getUuid(), dto
.getIdentifier());
75 model
.add(dto
.getCdmEntity());
78 }else if (isUseIdentifier() ){
79 List
<IdentifiedEntityDTO
<Reference
>> list
= CdmStore
.getService(IReferenceService
.class).listByIdentifierAbbrev(pattern
, null, MatchMode
.BEGINNING
, limitOfInitialElements
);
83 identifierMap
= new HashMap();
84 for (IdentifiedEntityDTO dto
: list
){
85 identifierMap
.put(dto
.getCdmEntity().getUuid(), dto
.getIdentifier());
86 model
.add(dto
.getCdmEntity());
90 model
= CdmStore
.getService(IReferenceService
.class).getUuidAndAbbrevTitleCache(null, pattern
);
105 protected ILabelProvider
createListLabelProvider() {
106 return new FilteredCdmResourceAbbrevLabelProvider();
109 protected void addIdentifierCheckButton(Composite searchAndFilter
) {
110 Button btnCheckButton
= new Button(searchAndFilter
, SWT
.CHECK
);
111 btnCheckButton
.setText("Use Identifier");
112 btnCheckButton
.addSelectionListener(new SelectionAdapter() {
114 public void widgetSelected(SelectionEvent e
) {
115 useIdentifier
= btnCheckButton
.getSelection();
123 * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
124 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
125 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
126 * @return a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
129 public static Reference
select(Shell shell
, //ConversationHolder conversation,
130 Reference reference
, TeamOrPersonBase author
) {
131 NomenclaturalReferenceSelectionDialog dialog
= new NomenclaturalReferenceSelectionDialog(shell
,// conversation,
132 "Choose a reference", isInReference
, reference
, false, author
);
133 return getSelectionFromDialog(dialog
);
137 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter()
141 // protected ItemsFilter createFilter() {
142 // return new ItemsFilter() {
145 // * Always returns false to enforce refiltering even if the pattern is equal
148 // public boolean equalsFilter(ItemsFilter filter) {
153 // public boolean isConsistentItem(Object item) {
158 // public boolean matchItem(Object item) {
160 //// String text = null;
161 //// if(item instanceof UuidAndTitleCache){
162 //// if (((UuidAndTitleCache) item).getAbbrevTitleCache() != null){
163 //// text = ((UuidAndTitleCache) item).getAbbrevTitleCache();
165 //// text = ((UuidAndTitleCache) item).getTitleCache();
167 //// }else if(item instanceof String){
168 //// text = (String) item;
170 //// return text != null ? matches(text) : false;
178 public class FilteredCdmResourceAbbrevLabelProvider
extends LabelProvider
{
180 public String
getText(Object element
) {
181 if (element
== null) {
184 UuidAndTitleCache uuidAndTitleCache
;
185 if (element
instanceof CdmEntity
){
186 uuidAndTitleCache
= (CdmEntity
)element
;
188 uuidAndTitleCache
= (UuidAndTitleCache
) element
;
190 String titleCache
= uuidAndTitleCache
.getTitleCache();
191 String abbrevTitleCache
= uuidAndTitleCache
.getAbbrevTitleCache();
192 if (!abbrevTitleCache
.equals(titleCache
)){
193 abbrevTitleCache
+= " - " + titleCache
;
195 if(PreferencesUtil
.getBooleanValue(IPreferenceKeys
.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG
)){
196 abbrevTitleCache
+= " ["+uuidAndTitleCache
.getId()+"]";
198 return abbrevTitleCache
;
203 protected Comparator
getItemsComparator() {
204 return new Comparator
<UuidAndTitleCache
>() {
206 public int compare(UuidAndTitleCache entity1
,
207 UuidAndTitleCache entity2
) {
209 if (entity1
.getUuid().equals(entity2
.getUuid())){
212 Collator collator
= Collator
.getInstance();
213 String compareString1
= "";
214 if (entity1
.getAbbrevTitleCache() != null){
215 compareString1
= entity1
.getAbbrevTitleCache();
217 if (entity1
.getTitleCache() != null){
218 compareString1
+= entity1
.getTitleCache();
222 String compareString2
= "";
223 if (entity2
.getAbbrevTitleCache() != null){
224 compareString2
= entity2
.getAbbrevTitleCache();
226 if (entity2
.getTitleCache() != null){
227 compareString2
+= entity2
.getTitleCache();
231 int result
=collator
.compare(compareString1
, compareString2
);
233 result
= entity1
.getUuid().compareTo(entity2
.getUuid());
240 protected String
getTitle(Reference cdmObject
) {
241 if(cdmObject
== null){
244 if (cdmObject
.getAbbrevTitle() != null){
245 return cdmObject
.getAbbrevTitleCache();
247 return cdmObject
.getTitleCache();
255 protected void search() {
256 Control control
=getSearchField();
257 String pattern
= null;
258 if (control
!= null){
259 pattern
= ((Text
)control
).getText();
260 if (pattern
.equals("*") || pattern
.equals("?")){
262 }else if (StringUtils
.isNotBlank(pattern
)){
263 callService(pattern
);
264 } else if (firstCall
){
267 fillContentProvider(null);