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
;
14 import java
.util
.HashSet
;
15 import java
.util
.Iterator
;
18 import java
.util
.UUID
;
20 import org
.eclipse
.jface
.action
.Action
;
21 import org
.eclipse
.jface
.action
.GroupMarker
;
22 import org
.eclipse
.jface
.action
.IAction
;
23 import org
.eclipse
.jface
.action
.IToolBarManager
;
24 import org
.eclipse
.jface
.action
.MenuManager
;
25 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
26 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
27 import org
.eclipse
.jface
.viewers
.IDoubleClickListener
;
28 import org
.eclipse
.jface
.viewers
.ISelection
;
29 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
30 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
31 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
32 import org
.eclipse
.jface
.viewers
.TreeNode
;
33 import org
.eclipse
.jface
.viewers
.TreeSelection
;
34 import org
.eclipse
.jface
.viewers
.TreeViewer
;
35 import org
.eclipse
.jface
.viewers
.Viewer
;
36 import org
.eclipse
.swt
.SWT
;
37 import org
.eclipse
.swt
.dnd
.DND
;
38 import org
.eclipse
.swt
.dnd
.Transfer
;
39 import org
.eclipse
.swt
.graphics
.ImageData
;
40 import org
.eclipse
.swt
.widgets
.Composite
;
41 import org
.eclipse
.swt
.widgets
.Control
;
42 import org
.eclipse
.swt
.widgets
.Menu
;
43 import org
.eclipse
.swt
.widgets
.Tree
;
44 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
45 import org
.eclipse
.ui
.IWorkbenchPart
;
46 import org
.eclipse
.ui
.PartInitException
;
48 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
49 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
50 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
51 import eu
.etaxonomy
.cdm
.model
.description
.IDescribable
;
52 import eu
.etaxonomy
.cdm
.model
.description
.IndividualsAssociation
;
53 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
54 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
55 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
56 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
57 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditor
;
58 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
59 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateViewEditorInput
;
60 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
61 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
62 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainerTree
;
63 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
64 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
65 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
66 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
67 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
68 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmEditorViewPart
;
69 import eu
.etaxonomy
.taxeditor
.view
.detail
.DetailsViewPart
;
70 import eu
.etaxonomy
.taxeditor
.view
.supplementaldata
.SupplementalDataViewPart
;
73 * <p>DescriptiveViewPart class.</p>
76 * @created Jun 9, 2010
79 public class DescriptiveViewPart
extends AbstractCdmEditorViewPart
implements IPartContentHasDetails
, IPartContentHasSupplementalData
{
81 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.desc"{trunked}</code> */
82 public static final String ID
= "eu.etaxonomy.taxeditor.editor.view.descriptive";
84 protected TreeViewer viewer
;
87 * Maps {@link FeatureNodeContainerTree} to their corresponding {@link TaxonDescritpion}.<br>
88 * This serves as input for the {@link ITreeContentProvider} of the {@link TreeViewer}
90 protected Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
= new HashMap
<DescriptionBase
<?
>, FeatureNodeContainerTree
>();
92 protected ToggleDescriptionAction showAllElementsAction
;
94 protected ToggleDescriptionAction hideAllElementsAction
;
96 protected int dndOperations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
98 private DescriptiveContentProvider provider
;
102 public void createViewer(Composite parent
) {
103 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
104 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
105 provider
= new DescriptiveContentProvider(featureNodeContainerCache
);
106 viewer
.setContentProvider(provider
);
107 viewer
.setLabelProvider(new DescriptiveLabelProvider());
108 viewer
.setSorter(new DescriptiveViewerSorter());
109 viewer
.setAutoExpandLevel(2);
110 Transfer
[] transfers
= new Transfer
[] { DescriptionElementTransfer
.getInstance() };
111 viewer
.addDragSupport(dndOperations
, transfers
, new DescriptionElementDragListener(
113 viewer
.addDropSupport(dndOperations
, transfers
,
114 new DescriptionElementDropAdapter(viewer
));
116 // Propagate selection from viewer
117 getSite().setSelectionProvider(viewer
);
119 showAllElementsAction
= new ToggleDescriptionAction(false);
120 hideAllElementsAction
= new ToggleDescriptionAction(true);
122 // Add context menu to tree
128 // if(getEditor() != null){
129 // viewer.setInput(getEditor().getEditorInput());
132 viewer
.addDoubleClickListener(new IDoubleClickListener() {
134 public void doubleClick(DoubleClickEvent event
) {
135 //Open derivate editor when specimen description element is double clicked
136 TreeSelection selection
= (TreeSelection
) viewer
.getSelection();
137 Iterator iterator
= selection
.iterator();
138 Set
<UUID
> specimenUuids
= new HashSet
<UUID
>();
139 while(iterator
.hasNext()){
140 Object next
= iterator
.next();
141 if(next
instanceof IndividualsAssociation
){
142 SpecimenOrObservationBase
<?
> specimen
= ((IndividualsAssociation
)next
).getAssociatedSpecimenOrObservation();
144 specimenUuids
.add(specimen
.getUuid());
149 if(!specimenUuids
.isEmpty()){
150 EditorUtil
.open(new DerivateViewEditorInput(specimenUuids
));
152 } catch (PartInitException e
) {
153 MessagingUtils
.error(DescriptiveViewPart
.class, "Could not open Derivate Editor", e
);
157 //TODO: extend command to accept parameter to open editor
158 // String commandId = "eu.etaxonomy.taxeditor.editor.handler.openDerivateView";
159 // IHandlerService handlerService = (IHandlerService) AbstractUtility.getService(IHandlerService.class);
161 // handlerService.executeCommand(commandId, null);
162 // } catch (ExecutionException e) {
163 // MessagingUtils.error(DerivateSearchCompositeController.class, e);
164 // } catch (NotDefinedException e) {
165 // MessagingUtils.error(DerivateSearchCompositeController.class, e);
166 // } catch (NotEnabledException e) {
167 // MessagingUtils.error(DerivateSearchCompositeController.class, e);
168 // } catch (NotHandledException e) {
169 // MessagingUtils.error(DerivateSearchCompositeController.class, e);
177 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getInitialSelection()
181 protected ISelection
getInitialSelection() {
182 if(getEditor() != null){
183 return new StructuredSelection(getEditor().getEditorInput());
186 return super.getInitialSelection();
189 protected void createToolbar() {
190 IToolBarManager toolBarManager
= getViewSite().getActionBars().getToolBarManager();
191 toolBarManager
.add(showAllElementsAction
);
192 toolBarManager
.add(hideAllElementsAction
);
196 * TODO add to the views menu
198 protected void createMenu(){
199 MenuManager menuManager
= new MenuManager();
200 menuManager
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
202 getSite().registerContextMenu(menuManager
, viewer
);
204 // FIXME for some reason this is not working
205 menuManager
.add(showAllElementsAction
);
206 menuManager
.add(hideAllElementsAction
);
208 Control control
= viewer
.getControl();
209 Menu menu
= menuManager
.createContextMenu(control
);
211 control
.setMenu(menu
);
216 protected void selectionChanged_internal(IWorkbenchPart part
, ISelection selection
) {
217 if(AbstractUtility
.getActiveEditor() == null){
218 setPartName("Factual Data");
226 if(part
instanceof DetailsViewPart
|| part
instanceof SupplementalDataViewPart
){
227 // do not show empty page as these views are also used to edit the description selected in this view
230 // unpackage TreeNode of DerivateView
231 else if(part
instanceof DerivateView
){
232 TreeNode treeNodeOfSelection
= EditorUtil
.getTreeNodeOfSelection(selection
);
233 if(treeNodeOfSelection
!=null){
234 selection
= new StructuredSelection(treeNodeOfSelection
.getValue());
237 else if(part
instanceof ChecklistEditor
){
238 if(selection
instanceof StructuredSelection
&& ((IStructuredSelection
) selection
).getFirstElement() instanceof Taxon
){
239 Taxon taxon
= (Taxon
)((IStructuredSelection
) selection
).getFirstElement();
240 selection
= new StructuredSelection(taxon
);
243 if(selection
instanceof IStructuredSelection
244 && ((IStructuredSelection
) selection
).getFirstElement() instanceof IDescribable
<?
>
245 && part
instanceof IPartContentHasFactualData
&& !(((IStructuredSelection
) selection
).getFirstElement() instanceof SpecimenOrObservationBase
&& part
instanceof BulkEditor
)){
246 featureNodeContainerCache
.clear();
247 setPartName("Factual Data: " + ((IStructuredSelection
) selection
).getFirstElement());
248 showViewer(part
, (IStructuredSelection
) selection
);
252 setPartName("Factual Data");
260 * @created May 28, 2010
263 protected class ToggleDescriptionAction
extends Action
{
264 private final boolean expanded
;
266 public ToggleDescriptionAction(boolean expand
){
267 super(null, IAction
.AS_PUSH_BUTTON
);
269 setImageAndTooltip();
272 private void setImageAndTooltip(){
273 setImageDescriptor(new ImageDescriptor() {
275 public ImageData
getImageData() {
276 setText(expanded ?
"Collapse All" : "Expand All");
277 String resource
= expanded ? ImageResources
.COLLAPSE_ALL
: ImageResources
.EXPAND_ALL
;
278 return ImageResources
.getImage(resource
).getImageData();
282 String toolTipText
= expanded ?
"Collapse all" : "Show all factual data";
283 setToolTipText(toolTipText
);
289 viewer
.collapseAll();
293 setImageAndTooltip();
299 public boolean postOperation(CdmBase objectAffectedByOperation
) {
300 return super.postOperation(objectAffectedByOperation
);
305 public void changed(Object object
) {
306 for(FeatureNodeContainerTree containerTree
: featureNodeContainerCache
.values()){
307 containerTree
.buildTree();
311 if(object
instanceof DescriptionElementBase
){
312 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) object
;
313 DescriptionBase description
= descriptionElement
.getInDescription();
314 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
316 FeatureNodeContainer featureNodeContainer
= containerTree
.getFeatureNodeContainerForDescriptionElement(descriptionElement
);
317 viewer
.expandToLevel(featureNodeContainer
, 1);
322 StructuredSelection selection
= new StructuredSelection(object
);
323 viewer
.setSelection(selection
, true);
325 if(part
instanceof BulkEditor
&& !(object
instanceof SpecimenOrObservationBase
<?
>)){
326 ((BulkEditor
) part
).forceDirty();
329 super.changed(object
);
334 public Viewer
getViewer() {
344 public boolean onComplete() {
348 public void toggleShowOnlyIndividualAssociations(){
349 provider
.toggleShowOnlyIndividualAssociations();