- refactored DerivateViewEditorInput to use UUIDs instead of CDM entities
[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.UUID;
13
14 import org.eclipse.jface.resource.ImageDescriptor;
15 import org.eclipse.ui.IEditorInput;
16 import org.eclipse.ui.IPersistableElement;
17
18 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
19 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
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.editor.EditorUtil;
24 import eu.etaxonomy.taxeditor.model.MessagingUtils;
25 import eu.etaxonomy.taxeditor.store.CdmStore;
26
27 /**
28 * Editor input for the {@link DerivateView} which holds the currently selected derivate for which
29 * the derivate hierarchy should be shown in the DerivateView.<br>
30 * It also holds a {@link SpecimenOrObservationBase} which is the root of the hierarchy. (both may be the same object)
31 * @author pplitzner
32 * @date 25.11.2013
33 *
34 */
35 public class DerivateViewEditorInput implements IEditorInput{
36
37 /**
38 * The selected derivate {@link UUID}
39 */
40 private UUID derivateUUID;
41 /**
42 * The {@link UUID} of the root of the hierarchy (may be the same object as the derivate)
43 */
44 private UUID rootUUID;
45
46 private final ConversationHolder conversationHolder;
47
48 /**
49 * Creates an editor input for the {@link DerivateView} with the currently selected derivate and the
50 * corresponding {@link FieldUnit} (both may be the same object).
51 * @param derivateUuid the {@link UUID} of the derivate for which the derivate hierarchy should be shown
52 * @param rootUUID the root of the hierarchy
53 */
54 public DerivateViewEditorInput(UUID derivateUuid) {
55 super();
56 this.conversationHolder = CdmStore.createConversation();
57 this.derivateUUID = derivateUuid;
58 SpecimenOrObservationBase<?> derivate = CdmStore.getService(IOccurrenceService.class).load(derivateUuid);
59 if(derivate instanceof FieldUnit){
60 this.rootUUID = derivateUUID;
61 }
62 else if(derivate instanceof DerivedUnit){
63 SpecimenOrObservationBase<?> topMostDerivate = EditorUtil.getTopMostDerivate(derivate);
64 rootUUID = topMostDerivate==null?null:topMostDerivate.getUuid();
65 if(rootUUID==null){
66 rootUUID = derivateUUID;
67 }
68 }
69 if(rootUUID==null){
70 MessagingUtils.messageDialog("Failed initializing editor", DerivateViewEditorInput.class, "No root element found!");
71 }
72 }
73
74 /* (non-Javadoc)
75 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
76 */
77 @Override
78 public Object getAdapter(Class adapter) {
79 // TODO Auto-generated method stub
80 return null;
81 }
82
83 /* (non-Javadoc)
84 * @see org.eclipse.ui.IEditorInput#exists()
85 */
86 @Override
87 public boolean exists() {
88 return false;
89 }
90
91 /* (non-Javadoc)
92 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
93 */
94 @Override
95 public ImageDescriptor getImageDescriptor() {
96 // TODO Auto-generated method stub
97 return null;
98 }
99
100 /* (non-Javadoc)
101 * @see org.eclipse.ui.IEditorInput#getName()
102 */
103 @Override
104 public String getName() {
105 return derivateUUID.toString();
106 }
107
108 /* (non-Javadoc)
109 * @see org.eclipse.ui.IEditorInput#getPersistable()
110 */
111 @Override
112 public IPersistableElement getPersistable() {
113 return null;
114 }
115
116 /* (non-Javadoc)
117 * @see org.eclipse.ui.IEditorInput#getToolTipText()
118 */
119 @Override
120 public String getToolTipText() {
121 return derivateUUID.toString();
122 }
123
124 public UUID getDerivateUUID() {
125 return derivateUUID;
126 }
127
128 public UUID getRootElementUUID() {
129 return rootUUID;
130 }
131
132 public void setRootAsDerivate(){
133 this.derivateUUID = this.rootUUID;
134 }
135
136 public ConversationHolder getConversationHolder() {
137 return conversationHolder;
138 }
139
140 /* (non-Javadoc)
141 * @see java.lang.Object#hashCode()
142 */
143 @Override
144 public int hashCode() {
145 final int prime = 31;
146 int result = 1;
147 result = prime * result + ((rootUUID == null) ? 0 : rootUUID.hashCode());
148 return result;
149 }
150
151 /* (non-Javadoc)
152 * @see java.lang.Object#equals(java.lang.Object)
153 */
154 @Override
155 public boolean equals(Object obj) {
156 if (this == obj) {
157 return true;
158 }
159 if (obj == null) {
160 return false;
161 }
162 if (getClass() != obj.getClass()) {
163 return false;
164 }
165 DerivateViewEditorInput other = (DerivateViewEditorInput) obj;
166 if (rootUUID == null) {
167 if (other.rootUUID != null) {
168 return false;
169 }
170 } else if (!rootUUID.equals(other.rootUUID)) {
171 return false;
172 }
173 return true;
174 }
175
176 }