Project

General

Profile

Download (4.46 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.taxon.Synonym;
28
import eu.etaxonomy.cdm.model.taxon.Taxon;
29
import eu.etaxonomy.taxeditor.editor.EditorUtil;
30
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
31
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
32
import eu.etaxonomy.taxeditor.editor.name.handler.NameEditorMenuPropertyTester;
33
import eu.etaxonomy.taxeditor.editor.name.operation.SwapSynonymAndAcceptedOperation;
34
import eu.etaxonomy.taxeditor.model.AbstractUtility;
35
import eu.etaxonomy.taxeditor.model.MessagingUtils;
36
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
37

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

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

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

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

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

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

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

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

    
77
        AbstractUtility.executeOperation(operation, sync);
78

    
79
    }
80

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

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

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

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

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

    
120
            }
121
        });
122
        return true;
123
    }
124
}
(17-17/17)