589869ab44f37098ede72b244374fb0586d85a94
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / derivate / contextMenu / CreateFieldUnitContextMenu.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.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.hibernate.HibernateProxyHelper;
17 import eu.etaxonomy.cdm.model.common.CdmBase;
18 import eu.etaxonomy.cdm.model.description.Feature;
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.view.derivate.DerivateView;
24 import eu.etaxonomy.taxeditor.model.MessagingUtils;
25 import eu.etaxonomy.taxeditor.store.CdmStore;
26
27 /**
28 * Context menu for the SingleReads in the derivate hierarchy.
29 *
30 */
31 public class CreateFieldUnitContextMenu extends CompoundContributionItem {
32
33 private enum CommandType {
34 COPY_TO_CLIPBOARD, REMOVE_FROM_SEQUENCE, ADD_TO_SEQUENCE
35 }
36
37 @Override
38 protected IContributionItem[] getContributionItems() {
39 IContributionItem[] contributionItems = new IContributionItem[] {
40 new ContributionItem() {
41 @Override
42 public void fill(Menu menu, int index) {
43 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
44 IWorkbenchPart activePart = window.getActivePage().getActivePart();
45 if(activePart instanceof DerivateView){
46 final DerivateView derivateView = (DerivateView) activePart;
47 if(derivateView.isListenToSelectionChange()){
48 Object selectionInput = derivateView.getSelectionInput();
49 if(selectionInput instanceof CdmBase && ((CdmBase) selectionInput).isInstanceOf(Taxon.class)){
50 final Taxon taxon = HibernateProxyHelper.deproxy(selectionInput, Taxon.class);
51 MenuItem item = new MenuItem(menu, SWT.NONE);
52 item.setText("Create FieldUnit (attach to "+taxon.getTitleCache()+")");
53 item.addSelectionListener(new SelectionAdapter() {
54 @Override
55 public void widgetSelected(SelectionEvent e) {
56 if(derivateView.isDirty()){
57 MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
58 return;
59 }
60 FieldUnit fieldUnit = FieldUnit.NewInstance();
61 fieldUnit = CdmBase.deproxy(CdmStore.getService(IOccurrenceService.class).save(fieldUnit), FieldUnit.class);
62 IndividualsAssociation association = IndividualsAssociation.NewInstance(fieldUnit);
63 association.setFeature(Feature.SPECIMEN());
64 TaxonDescription description;
65 if(!taxon.getDescriptions().isEmpty()){
66 description = taxon.getDescriptions().iterator().next();
67 }
68 else{
69 description = TaxonDescription.NewInstance(taxon);
70 }
71 description.addElement(IndividualsAssociation.NewInstance(fieldUnit));
72
73 derivateView.getConversationHolder().commit();
74 derivateView.addFieldUnit(fieldUnit);
75 derivateView.refreshTree();
76 }
77 });
78 }
79 }
80 else{
81 MenuItem item = new MenuItem(menu, SWT.NONE);
82 item.setText("Create FieldUnit");
83 item.addSelectionListener(new SelectionAdapter() {
84 @Override
85 public void widgetSelected(SelectionEvent e) {
86 if(derivateView.isDirty()){
87 MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
88 return;
89 }
90 FieldUnit fieldUnit = FieldUnit.NewInstance();
91 fieldUnit = CdmBase.deproxy(CdmStore.getService(IOccurrenceService.class).save(fieldUnit), FieldUnit.class);
92 derivateView.getConversationHolder().commit();
93 derivateView.addFieldUnit(fieldUnit);
94 derivateView.refreshTree();
95 }
96 });
97 }
98 }
99 }
100 }
101 };
102 return contributionItems;
103 }
104 }
105