Project

General

Profile

Download (6.45 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
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.di.UISynchronize;
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.jface.viewers.IStructuredSelection;
20
import org.eclipse.swt.widgets.Shell;
21

    
22
import eu.etaxonomy.cdm.api.service.DeleteResult;
23
import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
24
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.model.taxon.Synonym;
27
import eu.etaxonomy.cdm.model.taxon.Taxon;
28
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
29
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
30
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
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.DeleteMisapplicationOperation;
35
import eu.etaxonomy.taxeditor.editor.name.operation.DeleteSynonymOperation;
36
import eu.etaxonomy.taxeditor.editor.name.operation.DeleteTaxonBaseOperation;
37
import eu.etaxonomy.taxeditor.model.AbstractUtility;
38
import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
39
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
40
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
41
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
42

    
43
/**
44
 *
45
 * @author pplitzner
46
 * @since Aug 28, 2017
47
 *
48
 */
49
public class DeleteTaxonBaseHandlerE4 implements IPostOperationEnabled {
50

    
51
    protected TaxonNameEditorE4 editor;
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,
57
	        MHandledMenuItem menuItem,
58
            UISynchronize sync) {
59

    
60
	    editor = (TaxonNameEditorE4) activePart.getObject();
61

    
62
	    doExecute(menuItem.getLocalizedLabel(), shell, editor, selection.getFirstElement(), sync);
63

    
64
	}
65

    
66
	protected void doExecute(String commandName, Shell shell, TaxonNameEditorE4 editor, Object selectedElement,
67
            UISynchronize sync) {
68
		AbstractPostOperation operation = null;
69

    
70
		if (selectedElement instanceof TaxonBase){
71
		    if (((TaxonBase)selectedElement).getId() == 0){
72
		        if (selectedElement instanceof Taxon && ((Taxon) selectedElement).isMisapplication()) {
73
                    editor.getTaxon().removeTaxon((Taxon)selectedElement, TaxonRelationshipType.MISAPPLIED_NAME_FOR());
74

    
75
                } else if (selectedElement instanceof Synonym){
76
                    editor.getTaxon().removeSynonym((Synonym)selectedElement);
77
                }
78
		        editor.redraw();
79
		        return;
80

    
81
	        }
82

    
83
		}
84

    
85
		// synonym
86
		if(selectedElement instanceof Synonym){
87
		    SynonymDeletionConfigurator deleteConfig = new SynonymDeletionConfigurator();
88
		    if(! DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfig, shell, Messages.DeleteTaxonBaseHandler_CONFIRM_DELETION,  Messages.DeleteTaxonBaseHandler_REALLY_DELETE_SYNONYM)){
89
		           return ;
90
            }
91
			operation = new DeleteSynonymOperation(commandName, editor.getUndoContext(), deleteConfig, editor.getTaxon(), (Synonym) selectedElement,this, editor, editor.getEditorInput());
92

    
93
		}
94
		// misapplication
95
		else if(selectedElement instanceof Taxon && ((Taxon) selectedElement).isMisapplication()){
96
		    TaxonDeletionConfigurator deleteConfig = new TaxonDeletionConfigurator();
97
		    if(! DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfig, shell, Messages.DeleteTaxonBaseHandler_CONFIRM_DELETION,  Messages.DeleteTaxonBaseHandler_REALLY_DELETE_MISAPPLICATION)){
98
		        return ;
99
		    }
100
			operation = new DeleteMisapplicationOperation(commandName, editor.getUndoContext(),  deleteConfig, editor.getTaxon(), (Taxon) selectedElement,this, editor, editor.getEditorInput());
101
		} else {
102
			throw new IllegalArgumentException(Messages.DeleteTaxonBaseHandler_ELEMENT_MUST_BE_SYNONYM_MISAPP_CONCEPT);
103
		}
104

    
105
		AbstractUtility.executeOperation(operation, sync);
106
		DeleteResult result = ((DeleteTaxonBaseOperation)operation).getResult();
107
		if (result != null){
108
    		if (result.isError()){
109
                DeleteResultMessagingUtils.messageDialogWithDetails(result, Messages.DeleteDerivateOperation_DELETE_FAILED, TaxeditorEditorPlugin.PLUGIN_ID);
110
            } else if (selectedElement instanceof Synonym){
111
                this.editor.redraw();
112
    		    if (!result.getExceptions().isEmpty()){
113
                    DeleteResultMessagingUtils.messageDialogWithDetails(result, Messages.DeleteTaxonBaseHandler_DELETE_SYNONYM_SUCCESSFULL_BUT_REMAINING_RELATED_OBJECTS, TaxeditorEditorPlugin.PLUGIN_ID);
114
                }
115
    		} else if (selectedElement instanceof Taxon && !result.getExceptions().isEmpty()){
116
    	        this.editor.redraw();
117
                DeleteResultMessagingUtils.messageDialogWithDetails(result, Messages.DeleteTaxonBaseHandler_DELETE_MISAPPLIEDNAME_SUCCESSFULL_BUT_REMAINING_RELATED_OBJECTS, TaxeditorEditorPlugin.PLUGIN_ID);
118
    		}
119
		}
120
	}
121

    
122
    @CanExecute
123
    public boolean canExecute(
124
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
125
            @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
126
            MHandledMenuItem menuItem){
127
        boolean canExecute = false;
128
        Object selectedElement = selection.getFirstElement();
129
        canExecute =
130
                NameEditorMenuPropertyTester.isSynonym(selectedElement)
131
                || NameEditorMenuPropertyTester.isMisapplication(selectedElement)
132
                || NameEditorMenuPropertyTester.isRelatedConcept(selectedElement);
133
        menuItem.setVisible(canExecute);
134
        return canExecute;
135
    }
136

    
137
	@Override
138
	public boolean postOperation(CdmBase objectAffectedByOperation) {
139
	    editor.redraw();
140
		return true;
141
	}
142

    
143
	@Override
144
	public boolean onComplete() {
145

    
146
		return false;
147
	}
148

    
149
}
(9-9/12)