3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
;
13 import java
.util
.HashMap
;
14 import java
.util
.HashSet
;
15 import java
.util
.Iterator
;
18 import java
.util
.UUID
;
20 import org
.eclipse
.jface
.action
.Action
;
21 import org
.eclipse
.jface
.action
.GroupMarker
;
22 import org
.eclipse
.jface
.action
.IAction
;
23 import org
.eclipse
.jface
.action
.IToolBarManager
;
24 import org
.eclipse
.jface
.action
.MenuManager
;
25 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
26 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
27 import org
.eclipse
.jface
.viewers
.IDoubleClickListener
;
28 import org
.eclipse
.jface
.viewers
.ISelection
;
29 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
30 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
31 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
32 import org
.eclipse
.jface
.viewers
.TreeNode
;
33 import org
.eclipse
.jface
.viewers
.TreeSelection
;
34 import org
.eclipse
.jface
.viewers
.TreeViewer
;
35 import org
.eclipse
.jface
.viewers
.Viewer
;
36 import org
.eclipse
.swt
.SWT
;
37 import org
.eclipse
.swt
.dnd
.DND
;
38 import org
.eclipse
.swt
.dnd
.Transfer
;
39 import org
.eclipse
.swt
.graphics
.ImageData
;
40 import org
.eclipse
.swt
.widgets
.Composite
;
41 import org
.eclipse
.swt
.widgets
.Control
;
42 import org
.eclipse
.swt
.widgets
.Menu
;
43 import org
.eclipse
.swt
.widgets
.Tree
;
44 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
45 import org
.eclipse
.ui
.IWorkbenchPart
;
46 import org
.eclipse
.ui
.PartInitException
;
48 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
49 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
50 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
51 import eu
.etaxonomy
.cdm
.model
.description
.IDescribable
;
52 import eu
.etaxonomy
.cdm
.model
.description
.IndividualsAssociation
;
53 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
54 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
55 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
56 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
57 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditor
;
58 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
59 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateViewEditorInput
;
60 import eu
.etaxonomy
.taxeditor
.editor
.view
.media
.MediaViewPart
;
61 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
62 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
63 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainerTree
;
64 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
65 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
66 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
67 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
68 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
69 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
70 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmEditorViewPart
;
71 import eu
.etaxonomy
.taxeditor
.view
.detail
.DetailsViewPart
;
72 import eu
.etaxonomy
.taxeditor
.view
.supplementaldata
.SupplementalDataViewPart
;
75 * <p>DescriptiveViewPart class.</p>
78 * @created Jun 9, 2010
81 public class DescriptiveViewPart
extends AbstractCdmEditorViewPart
implements IPartContentHasDetails
, IPartContentHasSupplementalData
{
83 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.desc"{trunked}</code> */
84 public static final String ID
= "eu.etaxonomy.taxeditor.editor.view.descriptive";
86 protected TreeViewer viewer
;
89 * Maps {@link FeatureNodeContainerTree} to their corresponding {@link TaxonDescritpion}.<br>
90 * This serves as input for the {@link ITreeContentProvider} of the {@link TreeViewer}
92 protected Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
= new HashMap
<DescriptionBase
<?
>, FeatureNodeContainerTree
>();
94 protected ToggleDescriptionAction showAllElementsAction
;
96 protected ToggleDescriptionAction hideAllElementsAction
;
98 protected int dndOperations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
100 private DescriptiveContentProvider provider
;
104 public void createViewer(Composite parent
) {
105 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
106 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
107 provider
= new DescriptiveContentProvider(featureNodeContainerCache
);
108 viewer
.setContentProvider(provider
);
109 viewer
.setLabelProvider(new DescriptiveLabelProvider());
110 viewer
.setSorter(new DescriptiveViewerSorter());
111 viewer
.setAutoExpandLevel(2);
112 Transfer
[] transfers
= new Transfer
[] { DescriptionElementTransfer
.getInstance() };
113 viewer
.addDragSupport(dndOperations
, transfers
, new DescriptionElementDragListener(
115 viewer
.addDropSupport(dndOperations
, transfers
,
116 new DescriptionElementDropAdapter(viewer
));
118 // Propagate selection from viewer
119 getSite().setSelectionProvider(viewer
);
121 showAllElementsAction
= new ToggleDescriptionAction(false);
122 hideAllElementsAction
= new ToggleDescriptionAction(true);
124 // Add context menu to tree
130 // if(getEditor() != null){
131 // viewer.setInput(getEditor().getEditorInput());
134 viewer
.addDoubleClickListener(new IDoubleClickListener() {
136 public void doubleClick(DoubleClickEvent event
) {
137 //Open derivate editor when specimen description element is double clicked
138 TreeSelection selection
= (TreeSelection
) viewer
.getSelection();
139 Iterator iterator
= selection
.iterator();
140 Set
<UUID
> specimenUuids
= new HashSet
<UUID
>();
141 while(iterator
.hasNext()){
142 Object next
= iterator
.next();
143 if(next
instanceof IndividualsAssociation
){
144 SpecimenOrObservationBase
<?
> specimen
= ((IndividualsAssociation
)next
).getAssociatedSpecimenOrObservation();
146 specimenUuids
.add(specimen
.getUuid());
151 if(!specimenUuids
.isEmpty()){
152 EditorUtil
.open(new DerivateViewEditorInput(specimenUuids
));
154 } catch (PartInitException e
) {
155 MessagingUtils
.error(DescriptiveViewPart
.class, "Could not open Derivative Editor", e
);
159 //TODO: extend command to accept parameter to open editor
160 // String commandId = "eu.etaxonomy.taxeditor.editor.handler.openDerivateView";
161 // IHandlerService handlerService = (IHandlerService) AbstractUtility.getService(IHandlerService.class);
163 // handlerService.executeCommand(commandId, null);
164 // } catch (ExecutionException e) {
165 // MessagingUtils.error(DerivateSearchCompositeController.class, e);
166 // } catch (NotDefinedException e) {
167 // MessagingUtils.error(DerivateSearchCompositeController.class, e);
168 // } catch (NotEnabledException e) {
169 // MessagingUtils.error(DerivateSearchCompositeController.class, e);
170 // } catch (NotHandledException e) {
171 // MessagingUtils.error(DerivateSearchCompositeController.class, e);
179 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getInitialSelection()
183 protected ISelection
getInitialSelection() {
184 if(getEditor() != null){
185 return new StructuredSelection(getEditor().getEditorInput());
188 return super.getInitialSelection();
191 protected void createToolbar() {
192 IToolBarManager toolBarManager
= getViewSite().getActionBars().getToolBarManager();
193 toolBarManager
.add(showAllElementsAction
);
194 toolBarManager
.add(hideAllElementsAction
);
198 * TODO add to the views menu
200 protected void createMenu(){
201 MenuManager menuManager
= new MenuManager();
202 menuManager
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
204 getSite().registerContextMenu(menuManager
, viewer
);
206 // FIXME for some reason this is not working
207 menuManager
.add(showAllElementsAction
);
208 menuManager
.add(hideAllElementsAction
);
210 Control control
= viewer
.getControl();
211 Menu menu
= menuManager
.createContextMenu(control
);
213 control
.setMenu(menu
);
218 protected void selectionChanged_internal(IWorkbenchPart part
, ISelection selection
) {
219 if(AbstractUtility
.getActiveEditor() == null){
220 setPartName(createPartTitle(null));
228 if (part
instanceof DetailsViewPart
|| part
instanceof SupplementalDataViewPart
229 || part
instanceof MediaViewPart
) {
230 // do not show empty page as these views are also used to edit the
231 // description selected in this view
234 // unpackage TreeNode of DerivateView
235 else if(part
instanceof DerivateView
){
236 TreeNode treeNodeOfSelection
= EditorUtil
.getTreeNodeOfSelection(selection
);
237 if(treeNodeOfSelection
!=null){
238 selection
= new StructuredSelection(treeNodeOfSelection
.getValue());
241 else if(part
instanceof ChecklistEditor
){
242 if(selection
instanceof StructuredSelection
&& ((IStructuredSelection
) selection
).getFirstElement() instanceof Taxon
){
243 Taxon taxon
= (Taxon
)((IStructuredSelection
) selection
).getFirstElement();
244 selection
= new StructuredSelection(taxon
);
247 if(selection
instanceof IStructuredSelection
248 && ((IStructuredSelection
) selection
).getFirstElement() instanceof IDescribable
<?
>
249 && part
instanceof IPartContentHasFactualData
&& !(((IStructuredSelection
) selection
).getFirstElement() instanceof SpecimenOrObservationBase
&& part
instanceof BulkEditor
)){
250 featureNodeContainerCache
.clear();
251 setPartName(createPartTitle(((IStructuredSelection
) selection
).getFirstElement()));
252 showViewer(part
, (IStructuredSelection
) selection
);
256 setPartName(createPartTitle(null));
261 protected String
getViewName(){
262 return "Factual Data";
265 private String
createPartTitle(Object selectedObject
){
266 if(selectedObject
!=null){
267 return getViewName()+": "+selectedObject
;
269 return getViewName();
275 * @created May 28, 2010
278 protected class ToggleDescriptionAction
extends Action
{
279 private final boolean expanded
;
281 public ToggleDescriptionAction(boolean expand
){
282 super(null, IAction
.AS_PUSH_BUTTON
);
284 setImageAndTooltip();
287 private void setImageAndTooltip(){
288 setImageDescriptor(new ImageDescriptor() {
290 public ImageData
getImageData() {
291 setText(expanded ?
"Collapse All" : "Expand All");
292 String resource
= expanded ? ImageResources
.COLLAPSE_ALL
: ImageResources
.EXPAND_ALL
;
293 return ImageResources
.getImage(resource
).getImageData();
297 String toolTipText
= expanded ?
"Collapse all" : "Show all factual data";
298 setToolTipText(toolTipText
);
304 viewer
.collapseAll();
308 setImageAndTooltip();
314 public boolean postOperation(CdmBase objectAffectedByOperation
) {
315 return super.postOperation(objectAffectedByOperation
);
320 public void changed(Object object
) {
321 for(FeatureNodeContainerTree containerTree
: featureNodeContainerCache
.values()){
322 containerTree
.buildTree();
324 viewer
.expandToLevel(2);
327 if(object
instanceof DescriptionElementBase
){
328 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) object
;
329 DescriptionBase description
= descriptionElement
.getInDescription();
330 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
332 FeatureNodeContainer featureNodeContainer
= containerTree
.getFeatureNodeContainerForDescriptionElement(descriptionElement
);
333 viewer
.expandToLevel(featureNodeContainer
, 1);
338 StructuredSelection selection
= new StructuredSelection(object
);
339 viewer
.setSelection(selection
, true);
341 if(part
instanceof BulkEditor
&& !(object
instanceof SpecimenOrObservationBase
<?
>)){
342 ((BulkEditor
) part
).forceDirty();
343 if(CdmStore
.getCurrentSessionManager().isRemoting()) {
344 IStructuredSelection selection
= (IStructuredSelection
) ((BulkEditor
) part
).getSelectionProvider().getSelection();
345 ((BulkEditor
) part
).changed(selection
.getFirstElement());
350 super.changed(object
);
355 public Viewer
getViewer() {
365 public boolean onComplete() {
369 public void toggleShowOnlyIndividualAssociations(){
370 provider
.toggleShowOnlyIndividualAssociations();