Project

General

Profile

Download (9.93 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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.navigation.navigator.e4.handler;
10

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

    
16
import javax.inject.Named;
17

    
18
import org.eclipse.core.commands.operations.AbstractOperation;
19
import org.eclipse.core.runtime.IStatus;
20
import org.eclipse.core.runtime.Status;
21
import org.eclipse.e4.core.di.annotations.CanExecute;
22
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
23
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
24
import org.eclipse.e4.ui.services.IServiceConstants;
25
import org.eclipse.jface.viewers.IStructuredSelection;
26
import org.eclipse.jface.viewers.TreeSelection;
27
import org.eclipse.swt.widgets.Shell;
28

    
29
import eu.etaxonomy.cdm.api.service.IClassificationService;
30
import eu.etaxonomy.cdm.model.metadata.SecReferenceHandlingEnum;
31
import eu.etaxonomy.cdm.model.reference.Reference;
32
import eu.etaxonomy.cdm.model.taxon.Classification;
33
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
34
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
35
import eu.etaxonomy.taxeditor.editor.EditorUtil;
36
import eu.etaxonomy.taxeditor.model.MessagingUtils;
37
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
38
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
39
import eu.etaxonomy.taxeditor.navigation.navigator.operation.ChangeAcceptedTaxonToSynonymOperation;
40
import eu.etaxonomy.taxeditor.operation.e4.CdmHandlerE4;
41
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
42
import eu.etaxonomy.taxeditor.store.CdmStore;
43
import eu.etaxonomy.taxeditor.ui.dialog.selection.ReferenceSelectionDialog;
44
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
45

    
46
/**
47
 * @author pplitzner
48
 * @since Sep 6, 2017
49
 */
50
public class ChangeAcceptedTaxonToSynonymHandlerE4 extends CdmHandlerE4 {
51

    
52
    private Set<TaxonNodeDto> oldTaxonNodes = new HashSet<>();
53
    private Classification classification;
54
    protected boolean isSetSource = false;
55

    
56
    public ChangeAcceptedTaxonToSynonymHandlerE4() {
57
        super(TaxonNavigatorLabels.CHANGE_ACCEPTED_TAXON_TO_SYNONYM_LABEL);
58
    }
59

    
60
    @Override
61
    public IStatus allowOperations(IStructuredSelection selection,
62
            Shell shell,
63
            MPart activePart,
64
            MHandledMenuItem menuItem) {
65
        // check that only a single taxon tree node has been selected
66
//        if(selection.size() > 1) {
67
//            return new Status(IStatus.ERROR,
68
//                    "unknown", //$NON-NLS-1$
69
//                    TaxonNavigatorLabels.SINGLE_TAXON_SELECTION_MESSAGE);
70
//        }
71

    
72
        // check for no taxon tree node selected
73
        if(selection.size() == 0) {
74
            return new Status(IStatus.ERROR,
75
                    "unknown", //$NON-NLS-1$
76
                    TaxonNavigatorLabels.NO_TAXON_SELECTION_MESSAGE);
77
        }
78

    
79
        // check that selected object is a taxon node
80
        Iterator<?> it = selection.iterator();
81
        Classification nextClassification;
82
        oldTaxonNodes = new HashSet<>();
83
        while(it.hasNext()){
84
        	Object obj = it.next();
85
        	if(obj instanceof TaxonNodeDto) {
86
	            oldTaxonNodes.add((TaxonNodeDto)obj);
87
	            nextClassification = CdmStore.getService(IClassificationService.class).find(((TaxonNodeDto)obj).getClassificationUUID());
88
	            if (classification == null){
89
	            	classification = nextClassification;
90
	            }else if (!classification.equals(nextClassification)){
91
	            	return new Status(IStatus.ERROR,
92
	                        "unknown", //$NON-NLS-1$
93
	                        TaxonNavigatorLabels.ACCEPTED_TAXA_NEED_TO_BE_FROM_SAME_CLASSIFICATION);
94
	            }
95
	        } else {
96
	        	if (obj instanceof TaxonNode && !((TaxonNode)obj).hasTaxon()){
97
	        		return new Status(IStatus.ERROR,
98
	                        "Operation not available for Classifications", //$NON-NLS-1$
99
	                        TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
100
	        	}
101
	            return new Status(IStatus.ERROR,
102
	                    "unknown", //$NON-NLS-1$
103
	                    TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
104
	        }
105
	        // check that the source taxon node does not have children
106
	        if(((TaxonNodeDto)obj).getTaxonomicChildrenCount() > 0) {
107
	            return new Status(IStatus.ERROR,
108
	                    "unknown", //$NON-NLS-1$
109
	                    TaxonNavigatorLabels.SOURCE_TAXON_HAS_CHILDREN_MESSAGE);
110

    
111
	        }
112

    
113
	        // check if corresponding name editor is closed
114
	        EditorUtil.closeObsoleteEditor((TaxonNodeDto)obj, partService);
115
        }
116
        return Status.OK_STATUS;
117
    }
118

    
119
    @Override
120
    public AbstractOperation prepareOperation(IStructuredSelection selection,
121
            Shell shell,
122
            MPart activePart,
123
            MHandledMenuItem menuItem) {
124
        Set<UUID> excludeTaxa = new HashSet<>();
125
        Set<UUID> secUuids = new HashSet<>();
126
        Set<UUID> nodeUuids = new HashSet<>();
127
        boolean published = true;
128

    
129
        for (TaxonNodeDto oldNode:oldTaxonNodes){
130
        	excludeTaxa.add(oldNode.getTaxonUuid());
131
        	secUuids.add(oldNode.getSecUuid());
132
        	secUuids.addAll(oldNode.getHomotypicalGroupDto().getUuidsOfSecundumReferences());
133
        	nodeUuids.add(oldNode.getUuid());
134

    
135
        }
136
        TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(shell,
137
//                new ConversationHolderMock(),
138
                Messages.RemotingChangeAcceptedTaxonToSynonymHandler_CHOOSE_TAXON,
139
                excludeTaxa,
140
                null,
141
                classification.getUuid());
142

    
143
        if (newAcceptedTaxonNode == null) {
144
            return null;
145
        }
146

    
147
        SecReferenceHandlingEnum secHandling = PreferencesUtil.getSecReferenceHandlingPreference();
148
        UUID newSecUuid = null;
149
        UUID newTaxonUuid = newAcceptedTaxonNode.getTaxon() != null && newAcceptedTaxonNode.getTaxon().getSec() != null ? newAcceptedTaxonNode.getTaxon().getSec().getUuid(): null;
150
        //the moved taxa have different secundum references
151
        if (secUuids.size() > 1 && !(secHandling.equals(SecReferenceHandlingEnum.KeepAlways) || secHandling.equals(SecReferenceHandlingEnum.AlwaysDelete))){
152
            int result = MessagingUtils.confirmDialog(Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_title, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_message,
153
                    new String[]{Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Keep, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Parent, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Select});
154
            if (result == 2){
155
                //select new reference
156
                Reference sec = ReferenceSelectionDialog.select(shell, null);
157
                newSecUuid = sec != null? sec.getUuid(): null;
158
            }else if (result == 1){
159
                //use parent sec
160
                newSecUuid = newTaxonUuid;
161
            }else if (result == 0){
162
                //keep sec (also homotypic synonyms with different sec will keep the secundum)
163
                secHandling = SecReferenceHandlingEnum.KeepAlways;
164
            }else{
165
                return null;
166
            }
167
        }else{
168
            UUID oldSecUuid = secUuids.iterator().next();
169
            //the nodes moved have all the same sec, but the accepted taxon has a different one
170
            if ((secUuids.size() > 0 &&
171
                    ((newTaxonUuid != null && oldSecUuid != null && !newTaxonUuid.equals(oldSecUuid))
172
                            || (newTaxonUuid != null && oldSecUuid == null) ||(newTaxonUuid == null && oldSecUuid != null))
173
                    && secHandling.equals(SecReferenceHandlingEnum.KeepWhenSame) )|| secHandling.equals(SecReferenceHandlingEnum.WarningSelect)){
174
                int result = MessagingUtils.confirmDialog(Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_title, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_message,
175
                        new String[]{Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Keep, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Parent, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Select});
176
                if (result == 2){
177
                    Reference sec = ReferenceSelectionDialog.select(shell, null);
178
                    newSecUuid = sec != null? sec.getUuid(): null;
179
                }else if (result == 1){
180
                    newSecUuid = newTaxonUuid;
181
                }else if (result == 0){
182
                    secHandling = SecReferenceHandlingEnum.KeepAlways;
183

    
184
                }else{
185
                    return null;
186
                }
187
            }
188
        }
189

    
190
        ChangeAcceptedTaxonToSynonymOperation rcattso =
191
                new ChangeAcceptedTaxonToSynonymOperation(getTrigger(),
192
                        false,
193
                        nodeUuids,
194
                        newAcceptedTaxonNode.getUuid(), newSecUuid, secHandling, partService, activePart, application, isSetSource);
195

    
196
        return rcattso;
197
    }
198

    
199
    @CanExecute
200
    private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
201
        boolean canExecute = !selection.isEmpty();
202
        Object[] array = selection.toArray();
203
        for (Object object : array) {
204
            canExecute &= (object instanceof TaxonNodeDto) && ((TaxonNodeDto)selection.getFirstElement()).getTaxonUuid() != null;
205
        }
206
        menuItem.setVisible(canExecute);
207
        return canExecute;
208
    }
209

    
210
    @Override
211
    public void onComplete() {
212
    }
213

    
214
    @Override
215
    protected Object getTrigger() {
216
        return this;
217
    }
218
}
(2-2/25)