Project

General

Profile

Download (11.3 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.preference.PreferencesUtil;
44
import eu.etaxonomy.taxeditor.store.CdmStore;
45

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

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

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

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

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

    
70

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

    
89
		return Status.CANCEL_STATUS;
90
	}
91

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

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

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

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

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

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

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

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

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

    
163

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

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

    
174
		if(targetITaxonTreeNode instanceof TaxonNode){
175

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

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

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

    
201

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

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

    
219

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

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

    
233
				logger.info("Moved taxa to new parent " + targetITaxonTreeNode); //$NON-NLS-1$
234
				return Status.OK_STATUS;
235
//			}else if (returnCode == 2){
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.PREVIOUS);
245
//                    NavigationUtil.executeOperation(operation, null);
246
//                } else {
247
//                    AbstractPostOperation operation = new MoveTaxonOperation
248
//                            ("Move Taxon", workspaceUndoContext, uuids, targetNode, this, taxonNavigator, MovingType.PREVIOUS);
249
//                    NavigationUtil.executeOperation(operation);
250
//                }
251
//                logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
252
//                return Status.OK_STATUS;
253
            } else{
254
				return Status.CANCEL_STATUS;
255
			}
256

    
257

    
258
		}
259
	}
260

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

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

    
281
}
(21-21/21)