Project

General

Profile

Download (11.9 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.taxeditor.navigation.navigator;
12

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

    
19
import org.apache.log4j.Logger;
20
import org.eclipse.core.commands.operations.AbstractOperation;
21
import org.eclipse.core.commands.operations.IUndoContext;
22
import org.eclipse.core.runtime.IStatus;
23
import org.eclipse.core.runtime.Status;
24
import org.eclipse.jface.dialogs.MessageDialog;
25
import org.eclipse.jface.util.LocalSelectionTransfer;
26
import org.eclipse.jface.viewers.ISelection;
27
import org.eclipse.jface.viewers.TreeSelection;
28
import org.eclipse.swt.dnd.DropTargetEvent;
29
import org.eclipse.swt.dnd.TransferData;
30
import org.eclipse.ui.navigator.CommonDropAdapter;
31
import org.eclipse.ui.navigator.CommonDropAdapterAssistant;
32

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

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

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

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

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

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

    
72

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

    
91
		return Status.CANCEL_STATUS;
92
	}
93

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

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

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

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

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

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

    
128
                if(logger.isDebugEnabled()){
129
                    logger.debug("target: " + targetNode.getTaxon().getTitleCache());
130
                }
131

    
132
		        if(!hasTargetNodePermission || ! hasTargetNodePermission){
133
		            if(logger.isDebugEnabled()){
134
		                logger.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission.toString() + " " + hasTargetTaxonPermission.toString() + " ");
135
		            }
136
		            return Status.CANCEL_STATUS;
137
		        }
138
		    }
139

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

    
165

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

    
173
		TaxonNavigator taxonNavigator;
174
		taxonNavigator = (TaxonNavigator) NavigationUtil.showView(TaxonNavigator.ID);
175

    
176
		if(targetITaxonTreeNode instanceof TaxonNode){
177

    
178
			TaxonNode targetTaxonNode = (TaxonNode) targetITaxonTreeNode;
179
		// Make sure parent taxon does not have unsaved changes
180
			if (NavigationUtil.isDirty(targetTaxonNode)){
181
				MessageDialog.openWarning(NavigationUtil.getShell(), "Unsaved Parent Taxon", "There are unsaved " +
182
				"changes in the parent taxon. Pleas save first.");
183
				return Status.CANCEL_STATUS;
184
			}
185

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

    
201
			if(CdmStore.getCurrentSessionManager().isRemoting()) {
202
			    AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, (TaxonNode)targetITaxonTreeNode, MovingType.CHILD);
203
			    NavigationUtil.executeOperation(operation, null);
204
			} else {
205
			    AbstractPostOperation operation = new MoveTaxonOperation
206
			            ("Move Taxon", workspaceUndoContext, uuids, targetITaxonTreeNode, this, taxonNavigator, MovingType.CHILD);
207
			    NavigationUtil.executeOperation(operation);
208
            }
209

    
210
			logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
211
			return Status.OK_STATUS;
212
		}else{
213
			String[] buttonLables = {"Child", "Behind", "Cancel"};
214
			MessageDialog dialog = new MessageDialog(null, "Target node", null, "Do you want to move the taxon as child, before or behind the target.", MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
215
			dialog.open();
216
			int returnCode = dialog.getReturnCode();
217
			if (returnCode == 0){
218
				IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
219
				if (workspaceUndoContext == null) {
220
					logger.error("Workspace undo context is null. DND operation cancelled");
221
					return Status.CANCEL_STATUS;
222
				}
223

    
224
				if(CdmStore.getCurrentSessionManager().isRemoting()) {
225
				    AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, (TaxonNode)targetITaxonTreeNode, MovingType.CHILD);
226
				    NavigationUtil.executeOperation(operation, null);
227
				} else {
228
				    AbstractPostOperation operation = new MoveTaxonOperation
229
				            ("Move Taxon", workspaceUndoContext, uuids, targetITaxonTreeNode, this, taxonNavigator, MovingType.CHILD);
230
				    NavigationUtil.executeOperation(operation);
231
				}
232

    
233
				logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
234
				return Status.OK_STATUS;
235
			}else if (returnCode == 1){
236
				IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
237
				if (workspaceUndoContext == null) {
238
					logger.error("Workspace undo context is null. DND operation cancelled");
239
					return Status.CANCEL_STATUS;
240
				}
241
				TaxonNode targetNode = (TaxonNode) targetITaxonTreeNode;
242

    
243
				if(CdmStore.getCurrentSessionManager().isRemoting()) {
244
				    AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, targetNode, MovingType.BEHIND);
245
				    NavigationUtil.executeOperation(operation, null);
246
				} else {
247
				    AbstractPostOperation operation = new MoveTaxonOperation
248
				            ("Move Taxon", workspaceUndoContext, uuids, targetNode, this, taxonNavigator, MovingType.BEHIND);
249
				    NavigationUtil.executeOperation(operation);
250
				}
251
				logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
252
				return Status.OK_STATUS;
253
//			}else if (returnCode == 2){
254
//                IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
255
//                if (workspaceUndoContext == null) {
256
//                    logger.error("Workspace undo context is null. DND operation cancelled");
257
//                    return Status.CANCEL_STATUS;
258
//                }
259
//                TaxonNode targetNode = (TaxonNode) targetITaxonTreeNode;
260
//
261
//                if(CdmStore.getCurrentSessionManager().isRemoting()) {
262
//                    AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, targetNode, MovingType.PREVIOUS);
263
//                    NavigationUtil.executeOperation(operation, null);
264
//                } else {
265
//                    AbstractPostOperation operation = new MoveTaxonOperation
266
//                            ("Move Taxon", workspaceUndoContext, uuids, targetNode, this, taxonNavigator, MovingType.PREVIOUS);
267
//                    NavigationUtil.executeOperation(operation);
268
//                }
269
//                logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
270
//                return Status.OK_STATUS;
271
            } else{
272
				return Status.CANCEL_STATUS;
273
			}
274

    
275

    
276
		}
277
	}
278

    
279
	/* (non-Javadoc)
280
	 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
281
	 */
282
	/** {@inheritDoc} */
283
	@Override
284
    public boolean postOperation(CdmBase objectAffectedByOperation) {
285
		return true;
286
	}
287

    
288
	/**
289
	 * <p>onComplete</p>
290
	 *
291
	 * @return a boolean.
292
	 */
293
	@Override
294
    public boolean onComplete() {
295
		// TODO Auto-generated method stub
296
		return false;
297
	}
298

    
299
}
(21-21/21)