Project

General

Profile

Download (4.36 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.e4.handler;
11

    
12
import java.util.ArrayList;
13
import java.util.HashSet;
14
import java.util.Iterator;
15
import java.util.List;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import javax.inject.Named;
20

    
21
import org.eclipse.e4.core.di.annotations.CanExecute;
22
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
23
import org.eclipse.e4.ui.services.IServiceConstants;
24
import org.eclipse.jface.dialogs.MessageDialog;
25
import org.eclipse.jface.viewers.TreeSelection;
26
import org.eclipse.swt.widgets.Shell;
27

    
28
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
29
import eu.etaxonomy.cdm.model.common.CdmBase;
30
import eu.etaxonomy.cdm.model.taxon.Classification;
31
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
32
import eu.etaxonomy.taxeditor.model.AbstractUtility;
33
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
34
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
35
import eu.etaxonomy.taxeditor.navigation.navigator.TreeNodeDropAdapter.MovingType;
36
import eu.etaxonomy.taxeditor.navigation.navigator.e4.TaxonNavigatorE4;
37
import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveTaxonOperation;
38
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
39
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
40
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
41
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
42

    
43
/**
44
 *
45
 * @author pplitzner
46
 * @date 05.09.2017
47
 *
48
 */
49
public class MoveTaxonHandlerE4 implements IPostOperationEnabled {
50

    
51
	private TaxonNode parentTaxonNode;
52

    
53
    public void execute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection,
54
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
55
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart) {
56

    
57
        TaxonNavigatorE4 taxonNavigator = (TaxonNavigatorE4) activePart.getObject();
58

    
59
		Iterator<?> selectionIterator = selection.iterator();
60
		Set<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
61
		TaxonNode taxonNode= null;
62
		Set<UUID> taxonNodeUUIDs = new HashSet<UUID>();
63
		// do not show the current selection
64
		List<UUID> excludeTaxa = new ArrayList<UUID>();
65

    
66
		while (selectionIterator.hasNext()){
67
			Object object = selectionIterator.next();
68
			if(object instanceof TaxonNode){
69
			    taxonNode = HibernateProxyHelper.deproxy(object,TaxonNode.class);
70
				taxonNodes.add(taxonNode);
71
				taxonNodeUUIDs.add(taxonNode.getUuid());
72
				excludeTaxa.add(taxonNode.getTaxon().getUuid());
73
			}
74
		}
75

    
76

    
77
		if (taxonNodes.size() >= 1){
78
		    Classification classification = taxonNodes.iterator().next().getClassification();
79
			MovingType moveToNewParent = MovingType.CHILD;
80
			if (PreferencesUtil.getSortNodesNaturally()){
81
				if(!MessageDialog.openQuestion(null, Messages.MoveTaxonHandler_TARGET_NODE, Messages.MoveTaxonHandler_TARGET_NODE_MESSAGE)){
82
					moveToNewParent = MovingType.BEHIND;
83
				}
84
				parentTaxonNode = TaxonNodeSelectionDialog.select(shell, taxonNavigator.getConversationHolder(), Messages.MoveTaxonHandler_CHOOSE_ABOVE, excludeTaxa, null, classification);
85
			}else{
86
				parentTaxonNode = TaxonNodeSelectionDialog.select(shell, taxonNavigator.getConversationHolder(), Messages.MoveTaxonHandler_CHOOSE_PARENT, excludeTaxa, null, classification);
87
			}
88
			if(parentTaxonNode != null){
89
				if(NavigationUtil.isDirty(parentTaxonNode)){
90
					MessageDialog.openWarning(shell, Messages.MoveTaxonHandler_UNSAVED_PARENT, Messages.MoveTaxonHandler_UNSAVED_PARENT_MESSAGE);
91
					return;
92
				}
93

    
94
				AbstractPostOperation<?> operation = new MoveTaxonOperation
95
						(Messages.MoveTaxonHandler_MOVE_TO_PARENT, NavigationUtil.getUndoContext(), taxonNodeUUIDs, parentTaxonNode, taxonNavigator, taxonNavigator, moveToNewParent); //$NON-NLS-1$
96
				AbstractUtility.executeOperation(operation);
97
				taxonNavigator.refresh();
98

    
99
			}
100
		}
101
	}
102

    
103
    @CanExecute
104
    private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection){
105
        return selection.getFirstElement() instanceof TaxonNode;
106
    }
107

    
108
	/** {@inheritDoc} */
109
	@Override
110
    public boolean postOperation(CdmBase objectAffectedByOperation) {
111
		return true;
112
	}
113

    
114
	@Override
115
    public boolean onComplete() {
116
		return false;
117
	}
118

    
119
}
(7-7/11)