Project

General

Profile

Download (8.8 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
package eu.etaxonomy.taxeditor.editor.name.e4.handler;
10

    
11
import java.util.HashSet;
12
import java.util.Set;
13
import java.util.UUID;
14

    
15
import javax.inject.Named;
16

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

    
30
import eu.etaxonomy.cdm.model.metadata.SecReferenceHandlingSwapEnum;
31
import eu.etaxonomy.cdm.model.reference.Reference;
32
import eu.etaxonomy.cdm.model.taxon.Synonym;
33
import eu.etaxonomy.cdm.model.taxon.Taxon;
34
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
35
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
36
import eu.etaxonomy.taxeditor.editor.EditorUtil;
37
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
38
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonEditor;
39
import eu.etaxonomy.taxeditor.editor.name.handler.NameEditorMenuPropertyTester;
40
import eu.etaxonomy.taxeditor.editor.name.operation.SwapSynonymAndAcceptedOperation;
41
import eu.etaxonomy.taxeditor.event.EventUtility;
42
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
43
import eu.etaxonomy.taxeditor.model.AbstractUtility;
44
import eu.etaxonomy.taxeditor.model.MessagingUtils;
45
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
46
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
47
import eu.etaxonomy.taxeditor.ui.dialog.selection.ReferenceSelectionDialog;
48

    
49
/**
50
 * @author pplitzner
51
 * @since Aug 28, 2017
52
 */
53
public class SwapSynonymAndAcceptedHandler implements IPostOperationEnabled {
54

    
55
    protected TaxonEditor editor;
56
    protected Taxon taxon;
57
    protected EPartService partService;
58
    protected MPart activePart;
59
    protected MApplication application;
60
    protected EModelService modelService;
61
    protected boolean isSetNameInSource = false;
62

    
63
    @Execute
64
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
65
            @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
66
            @Named(IServiceConstants.ACTIVE_SHELL) Shell shell, MHandledMenuItem menuItem, EPartService partService,
67
            EModelService modelService, MApplication application,
68
            UISynchronize sync) {
69

    
70
        this.activePart = activePart;
71
        this.modelService = modelService;
72
        this.application = application;
73
        this.partService = partService;
74

    
75
        editor = (TaxonEditor) activePart.getObject();
76

    
77
        Synonym synonym = (Synonym) selection.getFirstElement();
78

    
79
        // Force user to save taxon - not really necessary though, is it?
80
        if (!EditorUtil.forceUserSaveE4Editor(editor, shell)) {
81
            return;
82
        }
83
        TaxonNode parentNode = editor.getTaxonNode()!= null? editor.getTaxonNode().getParent(): null;
84
        Boolean isParentPublish = null;
85
        if (parentNode != null){
86
            if (parentNode.getTaxon() != null){
87
                isParentPublish = parentNode.getTaxon().isPublish();
88
            }
89
        }
90

    
91
        if (synonym.isPublish() != editor.getTaxon().isPublish() || (isParentPublish != null && (isParentPublish.booleanValue() != synonym.isPublish() || isParentPublish.booleanValue() != synonym.isPublish()))){
92
            MessagingUtils.warningDialog("Publish flag", this, Messages.SwapSynonymToAcceptedHandler_Different_Publish_Flag);
93
        }
94

    
95
        Reference secRefAccepted = editor.getTaxon().getSec();
96
        Reference secRefSynonym = synonym.getSec();
97
        SecReferenceHandlingSwapEnum secHandling = PreferencesUtil.getSecReferenceHandlingSwapPreference();
98
        UUID newSecAcc = secRefAccepted != null? secRefAccepted.getUuid(): null;
99
        UUID newSecSyn = secRefSynonym != null? secRefSynonym.getUuid(): null;
100
        if (secHandling.equals(SecReferenceHandlingSwapEnum.AlwaysSelect) ||
101
                ((secHandling.equals(SecReferenceHandlingSwapEnum.KeepOrSelect) || secHandling.equals(SecReferenceHandlingSwapEnum.KeepOrWarn))
102
                        && (secRefAccepted != null && secRefSynonym != null &&!secRefAccepted.getUuid().equals(secRefSynonym.getUuid()))
103
                        || secRefAccepted != secRefSynonym)){
104
            if (secHandling.equals(SecReferenceHandlingSwapEnum.KeepOrWarn)){
105
                boolean doContinue = MessagingUtils.confirmDialog("Secundum Reference differ", Messages.ChangeSynonymToAcceptedHandler_Different_Secundum_references);
106
                if (!doContinue){
107
                   return;
108
                }
109
            }else{
110

    
111
                int result = MessagingUtils.confirmDialog(Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Handling_title,
112
                        Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Handling_message,
113
                        new String[]{Messages.SwapSynonymAndAcceptedHandler_Select_Sec_Reference_Select, Messages.SwapSynonymAndAcceptedHandler_Select_Sec_Reference_Delete});
114
                if (result == 0){
115
                    Set<Reference> oldSecs = new HashSet<>();
116
                    oldSecs.add(secRefAccepted);
117
                    oldSecs.add(secRefSynonym);
118
                    Reference sec = ReferenceSelectionDialog.select(shell, Messages.SwapSynonymAndAcceptedHandler_Select_Sec_Reference_Taxon,null, oldSecs);
119
                    newSecAcc = sec != null? sec.getUuid(): null;
120
                    sec = ReferenceSelectionDialog.select(shell, Messages.SwapSynonymAndAcceptedHandler_Select_Sec_Reference_Synonym,null, oldSecs);
121
                    newSecSyn = sec != null? sec.getUuid(): null;
122
                } else if (result == 1){
123
                    newSecAcc = null;
124
                    newSecSyn = null;
125
                } else{
126
                    return ;
127
                }
128
            }
129
        }
130
        if (secHandling.equals(SecReferenceHandlingSwapEnum.AlwaysDelete)){
131
            boolean doContinue = MessagingUtils.confirmDialog("Secundum Reference Delete", Messages.SwapSynonymToAcceptedHandler_Delete_Secundum);
132
            if (!doContinue){
133
                return;
134
            }
135

    
136
            newSecAcc = null;
137
            newSecSyn = null;
138

    
139
        }
140

    
141
        SwapSynonymAndAcceptedOperation operation = new SwapSynonymAndAcceptedOperation(menuItem.getLocalizedLabel(),
142
                editor.getUndoContext(), editor.getTaxon(), synonym, this, editor.getEditorInput(), isSetNameInSource, secHandling, newSecAcc, newSecSyn);
143

    
144
        AbstractUtility.executeOperation(operation, sync);
145
    }
146

    
147
    @CanExecute
148
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
149
            MHandledMenuItem menuItem){
150
        boolean canExecute = false;
151
        if(selection.size()==1){
152
            Object selectedElement = selection.getFirstElement();
153
            canExecute = NameEditorMenuPropertyTester.isSynonym(selectedElement);
154
        }
155
        menuItem.setVisible(canExecute);
156
        return canExecute;
157
    }
158

    
159
    @Override
160
    public boolean postOperation(Object objectAffectedByOperation) {
161
        partService.hidePart(activePart);
162
        if (objectAffectedByOperation instanceof Taxon) {
163
            taxon = (Taxon) objectAffectedByOperation;
164
        }
165
        return true;
166
    }
167

    
168
    @Override
169
    public boolean onComplete() {
170
        Display display = Display.getCurrent();
171
        display.asyncExec(new Runnable() {
172
            @Override
173
            public void run() {
174
                try {
175
                    TaxonNode node;
176

    
177
                    if (!taxon.getTaxonNodes().isEmpty()) {
178
                        node = taxon.getTaxonNodes().iterator().next();
179
                    } else {
180
                        node = null;
181
                    }
182
                    if (node != null){
183
                        EditorUtil.openTaxonNodeE4((node).getUuid(), modelService, partService, application);
184
                        for (TaxonNode nodetoUpdate: taxon.getTaxonNodes()){
185
                            EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAVIGATOR, new TaxonNodeDto(nodetoUpdate.getParent()));
186
                        }
187
                    }
188
                } catch (Exception e) {
189
                    MessagingUtils.warningDialog(Messages.SwapSynonymAndAcceptedHandler_COULD_NOT_OPEN, this,
190
                            e.getMessage());
191
                }
192
            }
193
        });
194
        return true;
195
    }
196
}
(14-14/15)