Project

General

Profile

Download (4 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.model.application.ui.basic.MPart;
17
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
18
import org.eclipse.e4.ui.services.IServiceConstants;
19
import org.eclipse.e4.ui.workbench.modeling.EPartService;
20
import org.eclipse.jface.viewers.IStructuredSelection;
21
import org.eclipse.swt.widgets.Display;
22
import org.eclipse.swt.widgets.Shell;
23

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

    
36
/**
37
 *
38
 * @author pplitzner
39
 * @since Aug 28, 2017
40
 *
41
 */
42
public class SwapSynonymAndAcceptedHandlerE4 implements IPostOperationEnabled {
43

    
44
    private TaxonNameEditorE4 editor;
45
    private Taxon taxon;
46
    private EPartService partService;
47
    private MPart activePart;
48

    
49
    @Execute
50
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
51
            @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
52
            @Named(IServiceConstants.ACTIVE_SHELL) Shell shell, MHandledMenuItem menuItem, EPartService partService) {
53

    
54
        this.activePart = activePart;
55
        this.partService = partService;
56

    
57
        editor = (TaxonNameEditorE4) activePart.getObject();
58

    
59
        Synonym synonym = (Synonym) selection.getFirstElement();
60

    
61
        // Force user to save taxon - not really necessary though, is it?
62
        if (!EditorUtil.forceUserSaveE4Editor(editor, shell)) {
63
            return;
64
        }
65

    
66
        SwapSynonymAndAcceptedOperation operation = new SwapSynonymAndAcceptedOperation(menuItem.getLocalizedLabel(),
67
                editor.getUndoContext(), editor.getTaxon(), synonym, this, editor.getEditorInput());
68

    
69
        AbstractUtility.executeOperation(operation);
70

    
71
    }
72

    
73
    @CanExecute
74
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
75
            MHandledMenuItem menuItem){
76
        boolean canExecute = false;
77
        Object selectedElement = selection.getFirstElement();
78
        canExecute =
79
                NameEditorMenuPropertyTester.isSynonym(selectedElement);
80
        menuItem.setVisible(canExecute);
81
        return canExecute;
82
    }
83

    
84
    @Override
85
    public boolean postOperation(CdmBase objectAffectedByOperation) {
86
        // Redraw existing editor
87
        // ((IPostOperationEnabled) editor).postOperation(null);
88

    
89
        editor.doSave(AbstractUtility.getMonitor());
90
        partService.hidePart(activePart);
91

    
92
        if (objectAffectedByOperation instanceof Taxon) {
93
            taxon = (Taxon) objectAffectedByOperation;
94
        }
95
        return true;
96
    }
97

    
98
    @Override
99
    public boolean onComplete() {
100
        Display display = Display.getCurrent();
101
        display.asyncExec(new Runnable() {
102
            @Override
103
            public void run() {
104
                try {
105
                    EditorUtil.openTaxonBaseE4(taxon.getUuid());
106
                } catch (Exception e) {
107
                    MessagingUtils.warningDialog(Messages.SwapSynonymAndAcceptedHandler_COULD_NOT_OPEN, this,
108
                            e.getMessage());
109
                }
110

    
111
            }
112
        });
113
        return true;
114
    }
115
}
(12-12/12)