Project

General

Profile

Download (10.2 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
package eu.etaxonomy.taxeditor.ui.dialog.selection;
10

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

    
19
import org.apache.commons.lang3.StringUtils;
20
import org.eclipse.jface.viewers.ILabelProvider;
21
import org.eclipse.swt.SWT;
22
import org.eclipse.swt.events.SelectionAdapter;
23
import org.eclipse.swt.events.SelectionEvent;
24
import org.eclipse.swt.widgets.Button;
25
import org.eclipse.swt.widgets.Composite;
26
import org.eclipse.swt.widgets.Control;
27
import org.eclipse.swt.widgets.Shell;
28
import org.eclipse.swt.widgets.Text;
29

    
30
import eu.etaxonomy.cdm.api.service.IReferenceService;
31
import eu.etaxonomy.cdm.api.service.dto.IdentifiedEntityDTO;
32
import eu.etaxonomy.cdm.api.service.dto.IdentifiedEntityDTO.AlternativeIdentifier;
33
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
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.PreferencesUtil;
40
import eu.etaxonomy.taxeditor.store.CdmStore;
41

    
42
/**
43
 * @author n.hoffmann
44
 * @created 04.06.2009
45
 */
46
public class ReferenceSelectionDialog extends AbstractFilteredCdmResourceSelectionDialog<Reference> {
47

    
48
    protected static boolean isInReference = false;
49
    private Reference currentReference;
50
    List<String> lastSelectedReferences = null;
51
    Map<UUID, AlternativeIdentifier> identifierMap;
52
    Set<Reference> preSelectedReferences = new HashSet<>();
53

    
54
	public static Reference select(Shell shell,
55
	        Reference reference, boolean isInReference) {
56
		ReferenceSelectionDialog dialog = new ReferenceSelectionDialog(shell, //conversation,
57
				"Choose a reference", false, reference, isInReference);
58
		return getSelectionFromDialog(dialog);
59
	}
60

    
61
    public static Reference select(Shell shell,
62
            Reference reference) {
63
        ReferenceSelectionDialog dialog = new ReferenceSelectionDialog(shell, //conversation,
64
                "Choose a reference", false, reference, null);
65
        return getSelectionFromDialog(dialog);
66
    }
67

    
68
    public static Reference select(Shell shell, String title,
69
            Reference reference, Set<Reference> preSelectedReferences) {
70
        ReferenceSelectionDialog dialog = new ReferenceSelectionDialog(shell, //conversation,
71
                title, false, reference, preSelectedReferences);
72
        return getSelectionFromDialog(dialog);
73
    }
74

    
75
    protected ReferenceSelectionDialog(Shell shell,
76
	        String title, boolean multi, Reference reference, Set<Reference> preSelectedReferences) {
77
		super(shell,
78
		        title, multi, ReferenceSelectionDialog.class.getCanonicalName(), reference);
79
		this.currentReference = reference;
80
		this.preSelectedReferences = preSelectedReferences;
81
	}
82
    protected ReferenceSelectionDialog(Shell shell,
83
            String title, boolean multi, Reference reference, boolean isInReference) {
84
        super(shell, //conversation,
85
                title, multi, ReferenceSelectionDialog.class.getCanonicalName());
86
        ReferenceSelectionDialog.isInReference = isInReference;
87
        this.currentReference = reference;
88
    }
89

    
90
	@Override
91
	protected Reference getPersistentObject(UUID cdmUuid) {
92
	    if (lastSelectedReferences == null){
93
	        lastSelectedReferences = new ArrayList<>();
94
	    }
95
	    if (lastSelectedReferences.size()<6 && !lastSelectedReferences.contains(cdmUuid.toString())){
96
	        lastSelectedReferences.add(cdmUuid.toString());
97
	    }else if (lastSelectedReferences.size()==6 && !lastSelectedReferences.contains(cdmUuid.toString())){
98
	        lastSelectedReferences.remove(0);
99
	        lastSelectedReferences.add(cdmUuid.toString());
100
	    }
101

    
102
	    PreferencesUtil.setLastSelectedReference(lastSelectedReferences);
103
		return CdmStore.getService(IReferenceService.class).load(cdmUuid);
104
	}
105

    
106
	@Override
107
	protected void callService(String pattern) {
108

    
109
	    if (StringUtils.isBlank(pattern) ){
110
	        lastSelectedReferences = PreferencesUtil.getLastSelectedReferences();
111
	        Set<UUID> uuids = new HashSet<>();
112
	        for (String uuidString: lastSelectedReferences){
113
	            uuids.add(UUID.fromString(uuidString));
114
	        }
115
	        if (preSelectedReferences != null){
116
    	        for (Reference ref: preSelectedReferences){
117
    	            if (ref != null){
118
    	                uuids.add(ref.getUuid());
119
    	            }
120
                }
121
	        }
122
	        if (!uuids.isEmpty()){
123
	            if (currentReference != null){
124
	                uuids.remove(currentReference.getUuid());
125
	                if (this.isInReference){
126
	                    model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCacheForUUIDS(uuids, currentReference.getType());
127
	                }else{
128
	                    model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCacheForUUIDS(uuids);
129
	                }
130
	            }else{
131
	                model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCacheForUUIDS(uuids);
132
	            }
133
	        }
134

    
135
	    } else if (isInReference && currentReference != null){
136

    
137
            if (isUseIdentifier()){
138
                List<IdentifiedEntityDTO<Reference>> list = CdmStore.getService(IReferenceService.class).listByIdentifierAbbrev(pattern, null, MatchMode.BEGINNING, limitOfInitialElements);
139
                if (model!= null){
140
                    model.clear();
141
                }
142
                for (IdentifiedEntityDTO dto: list){
143
                    model.add(dto.getCdmEntity());
144
                }
145
            }else{
146
                model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCache(limitOfInitialElements,pattern, currentReference.getType());
147
            }
148

    
149
        }else{
150
            if (isUseIdentifier() && PreferencesUtil.getBooleanValue(PreferencePredicate.SearchForIdentifierAndTitleCache.getKey())){
151
                List<IdentifiedEntityDTO<Reference>> list = CdmStore.getService(IReferenceService.class).listByIdentifierAndTitleCacheAbbrev(pattern, null, MatchMode.BEGINNING, limitOfInitialElements);
152
                if (model!= null){
153
                    model.clear();
154
                }
155
                identifierMap = new HashMap<>();
156
                for (IdentifiedEntityDTO dto: list){
157
                    if (dto.getIdentifier() != null){
158
                        identifierMap.put(dto.getCdmEntity().getUuid(), dto.getIdentifier());
159
                    }
160
                    model.add(dto.getCdmEntity());
161
                }
162
            }else if (isUseIdentifier() ){
163
                List<IdentifiedEntityDTO<Reference>> list = CdmStore.getService(IReferenceService.class).listByIdentifierAbbrev(pattern, null, MatchMode.BEGINNING, limitOfInitialElements);
164
                if (model!= null){
165
                    model.clear();
166
                }
167
                identifierMap = new HashMap<>();
168
                for (IdentifiedEntityDTO dto: list){
169
                    identifierMap.put(dto.getCdmEntity().getUuid(), dto.getIdentifier());
170
                    model.add(dto.getCdmEntity());
171
                }
172
            }else {
173
                model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCache(limitOfInitialElements,pattern);
174
            }
175
        }
176
	}
177

    
178
	@Override
179
    protected void addIdentifierCheckButton(Composite searchAndFilter) {
180
        Button btnCheckButton = new Button(searchAndFilter, SWT.CHECK);
181
        btnCheckButton.setText("Use Identifier");
182
        if (PreferencesUtil.getBooleanValue(PreferencePredicate.SearchForIdentifierAsDefault.getKey())){
183
            btnCheckButton.setSelection(true);
184
            useIdentifier = true;
185
        }
186
        btnCheckButton.addSelectionListener(new SelectionAdapter() {
187
            @Override
188
            public void widgetSelected(SelectionEvent e) {
189
                useIdentifier = btnCheckButton.getSelection();
190
                search();
191
            }
192
        });
193
    }
194

    
195
	@Override
196
	protected String getTitle(Reference cdmObject) {
197
		if(cdmObject == null){
198
			return "";
199
		}else{
200
		    return super.getTitle(cdmObject);
201
		}
202
	}
203

    
204
	@Override
205
	protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
206
	    if (isInReference){
207
	        if (currentReference != null) {
208
                return new NewReferenceWizard(this.currentReference.getType());
209
            }
210
	    }
211
		return new NewReferenceWizard(null);
212
	}
213

    
214
	@Override
215
	protected String[] getNewWizardText() {
216
		return new String[]{"New Reference"};
217
	}
218

    
219
    @Override
220
    protected void search() {
221
        Control control =getSearchField();
222
        String pattern = null;
223
        if (control != null){
224
            pattern = ((Text)control).getText();
225
            callService(pattern);
226

    
227
            fillContentProvider(null);
228
        }
229
    }
230
    @Override
231
    protected ILabelProvider createListLabelProvider() {
232
        return new FilteredReferenceLabelProvider();
233
    }
234

    
235
    public class FilteredReferenceLabelProvider extends FilteredCdmResourceLabelProvider {
236
        @Override
237
        public String getText(Object element) {
238
            if (element == null) {
239
                return null;
240
            }
241
            UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache) element;
242
            String titleCache = uuidAndTitleCache.getTitleCache();
243
            if(PreferencesUtil.getBooleanValue(PreferencePredicate.ShowIdInSelectionDialog.getKey())){
244
                titleCache += " ["+uuidAndTitleCache.getId()+"]";
245
            }
246
            if (isUseIdentifier()){
247
                if (identifierMap != null){
248
                    AlternativeIdentifier identifier = identifierMap.get(uuidAndTitleCache.getUuid());
249
                    if (identifier != null){
250
                        titleCache += " (" + identifier.getTypeLabel() +": " + identifier.getIdentifier() + ")";
251
                    }else{
252
                        titleCache += " (-)";
253
                    }
254
                }
255
            }
256

    
257
            return titleCache;
258
        }
259
    }
260
}
(33-33/46)