Project

General

Profile

Download (15.1 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.ArrayList;
14
import java.util.Collections;
15
import java.util.Comparator;
16
import java.util.HashSet;
17
import java.util.Iterator;
18
import java.util.List;
19
import java.util.Set;
20
import java.util.UUID;
21

    
22
import org.eclipse.core.runtime.IProgressMonitor;
23
import org.eclipse.core.runtime.IStatus;
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.Button;
36
import org.eclipse.swt.widgets.Composite;
37
import org.eclipse.swt.widgets.Control;
38
import org.eclipse.swt.widgets.Shell;
39
import org.eclipse.swt.widgets.Text;
40

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

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

    
62
//	private final ConversationHolder conversation = null;
63

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

    
69
	private T selectedObject;
70

    
71
	protected T cdmBaseToBeFiltered;
72

    
73

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

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

    
101
	}
102

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

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

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

    
128
	}
129

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

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

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

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

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

    
175

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

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

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

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

    
206

    
207

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

    
218

    
219

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

    
225
	@Override
226
    protected void fillContentProvider(IProgressMonitor progressMonitor)
227
		 {
228
		try {
229
		    if (model == null){
230
		        model = new ArrayList<UuidAndTitleCache<T>>();
231
		    }
232
			if(model != null){
233
			    if (progressMonitor != null){
234
			        progressMonitor.beginTask("Looking for entities", model.size());
235
			    }
236
			    sort();
237

    
238
			    contentProvider.reset();
239
				Iterator<UuidAndTitleCache<T>> iterator = model.iterator();
240
				UuidAndTitleCache<T> element;
241
				while(iterator.hasNext()){
242
				    element = iterator.next();
243
				    if (!element.equals(cdmBaseToBeFiltered)){
244
				        contentProvider.add(element);
245
				    }
246
				    if (progressMonitor != null){
247
    					if (progressMonitor.isCanceled()) {
248
    						return;
249
    					}
250
    					progressMonitor.worked(1);
251
				    }
252
				}
253
				this.refresh();
254
			}else{
255

    
256
				MessagingUtils.warn(getClass(), "Model for Filtered Selection is null:" + this.getClass().getSimpleName());
257
			}
258
		}
259
		finally {
260
		    if (progressMonitor != null) {
261
                progressMonitor.done();
262
            }
263
		}
264
	}
265

    
266

    
267
    protected void sort() {
268
        Collections.sort(model, getItemsComparator());
269
    }
270

    
271
    /* (non-Javadoc)
272
	 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getDialogSettings()
273
	 */
274
	/** {@inheritDoc} */
275

    
276
	protected IDialogSettings getDialogSettings() {
277
		IDialogSettings settings = TaxeditorStorePlugin.getDefault().getDialogSettings().getSection(getSettings());
278

    
279
		if (settings == null) {
280
			settings = TaxeditorStorePlugin.getDefault().getDialogSettings().addNewSection(getSettings());
281
		}
282
		return settings;
283
	}
284

    
285
	/* (non-Javadoc)
286
	 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getElementName(java.lang.Object)
287
	 */
288
	/** {@inheritDoc} */
289

    
290
	public String getElementName(Object item) {
291
		return ((UuidAndTitleCache) item).getTitleCache();
292
	}
293

    
294
	/* (non-Javadoc)
295
	 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getItemsComparator()
296
	 */
297
	/** {@inheritDoc} */
298

    
299
	@Override
300
    protected Comparator getItemsComparator() {
301
		return new Comparator<UuidAndTitleCache>() {
302
			@Override
303
			public int compare(UuidAndTitleCache entity1,
304
					UuidAndTitleCache entity2) {
305
				Collator collator = Collator.getInstance();
306
				if (entity1 == entity2){
307
				    return 0;
308
				}
309

    
310
				if (entity1 == null && entity2 != null){
311
				    return -1;
312
				}
313
				if (entity2 == null && entity1 != null){
314
				    return 1;
315
				}
316
				if (entity1.getUuid().equals(entity2.getUuid())){
317
                    return 0;
318
                }
319
				if (entity1.getTitleCache() == null && entity2.getTitleCache() != null){
320
				    return -1;
321
				}
322
				if (entity2.getTitleCache() == null){
323
				    return 1;
324
				}
325
				int result = collator.compare(entity1.getTitleCache(), entity2.getTitleCache());
326
				if (result == 0){
327
				    result = entity1.getUuid().compareTo(entity2.getUuid());
328
				}
329
				return result;
330
			}
331
		};
332
	}
333

    
334

    
335
	/**
336
	 * <p>getSelectedUuidAndTitleCache</p>
337
	 *
338
	 * @return a {@link eu.etaxonomy.cdm.model.common.UuidAndTitleCache} object.
339
	 */
340
	protected UuidAndTitleCache getSelectedUuidAndTitleCache() {
341
		Object result = getResult();
342
		if (result instanceof UuidAndTitleCache){
343
		    return (UuidAndTitleCache) result;
344
		}
345
		return null;
346
	}
347

    
348
	/**
349
     * @return
350
     */
351
    private Object getResult() {
352
        StructuredSelection selection = getCurrentSelection();
353
        if (selection == null){
354
            return null;
355
        }
356
        return selection.getFirstElement();
357
    }
358

    
359
    /**
360
	 * <p>Getter for the field <code>settings</code>.</p>
361
	 *
362
	 * @return a {@link java.lang.String} object.
363
	 */
364
	public String getSettings()  {
365
		if(settings == null){
366
			throw new IllegalStateException("No SETTINGS set.");
367
		}
368
		return settings;
369
	}
370

    
371

    
372

    
373
	/**
374
	 * <p>getNewWizardLinkText</p>
375
	 *
376
	 * @return a {@link java.lang.String} object.
377
	 */
378
	protected abstract String[] getNewWizardText();
379

    
380
	/**
381
	 * <p>getNewEntityWizard</p>
382
	 * @param parameter
383
	 * @return a {@link eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard} object.
384
	 */
385
	protected abstract AbstractNewEntityWizard getNewEntityWizard(String parameter);
386

    
387
	public class FilteredCdmResourceLabelProvider extends LabelProvider {
388
		@Override
389
		public String getText(Object element) {
390
			if (element == null) {
391
				return null;
392
			}
393
			UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache) element;
394
			String titleCache = uuidAndTitleCache.getTitleCache();
395
			if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG)){
396
			    titleCache += " ["+uuidAndTitleCache.getId()+"]";
397
			}
398
            return titleCache;
399
		}
400
	};
401

    
402
	/* (non-Javadoc)
403
	* @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createExtendedContentArea(org.eclipse.swt.widgets.Composite)
404
	*/
405
	/** {@inheritDoc} */
406

    
407
//	@Override
408
//    protected Control createExtendedContentArea(Composite parent) {
409
//		String newWizardLinkText = getNewWizardLinkText();
410
////        if(newWizardLinkText != null){
411
////            newButton1 = this.createButton(this.getShell(), new_id, newWizardLinkText, false);
412
////
413
////            newButton1.addSelectionListener(getNewWizardLinkSelectionListener());
414
////			return newButton1;
415
////		}
416
//		return null;
417
//	}
418

    
419
	@Override
420
    protected void createButtonsForButtonBar(Composite parent) {
421
	    String[] newButtonText = getNewWizardText();
422
	    if (newButtonText!= null){
423
	        this.newButton1 = createButton(parent, this.new_id, newButtonText[0], false);
424
	        newButton1.addSelectionListener(getNewWizardButtonSelectionListener());
425
	        if (newButtonText.length > 1){
426
	            newButton2 = createButton(parent, this.new_id2, newButtonText[1], false);
427
	            newButton2.addSelectionListener(getNewWizardButtonSelectionListener());
428
	        }
429
	    }
430
	    //Label label = new Label(parent, SWT.NONE);
431
        super.createButtonsForButtonBar(parent);
432
    }
433

    
434
	protected SelectionListener getNewWizardButtonSelectionListener(){
435
		return new SelectionAdapter() {
436

    
437
			/* (non-Javadoc)
438
			 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
439
			 */
440
			@Override
441
			public void widgetSelected(SelectionEvent e) {
442
			    Object source = e.getSource();
443
			    String text = null;
444
			    if (source instanceof Button){
445
			        Button sourceButton = (Button) source;
446
			        text = sourceButton.getText();
447
			    }
448
			    AbstractNewEntityWizard wizard = getNewEntityWizard(text);
449
			    if(wizard!=null){
450
			        wizard.init(null, null);
451
			        if(wizard.getEntity() != null) {
452
			            WizardDialog dialog = new WizardDialog(getShell(), wizard);
453
			            int status = dialog.open();
454

    
455
			            if (status == IStatus.OK) {
456

    
457
			                T entity = (T) wizard.getEntity();
458
			                refresh();
459
			                setPattern(entity);
460
//			                if (getConversationHolder() != null){
461
//			                    getConversationHolder().bind();
462
//			                }
463
			            }
464
			            //FIXME : Need to make sure this is a stable fix (ticket 3822)
465
//			            if (getConversationHolder() != null){
466
//			                getConversationHolder().commit();
467
//			            }
468
			        }
469
			    }
470
			}
471
		};
472
	}
473

    
474
	/**
475
	 * <p>getConversationHolder</p>
476
	 *
477
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
478
//	 */
479
//	@Override
480
//	public ConversationHolder getConversationHolder() {
481
//		return conversation;
482
//	}
483

    
484
	/** {@inheritDoc} */
485
//	@Override
486
//	public void update(CdmDataChangeMap changeEvents) {}
487

    
488
	/**
489
	 * Don't want to add for example a taxon or synonym to itself
490
	 * so filter the list to remove the taxon in question
491
	 * (<code>cdmBaseToBeFiltered</code>)
492
	 * so it is not available in the filtered list.
493
	 */
494
	private void filterExcludedObjects() {
495
		if (model != null && cdmBaseToBeFiltered != null) {
496

    
497
			UuidAndTitleCache uuidAndTitleCacheToRemove = null;
498

    
499
			for (UuidAndTitleCache uuidAndTitleCache : model){
500
				if ((cdmBaseToBeFiltered.getUuid()).equals(uuidAndTitleCache.getUuid())) {
501
					uuidAndTitleCacheToRemove = uuidAndTitleCache;
502
				}
503
			}
504
			model.remove(uuidAndTitleCacheToRemove);
505
		}
506
	}
507
	@Override
508
	void createFilterButton(Composite searchAndFilter){
509
	    //as default no filter button available
510
	}
511

    
512
	/** {@inheritDoc} */
513
    @Override
514
    protected void search() {
515
        Control control =getSearchField();
516
        String pattern = null;
517
        if (control != null){
518
            pattern = ((Text)control).getText();
519
            if (pattern.equals("*") || pattern.equals("?")){
520
                callService(null);
521
            }else if (pattern != null){
522
                callService(pattern);
523
            }
524
            fillContentProvider(null);
525
        }
526

    
527
//        if (pattern.equals("?")){
528
//            model = CdmStore.getService(INameService.class).getUuidAndTitleCache(null, null);
529
//        }else if (pattern != null){
530
//            model = CdmStore.getService(INameService.class).getUuidAndTitleCache(limitOfInitialElements, pattern);
531
//        }
532
    }
533

    
534
    abstract void callService(String pattern);
535

    
536
}
(2-2/39)