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
;
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
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
44 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
45 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
46 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
47 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
48 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditor
;
49 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
50 import eu
.etaxonomy
.taxeditor
.editor
.view
.media
.MediaViewPart
;
51 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
52 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
53 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainerTree
;
54 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
55 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
56 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
57 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
58 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmEditorViewPart
;
59 import eu
.etaxonomy
.taxeditor
.view
.detail
.DetailsViewPart
;
60 import eu
.etaxonomy
.taxeditor
.view
.supplementaldata
.SupplementalDataViewPart
;
64 * @created Jun 9, 2010
67 public class DescriptiveViewPart
extends AbstractCdmEditorViewPart
implements IPartContentHasDetails
, IPartContentHasSupplementalData
{
69 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.desc"{trunked}</code> */
70 public static final String ID
= "eu.etaxonomy.taxeditor.editor.view.descriptive"; //$NON-NLS-1$
72 protected TreeViewer viewer
;
75 * Maps {@link FeatureNodeContainerTree} to their corresponding {@link TaxonDescritpion}.<br>
76 * This serves as input for the {@link ITreeContentProvider} of the {@link TreeViewer}
78 protected Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
= new HashMap
<DescriptionBase
<?
>, FeatureNodeContainerTree
>();
80 protected ToggleDescriptionAction showAllElementsAction
;
82 protected ToggleDescriptionAction hideAllElementsAction
;
84 protected int dndOperations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
86 private DescriptiveContentProvider provider
;
90 public void createViewer(Composite parent
) {
91 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
92 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
93 provider
= new DescriptiveContentProvider(featureNodeContainerCache
);
94 viewer
.setContentProvider(provider
);
95 viewer
.setLabelProvider(new DescriptiveLabelProvider());
96 viewer
.setSorter(new DescriptiveViewerSorter());
97 viewer
.setAutoExpandLevel(2);
98 Transfer
[] transfers
= new Transfer
[] { DescriptionElementTransfer
.getInstance() };
99 viewer
.addDragSupport(dndOperations
, transfers
, new DescriptionElementDragListener(
101 viewer
.addDropSupport(dndOperations
, transfers
,
102 new DescriptionElementDropAdapter(viewer
));
104 // Propagate selection from viewer
105 getSite().setSelectionProvider(viewer
);
107 showAllElementsAction
= new ToggleDescriptionAction(false);
108 hideAllElementsAction
= new ToggleDescriptionAction(true);
110 // Add context menu to tree
119 protected ISelection
getInitialSelection() {
120 if(getEditor() != null){
121 return new StructuredSelection(getEditor().getEditorInput());
124 return super.getInitialSelection();
127 protected void createToolbar() {
128 IToolBarManager toolBarManager
= getViewSite().getActionBars().getToolBarManager();
129 toolBarManager
.add(showAllElementsAction
);
130 toolBarManager
.add(hideAllElementsAction
);
134 * TODO add to the views menu
136 protected void createMenu(){
137 MenuManager menuManager
= new MenuManager();
138 menuManager
.setRemoveAllWhenShown(true);
139 menuManager
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
141 getSite().registerContextMenu(menuManager
, viewer
);
143 // FIXME for some reason this is not working
144 menuManager
.add(showAllElementsAction
);
145 menuManager
.add(hideAllElementsAction
);
147 Control control
= viewer
.getControl();
148 Menu menu
= menuManager
.createContextMenu(control
);
150 control
.setMenu(menu
);
155 protected void selectionChanged_internal(IWorkbenchPart part
, ISelection selection
) {
156 if(AbstractUtility
.getActiveEditor() == null){
164 if (part
instanceof DetailsViewPart
|| part
instanceof SupplementalDataViewPart
165 || part
instanceof MediaViewPart
) {
166 // do not show empty page as these views are also used to edit the
167 // description selected in this view
170 // unpackage TreeNode of DerivateView
171 else if(part
instanceof DerivateView
){
172 TreeNode treeNodeOfSelection
= EditorUtil
.getTreeNodeOfSelection(selection
);
173 if(treeNodeOfSelection
!=null){
174 selection
= new StructuredSelection(treeNodeOfSelection
.getValue());
177 else if(part
instanceof ChecklistEditor
){
178 if(selection
instanceof StructuredSelection
&& ((IStructuredSelection
) selection
).getFirstElement() instanceof Taxon
){
179 Taxon taxon
= (Taxon
)((IStructuredSelection
) selection
).getFirstElement();
180 selection
= new StructuredSelection(taxon
);
183 if(selection
instanceof IStructuredSelection
184 && ((IStructuredSelection
) selection
).getFirstElement() instanceof IDescribable
<?
>
185 && part
instanceof IPartContentHasFactualData
&& !(((IStructuredSelection
) selection
).getFirstElement() instanceof SpecimenOrObservationBase
&& part
instanceof BulkEditor
)){
186 featureNodeContainerCache
.clear();
187 showViewer(part
, (IStructuredSelection
) selection
);
196 protected String
getViewName(){
197 return Messages
.DescriptiveViewPart_FACTUAL_DATA
;
203 * @created May 28, 2010
206 protected class ToggleDescriptionAction
extends Action
{
207 private final boolean expanded
;
209 public ToggleDescriptionAction(boolean expand
){
210 super(null, IAction
.AS_PUSH_BUTTON
);
212 setImageAndTooltip();
215 private void setImageAndTooltip(){
216 setImageDescriptor(new ImageDescriptor() {
218 public ImageData
getImageData() {
219 setText(expanded ? Messages
.DescriptiveViewPart_COLLAPSE_ALL
: Messages
.DescriptiveViewPart_EXPAND_ALL
);
220 String resource
= expanded ? ImageResources
.COLLAPSE_ALL
: ImageResources
.EXPAND_ALL
;
221 return ImageResources
.getImage(resource
).getImageData();
225 String toolTipText
= expanded ? Messages
.DescriptiveViewPart_COLLAPSE_ALL
: Messages
.DescriptiveViewPart_SHOW_ALL_DATA
;
226 setToolTipText(toolTipText
);
232 viewer
.collapseAll();
236 setImageAndTooltip();
242 public boolean postOperation(CdmBase objectAffectedByOperation
) {
243 return super.postOperation(objectAffectedByOperation
);
248 public void changed(Object object
) {
249 for(FeatureNodeContainerTree containerTree
: featureNodeContainerCache
.values()){
250 containerTree
.buildTree();
252 viewer
.expandToLevel(2);
255 if(object
instanceof DescriptionElementBase
){
256 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) object
;
257 DescriptionBase description
= descriptionElement
.getInDescription();
258 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
260 FeatureNodeContainer featureNodeContainer
= containerTree
.getFeatureNodeContainerForDescriptionElement(descriptionElement
);
261 viewer
.expandToLevel(featureNodeContainer
, 1);
266 StructuredSelection selection
= new StructuredSelection(object
);
267 viewer
.setSelection(selection
, true);
269 if(part
instanceof BulkEditor
&& !(object
instanceof SpecimenOrObservationBase
<?
>)){
270 ((BulkEditor
) part
).forceDirty();
271 IStructuredSelection selection
= (IStructuredSelection
) ((BulkEditor
) part
).getSelectionProvider().getSelection();
272 ((BulkEditor
) part
).changed(selection
.getFirstElement());
277 super.changed(object
);
282 public Viewer
getViewer() {
287 public boolean onComplete() {
291 public void toggleShowOnlyIndividualAssociations(){
292 provider
.toggleShowOnlyIndividualAssociations();