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