fix #8148: add widthHint to styledTextfield
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / NomenclaturalReferenceSelectionDialog.java
1 package eu.etaxonomy.taxeditor.ui.dialog.selection;
2
3 import java.text.Collator;
4 import java.util.Comparator;
5 import java.util.HashMap;
6 import java.util.List;
7
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;
19
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;
30
31 public class NomenclaturalReferenceSelectionDialog extends
32 ReferenceSelectionDialog {
33
34 private TeamOrPersonBase author;
35 private boolean firstCall = true;
36
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);
42 this.author = author;
43 firstCall = true;
44
45 }
46
47 /* (non-Javadoc)
48 * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#search
49 */
50 /** {@inheritDoc} */
51 @Override
52 protected void callService(String pattern) {
53 Control control =getSearchField();
54
55 if (firstCall && author != null){
56 pattern = author.getNomenclaturalTitle();
57 if (control!= null){
58 firstCall = false;
59 model = CdmStore.getService(IReferenceService.class).getUuidAndAbbrevTitleCacheForAuthor(null, pattern, null);
60
61 }
62
63 }else{
64
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);
67 if (model!= null){
68 model.clear();
69 }
70 identifierMap = new HashMap();
71 for (IdentifiedEntityDTO dto: list){
72 if (dto.getIdentifier() != null){
73 identifierMap.put(dto.getCdmEntity().getUuid(), dto.getIdentifier());
74 }
75 model.add(dto.getCdmEntity());
76
77 }
78 }else if (isUseIdentifier() ){
79 List<IdentifiedEntityDTO<Reference>> list = CdmStore.getService(IReferenceService.class).listByIdentifierAbbrev(pattern, null, MatchMode.BEGINNING, limitOfInitialElements);
80 if (model!= null){
81 model.clear();
82 }
83 identifierMap = new HashMap();
84 for (IdentifiedEntityDTO dto: list){
85 identifierMap.put(dto.getCdmEntity().getUuid(), dto.getIdentifier());
86 model.add(dto.getCdmEntity());
87 }
88
89 }else{
90 model = CdmStore.getService(IReferenceService.class).getUuidAndAbbrevTitleCache(null, pattern);
91 }
92
93 }
94
95
96
97
98 }
99
100 /**
101 *
102 * @return
103 */
104 @Override
105 protected ILabelProvider createListLabelProvider() {
106 return new FilteredCdmResourceAbbrevLabelProvider();
107 }
108 @Override
109 protected void addIdentifierCheckButton(Composite searchAndFilter) {
110 Button btnCheckButton = new Button(searchAndFilter, SWT.CHECK);
111 btnCheckButton.setText("Use Identifier");
112 btnCheckButton.addSelectionListener(new SelectionAdapter() {
113 @Override
114 public void widgetSelected(SelectionEvent e) {
115 useIdentifier = btnCheckButton.getSelection();
116 }
117 });
118 }
119
120 /**
121 * <p>select</p>
122 *
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.
127 */
128
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);
134 }
135
136 /* (non-Javadoc)
137 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter()
138 */
139 /** {@inheritDoc} */
140 // @Override
141 // protected ItemsFilter createFilter() {
142 // return new ItemsFilter() {
143 //
144 // /**
145 // * Always returns false to enforce refiltering even if the pattern is equal
146 // */
147 // @Override
148 // public boolean equalsFilter(ItemsFilter filter) {
149 // return false;
150 // }
151 //
152 // @Override
153 // public boolean isConsistentItem(Object item) {
154 // return false;
155 // }
156 //
157 // @Override
158 // public boolean matchItem(Object item) {
159 // return true;
160 //// String text = null;
161 //// if(item instanceof UuidAndTitleCache){
162 //// if (((UuidAndTitleCache) item).getAbbrevTitleCache() != null){
163 //// text = ((UuidAndTitleCache) item).getAbbrevTitleCache();
164 //// } else{
165 //// text = ((UuidAndTitleCache) item).getTitleCache();
166 //// }
167 //// }else if(item instanceof String){
168 //// text = (String) item;
169 //// }
170 //// return text != null ? matches(text) : false;
171 // }
172 //
173 // };
174 // }
175
176
177
178 public class FilteredCdmResourceAbbrevLabelProvider extends LabelProvider {
179 @Override
180 public String getText(Object element) {
181 if (element == null) {
182 return null;
183 }
184 UuidAndTitleCache uuidAndTitleCache;
185 if (element instanceof CdmEntity){
186 uuidAndTitleCache = (CdmEntity)element;
187 }else{
188 uuidAndTitleCache = (UuidAndTitleCache) element;
189 }
190 String titleCache = uuidAndTitleCache.getTitleCache();
191 String abbrevTitleCache = uuidAndTitleCache.getAbbrevTitleCache();
192 if (!abbrevTitleCache.equals(titleCache)){
193 abbrevTitleCache += " - " + titleCache;
194 }
195 if(PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG)){
196 abbrevTitleCache += " ["+uuidAndTitleCache.getId()+"]";
197 }
198 return abbrevTitleCache;
199 }
200 };
201
202 @Override
203 protected Comparator getItemsComparator() {
204 return new Comparator<UuidAndTitleCache>() {
205 @Override
206 public int compare(UuidAndTitleCache entity1,
207 UuidAndTitleCache entity2) {
208
209 if (entity1.getUuid().equals(entity2.getUuid())){
210 return 0;
211 }
212 Collator collator = Collator.getInstance();
213 String compareString1 = "";
214 if (entity1.getAbbrevTitleCache() != null){
215 compareString1 = entity1.getAbbrevTitleCache();
216 }
217 if (entity1.getTitleCache() != null){
218 compareString1 += entity1.getTitleCache();
219 }
220
221
222 String compareString2 = "";
223 if (entity2.getAbbrevTitleCache() != null){
224 compareString2 = entity2.getAbbrevTitleCache();
225 }
226 if (entity2.getTitleCache() != null){
227 compareString2 += entity2.getTitleCache();
228 }
229
230
231 int result =collator.compare(compareString1, compareString2);
232 if (result == 0){
233 result = entity1.getUuid().compareTo(entity2.getUuid());
234 }
235 return result;
236 }
237 };
238 }
239 @Override
240 protected String getTitle(Reference cdmObject) {
241 if(cdmObject == null){
242 return "";
243 }
244 if (cdmObject.getAbbrevTitle() != null){
245 return cdmObject.getAbbrevTitleCache();
246 }else{
247 return cdmObject.getTitleCache();
248 }
249
250
251
252 }
253
254 @Override
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("?")){
261 callService(null);
262 }else if (StringUtils.isNotBlank(pattern)){
263 callService(pattern);
264 } else if (firstCall){
265 callService(null);
266 }
267 fillContentProvider(null);
268 }
269 }
270
271
272
273
274 }