2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
;
12 import java
.util
.HashMap
;
15 import org
.eclipse
.jface
.action
.Action
;
16 import org
.eclipse
.jface
.action
.GroupMarker
;
17 import org
.eclipse
.jface
.action
.IAction
;
18 import org
.eclipse
.jface
.action
.IToolBarManager
;
19 import org
.eclipse
.jface
.action
.MenuManager
;
20 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
21 import org
.eclipse
.jface
.viewers
.ISelection
;
22 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
23 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
24 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
25 import org
.eclipse
.jface
.viewers
.TreeNode
;
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
;
38 import org
.eclipse
.ui
.internal
.E4PartWrapper
;
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
.MultiPageTaxonEditor
;
49 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
50 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditor
;
51 import eu
.etaxonomy
.taxeditor
.editor
.view
.media
.MediaViewPart
;
52 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
53 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainerTree
;
54 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
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
;
62 import eu
.etaxonomy
.taxeditor
.workbench
.WorkbenchUtility
;
66 * @created Jun 9, 2010
69 public class DescriptiveViewPart
extends AbstractCdmEditorViewPart
implements IPartContentHasDetails
, IPartContentHasSupplementalData
{
71 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.desc"{trunked}</code> */
72 public static final String ID
= "eu.etaxonomy.taxeditor.editor.view.descriptive"; //$NON-NLS-1$
74 protected TreeViewer viewer
;
77 * Maps {@link FeatureNodeContainerTree} to their corresponding {@link TaxonDescritpion}.<br>
78 * This serves as input for the {@link ITreeContentProvider} of the {@link TreeViewer}
80 protected Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
= new HashMap
<DescriptionBase
<?
>, FeatureNodeContainerTree
>();
82 protected ToggleDescriptionAction showAllElementsAction
;
84 protected ToggleDescriptionAction hideAllElementsAction
;
86 protected int dndOperations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
88 private DescriptiveContentProvider provider
;
92 public void createViewer(Composite parent
) {
93 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
94 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
95 provider
= new DescriptiveContentProvider(featureNodeContainerCache
);
96 viewer
.setContentProvider(provider
);
97 viewer
.setLabelProvider(new DescriptiveLabelProvider());
98 viewer
.setSorter(new DescriptiveViewerSorter());
99 viewer
.setAutoExpandLevel(2);
100 Transfer
[] transfers
= new Transfer
[] { DescriptionElementTransfer
.getInstance() };
101 viewer
.addDragSupport(dndOperations
, transfers
, new DescriptionElementDragListener(
103 viewer
.addDropSupport(dndOperations
, transfers
,
104 new DescriptionElementDropAdapter(viewer
));
106 // Propagate selection from viewer
107 getSite().setSelectionProvider(viewer
);
109 showAllElementsAction
= new ToggleDescriptionAction(false);
110 hideAllElementsAction
= new ToggleDescriptionAction(true);
112 // Add context menu to tree
121 protected ISelection
getInitialSelection() {
122 if(getEditor() != null){
123 return new StructuredSelection(getEditor().getEditorInput());
126 return super.getInitialSelection();
129 protected void createToolbar() {
130 IToolBarManager toolBarManager
= getViewSite().getActionBars().getToolBarManager();
131 toolBarManager
.add(showAllElementsAction
);
132 toolBarManager
.add(hideAllElementsAction
);
136 * TODO add to the views menu
138 protected void createMenu(){
139 MenuManager menuManager
= new MenuManager();
140 menuManager
.setRemoveAllWhenShown(true);
141 menuManager
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
143 getSite().registerContextMenu(menuManager
, viewer
);
145 // FIXME for some reason this is not working
146 menuManager
.add(showAllElementsAction
);
147 menuManager
.add(hideAllElementsAction
);
149 Control control
= viewer
.getControl();
150 Menu menu
= menuManager
.createContextMenu(control
);
152 control
.setMenu(menu
);
157 protected void selectionChanged_internal(IWorkbenchPart workbenchPart
, ISelection selection
) {
158 Object partObject
= workbenchPart
;
159 // if(AbstractUtility.getActiveE4Editor() == null){
165 if(partObject
== this){
169 Object wrappedPart
= WorkbenchUtility
.getE4WrappedPart(partObject
);
171 if(wrappedPart
!=null){
172 partObject
= wrappedPart
;
178 if (partObject
instanceof DetailsViewPart
|| partObject
instanceof SupplementalDataViewPart
179 || partObject
instanceof MediaViewPart
) {
180 // do not show empty page as these views are also used to edit the
181 // description selected in this view
184 else if(partObject
instanceof ChecklistEditor
){
185 if(selection
instanceof StructuredSelection
&& ((IStructuredSelection
) selection
).getFirstElement() instanceof Taxon
){
186 Taxon taxon
= (Taxon
)((IStructuredSelection
) selection
).getFirstElement();
187 selection
= new StructuredSelection(taxon
);
190 //check if it is a tree node selection
191 TreeNode treeNodeOfSelection
= EditorUtil
.getTreeNodeOfSelection(selection
);
192 if(treeNodeOfSelection
!=null){
193 selection
= new StructuredSelection(treeNodeOfSelection
.getValue());
195 if(selection
instanceof IStructuredSelection
){
196 Object firstElement
= ((IStructuredSelection
) selection
).getFirstElement();
197 if (partObject
instanceof MultiPageTaxonEditor
){
198 if (firstElement
!= ((MultiPageTaxonEditor
)partObject
).getTaxon()){
204 if(firstElement
instanceof IDescribable
<?
>
205 && partObject
instanceof IPartContentHasFactualData
206 && !(firstElement
instanceof SpecimenOrObservationBase
&& partObject
instanceof BulkEditor
)){
208 featureNodeContainerCache
.clear();
209 showViewer(partObject
, (IStructuredSelection
) selection
);
217 protected String
getViewName(){
218 return Messages
.DescriptiveViewPart_FACTUAL_DATA
;
224 * @created May 28, 2010
227 protected class ToggleDescriptionAction
extends Action
{
228 private final boolean expanded
;
230 public ToggleDescriptionAction(boolean expand
){
231 super(null, IAction
.AS_PUSH_BUTTON
);
233 setImageAndTooltip();
236 private void setImageAndTooltip(){
237 setImageDescriptor(new ImageDescriptor() {
239 public ImageData
getImageData() {
240 setText(expanded ? Messages
.DescriptiveViewPart_COLLAPSE_ALL
: Messages
.DescriptiveViewPart_EXPAND_ALL
);
241 String resource
= expanded ? ImageResources
.COLLAPSE_ALL
: ImageResources
.EXPAND_ALL
;
242 return ImageResources
.getImage(resource
).getImageData();
246 String toolTipText
= expanded ? Messages
.DescriptiveViewPart_COLLAPSE_ALL
: Messages
.DescriptiveViewPart_SHOW_ALL_DATA
;
247 setToolTipText(toolTipText
);
253 viewer
.collapseAll();
257 setImageAndTooltip();
263 public boolean postOperation(CdmBase objectAffectedByOperation
) {
264 return super.postOperation(objectAffectedByOperation
);
269 public void changed(Object object
) {
270 for(FeatureNodeContainerTree containerTree
: featureNodeContainerCache
.values()){
271 containerTree
.buildTree();
273 viewer
.expandToLevel(2);
276 if(object
instanceof DescriptionElementBase
){
277 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) object
;
278 DescriptionBase description
= descriptionElement
.getInDescription();
279 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
281 FeatureNodeContainer featureNodeContainer
= containerTree
.getFeatureNodeContainerForDescriptionElement(descriptionElement
);
282 viewer
.expandToLevel(featureNodeContainer
, 1);
287 StructuredSelection selection
= new StructuredSelection(object
);
288 viewer
.setSelection(selection
, true);
290 if(part
instanceof BulkEditor
&& !(object
instanceof SpecimenOrObservationBase
<?
>)){
291 ((BulkEditor
) part
).forceDirty();
292 IStructuredSelection selection
= (IStructuredSelection
) ((BulkEditor
) part
).getSelectionProvider().getSelection();
293 ((BulkEditor
) part
).changed(selection
.getFirstElement());
297 if (part
instanceof E4PartWrapper
){
298 part
= WorkbenchUtility
.getE4WrappedPart(part
);
299 if (part
instanceof IDirtyMarkable
){
300 StructuredSelection selection
= new StructuredSelection(object
);
301 ((IDirtyMarkable
)part
).changed(selection
.getFirstElement());
306 super.changed(object
);
311 public Viewer
getViewer() {
316 public boolean onComplete() {
320 public void toggleShowOnlyIndividualAssociations(){
321 provider
.toggleShowOnlyIndividualAssociations();