Merge branch 'develop' into LibrAlign
[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.model.PolytomousKeyRelationship;
21
22 /**
23 *
24 * @author n.hoffmann
25 * @created Mar 30, 2011
26 * @version 1.0
27 */
28 class PolytomousKeyGraphContentProvider implements IGraphContentProvider {
29
30 private List<PolytomousKeyRelationship> relations;
31
32 @Override
33 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
34
35 }
36
37 @Override
38 public void dispose() {
39
40 }
41
42 @Override
43 public Object[] getElements(Object inputElement) {
44 relations = new ArrayList<PolytomousKeyRelationship>();
45
46 if (inputElement instanceof PolytomousKey) {
47 getTreeRecursively(relations, inputElement);
48 }
49 return relations.toArray();
50 }
51
52 private void getTreeRecursively(List<PolytomousKeyRelationship> relations,
53 Object parent) {
54 List<PolytomousKeyNode> children;
55
56 if (parent instanceof PolytomousKeyNode) {
57 ((PolytomousKeyNode) parent).removeNullValueFromChildren();
58 children = ((PolytomousKeyNode) parent).getChildren();
59 } else if (parent instanceof PolytomousKey) {
60 children = new ArrayList<PolytomousKeyNode>();
61 children.add(((PolytomousKey) parent).getRoot());
62 } else {
63 throw new RuntimeException(
64 "Parent element has to be PolytomousKeyNode or PolytomousKey, but was: "
65 + parent.getClass());
66 }
67
68 for (PolytomousKeyNode childNode : children) {
69 relations.add(new PolytomousKeyRelationship(parent, childNode));
70 getTreeRecursively(relations, childNode);
71 }
72 }
73
74 @Override
75 public Object getSource(Object relationship) {
76 return ((PolytomousKeyRelationship) relationship).getSource();
77 }
78
79 @Override
80 public Object getDestination(Object relationship) {
81 return ((PolytomousKeyRelationship) relationship).getDestination();
82 }
83
84 }