Project

General

Profile

Download (6.56 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.core.runtime.CoreException;
12
import org.eclipse.jface.viewers.ILabelProvider;
13
import org.eclipse.swt.SWT;
14
import org.eclipse.swt.events.SelectionEvent;
15
import org.eclipse.swt.events.SelectionListener;
16
import org.eclipse.swt.layout.GridData;
17
import org.eclipse.swt.layout.GridLayout;
18
import org.eclipse.swt.widgets.Combo;
19
import org.eclipse.swt.widgets.Composite;
20
import org.eclipse.swt.widgets.Control;
21
import org.eclipse.swt.widgets.Label;
22
import org.eclipse.swt.widgets.Shell;
23
import org.eclipse.swt.widgets.Text;
24

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

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

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

    
49
	private Combo classificationSelectionCombo;
50

    
51
	private List<Classification> classifications;
52

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

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

    
59
		ILabelProvider labelProvider = new FilteredCdmResourceLabelProvider();
60

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

    
68
		fillClassifications();
69
		createClassificationSelectionCombo(shell);
70
	}
71

    
72
	/** {@inheritDoc} */
73
	@Override
74
	protected Control createDialogArea(Composite parent) {
75
	    Composite container = (Composite) super.createDialogArea(parent);
76
		return createClassificationSelectionCombo(container);
77
	}
78

    
79
	/*
80
	 * currently disabled tree selection composite
81
	 */
82
	private Control createClassificationSelectionCombo(Composite parent){
83
//		classifications = CdmStore.getTaxonTreeService().list(null, null, null, null, null);
84

    
85
		Composite classificationSelection = new Composite(parent, SWT.NULL);
86
		classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
87

    
88
		GridLayout layout = new GridLayout();
89
		classificationSelection.setLayout(layout);
90

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

    
97
		for(Classification tree : classifications){
98
			classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
99

    
100
		}
101

    
102
		classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
103

    
104
		// TODO remember last selection
105
		classificationSelectionCombo.addSelectionListener(this);
106

    
107

    
108

    
109
		return classificationSelection;
110
	}
111

    
112
	/** {@inheritDoc} */
113
	@Override
114
	protected String getTitle(TaxonNode taxonNode) {
115
		if(taxonNode != null && taxonNode.getTaxon() != null){
116
			return taxonNode.getTaxon().getTitleCache();
117
		}
118

    
119
		return "";
120
	}
121

    
122
	/** {@inheritDoc} */
123
	@Override
124
	protected TaxonNode getPersistentObject(UUID uuid) {
125
		return CdmStore.getService(IClassificationService.class).getTaxonNodeByUuid(uuid);
126
	}
127

    
128
	/** {@inheritDoc} */
129
	@Override
130
	protected void search() {
131
		// default to first tree
132
		// TODO this will be problematic and can only be seen as workaround
133

    
134
	    Control control =getSearchField();
135
        String pattern = null;
136
        if (control != null){
137
            pattern = ((Text)control).getText();
138
        }
139

    
140

    
141
//		fillClassifications();
142
		if (pattern == null || pattern.equals("?")){
143
		    model = CdmStore.getService(IClassificationService.class).getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(selectedClassification.getUuid(), null, null);
144
		}else{
145
		    model = CdmStore.getService(IClassificationService.class).getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(selectedClassification.getUuid(), limitOfInitialElements, pattern);
146
		}
147
		try {
148
            fillContentProvider(null);
149
        } catch (CoreException e) {
150
            // TODO Auto-generated catch block
151
            e.printStackTrace();
152
        }
153
	}
154

    
155
    private void fillClassifications() {
156
        if(classifications == null){
157
			classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
158
			Collections.sort(classifications, new Comparator<Classification>() {
159

    
160
                @Override
161
                public int compare(Classification o1, Classification o2) {
162
                    if (o1.equals(o2)){
163
                        return 0;
164
                    }
165
                    int result = o1.getTitleCache().compareTo(o2.getTitleCache());
166
                    if (result == 0){
167
                        return o1.getUuid().compareTo(o2.getUuid());
168
                    }
169
                    return result;
170
                }
171
            });
172
			if (this.cdmBaseToBeFiltered == null){
173
			    selectedClassification = classifications.iterator().next();
174
			} else {
175
			    selectedClassification = this.cdmBaseToBeFiltered.getClassification();
176
			}
177
		}
178
    }
179

    
180
	/** {@inheritDoc} */
181
	@Override
182
	protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
183
		return null;
184
	}
185

    
186
	/** {@inheritDoc} */
187
	@Override
188
	protected String[] getNewWizardText() {
189
		return null;
190
	}
191

    
192
	/** {@inheritDoc} */
193
	@Override
194
    public void widgetSelected(SelectionEvent e) {
195
		selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
196

    
197
//		refresh();
198
		if (getSearchField().getText() != null){
199
		    search();
200
		}
201
	}
202

    
203
	/** {@inheritDoc} */
204
	@Override
205
    public void widgetDefaultSelected(SelectionEvent e) {}
206

    
207

    
208
}
(37-37/40)