Merge branch 'release/4.6.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / key / polytomous / PolytomousKeyGraphContentProvider.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.key.polytomous;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.eclipse.jface.viewers.Viewer;
16 import org.eclipse.zest.core.viewers.IGraphContentProvider;
17
18 import eu.etaxonomy.cdm.model.description.PolytomousKey;
19 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
20 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
21 import eu.etaxonomy.taxeditor.model.PolytomousKeyRelationship;
22
23 /**
24 *
25 * @author n.hoffmann
26 * @created Mar 30, 2011
27 * @version 1.0
28 */
29 class PolytomousKeyGraphContentProvider implements IGraphContentProvider {
30
31 private List<PolytomousKeyRelationship> relations;
32
33 @Override
34 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
35
36 }
37
38 @Override
39 public void dispose() {
40
41 }
42
43 @Override
44 public Object[] getElements(Object inputElement) {
45 relations = new ArrayList<PolytomousKeyRelationship>();
46
47 if (inputElement instanceof PolytomousKey) {
48 getTreeRecursively(relations, inputElement);
49 }
50 return relations.toArray();
51 }
52
53 private void getTreeRecursively(List<PolytomousKeyRelationship> relations,
54 Object parent) {
55 List<PolytomousKeyNode> children;
56
57 if (parent instanceof PolytomousKeyNode) {
58 ((PolytomousKeyNode) parent).removeNullValueFromChildren();
59 children = ((PolytomousKeyNode) parent).getChildren();
60 } else if (parent instanceof PolytomousKey) {
61 children = new ArrayList<PolytomousKeyNode>();
62 children.add(((PolytomousKey) parent).getRoot());
63 } else {
64 throw new RuntimeException(
65 Messages.PolytomousKeyGraphContentProvider_WRONG_PARENT
66 + parent.getClass());
67 }
68
69 for (PolytomousKeyNode childNode : children) {
70 relations.add(new PolytomousKeyRelationship(parent, childNode));
71 getTreeRecursively(relations, childNode);
72 }
73 }
74
75 @Override
76 public Object getSource(Object relationship) {
77 return ((PolytomousKeyRelationship) relationship).getSource();
78 }
79
80 @Override
81 public Object getDestination(Object relationship) {
82 return ((PolytomousKeyRelationship) relationship).getDestination();
83 }
84
85 }