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
.store
.CdmStore
;
60 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmEditorViewPart
;
61 import eu
.etaxonomy
.taxeditor
.view
.detail
.DetailsViewPart
;
62 import eu
.etaxonomy
.taxeditor
.view
.supplementaldata
.SupplementalDataViewPart
;
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 part
, ISelection selection
) {
158 if(AbstractUtility
.getActiveEditor() == null){
159 setPartName(createPartTitle(null));
167 if (part
instanceof DetailsViewPart
|| part
instanceof SupplementalDataViewPart
168 || part
instanceof MediaViewPart
) {
169 // do not show empty page as these views are also used to edit the
170 // 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
&& !(((IStructuredSelection
) selection
).getFirstElement() instanceof SpecimenOrObservationBase
&& part
instanceof BulkEditor
)){
189 featureNodeContainerCache
.clear();
190 setPartName(createPartTitle(((IStructuredSelection
) selection
).getFirstElement()));
191 showViewer(part
, (IStructuredSelection
) selection
);
195 setPartName(createPartTitle(null));
200 protected String
getViewName(){
201 return Messages
.DescriptiveViewPart_FACTUAL_DATA
;
204 private String
createPartTitle(Object selectedObject
){
205 if(selectedObject
!=null){
206 return getViewName()+": "+selectedObject
; //$NON-NLS-1$
208 return getViewName();
214 * @created May 28, 2010
217 protected class ToggleDescriptionAction
extends Action
{
218 private final boolean expanded
;
220 public ToggleDescriptionAction(boolean expand
){
221 super(null, IAction
.AS_PUSH_BUTTON
);
223 setImageAndTooltip();
226 private void setImageAndTooltip(){
227 setImageDescriptor(new ImageDescriptor() {
229 public ImageData
getImageData() {
230 setText(expanded ? Messages
.DescriptiveViewPart_COLLAPSE_ALL
: Messages
.DescriptiveViewPart_EXPAND_ALL
);
231 String resource
= expanded ? ImageResources
.COLLAPSE_ALL
: ImageResources
.EXPAND_ALL
;
232 return ImageResources
.getImage(resource
).getImageData();
236 String toolTipText
= expanded ? Messages
.DescriptiveViewPart_COLLAPSE_ALL
: Messages
.DescriptiveViewPart_SHOW_ALL_DATA
;
237 setToolTipText(toolTipText
);
243 viewer
.collapseAll();
247 setImageAndTooltip();
253 public boolean postOperation(CdmBase objectAffectedByOperation
) {
254 return super.postOperation(objectAffectedByOperation
);
259 public void changed(Object object
) {
260 for(FeatureNodeContainerTree containerTree
: featureNodeContainerCache
.values()){
261 containerTree
.buildTree();
263 viewer
.expandToLevel(2);
266 if(object
instanceof DescriptionElementBase
){
267 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) object
;
268 DescriptionBase description
= descriptionElement
.getInDescription();
269 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
271 FeatureNodeContainer featureNodeContainer
= containerTree
.getFeatureNodeContainerForDescriptionElement(descriptionElement
);
272 viewer
.expandToLevel(featureNodeContainer
, 1);
277 StructuredSelection selection
= new StructuredSelection(object
);
278 viewer
.setSelection(selection
, true);
280 if(part
instanceof BulkEditor
&& !(object
instanceof SpecimenOrObservationBase
<?
>)){
281 ((BulkEditor
) part
).forceDirty();
282 if(CdmStore
.getCurrentSessionManager().isRemoting()) {
283 IStructuredSelection selection
= (IStructuredSelection
) ((BulkEditor
) part
).getSelectionProvider().getSelection();
284 ((BulkEditor
) part
).changed(selection
.getFirstElement());
289 super.changed(object
);
294 public Viewer
getViewer() {
299 public boolean onComplete() {
303 public void toggleShowOnlyIndividualAssociations(){
304 provider
.toggleShowOnlyIndividualAssociations();