Project

General

Profile

Download (11.4 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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;
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 org.apache.log4j.Logger;
19
import org.eclipse.core.commands.operations.AbstractOperation;
20
import org.eclipse.core.commands.operations.IUndoContext;
21
import org.eclipse.core.runtime.IStatus;
22
import org.eclipse.core.runtime.Status;
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.swt.dnd.DropTargetEvent;
28
import org.eclipse.swt.dnd.TransferData;
29
import org.eclipse.ui.navigator.CommonDropAdapter;
30
import org.eclipse.ui.navigator.CommonDropAdapterAssistant;
31

    
32
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
33
import eu.etaxonomy.cdm.model.common.CdmBase;
34
import eu.etaxonomy.cdm.model.taxon.Classification;
35
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
36
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
38
import eu.etaxonomy.taxeditor.model.AbstractUtility;
39
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
40
import eu.etaxonomy.taxeditor.navigation.navigator.TreeNodeDropAdapter.MovingType;
41
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveTaxonOperation;
42
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
43
import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
44
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
45
import eu.etaxonomy.taxeditor.store.CdmStore;
46

    
47
/**
48
 * <p>TaxonNodeDropAdapterAssistant class.</p>
49
 *
50
 * @author p.ciardelli
51
 * @created 03.06.2009
52
 * @version 1.0
53
 */
54
public class TreeNodeDropAdapterAssistant extends CommonDropAdapterAssistant implements IPostOperationEnabled {
55

    
56
	private static final Logger logger = Logger.getLogger(TreeNodeDropAdapterAssistant.class);
57

    
58
	/** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code> */
59
	public static final String ID = "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
60

    
61
	private static final EnumSet<CRUD> UPDATE = EnumSet.of(CRUD.UPDATE);
62

    
63
	/* (non-Javadoc)
64
	 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
65
	 */
66
	/** {@inheritDoc} */
67
	@Override
68
	public IStatus handleDrop(CommonDropAdapter dropAdapter,
69
			DropTargetEvent dropTargetEvent, Object target) {
70

    
71

    
72
		if (target instanceof ITaxonTreeNode) {
73
			Set<TaxonNode> taxonNodes = getSelectedTaxa();
74
			ITaxonTreeNode targetTreeNode = (ITaxonTreeNode) target;
75
			if (targetTreeNode instanceof Classification){
76
				targetTreeNode = ((Classification)targetTreeNode).getRootNode();
77
				targetTreeNode = HibernateProxyHelper.deproxy(targetTreeNode, TaxonNode.class);
78
			}
79
			//if(taxonNodes != null) {
80
				if (taxonNodes.size() >= 1){
81
					return moveTaxon(taxonNodes, targetTreeNode);
82
				/*} else{
83
					if( MessageDialog.openConfirm(null, "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
84
						return null;
85
					}
86
				}*/
87
            }
88
		}
89

    
90
		return Status.CANCEL_STATUS;
91
	}
92

    
93
	private Set<TaxonNode> getSelectedTaxa(){
94
		HashSet<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
95

    
96
		ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
97
		if (selection instanceof TreeSelection) {
98

    
99
			Iterator selectionIterator = ((TreeSelection) selection).iterator();
100

    
101
			while (selectionIterator.hasNext()){
102
				Object object = selectionIterator.next();
103
				if(object instanceof TaxonNode){
104
					TaxonNode taxonNode = (TaxonNode) object;
105
					taxonNodes.add(taxonNode);
106
				}
107
			}
108
		}
109
		return taxonNodes.size() > 0 ? taxonNodes : null;
110
	}
111

    
112
	/* (non-Javadoc)
113
	 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
114
	 */
115
	/** {@inheritDoc} */
116
	@Override
117
	public IStatus validateDrop(Object target, int operation,
118
			TransferData transferType) {
119
		if (target instanceof ITaxonTreeNode) {
120

    
121
		    // check users permissions with target taxonnode and taxon
122
		    if (target instanceof TaxonNode) {
123
		        TaxonNode targetNode = (TaxonNode)target;
124
		        Boolean hasTargetNodePermission = CdmStore.currentAuthentiationHasPermission(targetNode, UPDATE);
125
                Boolean hasTargetTaxonPermission = CdmStore.currentAuthentiationHasPermission(targetNode.getTaxon(), UPDATE);
126

    
127
                if(logger.isDebugEnabled()){
128
                    logger.debug("target: " + targetNode.getTaxon().getTitleCache()); //$NON-NLS-1$
129
                }
130

    
131
		        if(!hasTargetNodePermission || ! hasTargetNodePermission){
132
		            if(logger.isDebugEnabled()){
133
		                logger.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission.toString() + " " + hasTargetTaxonPermission.toString() + " "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
134
		            }
135
		            return Status.CANCEL_STATUS;
136
		        }
137
		    }
138

    
139
		    // do not allow to drop onto itself and
140
		    // check users permissions with all selected taxon nodes and taxa
141
		    for(TaxonNode taxonNode : getSelectedTaxa()){
142
			    logger.debug("selectedTaxa: " + taxonNode.getTaxon().getTitleCache()); //$NON-NLS-1$
143
				Boolean isSameTaxonNode = taxonNode.equals(target);
144
				Boolean hasTaxonNodePermission = CdmStore.currentAuthentiationHasPermission(taxonNode, UPDATE);
145
				Boolean hasTaxonPermission = CdmStore.currentAuthentiationHasPermission(taxonNode.getTaxon(), UPDATE);
146
                if (
147
			        isSameTaxonNode
148
			        || !hasTaxonNodePermission
149
    	            || !hasTaxonPermission
150
	                ) {
151
                    if(logger.isDebugEnabled()){
152
                        logger.debug("CANCEL_STATUS for selected  " + isSameTaxonNode.toString() + " " + hasTaxonNodePermission.toString() + " " + hasTaxonPermission.toString() + " "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
153
                    }
154
					return Status.CANCEL_STATUS;
155
				}
156
			}
157
			logger.debug("OK_STATUS"); //$NON-NLS-1$
158
			return Status.OK_STATUS;
159
		}
160
		logger.debug("CANCEL_STATUS"); //$NON-NLS-1$
161
		return Status.CANCEL_STATUS;
162
	}
163

    
164

    
165
	/**
166
	 * @param childTaxonNode
167
	 * @param parentTaxon
168
	 * @return
169
	 */
170
	private IStatus moveTaxon(Set<TaxonNode> taxonNodes, ITaxonTreeNode targetITaxonTreeNode) {
171

    
172
		TaxonNavigator taxonNavigator;
173
		taxonNavigator = (TaxonNavigator) AbstractUtility.showView(TaxonNavigator.ID);
174

    
175
		if(targetITaxonTreeNode instanceof TaxonNode){
176

    
177
			TaxonNode targetTaxonNode = (TaxonNode) targetITaxonTreeNode;
178
		// Make sure parent taxon does not have unsaved changes
179
			if (NavigationUtil.isDirty(targetTaxonNode)){
180
				MessageDialog.openWarning(NavigationUtil.getShell(), TreeNodeDropAdapter.TREE_NODE_DROP_ADAPTER_UNSAVED_PARENT, TreeNodeDropAdapter.TREE_NODE_DROP_ADAPTER_UNSAVED_PARENT_MESSAGE);
181
				return Status.CANCEL_STATUS;
182
			}
183

    
184
		}
185
		Iterator<TaxonNode> taxIterator = taxonNodes.iterator();
186
        Set<UUID> uuids = new HashSet<UUID>();
187
        TaxonNode node = null;
188
        while(taxIterator.hasNext()){
189
            node = taxIterator.next();
190
            uuids.add(node.getUuid());
191
        }
192
		if (!PreferencesUtil.getSortNodesNaturally()){
193
			IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
194
			if (workspaceUndoContext == null) {
195
				logger.error("Workspace undo context is null. DND operation cancelled"); //$NON-NLS-1$
196
				return Status.CANCEL_STATUS;
197
			}
198

    
199
			AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, (TaxonNode)targetITaxonTreeNode, MovingType.CHILD);
200
			AbstractUtility.executeOperation(operation, (RemotingCdmHandlerE4)null);
201

    
202

    
203
			logger.info("Moved taxa to new parent " + targetITaxonTreeNode); //$NON-NLS-1$
204
			return Status.OK_STATUS;
205
		}else{
206
			String[] buttonLables = {TreeNodeDropAdapter.TREE_NODE_DROP_ADAPTER_CHILD, TreeNodeDropAdapter.TREE_NODE_DROP_ADAPTER_BEHIND, TreeNodeDropAdapter.TREE_NODE_DROP_ADAPTER_CANCEL};
207
			MessageDialog dialog = new MessageDialog(null, TreeNodeDropAdapter.TARGET_NODE, null, TreeNodeDropAdapter.DO_YOU_WANT_TO_MOVE_THE_TAXONNODE_AS_CHILD_OR_BEHIND_THE_TARGET_NODE, MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
208
			dialog.open();
209
			int returnCode = dialog.getReturnCode();
210
			if (returnCode == 0){
211
				IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
212
				if (workspaceUndoContext == null) {
213
					logger.error("Workspace undo context is null. DND operation cancelled"); //$NON-NLS-1$
214
					return Status.CANCEL_STATUS;
215
				}
216

    
217
				AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, (TaxonNode)targetITaxonTreeNode, MovingType.CHILD);
218
				AbstractUtility.executeOperation(operation, (RemotingCdmHandlerE4)null);
219

    
220

    
221
				logger.info("Moved taxa to new parent " + targetITaxonTreeNode); //$NON-NLS-1$
222
				return Status.OK_STATUS;
223
			}else if (returnCode == 1){
224
				IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
225
				if (workspaceUndoContext == null) {
226
					logger.error("Workspace undo context is null. DND operation cancelled"); //$NON-NLS-1$
227
					return Status.CANCEL_STATUS;
228
				}
229
				TaxonNode targetNode = (TaxonNode) targetITaxonTreeNode;
230

    
231
				AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, targetNode, MovingType.BEHIND);
232
				AbstractUtility.executeOperation(operation, (RemotingCdmHandlerE4)null);
233

    
234
				logger.info("Moved taxa to new parent " + targetITaxonTreeNode); //$NON-NLS-1$
235
				return Status.OK_STATUS;
236
//			}else if (returnCode == 2){
237
//                IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
238
//                if (workspaceUndoContext == null) {
239
//                    logger.error("Workspace undo context is null. DND operation cancelled");
240
//                    return Status.CANCEL_STATUS;
241
//                }
242
//                TaxonNode targetNode = (TaxonNode) targetITaxonTreeNode;
243
//
244
//                if(CdmStore.getCurrentSessionManager().isRemoting()) {
245
//                    AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, targetNode, MovingType.PREVIOUS);
246
//                    NavigationUtil.executeOperation(operation, null);
247
//                } else {
248
//                    AbstractPostOperation operation = new MoveTaxonOperation
249
//                            ("Move Taxon", workspaceUndoContext, uuids, targetNode, this, taxonNavigator, MovingType.PREVIOUS);
250
//                    NavigationUtil.executeOperation(operation);
251
//                }
252
//                logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
253
//                return Status.OK_STATUS;
254
            } else{
255
				return Status.CANCEL_STATUS;
256
			}
257

    
258

    
259
		}
260
	}
261

    
262
	/* (non-Javadoc)
263
	 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
264
	 */
265
	/** {@inheritDoc} */
266
	@Override
267
    public boolean postOperation(CdmBase objectAffectedByOperation) {
268
		return true;
269
	}
270

    
271
	/**
272
	 * <p>onComplete</p>
273
	 *
274
	 * @return a boolean.
275
	 */
276
	@Override
277
    public boolean onComplete() {
278
		// TODO Auto-generated method stub
279
		return false;
280
	}
281

    
282
}
(21-21/21)