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
;
16 import org
.eclipse
.jface
.action
.Action
;
17 import org
.eclipse
.jface
.action
.GroupMarker
;
18 import org
.eclipse
.jface
.action
.IAction
;
19 import org
.eclipse
.jface
.action
.IToolBarManager
;
20 import org
.eclipse
.jface
.action
.MenuManager
;
21 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
22 import org
.eclipse
.jface
.viewers
.ISelection
;
23 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
24 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
25 import org
.eclipse
.jface
.viewers
.TreeViewer
;
26 import org
.eclipse
.jface
.viewers
.Viewer
;
27 import org
.eclipse
.swt
.SWT
;
28 import org
.eclipse
.swt
.dnd
.DND
;
29 import org
.eclipse
.swt
.dnd
.Transfer
;
30 import org
.eclipse
.swt
.graphics
.ImageData
;
31 import org
.eclipse
.swt
.widgets
.Composite
;
32 import org
.eclipse
.swt
.widgets
.Control
;
33 import org
.eclipse
.swt
.widgets
.Menu
;
34 import org
.eclipse
.swt
.widgets
.Tree
;
35 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
36 import org
.eclipse
.ui
.IWorkbenchPart
;
38 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
39 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
40 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
41 import eu
.etaxonomy
.cdm
.model
.description
.IDescribable
;
42 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
43 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
44 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainerTree
;
45 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
46 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
47 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
48 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
49 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmEditorViewPart
;
50 import eu
.etaxonomy
.taxeditor
.view
.detail
.DetailsViewPart
;
51 import eu
.etaxonomy
.taxeditor
.view
.supplementaldata
.SupplementalDataViewPart
;
54 * <p>DescriptiveViewPart class.</p>
57 * @created Jun 9, 2010
60 public class DescriptiveViewPart
extends AbstractCdmEditorViewPart
implements IPartContentHasDetails
, IPartContentHasSupplementalData
{
62 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.desc"{trunked}</code> */
63 public static final String ID
= "eu.etaxonomy.taxeditor.editor.view.descriptive";
65 protected TreeViewer viewer
;
67 protected Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
= new HashMap
<DescriptionBase
<?
>, FeatureNodeContainerTree
>();
69 protected ToggleDescriptionAction showAllElementsAction
;
71 protected ToggleDescriptionAction hideAllElementsAction
;
73 protected int dndOperations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
77 public void createViewer(Composite parent
) {
78 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
79 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
80 viewer
.setContentProvider(new DescriptiveContentProvider(featureNodeContainerCache
));
81 viewer
.setLabelProvider(new DescriptiveLabelProvider());
82 viewer
.setSorter(new DescriptiveViewerSorter());
83 viewer
.setAutoExpandLevel(2);
84 Transfer
[] transfers
= new Transfer
[] { DescriptionElementTransfer
.getInstance() };
85 viewer
.addDragSupport(dndOperations
, transfers
, new DescriptionElementDragListener(
87 viewer
.addDropSupport(dndOperations
, transfers
,
88 new DescriptionElementDropAdapter(viewer
));
90 // Propagate selection from viewer
91 getSite().setSelectionProvider(viewer
);
93 showAllElementsAction
= new ToggleDescriptionAction(false);
94 hideAllElementsAction
= new ToggleDescriptionAction(true);
96 // Add context menu to tree
102 // if(getEditor() != null){
103 // viewer.setInput(getEditor().getEditorInput());
108 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getInitialSelection()
112 protected ISelection
getInitialSelection() {
113 if(getEditor() != null){
114 return new StructuredSelection(getEditor().getEditorInput());
117 return super.getInitialSelection();
120 protected void createToolbar() {
121 IToolBarManager toolBarManager
= getViewSite().getActionBars().getToolBarManager();
122 toolBarManager
.add(showAllElementsAction
);
123 toolBarManager
.add(hideAllElementsAction
);
127 * TODO add to the views menu
129 protected void createMenu(){
130 MenuManager menuManager
= new MenuManager();
131 menuManager
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
133 getSite().registerContextMenu(menuManager
, viewer
);
135 // FIXME for some reason this is not working
136 menuManager
.add(showAllElementsAction
);
137 menuManager
.add(hideAllElementsAction
);
139 Control control
= viewer
.getControl();
140 Menu menu
= menuManager
.createContextMenu(control
);
142 control
.setMenu(menu
);
147 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
148 if(AbstractUtility
.getActiveEditor() == null){
156 if(part
instanceof DetailsViewPart
|| part
instanceof SupplementalDataViewPart
){
157 // do not show empty page as these views are also used to edit the description selected in this view
161 if(selection
instanceof IStructuredSelection
162 && ((IStructuredSelection
) selection
).getFirstElement() instanceof IDescribable
<?
>
163 && part
instanceof IPartContentHasFactualData
){
164 featureNodeContainerCache
.clear();
165 showViewer(part
, (IStructuredSelection
) selection
);
176 * @created May 28, 2010
179 protected class ToggleDescriptionAction
extends Action
{
180 private final boolean expanded
;
182 public ToggleDescriptionAction(boolean expand
){
183 super(null, IAction
.AS_PUSH_BUTTON
);
185 setImageAndTooltip();
188 private void setImageAndTooltip(){
189 setImageDescriptor(new ImageDescriptor() {
191 public ImageData
getImageData() {
192 setText(expanded ?
"Collapse All" : "Expand All");
193 String resource
= expanded ? ImageResources
.COLLAPSE_ALL
: ImageResources
.EXPAND_ALL
;
194 return ImageResources
.getImage(resource
).getImageData();
198 String toolTipText
= expanded ?
"Collapse all" : "Show all descriptive data";
199 setToolTipText(toolTipText
);
205 viewer
.collapseAll();
209 setImageAndTooltip();
215 public boolean postOperation(CdmBase objectAffectedByOperation
) {
216 return super.postOperation(objectAffectedByOperation
);
221 public void changed(Object object
) {
222 for(FeatureNodeContainerTree containerTree
: featureNodeContainerCache
.values()){
223 containerTree
.buildTree();
227 if(object
instanceof DescriptionElementBase
){
228 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) object
;
229 DescriptionBase description
= descriptionElement
.getInDescription();
230 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
232 FeatureNodeContainer featureNodeContainer
= containerTree
.getFeatureNodeContainerForDescriptionElement(descriptionElement
);
233 viewer
.expandToLevel(featureNodeContainer
, 1);
238 StructuredSelection selection
= new StructuredSelection(object
);
239 viewer
.setSelection(selection
, true);
242 super.changed(object
);
247 public Viewer
getViewer() {
257 public boolean onComplete() {