Project

General

Profile

Download (10.9 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.navigation.NavigationUtil;
39
import eu.etaxonomy.taxeditor.navigation.navigator.TreeNodeDropAdapter.MovingType;
40
import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveTaxonOperation;
41
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveTaxonOperation;
42
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
43
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
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());
129
                }
130

    
131
		        if(!hasTargetNodePermission || ! hasTargetNodePermission){
132
		            if(logger.isDebugEnabled()){
133
		                logger.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission.toString() + " " + hasTargetTaxonPermission.toString() + " ");
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());
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() + " ");
153
                    }
154
					return Status.CANCEL_STATUS;
155
				}
156
			}
157
			logger.debug("OK_STATUS");
158
			return Status.OK_STATUS;
159
		}
160
		logger.debug("CANCEL_STATUS");
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) NavigationUtil.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(), "Unsaved Parent Taxon", "There are unsaved " +
181
				"changes in the parent taxon. Pleas save first.");
182
				return Status.CANCEL_STATUS;
183
			}
184

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

    
200
			AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, (TaxonNode)targetITaxonTreeNode, MovingType.CHILD);
201
			NavigationUtil.executeOperation(operation, null);
202
			
203

    
204
			logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
205
			return Status.OK_STATUS;
206
		}else{
207
			String[] buttonLables = {"Child", "Behind", "Cancel"};
208
			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);
209
			dialog.open();
210
			int returnCode = dialog.getReturnCode();
211
			if (returnCode == 0){
212
				IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
213
				if (workspaceUndoContext == null) {
214
					logger.error("Workspace undo context is null. DND operation cancelled");
215
					return Status.CANCEL_STATUS;
216
				}
217

    
218
				AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, (TaxonNode)targetITaxonTreeNode, MovingType.CHILD);
219
				NavigationUtil.executeOperation(operation, null);
220
				
221

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

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

    
259

    
260
		}
261
	}
262

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

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

    
283
}
(21-21/21)