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