b3164a4586ad66bf81e18c829ad23aff663db6ef
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / derivate / contextMenu / SingleReadSequenceContextMenu.java
1 package eu.etaxonomy.taxeditor.editor.view.derivate.contextMenu;
2
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;
17
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.MessagingUtils;
24 import eu.etaxonomy.taxeditor.store.CdmStore;
25
26 /**
27 * Context menu for the SingleReads in the derivate hierarchy.
28 *
29 */
30 public class SingleReadSequenceContextMenu extends CompoundContributionItem {
31
32 private enum CommandType{
33 COPY_TO_CLIPBOARD,
34 REMOVE_FROM_SEQUENCE,
35 ADD_TO_SEQUENCE
36 }
37
38 /* (non-Javadoc)
39 * @see org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
40 */
41 @Override
42 protected IContributionItem[] getContributionItems() {
43 IContributionItem[] contributionItems = new IContributionItem[] {
44 new ContributionItem() {
45 @Override
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.getTreeNodeOfSelection(selection);
51 TreeNode clipboardNode = EditorUtil.getTreeNodeOfSelection(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));
59 }
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));
69 }
70 }
71 }
72 }
73 }
74 };
75 return contributionItems;
76 }
77
78 private class WidgetSelectionListener extends SelectionAdapter{
79 private final CommandType commandType;
80
81 /**
82 * @param selectedTreeNode
83 * @param clipboardNode
84 * @param commandType
85 */
86 public WidgetSelectionListener(CommandType commandType) {
87 this.commandType = commandType;
88 }
89
90 /* (non-Javadoc)
91 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
92 */
93 @Override
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.getTreeNodeOfSelection(clipBoardSelection);
99 TreeNode selectedTreeNode = EditorUtil.getTreeNodeOfSelection(selection);
100 switch (commandType) {
101 case REMOVE_FROM_SEQUENCE:
102 if(activeEditor.isDirty()){
103 MessagingUtils.warningDialog("View has unsaved changes", this, "You need to save before performing this action");
104 return;
105 }
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();
115 }
116 }
117
118 break;
119 case ADD_TO_SEQUENCE:
120 if(activeEditor.isDirty()){
121 MessagingUtils.warningDialog("View has unsaved changes", this, "You need to save before performing this action");
122 return;
123 }
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);
135 }
136 }
137 break;
138 case COPY_TO_CLIPBOARD:
139 LocalSelectionTransfer.getTransfer().setSelection(selection);
140 break;
141 }
142 }
143 }
144 }