1 package eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.contextMenu
;
3 import org
.eclipse
.jface
.action
.ContributionItem
;
4 import org
.eclipse
.jface
.action
.IContributionItem
;
5 import org
.eclipse
.jface
.util
.LocalSelectionTransfer
;
6 import org
.eclipse
.jface
.viewers
.ISelection
;
7 import org
.eclipse
.jface
.viewers
.TreeNode
;
8 import org
.eclipse
.swt
.SWT
;
9 import org
.eclipse
.swt
.events
.SelectionAdapter
;
10 import org
.eclipse
.swt
.events
.SelectionEvent
;
11 import org
.eclipse
.swt
.widgets
.Menu
;
12 import org
.eclipse
.swt
.widgets
.MenuItem
;
13 import org
.eclipse
.ui
.IEditorPart
;
14 import org
.eclipse
.ui
.IWorkbenchWindow
;
15 import org
.eclipse
.ui
.PlatformUI
;
16 import org
.eclipse
.ui
.actions
.CompoundContributionItem
;
18 import eu
.etaxonomy
.cdm
.api
.service
.molecular
.ISequenceService
;
19 import eu
.etaxonomy
.cdm
.model
.molecular
.Sequence
;
20 import eu
.etaxonomy
.cdm
.model
.molecular
.SingleRead
;
21 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
22 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
23 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
24 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
27 * Context menu for the creation of derivates in the derivate hierarchy.
30 public class SingleReadSequenceContextMenu
extends CompoundContributionItem
{
32 private enum CommandType
{
39 * @see org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
42 protected IContributionItem
[] getContributionItems() {
43 IContributionItem
[] contributionItems
= new IContributionItem
[] {
44 new ContributionItem() {
46 public void fill(Menu menu
, int index
) {
47 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
48 final IEditorPart activeEditor
= window
.getActivePage().getActiveEditor();
49 final ISelection selection
= window
.getActivePage().getSelection();
50 TreeNode selectedTreeNode
= EditorUtil
.getTreeNodeValueOfSelection(selection
);
51 TreeNode clipboardNode
= EditorUtil
.getTreeNodeValueOfSelection(LocalSelectionTransfer
.getTransfer().getSelection());
52 if(selectedTreeNode
!=null){
53 //context menu for Sequence
54 if(selectedTreeNode
.getValue() instanceof Sequence
55 && clipboardNode
!=null && clipboardNode
.getValue() instanceof SingleRead
){
56 MenuItem item
= new MenuItem(menu
, SWT
.NONE
);
57 item
.setText("Reuse SingleRead here");
58 item
.addSelectionListener(new WidgetSelectionListener(CommandType
.ADD_TO_SEQUENCE
));
60 else if(selectedTreeNode
.getValue() instanceof SingleRead
){
61 MenuItem item
= new MenuItem(menu
, SWT
.NONE
);
62 item
.setText("Reuse for other Sequence");
63 item
.addSelectionListener(new WidgetSelectionListener(CommandType
.COPY_TO_CLIPBOARD
));
64 if(activeEditor
instanceof DerivateView
65 && ((DerivateView
) activeEditor
).getMultiLinkSingleReads().contains(selectedTreeNode
.getValue())){
66 MenuItem unlinkItem
= new MenuItem(menu
, SWT
.NONE
);
67 unlinkItem
.setText("Remove from this Sequence");
68 unlinkItem
.addSelectionListener(new WidgetSelectionListener(CommandType
.REMOVE_FROM_SEQUENCE
));
75 return contributionItems
;
78 private class WidgetSelectionListener
extends SelectionAdapter
{
79 private final CommandType commandType
;
82 * @param selectedTreeNode
83 * @param clipboardNode
86 public WidgetSelectionListener(CommandType commandType
) {
87 this.commandType
= commandType
;
91 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
94 public void widgetSelected(SelectionEvent e
) {
95 IEditorPart activeEditor
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
96 ISelection clipBoardSelection
= LocalSelectionTransfer
.getTransfer().getSelection();
97 final ISelection selection
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
98 TreeNode clipBoardTreeNode
= EditorUtil
.getTreeNodeValueOfSelection(clipBoardSelection
);
99 TreeNode selectedTreeNode
= EditorUtil
.getTreeNodeValueOfSelection(selection
);
100 switch (commandType
) {
101 case REMOVE_FROM_SEQUENCE
:
102 if(activeEditor
.isDirty()){
103 AbstractUtility
.warningDialog("View has unsaved changes", this, "You need to save before performing this action");
106 if(selectedTreeNode
!=null && selectedTreeNode
.getParent()!=null &&
107 selectedTreeNode
.getValue() instanceof SingleRead
&& selectedTreeNode
.getParent().getValue() instanceof Sequence
) {
108 Sequence sequence
= (Sequence
) selectedTreeNode
.getParent().getValue();
109 sequence
.removeSingleRead((SingleRead
) selectedTreeNode
.getValue());
110 CdmStore
.getService(ISequenceService
.class).saveOrUpdate(sequence
);
111 if(activeEditor
instanceof DerivateView
) {
112 DerivateView derivateView
= (DerivateView
)activeEditor
;
113 derivateView
.getConversationHolder().commit();
114 derivateView
.refreshTree();
119 case ADD_TO_SEQUENCE
:
120 if(activeEditor
.isDirty()){
121 AbstractUtility
.warningDialog("View has unsaved changes", this, "You need to save before performing this action");
124 if(clipBoardTreeNode
!=null && clipBoardTreeNode
.getValue() instanceof SingleRead
125 && selectedTreeNode
!=null && selectedTreeNode
.getValue() instanceof Sequence
){
126 SingleRead singleRead
= (SingleRead
)clipBoardTreeNode
.getValue();
127 Sequence sequence
= (Sequence
)selectedTreeNode
.getValue();
128 sequence
.addSingleRead(singleRead
);
129 CdmStore
.getService(ISequenceService
.class).saveOrUpdate(sequence
);
130 if(activeEditor
instanceof DerivateView
) {
131 DerivateView derivateView
= (DerivateView
)activeEditor
;
132 derivateView
.getConversationHolder().commit();
133 derivateView
.refreshTree();
134 LocalSelectionTransfer
.getTransfer().setSelection(null);
138 case COPY_TO_CLIPBOARD
:
139 LocalSelectionTransfer
.getTransfer().setSelection(selection
);