Project

General

Profile

Download (4.48 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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

    
10
package eu.etaxonomy.taxeditor.editor.name.e4.handler;
11

    
12
import javax.inject.Named;
13

    
14
import org.eclipse.e4.core.di.annotations.CanExecute;
15
import org.eclipse.e4.core.di.annotations.Execute;
16
import org.eclipse.e4.ui.di.UISynchronize;
17
import org.eclipse.e4.ui.model.application.MApplication;
18
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
19
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
20
import org.eclipse.e4.ui.services.IServiceConstants;
21
import org.eclipse.e4.ui.workbench.modeling.EModelService;
22
import org.eclipse.e4.ui.workbench.modeling.EPartService;
23
import org.eclipse.jface.viewers.IStructuredSelection;
24
import org.eclipse.swt.widgets.Display;
25
import org.eclipse.swt.widgets.Shell;
26

    
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.taxon.Synonym;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30
import eu.etaxonomy.taxeditor.editor.EditorUtil;
31
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
32
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
33
import eu.etaxonomy.taxeditor.editor.name.handler.NameEditorMenuPropertyTester;
34
import eu.etaxonomy.taxeditor.editor.name.operation.SwapSynonymAndAcceptedOperation;
35
import eu.etaxonomy.taxeditor.model.AbstractUtility;
36
import eu.etaxonomy.taxeditor.model.MessagingUtils;
37
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
38

    
39
/**
40
 *
41
 * @author pplitzner
42
 * @since Aug 28, 2017
43
 *
44
 */
45
public class SwapSynonymAndAcceptedHandlerE4 implements IPostOperationEnabled {
46

    
47
    private TaxonNameEditorE4 editor;
48
    private Taxon taxon;
49
    private EPartService partService;
50
    private MPart activePart;
51
    private MApplication application;
52
    private EModelService modelService;
53

    
54
    @Execute
55
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
56
            @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
57
            @Named(IServiceConstants.ACTIVE_SHELL) Shell shell, MHandledMenuItem menuItem, EPartService partService,
58
            EModelService modelService, MApplication application,
59
            UISynchronize sync) {
60

    
61
        this.activePart = activePart;
62
        this.modelService = modelService;
63
        this.application = application;
64
        this.partService = partService;
65

    
66
        editor = (TaxonNameEditorE4) activePart.getObject();
67

    
68
        Synonym synonym = (Synonym) selection.getFirstElement();
69

    
70
        // Force user to save taxon - not really necessary though, is it?
71
        if (!EditorUtil.forceUserSaveE4Editor(editor, shell)) {
72
            return;
73
        }
74

    
75
        SwapSynonymAndAcceptedOperation operation = new SwapSynonymAndAcceptedOperation(menuItem.getLocalizedLabel(),
76
                editor.getUndoContext(), editor.getTaxon(), synonym, this, editor.getEditorInput());
77

    
78
        AbstractUtility.executeOperation(operation, sync);
79

    
80
    }
81

    
82
    @CanExecute
83
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
84
            MHandledMenuItem menuItem){
85
        boolean canExecute = false;
86
        if(selection.size()==1){
87
            Object selectedElement = selection.getFirstElement();
88
            canExecute = NameEditorMenuPropertyTester.isSynonym(selectedElement);
89
        }
90
        menuItem.setVisible(canExecute);
91
        return canExecute;
92
    }
93

    
94
    @Override
95
    public boolean postOperation(CdmBase objectAffectedByOperation) {
96
        // Redraw existing editor
97
        // ((IPostOperationEnabled) editor).postOperation(null);
98

    
99
        editor.save(AbstractUtility.getMonitor());
100
        partService.hidePart(activePart);
101

    
102
        if (objectAffectedByOperation instanceof Taxon) {
103
            taxon = (Taxon) objectAffectedByOperation;
104
        }
105
        return true;
106
    }
107

    
108
    @Override
109
    public boolean onComplete() {
110
        Display display = Display.getCurrent();
111
        display.asyncExec(new Runnable() {
112
            @Override
113
            public void run() {
114
                try {
115
                    EditorUtil.openTaxonBaseE4(taxon.getUuid(), modelService, partService, application);
116
                } catch (Exception e) {
117
                    MessagingUtils.warningDialog(Messages.SwapSynonymAndAcceptedHandler_COULD_NOT_OPEN, this,
118
                            e.getMessage());
119
                }
120

    
121
            }
122
        });
123
        return true;
124
    }
125
}
(16-16/16)