Project

General

Profile

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

    
12
import java.util.EnumSet;
13
import java.util.HashSet;
14
import java.util.Iterator;
15
import java.util.Set;
16
import java.util.UUID;
17

    
18
import javax.inject.Inject;
19

    
20
import org.apache.log4j.Logger;
21
import org.eclipse.core.commands.operations.IUndoContext;
22
import org.eclipse.e4.ui.workbench.modeling.EPartService;
23
import org.eclipse.jface.dialogs.MessageDialog;
24
import org.eclipse.jface.util.LocalSelectionTransfer;
25
import org.eclipse.jface.viewers.ISelection;
26
import org.eclipse.jface.viewers.TreeSelection;
27
import org.eclipse.jface.viewers.ViewerDropAdapter;
28
import org.eclipse.swt.dnd.DND;
29
import org.eclipse.swt.dnd.DropTargetEvent;
30
import org.eclipse.swt.dnd.TransferData;
31
import org.eclipse.swt.widgets.Display;
32

    
33
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
34
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
35
import eu.etaxonomy.cdm.api.service.UpdateResult;
36
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
37
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
38
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
39
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
40
import eu.etaxonomy.taxeditor.editor.EditorUtil;
41
import eu.etaxonomy.taxeditor.event.EventUtility;
42
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
43
import eu.etaxonomy.taxeditor.model.AbstractUtility;
44
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
45
import eu.etaxonomy.taxeditor.operation.IPostMoniteredOperationEnabled;
46
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
47
import eu.etaxonomy.taxeditor.store.CdmStore;
48

    
49
/**
50
 * @author k.luther
51
 * @date 02.06.2015
52
 */
53
public class TreeNodeDropAdapterE4 extends ViewerDropAdapter implements IPostMoniteredOperationEnabled{
54

    
55
    protected static final String TREE_NODE_DROP_ADAPTER_UNSAVED_PARENT_MESSAGE = Messages.TreeNodeDropAdapter_UNSAVED_PARENT_MESSAGE;
56
    protected static final String TREE_NODE_DROP_ADAPTER_UNSAVED_PARENT = Messages.TreeNodeDropAdapter_UNSAVED_PARENT;
57
    protected static final String TREE_NODE_DROP_ADAPTER_MOVE_TAXON = Messages.TreeNodeDropAdapter_MOVE_TAXON;
58
    protected static final String TREE_NODE_DROP_ADAPTER_CANCEL = Messages.TreeNodeDropAdapter_CANCEL;
59
    protected static final String TREE_NODE_DROP_ADAPTER_BEHIND = Messages.TreeNodeDropAdapter_BEHIND;
60
    protected static final String TREE_NODE_DROP_ADAPTER_CHILD = Messages.TreeNodeDropAdapter_CHILD;
61
    protected static final String DO_YOU_WANT_TO_MOVE_THE_TAXONNODE_AS_CHILD_OR_BEHIND_THE_TARGET_NODE = Messages.TreeNodeDropAdapter_MOVE_BEHIND;
62
    protected static final String TARGET_NODE = Messages.TreeNodeDropAdapter_TARGET_NODE;
63

    
64
    private static final Logger logger = Logger.getLogger(TreeNodeDropAdapterE4.class);
65

    
66
    private TaxonNavigatorE4 taxonNavigator;
67
    UpdateResult result;
68

    
69
	public static final String ID = "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
70

    
71
	private static final EnumSet<CRUD> UPDATE = EnumSet.of(CRUD.UPDATE);
72

    
73
	@Inject
74
	private EPartService partService;
75

    
76
	public enum MovingType{
77
	    CHILD, PREVIOUS, BEHIND
78
	}
79

    
80
	protected TreeNodeDropAdapterE4(TaxonNavigatorE4 navigator) {
81
	    super(navigator.getViewer());
82
	    this.taxonNavigator = navigator;
83
	}
84

    
85
	/** {@inheritDoc} */
86
	@Override
87
	public boolean performDrop(Object data) {
88
        Object target = getCurrentTarget();
89
		if (getCurrentTarget() instanceof TaxonNodeDto) {
90
			Set<TaxonNodeDto> taxonNodes = getSelectedTaxa();
91
			TaxonNodeDto targetTreeNode = (TaxonNodeDto) target;
92

    
93
			if(taxonNodes != null) {
94
			    boolean success = moveTaxon(taxonNodes, targetTreeNode);
95
				return success;
96
            }
97
		}
98
		return false;
99
	}
100

    
101
	private Set<TaxonNodeDto> getSelectedTaxa(){
102
		HashSet<TaxonNodeDto> taxonNodes = new HashSet();
103

    
104
		ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
105
		if (selection instanceof TreeSelection) {
106

    
107
			Iterator<?> selectionIterator = ((TreeSelection) selection).iterator();
108

    
109
			while (selectionIterator.hasNext()){
110
				Object object = selectionIterator.next();
111
				if(object instanceof TaxonNodeDto){
112
					TaxonNodeDto taxonNode = (TaxonNodeDto) object;
113
					taxonNodes.add(taxonNode);
114
				}
115
			}
116
		}
117
		return taxonNodes;
118
	}
119

    
120
	/** {@inheritDoc} */
121
	@Override
122
	public boolean validateDrop(Object target, int operation,
123
			TransferData transferType) {
124

    
125
		if (target instanceof TaxonNodeDto) {
126

    
127
		    // check users permissions with target taxonnode and taxon
128
		    if (target instanceof TaxonNodeDto) {
129
		        TaxonNodeDto targetNode = (TaxonNodeDto)target;
130
		        TaxonNode node = CdmStore.getService(ITaxonNodeService.class).load(targetNode.getUuid());
131
		        boolean hasTargetNodePermission = CdmStore.currentAuthentiationHasPermission(node, UPDATE);
132
		        boolean hasTargetTaxonPermission = node.getTaxon() == null ?
133
		        			true :
134
		        			CdmStore.currentAuthentiationHasPermission(node.getTaxon(), UPDATE);
135

    
136
		        if(!hasTargetNodePermission || ! hasTargetNodePermission){
137
		            if(logger.isDebugEnabled()){
138
		                logger.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission + " " + hasTargetTaxonPermission + " "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
139
		            }
140
		            return false;
141
		        }
142
		    }
143

    
144
		    // do not allow to drop onto itself and
145
		    // check users permissions with all selected taxon nodes and taxa
146
		    for(TaxonNodeDto taxonNode : getSelectedTaxa()){
147
			    if (logger.isDebugEnabled()){
148
			    	logger.debug("selectedTaxa: " + taxonNode.getTaxonUuid() == null? "-" : taxonNode.getTitleCache()); //$NON-NLS-1$
149
			    }
150
			    boolean isClassification = taxonNode.getTaxonUuid()== null;
151
			    if (isClassification) {
152
	                    if(logger.isDebugEnabled()){
153
	                        logger.debug("CANCEL_STATUS for selected classification " + taxonNode.getClassificationUUID()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
154
	                    }
155
	                    return false;
156
	                }
157
			    boolean isSameTaxonNode = taxonNode.equals(target);
158
			    TaxonNode node = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid());
159
				boolean hasTaxonNodePermission = CdmStore.currentAuthentiationHasPermission(node, UPDATE);
160

    
161
				boolean hasTaxonPermission =
162
	        			CdmStore.currentAuthentiationHasPermission(node.getTaxon(), UPDATE);CdmStore.currentAuthentiationHasPermission(node.getTaxon(), UPDATE);
163
                if (
164
			        isSameTaxonNode
165
			        || !hasTaxonNodePermission
166
    	            || !hasTaxonPermission
167
    	            ) {
168
                    if(logger.isDebugEnabled()){
169
                        logger.debug("CANCEL_STATUS for selected  " + isSameTaxonNode + Messages.TreeNodeDropAdapter_10 + hasTaxonNodePermission + " " + hasTaxonPermission + " "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
170
                    }
171
					return false;
172
				}
173
			}
174
			logger.debug("OK_STATUS"); //$NON-NLS-1$
175
			return true;
176
		}
177
		logger.debug("CANCEL_STATUS"); //$NON-NLS-1$
178
		return false;
179
	}
180

    
181

    
182
	private boolean moveTaxon(Set<TaxonNodeDto> taxonNodes, TaxonNodeDto targetITaxonTreeNode) {
183
		Iterator<TaxonNodeDto> taxIterator = taxonNodes.iterator();
184
        Set<UUID> uuids = new HashSet<UUID>();
185
        TaxonNodeDto node = null;
186
        while(taxIterator.hasNext()){
187
            node = taxIterator.next();
188
            uuids.add(node.getUuid());
189
        }
190
        IUndoContext workspaceUndoContext = taxonNavigator.getUndoContext();
191
         int movingTypeInt = 0;
192
		if (PreferencesUtil.getSortNodesNaturally()){
193
			String[] buttonLables = {TREE_NODE_DROP_ADAPTER_CHILD, TREE_NODE_DROP_ADAPTER_BEHIND,TREE_NODE_DROP_ADAPTER_CANCEL};
194
			MessageDialog dialog = new MessageDialog(null, TARGET_NODE, null, DO_YOU_WANT_TO_MOVE_THE_TAXONNODE_AS_CHILD_OR_BEHIND_THE_TARGET_NODE, MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
195
			dialog.open();
196
			int returnCode = dialog.getReturnCode();
197

    
198
			if (returnCode == 0){
199
				if (workspaceUndoContext == null) {
200
					logger.error("Workspace undo context is null. DND operation cancelled"); //$NON-NLS-1$
201
					return false;
202
				}
203

    
204
			}else if (returnCode == 1){
205
				if (workspaceUndoContext == null) {
206
					logger.error("Workspace undo context is null. DND operation cancelled"); //$NON-NLS-1$
207
					return false;
208
				}
209
				movingTypeInt = 2;
210

    
211
			} else {
212
				return false;
213
			}
214

    
215

    
216
		}
217
		//close possible open name editors for the moved taxon nodes
218
		taxonNodes.forEach(nodeDto->EditorUtil.closeObsoleteEditor(nodeDto, partService));
219
		moveNodes(uuids, targetITaxonTreeNode.getUuid(), movingTypeInt);
220
        return true;
221
	}
222

    
223
	private void moveNodes(Set<UUID> taxonNodesToMoveUuid, UUID newParentTreeNodeUuid, int movingTypeInt){
224
	    UUID uuid = CdmApplicationState.getLongRunningTasksService().monitLongRunningTask(taxonNodesToMoveUuid,
225
                newParentTreeNodeUuid, movingTypeInt);
226

    
227
                Display.getDefault().asyncExec(new Runnable() {
228
                    @Override
229
                    public void run() {
230
                        AbstractUtility.executeMoniteredOperation("Move Taxon to new parent: ",
231
                                uuid,
232
                                1000,
233
                                false,
234
                                TreeNodeDropAdapterE4.this,
235
                                null);
236
                    }
237
                });
238
	}
239

    
240

    
241
	@Override
242
	public void dragOver(DropTargetEvent event) {
243
		super.dragOver(event);
244
		event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_INSERT_AFTER;
245
	}
246

    
247
    /**
248
     * {@inheritDoc}
249
     */
250
    @Override
251
    public void postOperation(IRemotingProgressMonitor monitor) {
252
        EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAVIGATOR, true);
253
    }
254

    
255
}
(6-6/6)