Project

General

Profile

Download (6.11 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 *
3
 */
4
package eu.etaxonomy.taxeditor.ui.dialog.selection;
5

    
6
import java.util.Collections;
7
import java.util.Comparator;
8
import java.util.List;
9
import java.util.UUID;
10

    
11
import org.eclipse.jface.viewers.ILabelProvider;
12
import org.eclipse.swt.SWT;
13
import org.eclipse.swt.events.SelectionEvent;
14
import org.eclipse.swt.events.SelectionListener;
15
import org.eclipse.swt.layout.GridData;
16
import org.eclipse.swt.layout.GridLayout;
17
import org.eclipse.swt.widgets.Combo;
18
import org.eclipse.swt.widgets.Composite;
19
import org.eclipse.swt.widgets.Control;
20
import org.eclipse.swt.widgets.Label;
21
import org.eclipse.swt.widgets.Shell;
22
import org.eclipse.swt.widgets.Text;
23

    
24
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
25
import eu.etaxonomy.cdm.api.service.IClassificationService;
26
import eu.etaxonomy.cdm.model.taxon.Classification;
27
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
28
import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
29
import eu.etaxonomy.taxeditor.store.CdmStore;
30

    
31
/**
32
 * @author p.ciardelli
33
 * @version $Id: $
34
 */
35
public class TaxonNodeSelectionDialog extends AbstractFilteredCdmResourceSelectionDialog<TaxonNode> implements SelectionListener{
36

    
37
	public static TaxonNode select(Shell shell, ConversationHolder conversation, String title, List<UUID> excludeTaxa, TaxonNode node, Classification classification) {
38
		TaxonNodeSelectionDialog dialog = new TaxonNodeSelectionDialog(shell,
39
				conversation,
40
				title,
41
				excludeTaxa,
42
				false,
43
				node,
44
				classification);
45
		return getSelectionFromDialog(dialog);
46
	}
47

    
48
	private Combo classificationSelectionCombo;
49

    
50
	private List<Classification> classifications;
51

    
52
	private Classification selectedClassification;
53
	private final List<UUID> excludeTaxa;
54

    
55
	protected TaxonNodeSelectionDialog(Shell shell, ConversationHolder conversation, String title, List<UUID> excludeTaxa, boolean multi, TaxonNode node, Classification classification) {
56
		super(shell, conversation, title, multi, TaxonNodeSelectionDialog.class.getCanonicalName(), node);
57

    
58
		ILabelProvider labelProvider = new FilteredCdmResourceLabelProvider();
59

    
60
		setListLabelProvider(labelProvider);
61
//		setDetailsLabelProvider(labelProvider);
62
		this.excludeTaxa = excludeTaxa;
63
		if(classification != null){
64
			selectedClassification = classification;
65
		}
66
		search();
67
	}
68

    
69
	/** {@inheritDoc} */
70
	@Override
71
	protected Control createExtendedContentArea(Composite parent) {
72
		return createClassificationSelectionCombo(parent);
73
	}
74

    
75
	/*
76
	 * currently disabled tree selection composite
77
	 */
78
	private Control createClassificationSelectionCombo(Composite parent){
79
//		classifications = CdmStore.getTaxonTreeService().list(null, null, null, null, null);
80

    
81
		Composite classificationSelection = new Composite(parent, SWT.NULL);
82
		classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
83

    
84
		GridLayout layout = new GridLayout();
85
		classificationSelection.setLayout(layout);
86

    
87
		Label label = new Label(classificationSelection, SWT.NULL);
88
		// TODO not working is not really true but leave it here to remind everyone that this is under construction
89
		label.setText("Select Classification");
90
		classificationSelectionCombo = new Combo(classificationSelection, SWT.BORDER | SWT.READ_ONLY);
91
		classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
92

    
93
		for(Classification tree : classifications){
94
			classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
95

    
96
		}
97

    
98
		classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
99

    
100
		// TODO remember last selection
101
		classificationSelectionCombo.addSelectionListener(this);
102

    
103

    
104

    
105
		return classificationSelection;
106
	}
107

    
108
	/** {@inheritDoc} */
109
	@Override
110
	protected String getTitle(TaxonNode taxonNode) {
111
		if(taxonNode != null && taxonNode.getTaxon() != null){
112
			return taxonNode.getTaxon().getTitleCache();
113
		}
114

    
115
		return "";
116
	}
117

    
118
	/** {@inheritDoc} */
119
	@Override
120
	protected TaxonNode getPersistentObject(UUID uuid) {
121
		return CdmStore.getService(IClassificationService.class).getTaxonNodeByUuid(uuid);
122
	}
123

    
124
	/** {@inheritDoc} */
125
	@Override
126
	protected void search() {
127
		// default to first tree
128
		// TODO this will be problematic and can only be seen as workaround
129

    
130
	    Control control =getSearchField();
131
        String pattern = null;
132
        if (control != null){
133
            pattern = ((Text)control).getText();
134
        }
135

    
136

    
137
		if(classifications == null){
138
			classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
139
			Collections.sort(classifications, new Comparator<Classification>() {
140

    
141
                @Override
142
                public int compare(Classification o1, Classification o2) {
143
                    if (o1.equals(o2)){
144
                        return 0;
145
                    }
146
                    int result = o1.getTitleCache().compareTo(o2.getTitleCache());
147
                    if (result == 0){
148
                        return o1.getUuid().compareTo(o2.getUuid());
149
                    }
150
                    return result;
151
                }
152
            });
153
			if (this.cdmBaseToBeFiltered == null){
154
			    selectedClassification = classifications.iterator().next();
155
			} else {
156
			    selectedClassification = this.cdmBaseToBeFiltered.getClassification();
157
			}
158
		}
159
		if (pattern == null || pattern.equals("?")){
160
		    model = CdmStore.getService(IClassificationService.class).getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(selectedClassification.getUuid(),  limitOfInitialElements, pattern);
161
		}else{
162
		    model = CdmStore.getService(IClassificationService.class).getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(selectedClassification.getUuid(),  null, null);
163
		}
164
	}
165

    
166
	/** {@inheritDoc} */
167
	@Override
168
	protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
169
		return null;
170
	}
171

    
172
	/** {@inheritDoc} */
173
	@Override
174
	protected String getNewWizardLinkText() {
175
		return null;
176
	}
177

    
178
	/** {@inheritDoc} */
179
	@Override
180
    public void widgetSelected(SelectionEvent e) {
181
		selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
182

    
183
//		refresh();
184
		setPattern(null);
185
	}
186

    
187
	/** {@inheritDoc} */
188
	@Override
189
    public void widgetDefaultSelected(SelectionEvent e) {}
190

    
191

    
192
}
(35-35/38)