Project

General

Profile

Download (8.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.HashSet;
14
import java.util.List;
15
import java.util.Set;
16
import java.util.UUID;
17

    
18
import org.apache.commons.lang.StringUtils;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.events.SelectionAdapter;
21
import org.eclipse.swt.events.SelectionEvent;
22
import org.eclipse.swt.widgets.Button;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.swt.widgets.Control;
25
import org.eclipse.swt.widgets.Shell;
26
import org.eclipse.swt.widgets.Text;
27

    
28
import eu.etaxonomy.cdm.api.service.IReferenceService;
29
import eu.etaxonomy.cdm.api.service.dto.IdentifiedEntityDTO;
30
import eu.etaxonomy.cdm.model.reference.Reference;
31
import eu.etaxonomy.cdm.persistence.query.MatchMode;
32
import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
33
import eu.etaxonomy.taxeditor.newWizard.NewReferenceWizard;
34
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
35
import eu.etaxonomy.taxeditor.store.CdmStore;
36

    
37
/**
38
 * <p>FilteredReferenceSelectionDialog class.</p>
39
 *
40
 * @author n.hoffmann
41
 * @created 04.06.2009
42
 * @version 1.0
43
 */
44
public class ReferenceSelectionDialog extends AbstractFilteredCdmResourceSelectionDialog<Reference> {
45

    
46
    protected static boolean isInReference = false;
47
    private Reference currentReference;
48
    List<String> lastSelectedReferences = null;
49

    
50
	/**
51
	 * <p>select</p>
52
	 *
53
	 * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
54
	 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
55
	 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
56
	 * @return a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
57
	 */
58
	public static Reference select(Shell shell, //ConversationHolder conversation,
59
	        Reference reference, boolean isInReference) {
60
		ReferenceSelectionDialog dialog = new ReferenceSelectionDialog(shell, //conversation,
61
				"Choose a reference", false, reference, isInReference);
62
		return getSelectionFromDialog(dialog);
63
	}
64

    
65
	/**
66
     * <p>select</p>
67
     *
68
     * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
69
     * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
70
     * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
71
     * @return a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
72
     */
73
    public static Reference select(Shell shell, //ConversationHolder conversation,
74
            Reference reference) {
75
        ReferenceSelectionDialog dialog = new ReferenceSelectionDialog(shell, //conversation,
76
                "Choose a reference", false, reference);
77
        return getSelectionFromDialog(dialog);
78
    }
79

    
80

    
81

    
82
	/**
83
	 * <p>Constructor for FilteredReferenceSelectionDialog.</p>
84
	 *
85
	 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
86
	 * @param title a {@link java.lang.String} object.
87
	 * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
88
	 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
89
	 * @param multi a boolean.
90
	 */
91
	protected ReferenceSelectionDialog(Shell shell, //ConversationHolder conversation,
92
	        String title, boolean multi, Reference reference) {
93
		super(shell, //conversation,
94
		        title, multi, ReferenceSelectionDialog.class.getCanonicalName(), null);
95
		this.currentReference = reference;
96

    
97

    
98
	}
99

    
100

    
101

    
102

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

    
119
    }
120

    
121

    
122
	/* (non-Javadoc)
123
	 * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#getPersistentObject(java.util.UUID)
124
	 */
125
	/** {@inheritDoc} */
126
	@Override
127
	protected Reference getPersistentObject(UUID cdmUuid) {
128
	    if (lastSelectedReferences == null){
129
	        lastSelectedReferences = new ArrayList<>();
130
	    }
131
	    if (lastSelectedReferences.size()<6){
132
	        lastSelectedReferences.add(cdmUuid.toString());
133
	    }else{
134
	        lastSelectedReferences.remove(0);
135
	        lastSelectedReferences.add(cdmUuid.toString());
136
	    }
137

    
138
	    PreferencesUtil.setLastSelectedReference(lastSelectedReferences);
139
		return CdmStore.getService(IReferenceService.class).load(cdmUuid);
140
	}
141

    
142
	/* (non-Javadoc)
143
	 * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#search
144
	 */
145
	/** {@inheritDoc} */
146
	@Override
147
	protected void callService(String pattern) {
148

    
149
	    if (StringUtils.isBlank(pattern) && lastSelectedReferences == null){
150
	        lastSelectedReferences = PreferencesUtil.getLastSelectedReferences();
151
	        Set<UUID> uuids = new HashSet<>();
152
	        for (String uuidString: lastSelectedReferences){
153
	            uuids.add(UUID.fromString(uuidString));
154
	        }
155
	        model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCacheForUUIDS(uuids);
156

    
157
	    } else if (isInReference && currentReference != null){
158

    
159
            if (isUseIdentifier()){
160
                List<IdentifiedEntityDTO<Reference>> list = CdmStore.getService(IReferenceService.class).listByIdentifierAbbrev(pattern, null, MatchMode.BEGINNING, limitOfInitialElements);
161
                if (model!= null){
162
                    model.clear();
163
                }
164
                for (IdentifiedEntityDTO dto: list){
165

    
166
                    model.add(dto.getCdmEntity());
167
                }
168
            }else{
169
                model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCache(limitOfInitialElements,pattern, currentReference.getType());
170
            }
171

    
172
        }else{
173
            if (isUseIdentifier()){
174
                List<IdentifiedEntityDTO<Reference>> list = CdmStore.getService(IReferenceService.class).listByIdentifierAbbrev(pattern, null, MatchMode.BEGINNING, limitOfInitialElements);
175
                if (model!= null){
176
                    model.clear();
177
                }
178
                for (IdentifiedEntityDTO dto: list){
179

    
180
                    model.add(dto.getCdmEntity());
181
                }
182
            }else{
183
                model = CdmStore.getService(IReferenceService.class).getUuidAndTitleCache(limitOfInitialElements,pattern);
184
            }
185

    
186
        }
187

    
188
	}
189

    
190
	@Override
191
    protected void addIdentifierCheckButton(Composite searchAndFilter) {
192
        Button btnCheckButton = new Button(searchAndFilter, SWT.CHECK);
193
        btnCheckButton.setText("Use Identifier");
194
        btnCheckButton.addSelectionListener(new SelectionAdapter() {
195
            @Override
196
            public void widgetSelected(SelectionEvent e) {
197
                useIdentifier = btnCheckButton.getSelection();
198
            }
199
        });
200
    }
201
	/** {@inheritDoc} */
202
	@Override
203
	protected String getTitle(Reference cdmObject) {
204
		if(cdmObject == null){
205
			return "";
206
		}else{
207
			return super.getTitle(cdmObject);
208
		}
209
//		}else{
210
//			return DefaultReferenceCacheStrategy.putAuthorToEndOfString(cdmObject.getTitleCache(), cdmObject.getAuthorship().getTitleCache());
211
//		}
212
	}
213

    
214
	/** {@inheritDoc} */
215
	@Override
216
	protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
217
		return new NewReferenceWizard();
218
	}
219

    
220
	/** {@inheritDoc} */
221
	@Override
222
	protected String[] getNewWizardText() {
223
		return new String[]{"New Reference"};
224
	}
225

    
226
	/** {@inheritDoc} */
227
    @Override
228
    protected void search() {
229
        Control control =getSearchField();
230
        String pattern = null;
231
        if (control != null){
232
            pattern = ((Text)control).getText();
233
            callService(pattern);
234

    
235
            fillContentProvider(null);
236
        }
237
    }
238

    
239
}
(30-30/41)