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
.occurrence
.SpecimenOrObservationBase
;
45 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
46 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
47 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
48 import eu
.etaxonomy
.taxeditor
.editor
.Messages
;
49 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditor
;
50 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
51 import eu
.etaxonomy
.taxeditor
.editor
.view
.media
.MediaViewPart
;
52 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
53 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
54 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainerTree
;
55 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
56 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
57 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
58 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
59 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmEditorViewPart
;
60 import eu
.etaxonomy
.taxeditor
.view
.detail
.DetailsViewPart
;
61 import eu
.etaxonomy
.taxeditor
.view
.supplementaldata
.SupplementalDataViewPart
;
65 * @created Jun 9, 2010
68 public class DescriptiveViewPart
extends AbstractCdmEditorViewPart
implements IPartContentHasDetails
, IPartContentHasSupplementalData
{
70 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.desc"{trunked}</code> */
71 public static final String ID
= "eu.etaxonomy.taxeditor.editor.view.descriptive"; //$NON-NLS-1$
73 protected TreeViewer viewer
;
76 * Maps {@link FeatureNodeContainerTree} to their corresponding {@link TaxonDescritpion}.<br>
77 * This serves as input for the {@link ITreeContentProvider} of the {@link TreeViewer}
79 protected Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
= new HashMap
<DescriptionBase
<?
>, FeatureNodeContainerTree
>();
81 protected ToggleDescriptionAction showAllElementsAction
;
83 protected ToggleDescriptionAction hideAllElementsAction
;
85 protected int dndOperations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
87 private DescriptiveContentProvider provider
;
91 public void createViewer(Composite parent
) {
92 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
93 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
94 provider
= new DescriptiveContentProvider(featureNodeContainerCache
);
95 viewer
.setContentProvider(provider
);
96 viewer
.setLabelProvider(new DescriptiveLabelProvider());
97 viewer
.setSorter(new DescriptiveViewerSorter());
98 viewer
.setAutoExpandLevel(2);
99 Transfer
[] transfers
= new Transfer
[] { DescriptionElementTransfer
.getInstance() };
100 viewer
.addDragSupport(dndOperations
, transfers
, new DescriptionElementDragListener(
102 viewer
.addDropSupport(dndOperations
, transfers
,
103 new DescriptionElementDropAdapter(viewer
));
105 // Propagate selection from viewer
106 getSite().setSelectionProvider(viewer
);
108 showAllElementsAction
= new ToggleDescriptionAction(false);
109 hideAllElementsAction
= new ToggleDescriptionAction(true);
111 // Add context menu to tree
120 protected ISelection
getInitialSelection() {
121 if(getEditor() != null){
122 return new StructuredSelection(getEditor().getEditorInput());
125 return super.getInitialSelection();
128 protected void createToolbar() {
129 IToolBarManager toolBarManager
= getViewSite().getActionBars().getToolBarManager();
130 toolBarManager
.add(showAllElementsAction
);
131 toolBarManager
.add(hideAllElementsAction
);
135 * TODO add to the views menu
137 protected void createMenu(){
138 MenuManager menuManager
= new MenuManager();
139 menuManager
.setRemoveAllWhenShown(true);
140 menuManager
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
142 getSite().registerContextMenu(menuManager
, viewer
);
144 // FIXME for some reason this is not working
145 menuManager
.add(showAllElementsAction
);
146 menuManager
.add(hideAllElementsAction
);
148 Control control
= viewer
.getControl();
149 Menu menu
= menuManager
.createContextMenu(control
);
151 control
.setMenu(menu
);
156 protected void selectionChanged_internal(IWorkbenchPart part
, ISelection selection
) {
157 if(AbstractUtility
.getActiveEditor() == null){
165 if (part
instanceof DetailsViewPart
|| part
instanceof SupplementalDataViewPart
166 || part
instanceof MediaViewPart
) {
167 // do not show empty page as these views are also used to edit the
168 // description selected in this view
171 // unpackage TreeNode of DerivateView
172 else if(part
instanceof DerivateView
){
173 TreeNode treeNodeOfSelection
= EditorUtil
.getTreeNodeOfSelection(selection
);
174 if(treeNodeOfSelection
!=null){
175 selection
= new StructuredSelection(treeNodeOfSelection
.getValue());
178 else if(part
instanceof ChecklistEditor
){
179 if(selection
instanceof StructuredSelection
&& ((IStructuredSelection
) selection
).getFirstElement() instanceof Taxon
){
180 Taxon taxon
= (Taxon
)((IStructuredSelection
) selection
).getFirstElement();
181 selection
= new StructuredSelection(taxon
);
184 if(selection
instanceof IStructuredSelection
185 && ((IStructuredSelection
) selection
).getFirstElement() instanceof IDescribable
<?
>
186 && part
instanceof IPartContentHasFactualData
&& !(((IStructuredSelection
) selection
).getFirstElement() instanceof SpecimenOrObservationBase
&& part
instanceof BulkEditor
)){
187 featureNodeContainerCache
.clear();
188 showViewer(part
, (IStructuredSelection
) selection
);
197 protected String
getViewName(){
198 return Messages
.DescriptiveViewPart_FACTUAL_DATA
;
204 * @created May 28, 2010
207 protected class ToggleDescriptionAction
extends Action
{
208 private final boolean expanded
;
210 public ToggleDescriptionAction(boolean expand
){
211 super(null, IAction
.AS_PUSH_BUTTON
);
213 setImageAndTooltip();
216 private void setImageAndTooltip(){
217 setImageDescriptor(new ImageDescriptor() {
219 public ImageData
getImageData() {
220 setText(expanded ? Messages
.DescriptiveViewPart_COLLAPSE_ALL
: Messages
.DescriptiveViewPart_EXPAND_ALL
);
221 String resource
= expanded ? ImageResources
.COLLAPSE_ALL
: ImageResources
.EXPAND_ALL
;
222 return ImageResources
.getImage(resource
).getImageData();
226 String toolTipText
= expanded ? Messages
.DescriptiveViewPart_COLLAPSE_ALL
: Messages
.DescriptiveViewPart_SHOW_ALL_DATA
;
227 setToolTipText(toolTipText
);
233 viewer
.collapseAll();
237 setImageAndTooltip();
243 public boolean postOperation(CdmBase objectAffectedByOperation
) {
244 return super.postOperation(objectAffectedByOperation
);
249 public void changed(Object object
) {
250 for(FeatureNodeContainerTree containerTree
: featureNodeContainerCache
.values()){
251 containerTree
.buildTree();
253 viewer
.expandToLevel(2);
256 if(object
instanceof DescriptionElementBase
){
257 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) object
;
258 DescriptionBase description
= descriptionElement
.getInDescription();
259 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
261 FeatureNodeContainer featureNodeContainer
= containerTree
.getFeatureNodeContainerForDescriptionElement(descriptionElement
);
262 viewer
.expandToLevel(featureNodeContainer
, 1);
267 StructuredSelection selection
= new StructuredSelection(object
);
268 viewer
.setSelection(selection
, true);
270 if(part
instanceof BulkEditor
&& !(object
instanceof SpecimenOrObservationBase
<?
>)){
271 ((BulkEditor
) part
).forceDirty();
272 IStructuredSelection selection
= (IStructuredSelection
) ((BulkEditor
) part
).getSelectionProvider().getSelection();
273 ((BulkEditor
) part
).changed(selection
.getFirstElement());
278 super.changed(object
);
283 public Viewer
getViewer() {
288 public boolean onComplete() {
292 public void toggleShowOnlyIndividualAssociations(){
293 provider
.toggleShowOnlyIndividualAssociations();