Project

General

Profile

Download (5.11 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2014 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9
package eu.etaxonomy.taxeditor.editor.view.derivate.handler;
10

    
11
import javax.inject.Named;
12

    
13
import org.eclipse.e4.core.di.annotations.CanExecute;
14
import org.eclipse.e4.core.di.annotations.Execute;
15
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
16
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
17
import org.eclipse.e4.ui.services.IServiceConstants;
18

    
19
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
20
import eu.etaxonomy.cdm.api.service.ITaxonService;
21
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
22
import eu.etaxonomy.cdm.model.common.CdmBase;
23
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
24
import eu.etaxonomy.cdm.model.description.TaxonDescription;
25
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
26
import eu.etaxonomy.cdm.model.taxon.Taxon;
27
import eu.etaxonomy.taxeditor.editor.AppModelId;
28
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
29
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
30
import eu.etaxonomy.taxeditor.model.MessagingUtils;
31
import eu.etaxonomy.taxeditor.store.CdmStore;
32

    
33
public class CreateFieldUnitHandler {
34

    
35
	@Execute
36
	public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, MHandledMenuItem item) {
37
        DerivateView derivateView = (DerivateView) part.getObject();
38
        if(item.getElementId().equals(AppModelId.HANDLEDMENUITEM_EU_ETAXONOMY_TAXEDITOR_EDITOR_HANDLEDMENUITEM_CREATE_FIELD_UNIT_FOR_TAXON)){
39
            Object selectionInput = derivateView.getSearchController().getSelectedTaxon();
40
            if(selectionInput instanceof CdmBase && ((CdmBase) selectionInput).isInstanceOf(Taxon.class)){
41
                final Taxon taxon = HibernateProxyHelper.deproxy(CdmStore.getService(ITaxonService.class).load(((CdmBase)selectionInput).getUuid()), Taxon.class);
42
                if(derivateView.isDirty()){
43
                    MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
44
                    return;
45
                }
46

    
47
                FieldUnit fieldUnit = FieldUnit.NewInstance();
48
                fieldUnit = CdmBase.deproxy(CdmStore.getService(IOccurrenceService.class).save(fieldUnit), FieldUnit.class);
49

    
50
                IndividualsAssociation association = IndividualsAssociation.NewInstance(fieldUnit);
51
                TaxonDescription description;
52
                if(!taxon.getDescriptions().isEmpty()){
53
                    description = taxon.getDescriptions().iterator().next();
54
                }
55
                else{
56
                    description = TaxonDescription.NewInstance(taxon);
57
                }
58
                description.addElement(association);
59
                CdmStore.getService(ITaxonService.class).merge(taxon);
60

    
61

    
62
                derivateView.addFieldUnit(fieldUnit);
63
                derivateView.updateRootEntities();
64
                derivateView.refreshTree(fieldUnit);
65
            }
66
        }
67
        else{
68
            if(derivateView.isDirty()){
69
                MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
70
                return;
71
            }
72
            FieldUnit fieldUnit = FieldUnit.NewInstance();
73
            fieldUnit = CdmBase.deproxy(CdmStore.getService(IOccurrenceService.class).save(fieldUnit), FieldUnit.class);
74

    
75
            derivateView.addFieldUnit(fieldUnit);
76
            derivateView.updateRootEntities();
77
            derivateView.refreshTree(fieldUnit);
78
        }
79
	}
80

    
81
	@CanExecute
82
	public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart part, MHandledMenuItem item) {
83
	    DerivateView derivateView = (DerivateView) part.getObject();
84
	    boolean listenToSelectionChange = derivateView.getSearchController().getSelectedTaxon()!= null;
85
	    //boolean listenToSelectionChange = true;
86
	    setItemVisibility(item, listenToSelectionChange);
87
	    if(listenToSelectionChange &&
88
	            item.getElementId().equals(AppModelId.HANDLEDMENUITEM_EU_ETAXONOMY_TAXEDITOR_EDITOR_HANDLEDMENUITEM_CREATE_FIELD_UNIT_FOR_TAXON)){
89
	        Object selectionInput = derivateView.getSelectionInput();
90
	        if(selectionInput instanceof CdmBase && ((CdmBase) selectionInput).isInstanceOf(Taxon.class)){
91
	            final Taxon taxon = HibernateProxyHelper.deproxy(CdmStore.getService(ITaxonService.class).load(((CdmBase)selectionInput).getUuid()), Taxon.class);
92
	            item.setLabel(String.format(Messages.CreateFieldUnitContextMenu_CREATE_FIELD_UNIT_FOR, taxon.getName()));
93
	        }
94
	    }
95
	    return true;
96
	}
97

    
98
    private void setItemVisibility(MHandledMenuItem item, boolean isListeningToSelectionChange) {
99
        if(item.getElementId().equals(AppModelId.HANDLEDMENUITEM_EU_ETAXONOMY_TAXEDITOR_EDITOR_HANDLEDMENUITEM_CREATEFIELDUNIT)){
100
            item.setVisible(!isListeningToSelectionChange);
101
        }
102
        else{
103
            item.setVisible(isListeningToSelectionChange);
104
        }
105
    }
106
}
(9-9/17)