Project

General

Profile

Download (7.99 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.ui.dialog.selection;
2

    
3
import java.text.Collator;
4
import java.util.Comparator;
5
import java.util.List;
6

    
7
import org.apache.commons.lang.StringUtils;
8
import org.eclipse.jface.viewers.ILabelProvider;
9
import org.eclipse.jface.viewers.LabelProvider;
10
import org.eclipse.swt.SWT;
11
import org.eclipse.swt.events.SelectionAdapter;
12
import org.eclipse.swt.events.SelectionEvent;
13
import org.eclipse.swt.widgets.Button;
14
import org.eclipse.swt.widgets.Composite;
15
import org.eclipse.swt.widgets.Control;
16
import org.eclipse.swt.widgets.Shell;
17
import org.eclipse.swt.widgets.Text;
18

    
19
import eu.etaxonomy.cdm.api.service.IReferenceService;
20
import eu.etaxonomy.cdm.api.service.dto.EntityDTOBase.CdmEntity;
21
import eu.etaxonomy.cdm.api.service.dto.IdentifiedEntityDTO;
22
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
23
import eu.etaxonomy.cdm.model.reference.Reference;
24
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
25
import eu.etaxonomy.cdm.persistence.query.MatchMode;
26
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
27
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
28
import eu.etaxonomy.taxeditor.store.CdmStore;
29

    
30
public class NomenclaturalReferenceSelectionDialog extends
31
		ReferenceSelectionDialog {
32

    
33
    private TeamOrPersonBase author;
34
    private boolean firstCall = true;
35

    
36
	protected NomenclaturalReferenceSelectionDialog(Shell shell,//ConversationHolder conversation,
37
			 String title, boolean multi,
38
			Reference reference, boolean inReference, TeamOrPersonBase author) {
39
		super(shell, //conversation,
40
		        title, multi, reference);
41
		this.author = author;
42
		firstCall = true;
43

    
44
	}
45

    
46
	/* (non-Javadoc)
47
	 * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#search
48
	 */
49
	/** {@inheritDoc} */
50
	@Override
51
	protected void callService(String pattern) {
52
        Control control =getSearchField();
53

    
54
        if (firstCall && author != null){
55
            pattern = author.getNomenclaturalTitle();
56
            if (control!= null){
57
                firstCall = false;
58
                model = CdmStore.getService(IReferenceService.class).getUuidAndAbbrevTitleCacheForAuthor(limitOfInitialElements, pattern, null);
59

    
60
            }
61

    
62
        }else{
63

    
64
            if (isUseIdentifier()){
65
                List<IdentifiedEntityDTO<Reference>> list = CdmStore.getService(IReferenceService.class).listByIdentifierAbbrev(pattern, null, MatchMode.BEGINNING, limitOfInitialElements);
66
                if (model!= null){
67
                    model.clear();
68
                }
69
                for (IdentifiedEntityDTO dto: list){
70

    
71
                    model.add(dto.getCdmEntity());
72
                }
73
            }else{
74
                model = CdmStore.getService(IReferenceService.class).getUuidAndAbbrevTitleCache(limitOfInitialElements, pattern);
75
            }
76

    
77
        }
78

    
79

    
80

    
81

    
82
	}
83

    
84
	/**
85
	 *
86
	 * @return
87
	 */
88
   @Override
89
	protected ILabelProvider createListLabelProvider() {
90
		return new FilteredCdmResourceAbbrevLabelProvider();
91
	}
92
   @Override
93
   protected void addIdentifierCheckButton(Composite searchAndFilter) {
94
       Button btnCheckButton = new Button(searchAndFilter, SWT.CHECK);
95
       btnCheckButton.setText("Use Identifier");
96
       btnCheckButton.addSelectionListener(new SelectionAdapter() {
97
           @Override
98
           public void widgetSelected(SelectionEvent e) {
99
               useIdentifier = btnCheckButton.getSelection();
100
           }
101
       });
102
   }
103

    
104
	/**
105
	 * <p>select</p>
106
	 *
107
	 * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
108
	 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
109
	 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
110
	 * @return a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
111
	 */
112

    
113
	public static Reference select(Shell shell, //ConversationHolder conversation,
114
	        Reference reference, TeamOrPersonBase author) {
115
		NomenclaturalReferenceSelectionDialog dialog = new NomenclaturalReferenceSelectionDialog(shell,// conversation,
116
				"Choose a reference", isInReference, reference, false, author);
117
		return getSelectionFromDialog(dialog);
118
	}
119

    
120
	/* (non-Javadoc)
121
	 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter()
122
	 */
123
	/** {@inheritDoc} */
124
//	@Override
125
//	protected ItemsFilter createFilter() {
126
//		return new ItemsFilter() {
127
//
128
//			/**
129
//			 * Always returns false to enforce refiltering even if the pattern is equal
130
//			 */
131
//			@Override
132
//			public boolean equalsFilter(ItemsFilter filter) {
133
//				return false;
134
//			}
135
//
136
//			@Override
137
//			public boolean isConsistentItem(Object item) {
138
//				return false;
139
//			}
140
//
141
//			@Override
142
//			public boolean matchItem(Object item) {
143
//				return true;
144
////				String text = null;
145
////				if(item instanceof UuidAndTitleCache){
146
////					if (((UuidAndTitleCache) item).getAbbrevTitleCache() != null){
147
////						text = ((UuidAndTitleCache) item).getAbbrevTitleCache();
148
////					} else{
149
////						text = ((UuidAndTitleCache) item).getTitleCache();
150
////					}
151
////				}else if(item instanceof String){
152
////					text = (String) item;
153
////				}
154
////				return text != null ? matches(text) : false;
155
//			}
156
//
157
//		};
158
//	}
159

    
160

    
161

    
162
	public class FilteredCdmResourceAbbrevLabelProvider extends LabelProvider {
163
		@Override
164
		public String getText(Object element) {
165
			if (element == null) {
166
				return null;
167
			}
168
			UuidAndTitleCache uuidAndTitleCache;
169
			if (element instanceof CdmEntity){
170
			    uuidAndTitleCache = (CdmEntity)element;
171
			}else{
172
			    uuidAndTitleCache = (UuidAndTitleCache) element;
173
			}
174
			String titleCache = uuidAndTitleCache.getTitleCache();
175
			String abbrevTitleCache = uuidAndTitleCache.getAbbrevTitleCache();
176
			if (!abbrevTitleCache.equals(titleCache)){
177
				abbrevTitleCache += " - " + titleCache;
178
			}
179
			if(PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG)){
180
				abbrevTitleCache += " ["+uuidAndTitleCache.getId()+"]";
181
			}
182
            return abbrevTitleCache;
183
		}
184
	};
185

    
186
	@Override
187
    protected Comparator getItemsComparator() {
188
        return new Comparator<UuidAndTitleCache>() {
189
            @Override
190
            public int compare(UuidAndTitleCache entity1,
191
                    UuidAndTitleCache entity2) {
192

    
193
                if (entity1.getUuid().equals(entity2.getUuid())){
194
                    return 0;
195
                }
196
                Collator collator = Collator.getInstance();
197
                String compareString1 = "";
198
                if (entity1.getAbbrevTitleCache() != null){
199
                    compareString1 = entity1.getAbbrevTitleCache();
200
                }
201
                if (entity1.getTitleCache() != null){
202
                    compareString1 += entity1.getTitleCache();
203
                }
204

    
205

    
206
                String compareString2 = "";
207
                if (entity2.getAbbrevTitleCache() != null){
208
                    compareString2 = entity2.getAbbrevTitleCache();
209
                }
210
                if (entity2.getTitleCache() != null){
211
                    compareString2 += entity2.getTitleCache();
212
                }
213

    
214

    
215
                int result =collator.compare(compareString1, compareString2);
216
                if (result == 0){
217
                    result = entity1.getUuid().compareTo(entity2.getUuid());
218
                }
219
                return result;
220
            }
221
        };
222
    }
223
	@Override
224
	protected String getTitle(Reference cdmObject) {
225
        if(cdmObject == null){
226
            return "";
227
        }
228
        if (cdmObject.getAbbrevTitle() != null){
229
            return cdmObject.getAbbrevTitleCache();
230
        }else{
231
            return cdmObject.getTitleCache();
232
        }
233

    
234

    
235

    
236
    }
237

    
238
	 @Override
239
	    protected void search() {
240
	        Control control =getSearchField();
241
	        String pattern = null;
242
	        if (control != null){
243
	            pattern = ((Text)control).getText();
244
	            if (pattern.equals("*") || pattern.equals("?")){
245
	                callService(null);
246
	            }else if (StringUtils.isNotBlank(pattern)){
247
	                callService(pattern);
248
	            } else if (firstCall){
249
	                callService(null);
250
	            }
251
	            fillContentProvider(null);
252
	        }
253
	 }
254

    
255

    
256

    
257

    
258
}
(27-27/44)