Moving editor sources back into trunk
[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 org.apache.log4j.Logger;
15
16 import eu.etaxonomy.cdm.model.name.NameRelationship;
17 import eu.etaxonomy.cdm.model.name.NameRelationshipType;
18 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
19 import eu.etaxonomy.cdm.model.name.ZoologicalName;
20 import eu.etaxonomy.taxeditor.propertysheet.CollectionPropertySource;
21 import eu.etaxonomy.taxeditor.store.model.NameRelationsUtil;
22
23 public class NameRelationsPropertySource extends CollectionPropertySource {
24 private static final Logger logger = Logger
25 .getLogger(NameRelationsPropertySource.class);
26
27 private TaxonNameBase name;
28 private boolean isZoological = false;
29
30 private Set nameRelations;
31
32 public NameRelationsPropertySource(TaxonNameBase name, Collection collection) {
33 super(collection, name);
34 this.name = name;
35 if (name instanceof ZoologicalName) {
36 isZoological = true;
37 }
38
39 this.nameRelations = name.getNameRelations();
40 }
41
42 @Override
43 protected String getItemDisplayName(Object item) {
44 String itemDisplayName = "";
45
46 if (item instanceof NameRelationship) {
47
48 NameRelationship nameRelation = (NameRelationship) item;
49
50 if (nameRelation.getFromName() instanceof ZoologicalName &&
51 nameRelation.getToName() instanceof ZoologicalName) {
52 isZoological = true;
53 }
54
55 if (nameRelation.getType() != null) {
56 NameRelationshipType type = nameRelation.getType();
57 String label;
58 if (nameRelation.getFromName().equals(getCollectionOwner())) {
59 label = NameRelationsUtil.getNameRelationTypeLabel(type, isZoological);
60 } else {
61 label = NameRelationsUtil.getNameRelationInverseTypeLabel(type, isZoological);
62 }
63 itemDisplayName = label;
64 } else {
65 itemDisplayName = "Relation has no type";
66 }
67 }
68 return itemDisplayName;
69 }
70
71 @Override
72 public Object getPropertyValue(Object id) {
73 if (id instanceof NameRelationship) {
74 TaxonNameBase relatedName;
75 if (((NameRelationship) id).getFromName().equals(name)) {
76 relatedName = ((NameRelationship) id).getToName();
77 } else {
78 relatedName = ((NameRelationship) id).getFromName();
79 }
80 return relatedName.getTitleCache();
81 }
82 return null;
83 }
84
85 @Override
86 public void setPropertyValue(Object id, Object value) {
87 // Fields not editable in property sheet view
88 }
89
90 @Override
91 public String toString() {
92 // "Name Relations" header has no value
93 return "";
94 }
95 }