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