Project

General

Profile

Download (4.56 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.model.application.ui.menu.MHandledMenuItem;
24
import org.eclipse.e4.ui.services.IServiceConstants;
25
import org.eclipse.jface.dialogs.MessageDialog;
26
import org.eclipse.jface.viewers.TreeSelection;
27
import org.eclipse.swt.widgets.Shell;
28

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

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

    
52
	private TaxonNode parentTaxonNode;
53

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

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

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

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

    
77

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

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

    
100
			}
101
		}
102
	}
103

    
104
    @CanExecute
105
    private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
106
        boolean canExecute = false;
107
        menuItem.setVisible(canExecute);
108
        canExecute = selection.getFirstElement() instanceof TaxonNode;
109
        return canExecute;
110
    }
111

    
112
	/** {@inheritDoc} */
113
	@Override
114
    public boolean postOperation(CdmBase objectAffectedByOperation) {
115
		return true;
116
	}
117

    
118
	@Override
119
    public boolean onComplete() {
120
		return false;
121
	}
122

    
123
}
(7-7/11)