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
.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
.store
.CdmStore
;
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";
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){
158 setPartName(createPartTitle(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 DerivateView
){
174 TreeNode treeNodeOfSelection
= EditorUtil
.getTreeNodeOfSelection(selection
);
175 if(treeNodeOfSelection
!=null){
176 selection
= new StructuredSelection(treeNodeOfSelection
.getValue());
179 else if(part
instanceof ChecklistEditor
){
180 if(selection
instanceof StructuredSelection
&& ((IStructuredSelection
) selection
).getFirstElement() instanceof Taxon
){
181 Taxon taxon
= (Taxon
)((IStructuredSelection
) selection
).getFirstElement();
182 selection
= new StructuredSelection(taxon
);
185 if(selection
instanceof IStructuredSelection
186 && ((IStructuredSelection
) selection
).getFirstElement() instanceof IDescribable
<?
>
187 && part
instanceof IPartContentHasFactualData
&& !(((IStructuredSelection
) selection
).getFirstElement() instanceof SpecimenOrObservationBase
&& part
instanceof BulkEditor
)){
188 featureNodeContainerCache
.clear();
189 setPartName(createPartTitle(((IStructuredSelection
) selection
).getFirstElement()));
190 showViewer(part
, (IStructuredSelection
) selection
);
194 setPartName(createPartTitle(null));
199 protected String
getViewName(){
200 return "Factual Data";
203 private String
createPartTitle(Object selectedObject
){
204 if(selectedObject
!=null){
205 return getViewName()+": "+selectedObject
;
207 return getViewName();
213 * @created May 28, 2010
216 protected class ToggleDescriptionAction
extends Action
{
217 private final boolean expanded
;
219 public ToggleDescriptionAction(boolean expand
){
220 super(null, IAction
.AS_PUSH_BUTTON
);
222 setImageAndTooltip();
225 private void setImageAndTooltip(){
226 setImageDescriptor(new ImageDescriptor() {
228 public ImageData
getImageData() {
229 setText(expanded ?
"Collapse All" : "Expand All");
230 String resource
= expanded ? ImageResources
.COLLAPSE_ALL
: ImageResources
.EXPAND_ALL
;
231 return ImageResources
.getImage(resource
).getImageData();
235 String toolTipText
= expanded ?
"Collapse all" : "Show all factual data";
236 setToolTipText(toolTipText
);
242 viewer
.collapseAll();
246 setImageAndTooltip();
252 public boolean postOperation(CdmBase objectAffectedByOperation
) {
253 return super.postOperation(objectAffectedByOperation
);
258 public void changed(Object object
) {
259 for(FeatureNodeContainerTree containerTree
: featureNodeContainerCache
.values()){
260 containerTree
.buildTree();
262 viewer
.expandToLevel(2);
265 if(object
instanceof DescriptionElementBase
){
266 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) object
;
267 DescriptionBase description
= descriptionElement
.getInDescription();
268 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
270 FeatureNodeContainer featureNodeContainer
= containerTree
.getFeatureNodeContainerForDescriptionElement(descriptionElement
);
271 viewer
.expandToLevel(featureNodeContainer
, 1);
276 StructuredSelection selection
= new StructuredSelection(object
);
277 viewer
.setSelection(selection
, true);
279 if(part
instanceof BulkEditor
&& !(object
instanceof SpecimenOrObservationBase
<?
>)){
280 ((BulkEditor
) part
).forceDirty();
281 if(CdmStore
.getCurrentSessionManager().isRemoting()) {
282 IStructuredSelection selection
= (IStructuredSelection
) ((BulkEditor
) part
).getSelectionProvider().getSelection();
283 ((BulkEditor
) part
).changed(selection
.getFirstElement());
288 super.changed(object
);
293 public Viewer
getViewer() {
298 public boolean onComplete() {
302 public void toggleShowOnlyIndividualAssociations(){
303 provider
.toggleShowOnlyIndividualAssociations();