Project

General

Profile

Download (11.4 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.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;
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

    
57
	/**
58
	 * <p>select</p>
59
	 *
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.
64
	 */
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);
70
	}
71

    
72
	/**
73
     * <p>select</p>
74
     *
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.
79
     */
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);
85
    }
86

    
87

    
88

    
89
	/**
90
	 * <p>Constructor for FilteredReferenceSelectionDialog.</p>
91
	 *
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.
97
	 */
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;
103

    
104

    
105
	}
106

    
107

    
108

    
109

    
110
	/**
111
     * <p>Constructor for FilteredReferenceSelectionDialog.</p>
112
     *
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.
118
     */
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;
125

    
126
    }
127

    
128

    
129
	/* (non-Javadoc)
130
	 * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#getPersistentObject(java.util.UUID)
131
	 */
132
	/** {@inheritDoc} */
133
	@Override
134
	protected Reference getPersistentObject(UUID cdmUuid) {
135
	    if (lastSelectedReferences == null){
136
	        lastSelectedReferences = new ArrayList<>();
137
	    }
138
	    if (lastSelectedReferences.size()<6){
139
	        lastSelectedReferences.add(cdmUuid.toString());
140
	    }else{
141
	        lastSelectedReferences.remove(0);
142
	        lastSelectedReferences.add(cdmUuid.toString());
143
	    }
144

    
145
	    PreferencesUtil.setLastSelectedReference(lastSelectedReferences);
146
		return CdmStore.getService(IReferenceService.class).load(cdmUuid);
147
	}
148

    
149
	/* (non-Javadoc)
150
	 * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#search
151
	 */
152
	/** {@inheritDoc} */
153
	@Override
154
	protected void callService(String pattern) {
155

    
156
	    if (StringUtils.isBlank(pattern) ){
157
	        lastSelectedReferences = PreferencesUtil.getLastSelectedReferences();
158
	        Set<UUID> uuids = new HashSet<>();
159
	        for (String uuidString: lastSelectedReferences){
160
	            uuids.add(UUID.fromString(uuidString));
161
	        }
162
	        if (!uuids.isEmpty()){
163
	            if (currentReference != null){
164
	                uuids.remove(currentReference.getUuid());
165
	                if (this.isInReference){
166
	                    model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCacheForUUIDS(uuids, currentReference.getType());
167
	                }else{
168
	                    model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCacheForUUIDS(uuids);
169
	                }
170
	            }else{
171
	                model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCacheForUUIDS(uuids);
172
	            }
173

    
174
	        }
175

    
176
	    } else if (isInReference && currentReference != null){
177

    
178
            if (isUseIdentifier()){
179
                List<IdentifiedEntityDTO<Reference>> list = CdmStore.getService(IReferenceService.class).listByIdentifierAbbrev(pattern, null, MatchMode.BEGINNING, limitOfInitialElements);
180
                if (model!= null){
181
                    model.clear();
182
                }
183
                for (IdentifiedEntityDTO dto: list){
184

    
185
                    model.add(dto.getCdmEntity());
186
                }
187
            }else{
188

    
189
                model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCache(limitOfInitialElements,pattern, currentReference.getType());
190
            }
191

    
192
        }else{
193
            if (isUseIdentifier() && PreferencesUtil.getBooleanValue(PreferencePredicate.SearchForIdentifierAndTitleCache.getKey())){
194
                List<IdentifiedEntityDTO<Reference>> list = CdmStore.getService(IReferenceService.class).listByIdentifierAndTitleCacheAbbrev(pattern, null, MatchMode.BEGINNING, limitOfInitialElements);
195
                if (model!= null){
196
                    model.clear();
197
                }
198
                identifierMap = new HashMap();
199
                for (IdentifiedEntityDTO dto: list){
200
                    if (dto.getIdentifier() != null){
201
                        identifierMap.put(dto.getCdmEntity().getUuid(), dto.getIdentifier());
202
                    }
203
                    model.add(dto.getCdmEntity());
204

    
205
                }
206
            }else if (isUseIdentifier() ){
207
                List<IdentifiedEntityDTO<Reference>> list = CdmStore.getService(IReferenceService.class).listByIdentifierAbbrev(pattern, null, MatchMode.BEGINNING, limitOfInitialElements);
208
                if (model!= null){
209
                    model.clear();
210
                }
211
                identifierMap = new HashMap();
212
                for (IdentifiedEntityDTO dto: list){
213
                    identifierMap.put(dto.getCdmEntity().getUuid(), dto.getIdentifier());
214
                    model.add(dto.getCdmEntity());
215

    
216
                }
217
            }else {
218
                model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCache(limitOfInitialElements,pattern);
219
            }
220

    
221
        }
222

    
223
	}
224

    
225
	@Override
226
    protected void addIdentifierCheckButton(Composite searchAndFilter) {
227
        Button btnCheckButton = new Button(searchAndFilter, SWT.CHECK);
228
        btnCheckButton.setText("Use Identifier");
229
        if (PreferencesUtil.getBooleanValue(PreferencePredicate.SearchForIdentifierAsDefault.getKey())){
230
            btnCheckButton.setSelection(true);
231
            useIdentifier = true;
232
        }
233
        btnCheckButton.addSelectionListener(new SelectionAdapter() {
234
            @Override
235
            public void widgetSelected(SelectionEvent e) {
236
                useIdentifier = btnCheckButton.getSelection();
237
                search();
238
            }
239
        });
240
    }
241
	/** {@inheritDoc} */
242
	@Override
243
	protected String getTitle(Reference cdmObject) {
244
		if(cdmObject == null){
245
			return "";
246
		}else{
247
		    return super.getTitle(cdmObject);
248
		}
249
//		}else{
250
//			return DefaultReferenceCacheStrategy.putAuthorToEndOfString(cdmObject.getTitleCache(), cdmObject.getAuthorship().getTitleCache());
251
//		}
252
	}
253

    
254
	/** {@inheritDoc} */
255
	@Override
256
	protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
257
		return new NewReferenceWizard();
258
	}
259

    
260
	/** {@inheritDoc} */
261
	@Override
262
	protected String[] getNewWizardText() {
263
		return new String[]{"New Reference"};
264
	}
265

    
266
	/** {@inheritDoc} */
267
    @Override
268
    protected void search() {
269
        Control control =getSearchField();
270
        String pattern = null;
271
        if (control != null){
272
            pattern = ((Text)control).getText();
273
            callService(pattern);
274

    
275
            fillContentProvider(null);
276
        }
277
    }
278
    @Override
279
    protected ILabelProvider createListLabelProvider() {
280
        return new FilteredReferenceLabelProvider();
281
    }
282

    
283
    public class FilteredReferenceLabelProvider extends FilteredCdmResourceLabelProvider {
284
        @Override
285
        public String getText(Object element) {
286
            if (element == null) {
287
                return null;
288
            }
289
            UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache) element;
290
            String titleCache = uuidAndTitleCache.getTitleCache();
291
            if(PreferencesUtil.getBooleanValue(PreferencePredicate.ShowIdInSelectionDialog.getKey())){
292
                titleCache += " ["+uuidAndTitleCache.getId()+"]";
293
            }
294
            if (isUseIdentifier()){
295
                if (identifierMap != null){
296
                    AlternativeIdentifier identifier = identifierMap.get(uuidAndTitleCache.getUuid());
297
                    if (identifier != null){
298
                        titleCache += " (" + identifier.getTypeLabel() +": " + identifier.getIdentifier() + ")";
299
                    }else{
300
                        titleCache += " (-)";
301
                    }
302
                }
303

    
304
            }
305

    
306
            return titleCache;
307
        }
308
    };
309

    
310
}
(32-32/45)