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
.ITreeContentProvider
;
25 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
26 import org
.eclipse
.jface
.viewers
.TreeViewer
;
27 import org
.eclipse
.jface
.viewers
.Viewer
;
28 import org
.eclipse
.swt
.SWT
;
29 import org
.eclipse
.swt
.dnd
.DND
;
30 import org
.eclipse
.swt
.dnd
.Transfer
;
31 import org
.eclipse
.swt
.graphics
.ImageData
;
32 import org
.eclipse
.swt
.widgets
.Composite
;
33 import org
.eclipse
.swt
.widgets
.Control
;
34 import org
.eclipse
.swt
.widgets
.Menu
;
35 import org
.eclipse
.swt
.widgets
.Tree
;
36 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
37 import org
.eclipse
.ui
.IWorkbenchPart
;
39 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
40 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
41 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
42 import eu
.etaxonomy
.cdm
.model
.description
.IDescribable
;
43 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
44 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
45 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainerTree
;
46 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
47 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
48 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
49 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
50 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmEditorViewPart
;
51 import eu
.etaxonomy
.taxeditor
.view
.detail
.DetailsViewPart
;
52 import eu
.etaxonomy
.taxeditor
.view
.supplementaldata
.SupplementalDataViewPart
;
55 * <p>DescriptiveViewPart class.</p>
58 * @created Jun 9, 2010
61 public class DescriptiveViewPart
extends AbstractCdmEditorViewPart
implements IPartContentHasDetails
, IPartContentHasSupplementalData
{
63 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.desc"{trunked}</code> */
64 public static final String ID
= "eu.etaxonomy.taxeditor.editor.view.descriptive";
66 protected TreeViewer viewer
;
69 * Maps {@link FeatureNodeContainerTree} to their corresponding {@link TaxonDescritpion}.<br>
70 * This serves as input for the {@link ITreeContentProvider} of the {@link TreeViewer}
72 protected Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
= new HashMap
<DescriptionBase
<?
>, FeatureNodeContainerTree
>();
74 protected ToggleDescriptionAction showAllElementsAction
;
76 protected ToggleDescriptionAction hideAllElementsAction
;
78 protected int dndOperations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
82 public void createViewer(Composite parent
) {
83 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
84 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
85 viewer
.setContentProvider(new DescriptiveContentProvider(featureNodeContainerCache
));
86 viewer
.setLabelProvider(new DescriptiveLabelProvider());
87 viewer
.setSorter(new DescriptiveViewerSorter());
88 viewer
.setAutoExpandLevel(2);
89 Transfer
[] transfers
= new Transfer
[] { DescriptionElementTransfer
.getInstance() };
90 viewer
.addDragSupport(dndOperations
, transfers
, new DescriptionElementDragListener(
92 viewer
.addDropSupport(dndOperations
, transfers
,
93 new DescriptionElementDropAdapter(viewer
));
95 // Propagate selection from viewer
96 getSite().setSelectionProvider(viewer
);
98 showAllElementsAction
= new ToggleDescriptionAction(false);
99 hideAllElementsAction
= new ToggleDescriptionAction(true);
101 // Add context menu to tree
107 // if(getEditor() != null){
108 // viewer.setInput(getEditor().getEditorInput());
113 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getInitialSelection()
117 protected ISelection
getInitialSelection() {
118 if(getEditor() != null){
119 return new StructuredSelection(getEditor().getEditorInput());
122 return super.getInitialSelection();
125 protected void createToolbar() {
126 IToolBarManager toolBarManager
= getViewSite().getActionBars().getToolBarManager();
127 toolBarManager
.add(showAllElementsAction
);
128 toolBarManager
.add(hideAllElementsAction
);
132 * TODO add to the views menu
134 protected void createMenu(){
135 MenuManager menuManager
= new MenuManager();
136 menuManager
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
138 getSite().registerContextMenu(menuManager
, viewer
);
140 // FIXME for some reason this is not working
141 menuManager
.add(showAllElementsAction
);
142 menuManager
.add(hideAllElementsAction
);
144 Control control
= viewer
.getControl();
145 Menu menu
= menuManager
.createContextMenu(control
);
147 control
.setMenu(menu
);
152 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
153 if(AbstractUtility
.getActiveEditor() == null){
161 if(part
instanceof DetailsViewPart
|| part
instanceof SupplementalDataViewPart
){
162 // do not show empty page as these views are also used to edit the description selected in this view
166 if(selection
instanceof IStructuredSelection
167 && ((IStructuredSelection
) selection
).getFirstElement() instanceof IDescribable
<?
>
168 && part
instanceof IPartContentHasFactualData
){
169 featureNodeContainerCache
.clear();
170 showViewer(part
, (IStructuredSelection
) selection
);
181 * @created May 28, 2010
184 protected class ToggleDescriptionAction
extends Action
{
185 private final boolean expanded
;
187 public ToggleDescriptionAction(boolean expand
){
188 super(null, IAction
.AS_PUSH_BUTTON
);
190 setImageAndTooltip();
193 private void setImageAndTooltip(){
194 setImageDescriptor(new ImageDescriptor() {
196 public ImageData
getImageData() {
197 setText(expanded ?
"Collapse All" : "Expand All");
198 String resource
= expanded ? ImageResources
.COLLAPSE_ALL
: ImageResources
.EXPAND_ALL
;
199 return ImageResources
.getImage(resource
).getImageData();
203 String toolTipText
= expanded ?
"Collapse all" : "Show all factual data";
204 setToolTipText(toolTipText
);
210 viewer
.collapseAll();
214 setImageAndTooltip();
220 public boolean postOperation(CdmBase objectAffectedByOperation
) {
221 return super.postOperation(objectAffectedByOperation
);
226 public void changed(Object object
) {
227 for(FeatureNodeContainerTree containerTree
: featureNodeContainerCache
.values()){
228 containerTree
.buildTree();
232 if(object
instanceof DescriptionElementBase
){
233 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) object
;
234 DescriptionBase description
= descriptionElement
.getInDescription();
235 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
237 FeatureNodeContainer featureNodeContainer
= containerTree
.getFeatureNodeContainerForDescriptionElement(descriptionElement
);
238 viewer
.expandToLevel(featureNodeContainer
, 1);
243 StructuredSelection selection
= new StructuredSelection(object
);
244 viewer
.setSelection(selection
, true);
247 super.changed(object
);
252 public Viewer
getViewer() {
262 public boolean onComplete() {