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
.Collections
;
14 import java
.util
.HashMap
;
17 import org
.eclipse
.jface
.action
.Action
;
18 import org
.eclipse
.jface
.action
.GroupMarker
;
19 import org
.eclipse
.jface
.action
.IAction
;
20 import org
.eclipse
.jface
.action
.IToolBarManager
;
21 import org
.eclipse
.jface
.action
.MenuManager
;
22 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
23 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
24 import org
.eclipse
.jface
.viewers
.IDoubleClickListener
;
25 import org
.eclipse
.jface
.viewers
.ISelection
;
26 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
27 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
28 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
29 import org
.eclipse
.jface
.viewers
.TreeNode
;
30 import org
.eclipse
.jface
.viewers
.TreeSelection
;
31 import org
.eclipse
.jface
.viewers
.TreeViewer
;
32 import org
.eclipse
.jface
.viewers
.Viewer
;
33 import org
.eclipse
.swt
.SWT
;
34 import org
.eclipse
.swt
.dnd
.DND
;
35 import org
.eclipse
.swt
.dnd
.Transfer
;
36 import org
.eclipse
.swt
.graphics
.ImageData
;
37 import org
.eclipse
.swt
.widgets
.Composite
;
38 import org
.eclipse
.swt
.widgets
.Control
;
39 import org
.eclipse
.swt
.widgets
.Menu
;
40 import org
.eclipse
.swt
.widgets
.Tree
;
41 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
42 import org
.eclipse
.ui
.IWorkbenchPart
;
43 import org
.eclipse
.ui
.PartInitException
;
45 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
46 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
47 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
48 import eu
.etaxonomy
.cdm
.model
.description
.IDescribable
;
49 import eu
.etaxonomy
.cdm
.model
.description
.IndividualsAssociation
;
50 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
51 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
52 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
53 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
54 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditor
;
55 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
56 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateViewEditorInput
;
57 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
58 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
59 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainerTree
;
60 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
61 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
62 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
63 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
64 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
65 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmEditorViewPart
;
66 import eu
.etaxonomy
.taxeditor
.view
.detail
.DetailsViewPart
;
67 import eu
.etaxonomy
.taxeditor
.view
.supplementaldata
.SupplementalDataViewPart
;
70 * <p>DescriptiveViewPart class.</p>
73 * @created Jun 9, 2010
76 public class DescriptiveViewPart
extends AbstractCdmEditorViewPart
implements IPartContentHasDetails
, IPartContentHasSupplementalData
{
78 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.desc"{trunked}</code> */
79 public static final String ID
= "eu.etaxonomy.taxeditor.editor.view.descriptive";
81 protected TreeViewer viewer
;
84 * Maps {@link FeatureNodeContainerTree} to their corresponding {@link TaxonDescritpion}.<br>
85 * This serves as input for the {@link ITreeContentProvider} of the {@link TreeViewer}
87 protected Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
= new HashMap
<DescriptionBase
<?
>, FeatureNodeContainerTree
>();
89 protected ToggleDescriptionAction showAllElementsAction
;
91 protected ToggleDescriptionAction hideAllElementsAction
;
93 protected int dndOperations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
95 private DescriptiveContentProvider provider
;
99 public void createViewer(Composite parent
) {
100 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
101 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
102 provider
= new DescriptiveContentProvider(featureNodeContainerCache
);
103 viewer
.setContentProvider(provider
);
104 viewer
.setLabelProvider(new DescriptiveLabelProvider());
105 viewer
.setSorter(new DescriptiveViewerSorter());
106 viewer
.setAutoExpandLevel(2);
107 Transfer
[] transfers
= new Transfer
[] { DescriptionElementTransfer
.getInstance() };
108 viewer
.addDragSupport(dndOperations
, transfers
, new DescriptionElementDragListener(
110 viewer
.addDropSupport(dndOperations
, transfers
,
111 new DescriptionElementDropAdapter(viewer
));
113 // Propagate selection from viewer
114 getSite().setSelectionProvider(viewer
);
116 showAllElementsAction
= new ToggleDescriptionAction(false);
117 hideAllElementsAction
= new ToggleDescriptionAction(true);
119 // Add context menu to tree
125 // if(getEditor() != null){
126 // viewer.setInput(getEditor().getEditorInput());
129 viewer
.addDoubleClickListener(new IDoubleClickListener() {
131 public void doubleClick(DoubleClickEvent event
) {
132 //Open derivate editor when specimen description element is double clicked
133 TreeSelection selection
= (TreeSelection
) viewer
.getSelection();
134 if(selection
.getFirstElement() instanceof IndividualsAssociation
){
135 SpecimenOrObservationBase specimen
= ((IndividualsAssociation
)selection
.getFirstElement()).getAssociatedSpecimenOrObservation();
138 EditorUtil
.open(new DerivateViewEditorInput(Collections
.singleton(specimen
.getUuid())));
139 } catch (PartInitException e
) {
140 MessagingUtils
.error(DescriptiveViewPart
.class, "Could not open Derivate Editor", e
);
145 //TODO: extend command to accept parameter to open editor
146 // String commandId = "eu.etaxonomy.taxeditor.editor.handler.openDerivateView";
147 // IHandlerService handlerService = (IHandlerService) AbstractUtility.getService(IHandlerService.class);
149 // handlerService.executeCommand(commandId, null);
150 // } catch (ExecutionException e) {
151 // MessagingUtils.error(DerivateSearchCompositeController.class, e);
152 // } catch (NotDefinedException e) {
153 // MessagingUtils.error(DerivateSearchCompositeController.class, e);
154 // } catch (NotEnabledException e) {
155 // MessagingUtils.error(DerivateSearchCompositeController.class, e);
156 // } catch (NotHandledException e) {
157 // MessagingUtils.error(DerivateSearchCompositeController.class, e);
166 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getInitialSelection()
170 protected ISelection
getInitialSelection() {
171 if(getEditor() != null){
172 return new StructuredSelection(getEditor().getEditorInput());
175 return super.getInitialSelection();
178 protected void createToolbar() {
179 IToolBarManager toolBarManager
= getViewSite().getActionBars().getToolBarManager();
180 toolBarManager
.add(showAllElementsAction
);
181 toolBarManager
.add(hideAllElementsAction
);
185 * TODO add to the views menu
187 protected void createMenu(){
188 MenuManager menuManager
= new MenuManager();
189 menuManager
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
191 getSite().registerContextMenu(menuManager
, viewer
);
193 // FIXME for some reason this is not working
194 menuManager
.add(showAllElementsAction
);
195 menuManager
.add(hideAllElementsAction
);
197 Control control
= viewer
.getControl();
198 Menu menu
= menuManager
.createContextMenu(control
);
200 control
.setMenu(menu
);
205 protected void selectionChanged_internal(IWorkbenchPart part
, ISelection selection
) {
206 if(AbstractUtility
.getActiveEditor() == null){
214 if(part
instanceof DetailsViewPart
|| part
instanceof SupplementalDataViewPart
){
215 // do not show empty page as these views are also used to edit the description selected in this view
218 // unpackage TreeNode of DerivateView
219 else if(part
instanceof DerivateView
){
220 TreeNode treeNodeOfSelection
= EditorUtil
.getTreeNodeOfSelection(selection
);
221 if(treeNodeOfSelection
!=null){
222 selection
= new StructuredSelection(treeNodeOfSelection
.getValue());
225 else if(part
instanceof ChecklistEditor
){
226 if(selection
instanceof StructuredSelection
&& ((IStructuredSelection
) selection
).getFirstElement() instanceof Taxon
){
227 Taxon taxon
= (Taxon
)((IStructuredSelection
) selection
).getFirstElement();
228 selection
= new StructuredSelection(taxon
);
231 if(selection
instanceof IStructuredSelection
232 && ((IStructuredSelection
) selection
).getFirstElement() instanceof IDescribable
<?
>
233 && part
instanceof IPartContentHasFactualData
&& !(((IStructuredSelection
) selection
).getFirstElement() instanceof SpecimenOrObservationBase
&& part
instanceof BulkEditor
)){
234 featureNodeContainerCache
.clear();
235 showViewer(part
, (IStructuredSelection
) selection
);
246 * @created May 28, 2010
249 protected class ToggleDescriptionAction
extends Action
{
250 private final boolean expanded
;
252 public ToggleDescriptionAction(boolean expand
){
253 super(null, IAction
.AS_PUSH_BUTTON
);
255 setImageAndTooltip();
258 private void setImageAndTooltip(){
259 setImageDescriptor(new ImageDescriptor() {
261 public ImageData
getImageData() {
262 setText(expanded ?
"Collapse All" : "Expand All");
263 String resource
= expanded ? ImageResources
.COLLAPSE_ALL
: ImageResources
.EXPAND_ALL
;
264 return ImageResources
.getImage(resource
).getImageData();
268 String toolTipText
= expanded ?
"Collapse all" : "Show all factual data";
269 setToolTipText(toolTipText
);
275 viewer
.collapseAll();
279 setImageAndTooltip();
285 public boolean postOperation(CdmBase objectAffectedByOperation
) {
286 return super.postOperation(objectAffectedByOperation
);
291 public void changed(Object object
) {
292 for(FeatureNodeContainerTree containerTree
: featureNodeContainerCache
.values()){
293 containerTree
.buildTree();
297 if(object
instanceof DescriptionElementBase
){
298 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) object
;
299 DescriptionBase description
= descriptionElement
.getInDescription();
300 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
302 FeatureNodeContainer featureNodeContainer
= containerTree
.getFeatureNodeContainerForDescriptionElement(descriptionElement
);
303 viewer
.expandToLevel(featureNodeContainer
, 1);
308 StructuredSelection selection
= new StructuredSelection(object
);
309 viewer
.setSelection(selection
, true);
311 if(part
instanceof BulkEditor
&& !(object
instanceof SpecimenOrObservationBase
<?
>)){
312 ((BulkEditor
) part
).forceDirty();
315 super.changed(object
);
320 public Viewer
getViewer() {
330 public boolean onComplete() {
334 public void toggleShowOnlyIndividualAssociations(){
335 provider
.toggleShowOnlyIndividualAssociations();