Project

General

Profile

Download (8.1 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.Set;
10
import java.util.UUID;
11

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

    
25
import eu.etaxonomy.cdm.api.service.IClassificationService;
26
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
27
import eu.etaxonomy.cdm.model.taxon.Classification;
28
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
29
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
30
import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
31
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
32
import eu.etaxonomy.taxeditor.store.CdmStore;
33

    
34
/**
35
 * @author p.ciardelli
36
 */
37
public class TaxonNodeSelectionDialog extends AbstractFilteredCdmResourceSelectionDialog<TaxonNode> implements SelectionListener{
38

    
39
	public static TaxonNode select(Shell shell, //ConversationHolder conversation,
40
	        String title, Set<UUID> excludeTaxa, TaxonNode node, UUID classificationUUID, boolean allowSelectClassification) {
41
		TaxonNodeSelectionDialog dialog = new TaxonNodeSelectionDialog(shell,
42
				//conversation,
43
				title,
44
				excludeTaxa,
45
				false,
46
				node,
47
				classificationUUID, allowSelectClassification);
48
		return getSelectionFromDialog(dialog);
49
	}
50
	public static TaxonNode select(Shell shell, //ConversationHolder conversation,
51
            String title, Set<UUID> excludeTaxa, TaxonNode node, UUID classificationUUID) {
52
        TaxonNodeSelectionDialog dialog = new TaxonNodeSelectionDialog(shell,
53
                //conversation,
54
                title,
55
                excludeTaxa,
56
                false,
57
                node,
58
                classificationUUID);
59
        return getSelectionFromDialog(dialog);
60
    }
61

    
62
	public static UuidAndTitleCache<TaxonNode> selectUuidAndTitleCache(Shell shell, //ConversationHolder conversation,
63
            String title, Set<UUID> excludeTaxa, TaxonNode node, UUID classificationUUID) {
64
        TaxonNodeSelectionDialog dialog = new TaxonNodeSelectionDialog(shell,
65
                //conversation,
66
                title,
67
                excludeTaxa,
68
                false,
69
                node,
70
                classificationUUID, false);
71
        return getUuidAndTitleCacheSelectionFromDialog(dialog);
72
    }
73

    
74
	private Combo classificationSelectionCombo;
75

    
76
	private List<Classification> classifications;
77

    
78
	private Classification selectedClassification;
79
	private UUID selectedUuid;
80
//	private final Set<UUID> excludeTaxa;
81
	private boolean allowClassificationSelection = false;
82

    
83
	protected TaxonNodeSelectionDialog(Shell shell, //ConversationHolder conversation,
84
	        String title, Set<UUID> excludeTaxa, boolean multi, TaxonNode node, UUID classificationUUID, boolean allowSelectClassification) {
85
		super(shell, //conversation,
86
		        title, multi, TaxonNodeSelectionDialog.class.getCanonicalName(), node);
87

    
88
		ILabelProvider labelProvider = new FilteredCdmResourceLabelProvider();
89

    
90
		setListLabelProvider(labelProvider);
91
//		setDetailsLabelProvider(labelProvider);
92
		this.cdmBaseToBeFiltered = excludeTaxa;
93
		if(classificationUUID != null){
94
            selectedUuid = classificationUUID;
95
        }
96

    
97
		fillClassifications();
98
		this.allowClassificationSelection = allowSelectClassification;
99

    
100
		//createClassificationSelectionCombo(shell);
101
	}
102
	protected TaxonNodeSelectionDialog(Shell shell, //ConversationHolder conversation,
103
            String title, Set<UUID> excludeTaxa, boolean multi, TaxonNode node, UUID classificationUUID) {
104
	    this(shell, title, excludeTaxa, multi, node, classificationUUID, false);
105
	}
106

    
107
	/** {@inheritDoc} */
108
	@Override
109
	protected Control createDialogArea(Composite parent) {
110
	    Composite container = (Composite) super.createDialogArea(parent);
111
		return createClassificationSelectionCombo(container);
112
	}
113

    
114
	/*
115
	 * currently disabled tree selection composite
116
	 */
117
	private Control createClassificationSelectionCombo(Composite parent){
118
//		classifications = CdmStore.getTaxonTreeService().list(null, null, null, null, null);
119

    
120
		Composite classificationSelection = new Composite(parent, SWT.NULL);
121
		classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
122

    
123
		GridLayout layout = new GridLayout();
124
		classificationSelection.setLayout(layout);
125

    
126
		Label label = new Label(classificationSelection, SWT.NULL);
127
		// TODO not working is not really true but leave it here to remind everyone that this is under construction
128
		label.setText("Select Classification");
129
		classificationSelectionCombo = new Combo(classificationSelection, SWT.BORDER | SWT.READ_ONLY);
130
		classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
131

    
132
		for(Classification tree : classifications){
133
			classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
134
			if (tree.getUuid().equals(selectedUuid)){
135
			    selectedClassification = tree;
136
			}
137

    
138
		}
139

    
140
		classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
141

    
142
		// TODO remember last selection
143
		classificationSelectionCombo.addSelectionListener(this);
144

    
145

    
146

    
147
		return classificationSelection;
148
	}
149

    
150
	@Override
151
	protected String getTitle(TaxonNode taxonNode) {
152
		if(taxonNode != null && taxonNode.getTaxon() != null){
153
			return taxonNode.getTaxon().getTitleCache();
154
		}
155

    
156
		return "";
157
	}
158

    
159
	@Override
160
    protected SelectionListener getNewWizardButtonSelectionListener(){
161
        return new SelectionAdapter() {
162

    
163
            @Override
164
            public void widgetSelected(SelectionEvent e) {
165
                Classification tree = selectedClassification;
166
                setPattern(tree.getRootNode());
167

    
168
            }
169
        };
170
    }
171

    
172
	@Override
173
	protected TaxonNode getPersistentObject(UUID uuid) {
174
		return CdmStore.getService(ITaxonNodeService.class).find(uuid);
175
	}
176

    
177
	@Override
178
	protected void callService(String pattern) {
179
	    model = CdmStore.getService(IClassificationService.class)
180
	    		.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(
181
	    				selectedClassification.getUuid(), limitOfInitialElements, pattern, allowClassificationSelection, true);
182
	}
183

    
184
	@Override
185
	protected void sort() {
186
	    if(!PreferencesUtil.isSortTaxaByRankAndName()){
187
            Collections.sort(model, getItemsComparator());
188
        }
189
      // otherwise result is already sorted
190
    }
191

    
192
    private void fillClassifications() {
193
        if(classifications == null){
194
			classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
195
			Collections.sort(classifications, new Comparator<Classification>() {
196

    
197
                @Override
198
                public int compare(Classification o1, Classification o2) {
199
                    if (o1.equals(o2)){
200
                        return 0;
201
                    }
202
                    int result = o1.getTitleCache().compareTo(o2.getTitleCache());
203
                    if (result == 0){
204
                        return o1.getUuid().compareTo(o2.getUuid());
205
                    }
206
                    return result;
207
                }
208
            });
209
			if (selectedClassification == null){
210
//			    if (this.cdmBaseToBeFiltered.isEmpty()){
211
			        selectedClassification = classifications.iterator().next();
212

    
213
//			    } else {
214
//			        selectedClassification = this.cdmBaseToBeFiltered.getClassification();
215
//			    }
216
			}
217
		}
218
    }
219

    
220
	@Override
221
	protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
222
		return null;
223
	}
224

    
225
	@Override
226
	protected String[] getNewWizardText() {
227
		return null;
228
	}
229

    
230
	@Override
231
    public void widgetSelected(SelectionEvent e) {
232
		selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
233

    
234
//		refresh();
235
		if (getSearchField().getText() != null){
236
		    search();
237
		}
238
	}
239

    
240
	@Override
241
    public void widgetDefaultSelected(SelectionEvent e) {}
242
}
(39-39/45)