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.swt.SWT;
|
6
|
import org.eclipse.swt.events.SelectionAdapter;
|
7
|
import org.eclipse.swt.events.SelectionEvent;
|
8
|
import org.eclipse.swt.widgets.Menu;
|
9
|
import org.eclipse.swt.widgets.MenuItem;
|
10
|
import org.eclipse.ui.IWorkbenchPart;
|
11
|
import org.eclipse.ui.IWorkbenchWindow;
|
12
|
import org.eclipse.ui.PlatformUI;
|
13
|
import org.eclipse.ui.actions.CompoundContributionItem;
|
14
|
|
15
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
16
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
17
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
18
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
19
|
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
|
20
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
21
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
22
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
23
|
import eu.etaxonomy.taxeditor.editor.Messages;
|
24
|
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
|
25
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
26
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
27
|
|
28
|
/**
|
29
|
* Context menu for the SingleReads in the derivate hierarchy.
|
30
|
*
|
31
|
*/
|
32
|
public class CreateFieldUnitContextMenu extends CompoundContributionItem {
|
33
|
|
34
|
private enum CommandType {
|
35
|
COPY_TO_CLIPBOARD, REMOVE_FROM_SEQUENCE, ADD_TO_SEQUENCE
|
36
|
}
|
37
|
|
38
|
@Override
|
39
|
protected IContributionItem[] getContributionItems() {
|
40
|
IContributionItem[] contributionItems = new IContributionItem[] {
|
41
|
new ContributionItem() {
|
42
|
@Override
|
43
|
public void fill(Menu menu, int index) {
|
44
|
final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
|
45
|
IWorkbenchPart activePart = window.getActivePage().getActivePart();
|
46
|
if(activePart instanceof DerivateView){
|
47
|
final DerivateView derivateView = (DerivateView) activePart;
|
48
|
if(derivateView.isListenToSelectionChange()){
|
49
|
Object selectionInput = derivateView.getSelectionInput();
|
50
|
if(selectionInput instanceof CdmBase && ((CdmBase) selectionInput).isInstanceOf(Taxon.class)){
|
51
|
final Taxon taxon = HibernateProxyHelper.deproxy(CdmStore.getService(ITaxonService.class).load(((CdmBase)selectionInput).getUuid()), Taxon.class);
|
52
|
MenuItem item = new MenuItem(menu, SWT.NONE);
|
53
|
item.setText(String.format(Messages.CreateFieldUnitContextMenu_CREATE_FIELD_UNIT_FOR, taxon.getName()));
|
54
|
item.addSelectionListener(new SelectionAdapter() {
|
55
|
@Override
|
56
|
public void widgetSelected(SelectionEvent e) {
|
57
|
if(derivateView.isDirty()){
|
58
|
MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
|
59
|
return;
|
60
|
}
|
61
|
|
62
|
FieldUnit fieldUnit = FieldUnit.NewInstance();
|
63
|
fieldUnit = CdmBase.deproxy(CdmStore.getService(IOccurrenceService.class).save(fieldUnit), FieldUnit.class);
|
64
|
IndividualsAssociation association = IndividualsAssociation.NewInstance(fieldUnit);
|
65
|
TaxonDescription description;
|
66
|
if(!taxon.getDescriptions().isEmpty()){
|
67
|
description = taxon.getDescriptions().iterator().next();
|
68
|
}
|
69
|
else{
|
70
|
description = TaxonDescription.NewInstance(taxon);
|
71
|
}
|
72
|
description.addElement(association);
|
73
|
CdmStore.getService(ITaxonService.class).merge(taxon);
|
74
|
|
75
|
|
76
|
derivateView.getConversationHolder().commit();
|
77
|
derivateView.addFieldUnit(fieldUnit);
|
78
|
derivateView.refreshTree(fieldUnit);
|
79
|
}
|
80
|
});
|
81
|
}
|
82
|
}
|
83
|
else{
|
84
|
MenuItem item = new MenuItem(menu, SWT.NONE);
|
85
|
item.setText(Messages.CreateFieldUnitContextMenu_CREATE_FIELD_UNIT);
|
86
|
item.addSelectionListener(new SelectionAdapter() {
|
87
|
@Override
|
88
|
public void widgetSelected(SelectionEvent e) {
|
89
|
if(derivateView.isDirty()){
|
90
|
MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
|
91
|
return;
|
92
|
}
|
93
|
FieldUnit fieldUnit = FieldUnit.NewInstance();
|
94
|
fieldUnit = CdmBase.deproxy(CdmStore.getService(IOccurrenceService.class).save(fieldUnit), FieldUnit.class);
|
95
|
derivateView.getConversationHolder().commit();
|
96
|
derivateView.addFieldUnit(fieldUnit);
|
97
|
derivateView.updateRootEntities();
|
98
|
derivateView.refreshTree(fieldUnit);
|
99
|
}
|
100
|
});
|
101
|
}
|
102
|
}
|
103
|
}
|
104
|
}
|
105
|
};
|
106
|
return contributionItems;
|
107
|
}
|
108
|
}
|
109
|
|