Project

General

Profile

Download (9.1 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

    
126
        for (TaxonNodeDto oldNode:oldTaxonNodes){
127
        	excludeTaxa.add(oldNode.getTaxonUuid());
128
        	secUuids.add(oldNode.getSecUuid());
129
        	nodeUuids.add(oldNode.getUuid());
130
        }
131
        TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(shell,
132
//                new ConversationHolderMock(),
133
                Messages.RemotingChangeAcceptedTaxonToSynonymHandler_CHOOSE_TAXON,
134
                excludeTaxa,
135
                null,
136
                classification.getUuid());
137

    
138
        if (newAcceptedTaxonNode == null) {
139
            return null;
140
        }
141

    
142
        SecReferenceHandlingEnum secHandling = PreferencesUtil.getSecReferenceHandlingPreference();
143
        UUID newSecUuid = null;
144
        UUID newTaxonUuid = newAcceptedTaxonNode.getTaxon() != null && newAcceptedTaxonNode.getTaxon().getSec() != null ? newAcceptedTaxonNode.getTaxon().getSec().getUuid(): null;
145
        if (secUuids.size() > 1 && !(secHandling.equals(SecReferenceHandlingEnum.KeepAlways) || secHandling.equals(SecReferenceHandlingEnum.AlwaysDelete))){
146
            int result = MessagingUtils.confirmDialog(Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_title, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_message,
147
                    new String[]{Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Keep, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Parent, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Select});
148
            if (result == 2){
149
                Reference sec = ReferenceSelectionDialog.select(shell, null);
150
                newSecUuid = sec != null? sec.getUuid(): null;
151
            }else if (result == 1){
152
                newSecUuid = newTaxonUuid;
153
            }else if (result == 0){
154
                secHandling = SecReferenceHandlingEnum.UseNewParentSec;
155
            }else{
156
                return null;
157
            }
158

    
159
        }else{
160

    
161

    
162
            UUID oldSecUuid = secUuids.iterator().next();
163
            if ((secUuids.size() > 0 && newTaxonUuid != oldSecUuid && secHandling.equals(SecReferenceHandlingEnum.KeepWhenSame) )|| secHandling.equals(SecReferenceHandlingEnum.WarningSelect)){
164
                int result = MessagingUtils.confirmDialog(Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_title, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Handling_message,
165
                        new String[]{Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Keep, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Parent, Messages.RemotingChangeAcceptedTaxonToSynonymHandler_Select_Sec_Reference_Select});
166
                if (result == 2){
167
                    Reference sec = ReferenceSelectionDialog.select(shell, null);
168
                    newSecUuid = sec != null? sec.getUuid(): null;
169
                }else if (result == 1){
170
                    newSecUuid = newTaxonUuid;
171
                }else if (result == 0){
172
                    newSecUuid = oldSecUuid;
173
                }else{
174
                    return null;
175
                }
176

    
177
            }
178
        }
179

    
180

    
181
        RemotingChangeAcceptedTaxonToSynonymOperation rcattso =
182
                new RemotingChangeAcceptedTaxonToSynonymOperation(getTrigger(),
183
                        false,
184
                        nodeUuids,
185
                        newAcceptedTaxonNode.getUuid(), newSecUuid, secHandling, partService, activePart, application, isSetSource);
186

    
187
        return rcattso;
188
    }
189

    
190
    @CanExecute
191
    private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
192
        boolean canExecute = !selection.isEmpty();
193
        Object[] array = selection.toArray();
194
        for (Object object : array) {
195
            canExecute &= (object instanceof TaxonNodeDto) && ((TaxonNodeDto)selection.getFirstElement()).getTaxonUuid() != null;
196
        }
197
        menuItem.setVisible(canExecute);
198
        return canExecute;
199
    }
200

    
201
    @Override
202
    public void onComplete() {
203
    }
204

    
205
    /**
206
     * {@inheritDoc}
207
     */
208
    @Override
209
    protected Object getTrigger() {
210
        return this;
211
    }
212
}
(11-11/25)