2 * Copyright (C) 2007 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.
10 package eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
;
12 import java
.util
.ArrayList
;
13 import java
.util
.HashMap
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
18 import java
.util
.UUID
;
20 import org
.apache
.commons
.lang
.StringUtils
;
21 import org
.eclipse
.jface
.viewers
.ILabelProvider
;
22 import org
.eclipse
.swt
.SWT
;
23 import org
.eclipse
.swt
.events
.SelectionAdapter
;
24 import org
.eclipse
.swt
.events
.SelectionEvent
;
25 import org
.eclipse
.swt
.widgets
.Button
;
26 import org
.eclipse
.swt
.widgets
.Composite
;
27 import org
.eclipse
.swt
.widgets
.Control
;
28 import org
.eclipse
.swt
.widgets
.Shell
;
29 import org
.eclipse
.swt
.widgets
.Text
;
31 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
32 import eu
.etaxonomy
.cdm
.api
.service
.dto
.IdentifiedEntityDTO
;
33 import eu
.etaxonomy
.cdm
.api
.service
.dto
.IdentifiedEntityDTO
.AlternativeIdentifier
;
34 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
35 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
36 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
37 import eu
.etaxonomy
.taxeditor
.newWizard
.AbstractNewEntityWizard
;
38 import eu
.etaxonomy
.taxeditor
.newWizard
.NewReferenceWizard
;
39 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
40 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
41 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
44 * <p>FilteredReferenceSelectionDialog class.</p>
50 public class ReferenceSelectionDialog
extends AbstractFilteredCdmResourceSelectionDialog
<Reference
> {
52 protected static boolean isInReference
= false;
53 private Reference currentReference
;
54 List
<String
> lastSelectedReferences
= null;
55 Map
<UUID
, AlternativeIdentifier
> identifierMap
;
60 * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
61 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
62 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
63 * @return a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
65 public static Reference
select(Shell shell
, //ConversationHolder conversation,
66 Reference reference
, boolean isInReference
) {
67 ReferenceSelectionDialog dialog
= new ReferenceSelectionDialog(shell
, //conversation,
68 "Choose a reference", false, reference
, isInReference
);
69 return getSelectionFromDialog(dialog
);
75 * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
76 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
77 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
78 * @return a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
80 public static Reference
select(Shell shell
, //ConversationHolder conversation,
81 Reference reference
) {
82 ReferenceSelectionDialog dialog
= new ReferenceSelectionDialog(shell
, //conversation,
83 "Choose a reference", false, reference
);
84 return getSelectionFromDialog(dialog
);
90 * <p>Constructor for FilteredReferenceSelectionDialog.</p>
92 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
93 * @param title a {@link java.lang.String} object.
94 * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
95 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
96 * @param multi a boolean.
98 protected ReferenceSelectionDialog(Shell shell
, //ConversationHolder conversation,
99 String title
, boolean multi
, Reference reference
) {
100 super(shell
, //conversation,
101 title
, multi
, ReferenceSelectionDialog
.class.getCanonicalName(), reference
);
102 this.currentReference
= reference
;
111 * <p>Constructor for FilteredReferenceSelectionDialog.</p>
113 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
114 * @param title a {@link java.lang.String} object.
115 * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
116 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
117 * @param multi a boolean.
119 protected ReferenceSelectionDialog(Shell shell
, //ConversationHolder conversation,
120 String title
, boolean multi
, Reference reference
, boolean isInReference
) {
121 super(shell
, //conversation,
122 title
, multi
, ReferenceSelectionDialog
.class.getCanonicalName());
123 this.isInReference
= isInReference
;
124 this.currentReference
= reference
;
130 * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#getPersistentObject(java.util.UUID)
134 protected Reference
getPersistentObject(UUID cdmUuid
) {
135 if (lastSelectedReferences
== null){
136 lastSelectedReferences
= new ArrayList
<>();
138 if (lastSelectedReferences
.size()<6){
139 lastSelectedReferences
.add(cdmUuid
.toString());
141 lastSelectedReferences
.remove(0);
142 lastSelectedReferences
.add(cdmUuid
.toString());
145 PreferencesUtil
.setLastSelectedReference(lastSelectedReferences
);
146 return CdmStore
.getService(IReferenceService
.class).load(cdmUuid
);
150 * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#search
154 protected void callService(String pattern
) {
156 if (StringUtils
.isBlank(pattern
) && lastSelectedReferences
== null){
157 lastSelectedReferences
= PreferencesUtil
.getLastSelectedReferences();
158 Set
<UUID
> uuids
= new HashSet
<>();
159 for (String uuidString
: lastSelectedReferences
){
160 uuids
.add(UUID
.fromString(uuidString
));
162 if (!uuids
.isEmpty()){
163 model
= CdmStore
.getService(IReferenceService
.class).getUuidAndTitleCacheForUUIDS(uuids
);
166 } else if (isInReference
&& currentReference
!= null){
168 if (isUseIdentifier()){
169 List
<IdentifiedEntityDTO
<Reference
>> list
= CdmStore
.getService(IReferenceService
.class).listByIdentifierAbbrev(pattern
, null, MatchMode
.BEGINNING
, limitOfInitialElements
);
173 for (IdentifiedEntityDTO dto
: list
){
175 model
.add(dto
.getCdmEntity());
178 model
= CdmStore
.getService(IReferenceService
.class).getUuidAndTitleCache(limitOfInitialElements
,pattern
, currentReference
.getType());
182 if (isUseIdentifier() && PreferencesUtil
.getBooleanValue(IPreferenceKeys
.SEARCH_FOR_IDENTIFIER_AND_TITLECACHE
)){
183 List
<IdentifiedEntityDTO
<Reference
>> list
= CdmStore
.getService(IReferenceService
.class).listByIdentifierAndTitleCacheAbbrev(pattern
, null, MatchMode
.BEGINNING
, limitOfInitialElements
);
187 identifierMap
= new HashMap();
188 for (IdentifiedEntityDTO dto
: list
){
189 if (dto
.getIdentifier() != null){
190 identifierMap
.put(dto
.getCdmEntity().getUuid(), dto
.getIdentifier());
192 model
.add(dto
.getCdmEntity());
195 }else if (isUseIdentifier() ){
196 List
<IdentifiedEntityDTO
<Reference
>> list
= CdmStore
.getService(IReferenceService
.class).listByIdentifierAbbrev(pattern
, null, MatchMode
.BEGINNING
, limitOfInitialElements
);
200 identifierMap
= new HashMap();
201 for (IdentifiedEntityDTO dto
: list
){
202 identifierMap
.put(dto
.getCdmEntity().getUuid(), dto
.getIdentifier());
203 model
.add(dto
.getCdmEntity());
207 model
= CdmStore
.getService(IReferenceService
.class).getUuidAndTitleCache(limitOfInitialElements
,pattern
);
215 protected void addIdentifierCheckButton(Composite searchAndFilter
) {
216 Button btnCheckButton
= new Button(searchAndFilter
, SWT
.CHECK
);
217 btnCheckButton
.setText("Use Identifier");
218 if (PreferencesUtil
.getBooleanValue(IPreferenceKeys
.IS_SEARCH_FOR_IDENTIFIER_AS_DEFAULT
)){
219 btnCheckButton
.setSelection(true);
220 useIdentifier
= true;
222 btnCheckButton
.addSelectionListener(new SelectionAdapter() {
224 public void widgetSelected(SelectionEvent e
) {
225 useIdentifier
= btnCheckButton
.getSelection();
232 protected String
getTitle(Reference cdmObject
) {
233 if(cdmObject
== null){
236 return super.getTitle(cdmObject
);
239 // return DefaultReferenceCacheStrategy.putAuthorToEndOfString(cdmObject.getTitleCache(), cdmObject.getAuthorship().getTitleCache());
245 protected AbstractNewEntityWizard
getNewEntityWizard(String parameter
) {
246 return new NewReferenceWizard();
251 protected String
[] getNewWizardText() {
252 return new String
[]{"New Reference"};
257 protected void search() {
258 Control control
=getSearchField();
259 String pattern
= null;
260 if (control
!= null){
261 pattern
= ((Text
)control
).getText();
262 callService(pattern
);
264 fillContentProvider(null);
268 protected ILabelProvider
createListLabelProvider() {
269 return new FilteredReferenceLabelProvider();
272 public class FilteredReferenceLabelProvider
extends FilteredCdmResourceLabelProvider
{
274 public String
getText(Object element
) {
275 if (element
== null) {
278 UuidAndTitleCache uuidAndTitleCache
= (UuidAndTitleCache
) element
;
279 String titleCache
= uuidAndTitleCache
.getTitleCache();
280 if(PreferencesUtil
.getBooleanValue(IPreferenceKeys
.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG
)){
281 titleCache
+= " ["+uuidAndTitleCache
.getId()+"]";
283 if (isUseIdentifier()){
284 if (identifierMap
!= null){
285 AlternativeIdentifier identifier
= identifierMap
.get(uuidAndTitleCache
.getUuid());
286 if (identifier
!= null){
287 titleCache
+= " (" + identifier
.getTypeLabel() +": " + identifier
.getIdentifier() + ")";
289 titleCache
+= " (-)";