Project

General

Profile

Download (12.8 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.text.Collator;
13
import java.util.Comparator;
14
import java.util.HashSet;
15
import java.util.Iterator;
16
import java.util.List;
17
import java.util.Set;
18
import java.util.UUID;
19

    
20
import org.eclipse.core.runtime.CoreException;
21
import org.eclipse.core.runtime.IProgressMonitor;
22
import org.eclipse.core.runtime.IStatus;
23
import org.eclipse.core.runtime.OperationCanceledException;
24
import org.eclipse.jface.dialogs.IDialogSettings;
25
import org.eclipse.jface.viewers.ILabelProvider;
26
import org.eclipse.jface.viewers.LabelProvider;
27
import org.eclipse.jface.viewers.StructuredSelection;
28
import org.eclipse.jface.window.Window;
29
import org.eclipse.jface.wizard.WizardDialog;
30
import org.eclipse.swt.SWT;
31
import org.eclipse.swt.events.SelectionAdapter;
32
import org.eclipse.swt.events.SelectionEvent;
33
import org.eclipse.swt.events.SelectionListener;
34
import org.eclipse.swt.graphics.Cursor;
35
import org.eclipse.swt.widgets.Composite;
36
import org.eclipse.swt.widgets.Control;
37
import org.eclipse.swt.widgets.Link;
38
import org.eclipse.swt.widgets.Shell;
39

    
40
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
41
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
42
import eu.etaxonomy.cdm.model.common.CdmBase;
43
import eu.etaxonomy.cdm.model.common.ICdmBase;
44
import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
45
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
46
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
47
import eu.etaxonomy.taxeditor.l10n.Messages;
48
import eu.etaxonomy.taxeditor.model.MessagingUtils;
49
import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
50
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
51
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
52
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
53

    
54
/**
55
 * <p>Abstract AbstractFilteredCdmResourceSelectionDialog class.</p>
56
 *
57
 * @author n.hoffmann
58
 * @created 04.06.2009
59
 * @version 1.0
60
 */
61
public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmBase> extends
62
		SearchDialog implements IConversationEnabled {
63

    
64
	private final ConversationHolder conversation;
65

    
66
	protected List<UuidAndTitleCache<T>> model;
67
	private final Set<T> transientCdmObjects = new HashSet<T>();
68
	private final String settings;
69
	protected final int limitOfInitialElements = 100;
70

    
71
	private T selectedObject;
72

    
73
	protected T cdmBaseToBeFiltered;
74

    
75

    
76
	/**
77
	 * <p>Constructor for AbstractFilteredCdmResourceSelectionDialog.</p>
78
	 *
79
	 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
80
	 * @param conversation
81
	 * @param title a {@link java.lang.String} object.
82
	 * @param multi a boolean.
83
	 * @param settings a {@link java.lang.String} object.
84
	 * @param cdmObject a T object.
85
	 * @param <T> a T object.
86
	 */
87
	protected AbstractFilteredCdmResourceSelectionDialog(Shell shell, ConversationHolder conversation, String title, boolean multi, String settings, T cdmObject) {
88
		super(shell, multi);
89
		setShellStyle(SWT.DIALOG_TRIM);
90
		setTitle(title);
91
		setMessage(Messages.SearchDialog_patternLabel);
92
		this.settings = settings;
93

    
94
		this.conversation = conversation;
95
		this.cdmBaseToBeFiltered = cdmObject;
96
		Cursor cursor = shell.getCursor();
97
		shell.setCursor(shell.getDisplay().getSystemCursor(SWT.CURSOR_WAIT));
98
		init();
99
		shell.setCursor(cursor);
100
		setListLabelProvider(createListLabelProvider());
101

    
102
	}
103

    
104
	/**
105
	 * By default, we are returning the standard list label provider
106
	 *
107
	 * Override in subclasses if you want different behavior
108
	 *
109
	 * @return
110
	 */
111
	protected ILabelProvider createDetailsLabelProvider() {
112
		return createListLabelProvider();
113
	}
114

    
115
	/**
116
	 *
117
	 * @return
118
	 */
119
	protected ILabelProvider createListLabelProvider() {
120
		return new FilteredCdmResourceLabelProvider();
121
	}
122

    
123
	/**
124
	 * Override in subclasses.
125
	 * Will run before initModel()
126
	 */
127
	protected void init() {
128

    
129
	}
130

    
131
	/**
132
	 * <p>getSelectionFromDialog</p>
133
	 *
134
	 * @param dialog a {@link eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog} object.
135
	 * @param <TYPE> a TYPE object.
136
	 * @return a TYPE object.
137
	 */
138
	protected static <TYPE extends CdmBase> TYPE getSelectionFromDialog(AbstractFilteredCdmResourceSelectionDialog<TYPE> dialog) {
139
		int result = dialog.open();
140

    
141
		if (result == Window.CANCEL) {
142
			return null;
143
		}
144

    
145
		UUID uuid = dialog.getSelectedUuidAndTitleCache().getUuid();
146
		if(uuid == null){
147
			return null;
148
		}
149
		return dialog.getCdmObjectByUuid(uuid);
150
	}
151

    
152
	/**
153
	 * Check if object was created during the life of this dialog. If not,
154
	 * retrieve it from the CdmStore.
155
	 *
156
	 * @param cdmUuid a {@link java.util.UUID} object.
157
	 * @return a T object.
158
	 */
159
	protected T getCdmObjectByUuid(UUID cdmUuid) {
160
		for (T cdmObject : transientCdmObjects) {
161
			if (cdmObject.getUuid().equals(cdmUuid)) {
162
				return cdmObject;
163
			}
164
		}
165
		return getPersistentObject(cdmUuid);
166
	}
167

    
168
	/**
169
	 * <p>getPersistentObject</p>
170
	 *
171
	 * @param uuid a {@link java.util.UUID} object.
172
	 * @return a T object.
173
	 */
174
	abstract protected T getPersistentObject(UUID uuid);
175

    
176

    
177
	/**
178
	 * <p>isObjectTransient</p>
179
	 *
180
	 * @param cdmObject a T object.
181
	 * @return a boolean.
182
	 */
183
	protected boolean isObjectTransient(T cdmObject) {
184
		return (getPersistentObject(cdmObject.getUuid()) == null);
185
	}
186

    
187
	/**
188
	 * <p>getTitle</p>
189
	 *
190
	 * @param cdmObject a T object.
191
	 * @return a {@link java.lang.String} object.
192
	 */
193
	protected String getTitle(T cdmObject) {
194
		if(cdmObject == null){
195
			return "";
196
		}
197

    
198
		if (cdmObject instanceof IIdentifiableEntity) {
199
			return ((IIdentifiableEntity) cdmObject).getTitleCache();
200
		}
201

    
202
		throw new IllegalArgumentException("Generic method only" +
203
				" supports cdmObject of type IIdentifiableEntity." +
204
				" Please implement specific method in subclass.");
205
	}
206

    
207

    
208

    
209
	/**
210
	 * Set the filter input to the Agent's title cache
211
	 *
212
	 * @param cdmObject a T object.
213
	 */
214
	protected void setPattern(T cdmObject) {
215
		String pattern = getTitle(cdmObject);
216
		getSearchField().setText(pattern);
217
	}
218

    
219

    
220

    
221
	/* (non-Javadoc)
222
	* @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#fillContentProvider(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.AbstractContentProvider, org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter, org.eclipse.core.runtime.IProgressMonitor)
223
	*/
224
	/** {@inheritDoc} */
225

    
226
	@Override
227
    protected void fillContentProvider(IProgressMonitor progressMonitor)
228
		throws CoreException {
229
		try {
230
			if(model != null){
231
			    if (progressMonitor != null){
232
			        progressMonitor.beginTask("Looking for entities", model.size());
233
			    }
234
			    contentProvider.reset();
235
				Iterator<UuidAndTitleCache<T>> iterator = model.iterator();
236
				UuidAndTitleCache<T> element;
237
				while(iterator.hasNext()){
238
				    element = iterator.next();
239
				    if (!element.equals(cdmBaseToBeFiltered)){
240
				        contentProvider.add(element);
241
				    }
242
				    if (progressMonitor != null){
243
    					if (progressMonitor.isCanceled()) {
244
    						throw new OperationCanceledException();
245
    					}
246
    					progressMonitor.worked(1);
247
				    }
248
				}
249
				this.refresh();
250
			}else{
251
				MessagingUtils.warn(getClass(), "Model for Filtered Selection is null:" + this.getClass().getSimpleName());
252
			}
253
		}
254
		finally {
255
		    if (progressMonitor != null) {
256
                progressMonitor.done();
257
            }
258
		}
259
	}
260

    
261
	/* (non-Javadoc)
262
	 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getDialogSettings()
263
	 */
264
	/** {@inheritDoc} */
265

    
266
	protected IDialogSettings getDialogSettings() {
267
		IDialogSettings settings = TaxeditorStorePlugin.getDefault().getDialogSettings().getSection(getSettings());
268

    
269
		if (settings == null) {
270
			settings = TaxeditorStorePlugin.getDefault().getDialogSettings().addNewSection(getSettings());
271
		}
272
		return settings;
273
	}
274

    
275
	/* (non-Javadoc)
276
	 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getElementName(java.lang.Object)
277
	 */
278
	/** {@inheritDoc} */
279

    
280
	public String getElementName(Object item) {
281
		return ((UuidAndTitleCache) item).getTitleCache();
282
	}
283

    
284
	/* (non-Javadoc)
285
	 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getItemsComparator()
286
	 */
287
	/** {@inheritDoc} */
288

    
289
	protected Comparator getItemsComparator() {
290
		return new Comparator<UuidAndTitleCache>() {
291
			@Override
292
			public int compare(UuidAndTitleCache entity1,
293
					UuidAndTitleCache entity2) {
294
				Collator collator = Collator.getInstance();
295
				if (entity1.getUuid().equals(entity2.getUuid())){
296
				    return 0;
297
				}
298
				int result = collator.compare(entity1.getTitleCache(), entity2.getTitleCache());
299
				if (result == 0){
300
				    result = entity1.getUuid().compareTo(entity2.getUuid());
301
				}
302
				return result;
303
			}
304
		};
305
	}
306

    
307

    
308
	/**
309
	 * <p>getSelectedUuidAndTitleCache</p>
310
	 *
311
	 * @return a {@link eu.etaxonomy.cdm.model.common.UuidAndTitleCache} object.
312
	 */
313
	protected UuidAndTitleCache getSelectedUuidAndTitleCache() {
314
		Object result = getResult();
315
		if (result instanceof UuidAndTitleCache){
316
		    return (UuidAndTitleCache) result;
317
		}
318
		return null;
319
	}
320

    
321
	/**
322
     * @return
323
     */
324
    private Object getResult() {
325
        StructuredSelection selection = getCurrentSelection();
326
        return selection.getFirstElement();
327
    }
328

    
329
    /**
330
	 * <p>Getter for the field <code>settings</code>.</p>
331
	 *
332
	 * @return a {@link java.lang.String} object.
333
	 */
334
	public String getSettings()  {
335
		if(settings == null){
336
			throw new IllegalStateException("No SETTINGS set.");
337
		}
338
		return settings;
339
	}
340

    
341

    
342

    
343
	/**
344
	 * <p>getNewWizardLinkText</p>
345
	 *
346
	 * @return a {@link java.lang.String} object.
347
	 */
348
	protected abstract String getNewWizardLinkText();
349

    
350
	/**
351
	 * <p>getNewEntityWizard</p>
352
	 * @param parameter
353
	 * @return a {@link eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard} object.
354
	 */
355
	protected abstract AbstractNewEntityWizard getNewEntityWizard(String parameter);
356

    
357
	public class FilteredCdmResourceLabelProvider extends LabelProvider {
358
		@Override
359
		public String getText(Object element) {
360
			if (element == null) {
361
				return null;
362
			}
363
			UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache) element;
364
			String titleCache = uuidAndTitleCache.getTitleCache();
365
			if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG)){
366
			    titleCache += " ["+uuidAndTitleCache.getId()+"]";
367
			}
368
            return titleCache;
369
		}
370
	};
371

    
372
	/* (non-Javadoc)
373
	* @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createExtendedContentArea(org.eclipse.swt.widgets.Composite)
374
	*/
375
	/** {@inheritDoc} */
376

    
377
	@Override
378
    protected Control createExtendedContentArea(Composite parent) {
379
		String newWizardLinkText = getNewWizardLinkText();
380
        if(newWizardLinkText != null){
381
			Link link = new Link(parent, SWT.NONE);
382
			link.setText(newWizardLinkText);
383
			link.addSelectionListener(getNewWizardLinkSelectionListener());
384
			return link;
385
		}
386
		return null;
387
	}
388

    
389
	protected SelectionListener getNewWizardLinkSelectionListener(){
390
		return new SelectionAdapter() {
391

    
392
			/* (non-Javadoc)
393
			 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
394
			 */
395
			@Override
396
			public void widgetSelected(SelectionEvent e) {
397

    
398
			    AbstractNewEntityWizard wizard = getNewEntityWizard(e.text);
399
			    if(wizard!=null){
400
			        wizard.init(null, null);
401
			        if(wizard.getEntity() != null) {
402
			            WizardDialog dialog = new WizardDialog(getShell(), wizard);
403
			            int status = dialog.open();
404

    
405
			            if (status == IStatus.OK) {
406

    
407
			                T entity = (T) wizard.getEntity();
408
			                refresh();
409
			                setPattern(entity);
410
			                if (getConversationHolder() != null){
411
			                    getConversationHolder().bind();
412
			                }
413
			            }
414
			            //FIXME : Need to make sure this is a stable fix (ticket 3822)
415
			            if (getConversationHolder() != null){
416
			                getConversationHolder().commit();
417
			            }
418
			        }
419
			    }
420
			}
421
		};
422
	}
423

    
424
	/**
425
	 * <p>getConversationHolder</p>
426
	 *
427
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
428
	 */
429
	@Override
430
	public ConversationHolder getConversationHolder() {
431
		return conversation;
432
	}
433

    
434
	/** {@inheritDoc} */
435
	@Override
436
	public void update(CdmDataChangeMap changeEvents) {}
437

    
438
	/**
439
	 * Don't want to add for example a taxon or synonym to itself
440
	 * so filter the list to remove the taxon in question
441
	 * (<code>cdmBaseToBeFiltered</code>)
442
	 * so it is not available in the filtered list.
443
	 */
444
	private void filterExcludedObjects() {
445
		if (model != null && cdmBaseToBeFiltered != null) {
446

    
447
			UuidAndTitleCache uuidAndTitleCacheToRemove = null;
448

    
449
			for (UuidAndTitleCache uuidAndTitleCache : model){
450
				if ((cdmBaseToBeFiltered.getUuid()).equals(uuidAndTitleCache.getUuid())) {
451
					uuidAndTitleCacheToRemove = uuidAndTitleCache;
452
				}
453
			}
454
			model.remove(uuidAndTitleCacheToRemove);
455
		}
456
	}
457
}
(2-2/38)