Project

General

Profile

Download (9.82 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 *
3
 */
4
package eu.etaxonomy.taxeditor.navigation.navigator.e4.handler;
5

    
6
import java.util.HashSet;
7
import java.util.Iterator;
8
import java.util.Set;
9
import java.util.UUID;
10

    
11
import javax.inject.Named;
12

    
13
import org.eclipse.core.commands.operations.AbstractOperation;
14
import org.eclipse.core.runtime.IStatus;
15
import org.eclipse.core.runtime.Status;
16
import org.eclipse.e4.core.di.annotations.CanExecute;
17
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
18
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
19
import org.eclipse.e4.ui.services.IServiceConstants;
20
import org.eclipse.jface.viewers.IStructuredSelection;
21
import org.eclipse.jface.viewers.TreeSelection;
22
import org.eclipse.swt.widgets.Shell;
23

    
24
import eu.etaxonomy.cdm.api.service.IClassificationService;
25
import eu.etaxonomy.cdm.model.metadata.SecReferenceHandlingEnum;
26
import eu.etaxonomy.cdm.model.reference.Reference;
27
import eu.etaxonomy.cdm.model.taxon.Classification;
28
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
29
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
30
import eu.etaxonomy.taxeditor.editor.EditorUtil;
31
import eu.etaxonomy.taxeditor.model.MessagingUtils;
32
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
33
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
34
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingChangeAcceptedTaxonToSynonymOperation;
35
import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
36
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
37
import eu.etaxonomy.taxeditor.store.CdmStore;
38
import eu.etaxonomy.taxeditor.ui.dialog.selection.ReferenceSelectionDialog;
39
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
40

    
41
/**
42
 *
43
 * @author pplitzner
44
 * @since Sep 6, 2017
45
 *
46
 */
47
public class RemotingChangeAcceptedTaxonToSynonymHandlerE4 extends RemotingCdmHandlerE4 {
48

    
49

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

    
54
    public RemotingChangeAcceptedTaxonToSynonymHandlerE4() {
55
        super(TaxonNavigatorLabels.CHANGE_ACCEPTED_TAXON_TO_SYNONYM_LABEL);
56
    }
57

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

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

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

    
109
	        }
110

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

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

    
127
        for (TaxonNodeDto oldNode:oldTaxonNodes){
128
        	excludeTaxa.add(oldNode.getTaxonUuid());
129
        	secUuids.add(oldNode.getSecUuid());
130
        	nodeUuids.add(oldNode.getUuid());
131

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

    
140
        if (newAcceptedTaxonNode == null) {
141
            return null;
142
        }
143

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

    
182
                }else{
183
                    return null;
184
                }
185

    
186
            }
187
        }
188

    
189

    
190

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

    
197
        return rcattso;
198
    }
199

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

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

    
215
    /**
216
     * {@inheritDoc}
217
     */
218
    @Override
219
    protected Object getTrigger() {
220
        return this;
221
    }
222
}
(11-11/25)