Merge branch 'release/5.19.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / ChooseFromMultipleTaxonNodesDialog.java
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 package eu.etaxonomy.taxeditor.editor;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Set;
14
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.jface.viewers.IStructuredContentProvider;
17 import org.eclipse.jface.viewers.LabelProvider;
18 import org.eclipse.jface.viewers.Viewer;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.ui.dialogs.ListDialog;
21
22 import eu.etaxonomy.cdm.model.taxon.Classification;
23 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
24 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
25
26 /**
27 * <p>ChooseClassificationWizard class.</p>
28 *
29 * @author n.hoffmann
30 * @created 19.03.2009
31 */
32 public class ChooseFromMultipleTaxonNodesDialog extends ListDialog {
33
34 public static TaxonNode choose(Set<TaxonNode> taxonNodes){
35 ChooseFromMultipleTaxonNodesDialog dialog = new ChooseFromMultipleTaxonNodesDialog(EditorUtil.getShell());
36 dialog.setInput(taxonNodes);
37 int result = dialog.open();
38
39 if(result == IStatus.OK){
40 Classification selectedClassification = (Classification) dialog.getResult()[0];
41
42 for(TaxonNode taxonNode : taxonNodes){
43 if(taxonNode.getClassification().equals(selectedClassification)){
44 return taxonNode;
45 }
46 }
47 }
48 return null;
49 }
50
51 public ChooseFromMultipleTaxonNodesDialog(Shell parent) {
52 super(parent);
53 setTitle(Messages.ChooseFromMultipleTaxonNodesDialog_CHOOSE_CLASSIFICATION);
54 setMessage(Messages.ChooseFromMultipleTaxonNodesDialog_CHOOSE_CLASSIFICATION_MESSAGE);
55 setContentProvider(new ClassificationContentProvider());
56 setLabelProvider(new ClassificationLabelProvider());
57 }
58
59 private class ClassificationLabelProvider extends LabelProvider{
60 @Override
61 public String getText(Object element) {
62 return super.getText(((Classification) element).getTitleCache());
63 }
64 }
65
66 private class ClassificationContentProvider implements IStructuredContentProvider{
67
68 @Override
69 public Object[] getElements(Object inputElement) {
70 Set<TaxonNode> taxonNodes = (Set<TaxonNode>) inputElement;
71
72 List<Classification> classifications = new ArrayList<>();
73
74 for(TaxonNode node : taxonNodes){
75 classifications.add(node.getClassification());
76 }
77
78 return classifications.toArray();
79 }
80
81 @Override
82 public void dispose() {
83
84 }
85
86 @Override
87 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
88
89 }
90 }
91 }