Project

General

Profile

Download (9.89 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.RemotingChangeAcceptedTaxonToSynonymOperation;
40
import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
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 RemotingChangeAcceptedTaxonToSynonymHandlerE4 extends RemotingCdmHandlerE4 {
51

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

    
56
    public RemotingChangeAcceptedTaxonToSynonymHandlerE4() {
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
        	nodeUuids.add(oldNode.getUuid());
133

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

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

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

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

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

    
195
        return rcattso;
196
    }
197

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

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

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