merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / derivate / DerivateViewEditorInput.java
1 // $Id$
2 /**
3 * Copyright (C) 2013 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.editor.view.derivate;
11
12 import java.util.Collection;
13 import java.util.UUID;
14
15 import org.eclipse.jface.resource.ImageDescriptor;
16 import org.eclipse.ui.IEditorInput;
17 import org.eclipse.ui.IPersistableElement;
18
19 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
20 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
21 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
22 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
23 import eu.etaxonomy.taxeditor.model.AbstractUtility;
24 import eu.etaxonomy.taxeditor.store.CdmStore;
25
26 /**
27 * Editor input for the {@link DerivateView} which holds the currently selected derivate for which
28 * the derivate hierarchy should be shown in the DerivateView.<br>
29 * It also holds a {@link FieldUnit} which is the root of the hierarchy. (both may be the same object)
30 * @author pplitzner
31 * @date 25.11.2013
32 *
33 */
34 public class DerivateViewEditorInput implements IEditorInput {
35
36 /**
37 * The selected derivate
38 */
39 private final SpecimenOrObservationBase<?> derivate;
40 /**
41 * The root of the hierarchy (may be the same object as the derivate)
42 */
43 private FieldUnit fieldUnit;
44
45 private ConversationHolder conversationHolder;
46
47 /**
48 * Creates an editor input for the {@link DerivateView} with the currently selected derivate and the
49 * corresponding {@link FieldUnit} (both may be the same object).
50 * @param derivateUuid the {@link UUID} of the derivate for which the derivate hierarchy should be shown
51 * @param fieldUnit the root of the hierarchy
52 */
53 public DerivateViewEditorInput(UUID derivateUuid) {
54 super();
55 this.conversationHolder = CdmStore.createConversation();
56 this.derivate = CdmStore.getCurrentApplicationConfiguration().getOccurrenceService().load(derivateUuid);
57 if(derivate instanceof FieldUnit){
58 this.fieldUnit = (FieldUnit) derivate;
59 }
60 else if(derivate instanceof DerivedUnit){
61 Collection<FieldUnit> fieldUnits = CdmStore.getCurrentApplicationConfiguration().getOccurrenceService().getFieldUnits(derivateUuid);
62 if(!fieldUnits.isEmpty()){
63 // TODO think about handling multiple parent FieldUnits
64 this.fieldUnit = fieldUnits.iterator().next();
65 }
66 }
67 if(fieldUnit==null){
68 AbstractUtility.errorDialog("Failed initializing editor", DerivateViewEditorInput.class, "No FieldUnit found!");
69 }
70 }
71
72 /* (non-Javadoc)
73 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
74 */
75 @Override
76 public Object getAdapter(Class adapter) {
77 // TODO Auto-generated method stub
78 return null;
79 }
80
81 /* (non-Javadoc)
82 * @see org.eclipse.ui.IEditorInput#exists()
83 */
84 @Override
85 public boolean exists() {
86 return false;
87 }
88
89 /* (non-Javadoc)
90 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
91 */
92 @Override
93 public ImageDescriptor getImageDescriptor() {
94 // TODO Auto-generated method stub
95 return null;
96 }
97
98 /* (non-Javadoc)
99 * @see org.eclipse.ui.IEditorInput#getName()
100 */
101 @Override
102 public String getName() {
103 return derivate.toString();
104 }
105
106 /* (non-Javadoc)
107 * @see org.eclipse.ui.IEditorInput#getPersistable()
108 */
109 @Override
110 public IPersistableElement getPersistable() {
111 return null;
112 }
113
114 /* (non-Javadoc)
115 * @see org.eclipse.ui.IEditorInput#getToolTipText()
116 */
117 @Override
118 public String getToolTipText() {
119 return derivate.toString();
120 }
121
122
123 /**
124 * @return the specimen
125 */
126 public SpecimenOrObservationBase<?> getDerivate() {
127 return derivate;
128 }
129
130 /**
131 * @return the fieldUnit
132 */
133 public FieldUnit getFieldUnit() {
134 return fieldUnit;
135 }
136
137 /**
138 * @return the conversationHolder
139 */
140 public ConversationHolder getConversationHolder() {
141 return conversationHolder;
142 }
143
144 /* (non-Javadoc)
145 * @see java.lang.Object#hashCode()
146 */
147 @Override
148 public int hashCode() {
149 final int prime = 31;
150 int result = 1;
151 result = prime * result + ((fieldUnit == null) ? 0 : fieldUnit.hashCode());
152 return result;
153 }
154
155 /* (non-Javadoc)
156 * @see java.lang.Object#equals(java.lang.Object)
157 */
158 @Override
159 public boolean equals(Object obj) {
160 if (this == obj) {
161 return true;
162 }
163 if (obj == null) {
164 return false;
165 }
166 if (getClass() != obj.getClass()) {
167 return false;
168 }
169 DerivateViewEditorInput other = (DerivateViewEditorInput) obj;
170 if (fieldUnit == null) {
171 if (other.fieldUnit != null) {
172 return false;
173 }
174 } else if (!fieldUnit.equals(other.fieldUnit)) {
175 return false;
176 }
177 return true;
178 }
179
180 }