Project

General

Profile

Download (12.3 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
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.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.ui.dialog.selection;
11

    
12
import java.util.ArrayList;
13
import java.util.HashMap;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Set;
18
import java.util.UUID;
19

    
20
import org.apache.commons.lang3.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;
30

    
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.metadata.PreferencePredicate;
35
import eu.etaxonomy.cdm.model.reference.Reference;
36
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
37
import eu.etaxonomy.cdm.persistence.query.MatchMode;
38
import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
39
import eu.etaxonomy.taxeditor.newWizard.NewReferenceWizard;
40
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
41
import eu.etaxonomy.taxeditor.store.CdmStore;
42

    
43
/**
44
 * <p>FilteredReferenceSelectionDialog class.</p>
45
 *
46
 * @author n.hoffmann
47
 * @created 04.06.2009
48
 * @version 1.0
49
 */
50
public class ReferenceSelectionDialog extends AbstractFilteredCdmResourceSelectionDialog<Reference> {
51

    
52
    protected static boolean isInReference = false;
53
    private Reference currentReference;
54
    List<String> lastSelectedReferences = null;
55
    Map<UUID, AlternativeIdentifier> identifierMap;
56
    Set<Reference> preSelectedReferences = new HashSet<>();
57

    
58
	/**
59
	 * <p>select</p>
60
	 *
61
	 * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
62
	 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
63
	 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
64
	 * @return a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
65
	 */
66
	public static Reference select(Shell shell, //ConversationHolder conversation,
67
	        Reference reference, boolean isInReference) {
68
		ReferenceSelectionDialog dialog = new ReferenceSelectionDialog(shell, //conversation,
69
				"Choose a reference", false, reference, isInReference);
70
		return getSelectionFromDialog(dialog);
71
	}
72

    
73
	/**
74
     * <p>select</p>
75
     *
76
     * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
77
     * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
78
     * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
79
     * @return a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
80
     */
81
    public static Reference select(Shell shell, //ConversationHolder conversation,
82
            Reference reference) {
83
        ReferenceSelectionDialog dialog = new ReferenceSelectionDialog(shell, //conversation,
84
                "Choose a reference", false, reference, null);
85
        return getSelectionFromDialog(dialog);
86
    }
87

    
88
    public static Reference select(Shell shell, String title,
89
            Reference reference, Set<Reference> preSelectedReferences) {
90
        ReferenceSelectionDialog dialog = new ReferenceSelectionDialog(shell, //conversation,
91
                title, false, reference, preSelectedReferences);
92
        return getSelectionFromDialog(dialog);
93
    }
94

    
95

    
96

    
97

    
98
	/**
99
	 * <p>Constructor for FilteredReferenceSelectionDialog.</p>
100
	 *
101
	 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
102
	 * @param title a {@link java.lang.String} object.
103
	 * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
104
	 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
105
	 * @param multi a boolean.
106
	 */
107
	protected ReferenceSelectionDialog(Shell shell, //ConversationHolder conversation,
108
	        String title, boolean multi, Reference reference, Set<Reference> preSelectedReferences) {
109
		super(shell, //conversation,
110
		        title, multi, ReferenceSelectionDialog.class.getCanonicalName(), reference);
111
		this.currentReference = reference;
112
		this.preSelectedReferences = preSelectedReferences;
113

    
114
	}
115

    
116

    
117

    
118

    
119
	/**
120
     * <p>Constructor for FilteredReferenceSelectionDialog.</p>
121
     *
122
     * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
123
     * @param title a {@link java.lang.String} object.
124
     * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
125
     * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
126
     * @param multi a boolean.
127
     */
128
    protected ReferenceSelectionDialog(Shell shell, //ConversationHolder conversation,
129
            String title, boolean multi, Reference reference, boolean isInReference) {
130
        super(shell, //conversation,
131
                title, multi, ReferenceSelectionDialog.class.getCanonicalName());
132
        this.isInReference = isInReference;
133
        this.currentReference = reference;
134

    
135
    }
136

    
137

    
138
	/* (non-Javadoc)
139
	 * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#getPersistentObject(java.util.UUID)
140
	 */
141
	/** {@inheritDoc} */
142
	@Override
143
	protected Reference getPersistentObject(UUID cdmUuid) {
144
	    if (lastSelectedReferences == null){
145
	        lastSelectedReferences = new ArrayList<>();
146
	    }
147
	    if (lastSelectedReferences.size()<6){
148
	        lastSelectedReferences.add(cdmUuid.toString());
149
	    }else{
150
	        lastSelectedReferences.remove(0);
151
	        lastSelectedReferences.add(cdmUuid.toString());
152
	    }
153

    
154
	    PreferencesUtil.setLastSelectedReference(lastSelectedReferences);
155
		return CdmStore.getService(IReferenceService.class).load(cdmUuid);
156
	}
157

    
158
	/* (non-Javadoc)
159
	 * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#search
160
	 */
161
	/** {@inheritDoc} */
162
	@Override
163
	protected void callService(String pattern) {
164

    
165
	    if (StringUtils.isBlank(pattern) ){
166
	        lastSelectedReferences = PreferencesUtil.getLastSelectedReferences();
167
	        Set<UUID> uuids = new HashSet<>();
168
	        for (String uuidString: lastSelectedReferences){
169
	            uuids.add(UUID.fromString(uuidString));
170
	        }
171
	        if (preSelectedReferences != null){
172
    	        for (Reference ref: preSelectedReferences){
173
    	            if (ref != null){
174
    	                uuids.add(ref.getUuid());
175
    	            }
176
                }
177
	        }
178
	        if (!uuids.isEmpty()){
179
	            if (currentReference != null){
180
	                uuids.remove(currentReference.getUuid());
181
	                if (this.isInReference){
182
	                    model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCacheForUUIDS(uuids, currentReference.getType());
183
	                }else{
184
	                    model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCacheForUUIDS(uuids);
185
	                }
186
	            }else{
187
	                model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCacheForUUIDS(uuids);
188
	            }
189

    
190
	        }
191

    
192
	    } else if (isInReference && currentReference != null){
193

    
194
            if (isUseIdentifier()){
195
                List<IdentifiedEntityDTO<Reference>> list = CdmStore.getService(IReferenceService.class).listByIdentifierAbbrev(pattern, null, MatchMode.BEGINNING, limitOfInitialElements);
196
                if (model!= null){
197
                    model.clear();
198
                }
199
                for (IdentifiedEntityDTO dto: list){
200

    
201
                    model.add(dto.getCdmEntity());
202
                }
203
            }else{
204

    
205
                model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCache(limitOfInitialElements,pattern, currentReference.getType());
206
            }
207

    
208
        }else{
209
            if (isUseIdentifier() && PreferencesUtil.getBooleanValue(PreferencePredicate.SearchForIdentifierAndTitleCache.getKey())){
210
                List<IdentifiedEntityDTO<Reference>> list = CdmStore.getService(IReferenceService.class).listByIdentifierAndTitleCacheAbbrev(pattern, null, MatchMode.BEGINNING, limitOfInitialElements);
211
                if (model!= null){
212
                    model.clear();
213
                }
214
                identifierMap = new HashMap();
215
                for (IdentifiedEntityDTO dto: list){
216
                    if (dto.getIdentifier() != null){
217
                        identifierMap.put(dto.getCdmEntity().getUuid(), dto.getIdentifier());
218
                    }
219
                    model.add(dto.getCdmEntity());
220

    
221
                }
222
            }else if (isUseIdentifier() ){
223
                List<IdentifiedEntityDTO<Reference>> list = CdmStore.getService(IReferenceService.class).listByIdentifierAbbrev(pattern, null, MatchMode.BEGINNING, limitOfInitialElements);
224
                if (model!= null){
225
                    model.clear();
226
                }
227
                identifierMap = new HashMap();
228
                for (IdentifiedEntityDTO dto: list){
229
                    identifierMap.put(dto.getCdmEntity().getUuid(), dto.getIdentifier());
230
                    model.add(dto.getCdmEntity());
231

    
232
                }
233
            }else {
234
                model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCache(limitOfInitialElements,pattern);
235
            }
236

    
237
        }
238

    
239
	}
240

    
241
	@Override
242
    protected void addIdentifierCheckButton(Composite searchAndFilter) {
243
        Button btnCheckButton = new Button(searchAndFilter, SWT.CHECK);
244
        btnCheckButton.setText("Use Identifier");
245
        if (PreferencesUtil.getBooleanValue(PreferencePredicate.SearchForIdentifierAsDefault.getKey())){
246
            btnCheckButton.setSelection(true);
247
            useIdentifier = true;
248
        }
249
        btnCheckButton.addSelectionListener(new SelectionAdapter() {
250
            @Override
251
            public void widgetSelected(SelectionEvent e) {
252
                useIdentifier = btnCheckButton.getSelection();
253
                search();
254
            }
255
        });
256
    }
257
	/** {@inheritDoc} */
258
	@Override
259
	protected String getTitle(Reference cdmObject) {
260
		if(cdmObject == null){
261
			return "";
262
		}else{
263
		    return super.getTitle(cdmObject);
264
		}
265
//		}else{
266
//			return DefaultReferenceCacheStrategy.putAuthorToEndOfString(cdmObject.getTitleCache(), cdmObject.getAuthorship().getTitleCache());
267
//		}
268
	}
269

    
270
	/** {@inheritDoc} */
271
	@Override
272
	protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
273
	    if (isInReference){
274
	        if (currentReference != null) {
275
                return new NewReferenceWizard(this.currentReference.getType());
276
            }
277
	    }
278
		return new NewReferenceWizard(null);
279
	}
280

    
281
	/** {@inheritDoc} */
282
	@Override
283
	protected String[] getNewWizardText() {
284
		return new String[]{"New Reference"};
285
	}
286

    
287
	/** {@inheritDoc} */
288
    @Override
289
    protected void search() {
290
        Control control =getSearchField();
291
        String pattern = null;
292
        if (control != null){
293
            pattern = ((Text)control).getText();
294
            callService(pattern);
295

    
296
            fillContentProvider(null);
297
        }
298
    }
299
    @Override
300
    protected ILabelProvider createListLabelProvider() {
301
        return new FilteredReferenceLabelProvider();
302
    }
303

    
304
    public class FilteredReferenceLabelProvider extends FilteredCdmResourceLabelProvider {
305
        @Override
306
        public String getText(Object element) {
307
            if (element == null) {
308
                return null;
309
            }
310
            UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache) element;
311
            String titleCache = uuidAndTitleCache.getTitleCache();
312
            if(PreferencesUtil.getBooleanValue(PreferencePredicate.ShowIdInSelectionDialog.getKey())){
313
                titleCache += " ["+uuidAndTitleCache.getId()+"]";
314
            }
315
            if (isUseIdentifier()){
316
                if (identifierMap != null){
317
                    AlternativeIdentifier identifier = identifierMap.get(uuidAndTitleCache.getUuid());
318
                    if (identifier != null){
319
                        titleCache += " (" + identifier.getTypeLabel() +": " + identifier.getIdentifier() + ")";
320
                    }else{
321
                        titleCache += " (-)";
322
                    }
323
                }
324

    
325
            }
326

    
327
            return titleCache;
328
        }
329
    };
330

    
331
}
(33-33/46)