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
.TreeNode
;
27 import org
.eclipse
.jface
.viewers
.TreeViewer
;
28 import org
.eclipse
.jface
.viewers
.Viewer
;
29 import org
.eclipse
.swt
.SWT
;
30 import org
.eclipse
.swt
.dnd
.DND
;
31 import org
.eclipse
.swt
.dnd
.Transfer
;
32 import org
.eclipse
.swt
.graphics
.ImageData
;
33 import org
.eclipse
.swt
.widgets
.Composite
;
34 import org
.eclipse
.swt
.widgets
.Control
;
35 import org
.eclipse
.swt
.widgets
.Menu
;
36 import org
.eclipse
.swt
.widgets
.Tree
;
37 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
38 import org
.eclipse
.ui
.IWorkbenchPart
;
40 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
41 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
42 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
43 import eu
.etaxonomy
.cdm
.model
.description
.IDescribable
;
44 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
45 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
46 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditor
;
47 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
48 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
49 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
50 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainerTree
;
51 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
52 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
53 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
54 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
55 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmEditorViewPart
;
56 import eu
.etaxonomy
.taxeditor
.view
.detail
.DetailsViewPart
;
57 import eu
.etaxonomy
.taxeditor
.view
.supplementaldata
.SupplementalDataViewPart
;
60 * <p>DescriptiveViewPart class.</p>
63 * @created Jun 9, 2010
66 public class DescriptiveViewPart
extends AbstractCdmEditorViewPart
implements IPartContentHasDetails
, IPartContentHasSupplementalData
{
68 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.desc"{trunked}</code> */
69 public static final String ID
= "eu.etaxonomy.taxeditor.editor.view.descriptive";
71 protected TreeViewer viewer
;
74 * Maps {@link FeatureNodeContainerTree} to their corresponding {@link TaxonDescritpion}.<br>
75 * This serves as input for the {@link ITreeContentProvider} of the {@link TreeViewer}
77 protected Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
= new HashMap
<DescriptionBase
<?
>, FeatureNodeContainerTree
>();
79 protected ToggleDescriptionAction showAllElementsAction
;
81 protected ToggleDescriptionAction hideAllElementsAction
;
83 protected int dndOperations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
85 private DescriptiveContentProvider provider
;
89 public void createViewer(Composite parent
) {
90 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
91 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
92 provider
= new DescriptiveContentProvider(featureNodeContainerCache
);
93 viewer
.setContentProvider(provider
);
94 viewer
.setLabelProvider(new DescriptiveLabelProvider());
95 viewer
.setSorter(new DescriptiveViewerSorter());
96 viewer
.setAutoExpandLevel(2);
97 Transfer
[] transfers
= new Transfer
[] { DescriptionElementTransfer
.getInstance() };
98 viewer
.addDragSupport(dndOperations
, transfers
, new DescriptionElementDragListener(
100 viewer
.addDropSupport(dndOperations
, transfers
,
101 new DescriptionElementDropAdapter(viewer
));
103 // Propagate selection from viewer
104 getSite().setSelectionProvider(viewer
);
106 showAllElementsAction
= new ToggleDescriptionAction(false);
107 hideAllElementsAction
= new ToggleDescriptionAction(true);
109 // Add context menu to tree
115 // if(getEditor() != null){
116 // viewer.setInput(getEditor().getEditorInput());
121 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getInitialSelection()
125 protected ISelection
getInitialSelection() {
126 if(getEditor() != null){
127 return new StructuredSelection(getEditor().getEditorInput());
130 return super.getInitialSelection();
133 protected void createToolbar() {
134 IToolBarManager toolBarManager
= getViewSite().getActionBars().getToolBarManager();
135 toolBarManager
.add(showAllElementsAction
);
136 toolBarManager
.add(hideAllElementsAction
);
140 * TODO add to the views menu
142 protected void createMenu(){
143 MenuManager menuManager
= new MenuManager();
144 menuManager
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
146 getSite().registerContextMenu(menuManager
, viewer
);
148 // FIXME for some reason this is not working
149 menuManager
.add(showAllElementsAction
);
150 menuManager
.add(hideAllElementsAction
);
152 Control control
= viewer
.getControl();
153 Menu menu
= menuManager
.createContextMenu(control
);
155 control
.setMenu(menu
);
160 protected void selectionChanged_internal(IWorkbenchPart part
, ISelection selection
) {
161 if(AbstractUtility
.getActiveEditor() == null){
169 if(part
instanceof DetailsViewPart
|| part
instanceof SupplementalDataViewPart
){
170 // do not show empty page as these views are also used to edit the description selected in this view
173 // unpackage TreeNode of DerivateView
174 else if(part
instanceof DerivateView
){
175 TreeNode treeNodeOfSelection
= EditorUtil
.getTreeNodeOfSelection(selection
);
176 if(treeNodeOfSelection
!=null){
177 selection
= new StructuredSelection(treeNodeOfSelection
.getValue());
180 else if(part
instanceof ChecklistEditor
){
181 if(selection
instanceof StructuredSelection
&& ((IStructuredSelection
) selection
).getFirstElement() instanceof Taxon
){
182 Taxon taxon
= (Taxon
)((IStructuredSelection
) selection
).getFirstElement();
183 selection
= new StructuredSelection(taxon
);
186 if(selection
instanceof IStructuredSelection
187 && ((IStructuredSelection
) selection
).getFirstElement() instanceof IDescribable
<?
>
188 && part
instanceof IPartContentHasFactualData
){
189 featureNodeContainerCache
.clear();
190 showViewer(part
, (IStructuredSelection
) selection
);
201 * @created May 28, 2010
204 protected class ToggleDescriptionAction
extends Action
{
205 private final boolean expanded
;
207 public ToggleDescriptionAction(boolean expand
){
208 super(null, IAction
.AS_PUSH_BUTTON
);
210 setImageAndTooltip();
213 private void setImageAndTooltip(){
214 setImageDescriptor(new ImageDescriptor() {
216 public ImageData
getImageData() {
217 setText(expanded ?
"Collapse All" : "Expand All");
218 String resource
= expanded ? ImageResources
.COLLAPSE_ALL
: ImageResources
.EXPAND_ALL
;
219 return ImageResources
.getImage(resource
).getImageData();
223 String toolTipText
= expanded ?
"Collapse all" : "Show all factual data";
224 setToolTipText(toolTipText
);
230 viewer
.collapseAll();
234 setImageAndTooltip();
240 public boolean postOperation(CdmBase objectAffectedByOperation
) {
241 return super.postOperation(objectAffectedByOperation
);
246 public void changed(Object object
) {
247 for(FeatureNodeContainerTree containerTree
: featureNodeContainerCache
.values()){
248 containerTree
.buildTree();
252 if(object
instanceof DescriptionElementBase
){
253 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) object
;
254 DescriptionBase description
= descriptionElement
.getInDescription();
255 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
257 FeatureNodeContainer featureNodeContainer
= containerTree
.getFeatureNodeContainerForDescriptionElement(descriptionElement
);
258 viewer
.expandToLevel(featureNodeContainer
, 1);
263 StructuredSelection selection
= new StructuredSelection(object
);
264 viewer
.setSelection(selection
, true);
267 super.changed(object
);
272 public Viewer
getViewer() {
282 public boolean onComplete() {
286 public void toggleShowOnlyIndividualAssociations(){
287 provider
.toggleShowOnlyIndividualAssociations();