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
.l10n
.Messages
;
49 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditor
;
50 import eu
.etaxonomy
.taxeditor
.editor
.view
.media
.MediaViewPart
;
51 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
52 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainerTree
;
53 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
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
;
61 import eu
.etaxonomy
.taxeditor
.workbench
.WorkbenchUtility
;
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
;
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 part
, ISelection selection
) {
158 // if(AbstractUtility.getActiveE4Editor() == null){
166 if (part
instanceof DetailsViewPart
|| part
instanceof SupplementalDataViewPart
167 || part
instanceof MediaViewPart
) {
168 // do not show empty page as these views are also used to edit the
169 // description selected in this view
172 // unpackage TreeNode of DerivateView
173 else if(part
instanceof E4PartWrapper
&& ((E4PartWrapper
)part
).getPartName().equals("%command.label.DERIVATIVE_EDITOR")){
174 TreeNode treeNodeOfSelection
= EditorUtil
.getTreeNodeOfSelection(selection
);
175 if(treeNodeOfSelection
!=null){
176 selection
= new StructuredSelection(treeNodeOfSelection
.getValue());
177 showViewer(part
, (IStructuredSelection
) selection
);
181 else if(part
instanceof ChecklistEditor
){
182 if(selection
instanceof StructuredSelection
&& ((IStructuredSelection
) selection
).getFirstElement() instanceof Taxon
){
183 Taxon taxon
= (Taxon
)((IStructuredSelection
) selection
).getFirstElement();
184 selection
= new StructuredSelection(taxon
);
187 if(selection
instanceof IStructuredSelection
188 && ((IStructuredSelection
) selection
).getFirstElement() instanceof IDescribable
<?
>
189 && part
instanceof IPartContentHasFactualData
&& !(((IStructuredSelection
) selection
).getFirstElement() instanceof SpecimenOrObservationBase
&& part
instanceof BulkEditor
)){
190 featureNodeContainerCache
.clear();
191 showViewer(part
, (IStructuredSelection
) selection
);
200 protected String
getViewName(){
201 return Messages
.DescriptiveViewPart_FACTUAL_DATA
;
207 * @created May 28, 2010
210 protected class ToggleDescriptionAction
extends Action
{
211 private final boolean expanded
;
213 public ToggleDescriptionAction(boolean expand
){
214 super(null, IAction
.AS_PUSH_BUTTON
);
216 setImageAndTooltip();
219 private void setImageAndTooltip(){
220 setImageDescriptor(new ImageDescriptor() {
222 public ImageData
getImageData() {
223 setText(expanded ? Messages
.DescriptiveViewPart_COLLAPSE_ALL
: Messages
.DescriptiveViewPart_EXPAND_ALL
);
224 String resource
= expanded ? ImageResources
.COLLAPSE_ALL
: ImageResources
.EXPAND_ALL
;
225 return ImageResources
.getImage(resource
).getImageData();
229 String toolTipText
= expanded ? Messages
.DescriptiveViewPart_COLLAPSE_ALL
: Messages
.DescriptiveViewPart_SHOW_ALL_DATA
;
230 setToolTipText(toolTipText
);
236 viewer
.collapseAll();
240 setImageAndTooltip();
246 public boolean postOperation(CdmBase objectAffectedByOperation
) {
247 return super.postOperation(objectAffectedByOperation
);
252 public void changed(Object object
) {
253 for(FeatureNodeContainerTree containerTree
: featureNodeContainerCache
.values()){
254 containerTree
.buildTree();
256 viewer
.expandToLevel(2);
259 if(object
instanceof DescriptionElementBase
){
260 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) object
;
261 DescriptionBase description
= descriptionElement
.getInDescription();
262 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
264 FeatureNodeContainer featureNodeContainer
= containerTree
.getFeatureNodeContainerForDescriptionElement(descriptionElement
);
265 viewer
.expandToLevel(featureNodeContainer
, 1);
270 StructuredSelection selection
= new StructuredSelection(object
);
271 viewer
.setSelection(selection
, true);
273 if(part
instanceof BulkEditor
&& !(object
instanceof SpecimenOrObservationBase
<?
>)){
274 ((BulkEditor
) part
).forceDirty();
275 IStructuredSelection selection
= (IStructuredSelection
) ((BulkEditor
) part
).getSelectionProvider().getSelection();
276 ((BulkEditor
) part
).changed(selection
.getFirstElement());
280 if (part
instanceof E4PartWrapper
){
281 part
= WorkbenchUtility
.getE4WrappedPart(part
);
282 if (part
instanceof IDirtyMarkable
){
283 StructuredSelection selection
= new StructuredSelection(object
);
284 ((IDirtyMarkable
)part
).changed(selection
.getFirstElement());
289 super.changed(object
);
294 public Viewer
getViewer() {
299 public boolean onComplete() {
303 public void toggleShowOnlyIndividualAssociations(){
304 provider
.toggleShowOnlyIndividualAssociations();