Project

General

Profile

Download (9.82 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
			    boolean isSameParent = taxonNode.getParentUUID().equals(((TaxonNodeDto) target).getUuid());
159
			    TaxonNode node = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid());
160
				boolean hasTaxonNodePermission = CdmStore.currentAuthentiationHasPermission(node, UPDATE);
161

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

    
183

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

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

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

    
213
			} else {
214
				return false;
215
			}
216

    
217

    
218
		}
219
		//close possible open name editors for the moved taxon nodes
220

    
221
		taxonNodes.forEach(nodeDto->EditorUtil.closeObsoleteEditor(nodeDto, partService));
222

    
223
		moveNodes(uuids, targetITaxonTreeNode.getUuid(), movingTypeInt);
224
        return true;
225
	}
226

    
227
	private void moveNodes(Set<UUID> taxonNodesToMoveUuid, UUID newParentTreeNodeUuid, int movingTypeInt){
228
	    UUID uuid = CdmApplicationState.getLongRunningTasksService().monitLongRunningTask(taxonNodesToMoveUuid,
229
                newParentTreeNodeUuid, movingTypeInt);
230

    
231
        Display.getDefault().asyncExec(new Runnable() {
232
            @Override
233
            public void run() {
234
                AbstractUtility.executeMoniteredOperation("Move Taxon to new parent: ",
235
                        uuid,
236
                        500,
237
                        false,
238
                        TreeNodeDropAdapterE4.this,
239
                        null);
240
            }
241
        });
242
	}
243

    
244

    
245
	@Override
246
	public void dragOver(DropTargetEvent event) {
247
		super.dragOver(event);
248
		event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_INSERT_AFTER;
249
	}
250

    
251
    /**
252
     * {@inheritDoc}
253
     */
254
    @Override
255
    public void postOperation(IRemotingProgressMonitor monitor) {
256
        EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAVIGATOR, true);
257
    }
258

    
259
}
(6-6/6)