editor now updatable via updateSite
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / propertysheet / name / NameRelationsPropertySource.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.propertysheet.name;
10
11 import java.util.Collection;
12 import java.util.Set;
13
14 import eu.etaxonomy.cdm.model.name.NameRelationship;
15 import eu.etaxonomy.cdm.model.name.NameRelationshipType;
16 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
17 import eu.etaxonomy.cdm.model.name.ZoologicalName;
18 import eu.etaxonomy.taxeditor.propertysheet.CollectionPropertySource;
19 import eu.etaxonomy.taxeditor.store.model.NameRelationsUtil;
20
21 public class NameRelationsPropertySource extends CollectionPropertySource {
22
23 private TaxonNameBase<?, ?> name;
24 private boolean isZoological = false;
25
26 private Set<?> nameRelations;
27
28 public NameRelationsPropertySource(TaxonNameBase<?, ?> name, Collection<?> collection) {
29 super(collection, name);
30 this.name = name;
31 if (name instanceof ZoologicalName) {
32 isZoological = true;
33 }
34
35 this.setNameRelations(name.getNameRelations());
36 }
37
38 @Override
39 protected String getItemDisplayName(Object item) {
40 String itemDisplayName = "";
41
42 if (item instanceof NameRelationship) {
43
44 NameRelationship nameRelation = (NameRelationship) item;
45
46 if (nameRelation.getFromName() instanceof ZoologicalName &&
47 nameRelation.getToName() instanceof ZoologicalName) {
48 isZoological = true;
49 }
50
51 if (nameRelation.getType() != null) {
52 NameRelationshipType type = nameRelation.getType();
53 String label;
54 if (nameRelation.getFromName().equals(getCollectionOwner())) {
55 label = NameRelationsUtil.getNameRelationTypeLabel(type, isZoological);
56 } else {
57 label = NameRelationsUtil.getNameRelationInverseTypeLabel(type, isZoological);
58 }
59 itemDisplayName = label;
60 } else {
61 itemDisplayName = "Relation has no type";
62 }
63 }
64 return itemDisplayName;
65 }
66
67 @Override
68 public Object getPropertyValue(Object id) {
69 if (id instanceof NameRelationship) {
70 TaxonNameBase<?, ?> relatedName;
71 if (((NameRelationship) id).getFromName().equals(name)) {
72 relatedName = ((NameRelationship) id).getToName();
73 } else {
74 relatedName = ((NameRelationship) id).getFromName();
75 }
76 return relatedName.getTitleCache();
77 }
78 return null;
79 }
80
81 @Override
82 public void setPropertyValue(Object id, Object value) {
83 // Fields not editable in property sheet view
84 }
85
86 @Override
87 public String toString() {
88 // "Name Relations" header has no value
89 return "";
90 }
91
92 /**
93 * @param nameRelations the nameRelations to set
94 */
95 public void setNameRelations(Set<?> nameRelations) {
96 this.nameRelations = nameRelations;
97 }
98
99 /**
100 * @return the nameRelations
101 */
102 public Set<?> getNameRelations() {
103 return nameRelations;
104 }
105 }