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.IWorkbenchPart;
|
15
|
import org.eclipse.ui.IWorkbenchWindow;
|
16
|
import org.eclipse.ui.PlatformUI;
|
17
|
import org.eclipse.ui.actions.CompoundContributionItem;
|
18
|
|
19
|
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
|
20
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
21
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
22
|
import eu.etaxonomy.cdm.model.molecular.Sequence;
|
23
|
import eu.etaxonomy.cdm.model.molecular.SingleRead;
|
24
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
25
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
26
|
import eu.etaxonomy.taxeditor.editor.Messages;
|
27
|
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
|
28
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
29
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
30
|
|
31
|
/**
|
32
|
* Context menu for the SingleReads in the derivate hierarchy.
|
33
|
*
|
34
|
*/
|
35
|
public class CreateFieldUnitContextMenu extends CompoundContributionItem {
|
36
|
|
37
|
private enum CommandType{
|
38
|
COPY_TO_CLIPBOARD,
|
39
|
REMOVE_FROM_SEQUENCE,
|
40
|
ADD_TO_SEQUENCE
|
41
|
}
|
42
|
|
43
|
@Override
|
44
|
protected IContributionItem[] getContributionItems() {
|
45
|
IContributionItem[] contributionItems = new IContributionItem[] {
|
46
|
new ContributionItem() {
|
47
|
@Override
|
48
|
public void fill(Menu menu, int index) {
|
49
|
final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
|
50
|
IWorkbenchPart activePart = window.getActivePage().getActivePart();
|
51
|
if(activePart instanceof DerivateView){
|
52
|
MenuItem item = new MenuItem(menu, SWT.NONE);
|
53
|
item.setText("Create FieldUnit");
|
54
|
if(((DerivateView) activePart).isListenToSelectionChange()){
|
55
|
Object selectionInput = ((DerivateView) activePart).getSelectionInput();
|
56
|
if(selectionInput instanceof CdmBase && ((CdmBase) selectionInput).isInstanceOf(Taxon.class)){
|
57
|
Taxon taxon = HibernateProxyHelper.deproxy(selectionInput, Taxon.class);
|
58
|
item.setText("Create FieldUnit (attach to "+taxon.getTitleCache()+")");
|
59
|
}
|
60
|
}
|
61
|
item.addSelectionListener(new WidgetSelectionListener(CommandType.ADD_TO_SEQUENCE));
|
62
|
}
|
63
|
|
64
|
|
65
|
final ISelection selection = window.getActivePage().getSelection();
|
66
|
TreeNode selectedTreeNode = EditorUtil.getTreeNodeOfSelection(selection);
|
67
|
TreeNode clipboardNode = EditorUtil.getTreeNodeOfSelection(LocalSelectionTransfer.getTransfer().getSelection());
|
68
|
if(selectedTreeNode!=null){
|
69
|
//context menu for Sequence
|
70
|
if(selectedTreeNode.getValue() instanceof Sequence
|
71
|
&& clipboardNode!=null && clipboardNode.getValue() instanceof SingleRead){
|
72
|
MenuItem item = new MenuItem(menu, SWT.NONE);
|
73
|
item.setText(Messages.SingleReadSequenceContextMenu_REUSE_SINGLE_READ_HERE);
|
74
|
item.addSelectionListener(new WidgetSelectionListener(CommandType.ADD_TO_SEQUENCE));
|
75
|
}
|
76
|
else if(selectedTreeNode.getValue() instanceof SingleRead){
|
77
|
MenuItem item = new MenuItem(menu, SWT.NONE);
|
78
|
item.setText(Messages.SingleReadSequenceContextMenu_REUSE_FOR_SEQUENCE);
|
79
|
item.addSelectionListener(new WidgetSelectionListener(CommandType.COPY_TO_CLIPBOARD));
|
80
|
if(activePart instanceof DerivateView
|
81
|
&& ((DerivateView) activePart).getMultiLinkSingleReads().contains(selectedTreeNode.getValue())){
|
82
|
MenuItem unlinkItem = new MenuItem(menu, SWT.NONE);
|
83
|
unlinkItem.setText(Messages.SingleReadSequenceContextMenu_REMOVE_FROM_SEQUENCE);
|
84
|
unlinkItem.addSelectionListener(new WidgetSelectionListener(CommandType.REMOVE_FROM_SEQUENCE));
|
85
|
}
|
86
|
}
|
87
|
}
|
88
|
}
|
89
|
}
|
90
|
};
|
91
|
return contributionItems;
|
92
|
}
|
93
|
|
94
|
private class WidgetSelectionListener extends SelectionAdapter{
|
95
|
private final CommandType commandType;
|
96
|
|
97
|
/**
|
98
|
* @param selectedTreeNode
|
99
|
* @param clipboardNode
|
100
|
* @param commandType
|
101
|
*/
|
102
|
public WidgetSelectionListener(CommandType commandType) {
|
103
|
this.commandType = commandType;
|
104
|
}
|
105
|
|
106
|
@Override
|
107
|
public void widgetSelected(SelectionEvent e) {
|
108
|
IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
|
109
|
ISelection clipBoardSelection = LocalSelectionTransfer.getTransfer().getSelection();
|
110
|
final ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
|
111
|
TreeNode clipBoardTreeNode = EditorUtil.getTreeNodeOfSelection(clipBoardSelection);
|
112
|
TreeNode selectedTreeNode = EditorUtil.getTreeNodeOfSelection(selection);
|
113
|
switch (commandType) {
|
114
|
case REMOVE_FROM_SEQUENCE:
|
115
|
if(activeEditor.isDirty()){
|
116
|
MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
|
117
|
return;
|
118
|
}
|
119
|
if(selectedTreeNode!=null && selectedTreeNode.getParent()!=null &&
|
120
|
selectedTreeNode.getValue() instanceof SingleRead && selectedTreeNode.getParent().getValue() instanceof Sequence) {
|
121
|
Sequence sequence = (Sequence) selectedTreeNode.getParent().getValue();
|
122
|
sequence.removeSingleRead((SingleRead) selectedTreeNode.getValue());
|
123
|
CdmStore.getService(ISequenceService.class).saveOrUpdate(sequence);
|
124
|
if(activeEditor instanceof DerivateView) {
|
125
|
DerivateView derivateView = (DerivateView)activeEditor;
|
126
|
derivateView.getConversationHolder().commit();
|
127
|
derivateView.refreshTree();
|
128
|
}
|
129
|
}
|
130
|
|
131
|
break;
|
132
|
case ADD_TO_SEQUENCE:
|
133
|
if(activeEditor.isDirty()){
|
134
|
MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
|
135
|
return;
|
136
|
}
|
137
|
if(clipBoardTreeNode!=null && clipBoardTreeNode.getValue() instanceof SingleRead
|
138
|
&& selectedTreeNode!=null && selectedTreeNode.getValue() instanceof Sequence){
|
139
|
SingleRead singleRead = (SingleRead)clipBoardTreeNode.getValue();
|
140
|
Sequence sequence = (Sequence)selectedTreeNode.getValue();
|
141
|
sequence.addSingleRead(singleRead);
|
142
|
CdmStore.getService(ISequenceService.class).saveOrUpdate(sequence);
|
143
|
if(activeEditor instanceof DerivateView) {
|
144
|
DerivateView derivateView = (DerivateView)activeEditor;
|
145
|
derivateView.getConversationHolder().commit();
|
146
|
derivateView.refreshTree();
|
147
|
LocalSelectionTransfer.getTransfer().setSelection(null);
|
148
|
}
|
149
|
}
|
150
|
break;
|
151
|
case COPY_TO_CLIPBOARD:
|
152
|
LocalSelectionTransfer.getTransfer().setSelection(selection);
|
153
|
break;
|
154
|
}
|
155
|
}
|
156
|
}
|
157
|
}
|