Project

General

Profile

Download (4.8 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.core.di.annotations.Execute;
23
import org.eclipse.e4.ui.di.UISynchronize;
24
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
25
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
26
import org.eclipse.e4.ui.services.IServiceConstants;
27
import org.eclipse.e4.ui.workbench.modeling.EPartService;
28
import org.eclipse.jface.dialogs.MessageDialog;
29
import org.eclipse.jface.viewers.TreeSelection;
30
import org.eclipse.swt.widgets.Shell;
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.TaxonNode;
36
import eu.etaxonomy.taxeditor.model.AbstractUtility;
37
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
38
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
39
import eu.etaxonomy.taxeditor.navigation.navigator.e4.TaxonNavigatorE4;
40
import eu.etaxonomy.taxeditor.navigation.navigator.e4.TreeNodeDropAdapterE4.MovingType;
41
import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveTaxonOperation;
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.ui.dialog.selection.TaxonNodeSelectionDialog;
46

    
47
/**
48
 *
49
 * @author pplitzner
50
 * @date 05.09.2017
51
 *
52
 */
53
public class MoveTaxonHandlerE4 implements IPostOperationEnabled {
54

    
55
	private TaxonNode parentTaxonNode;
56

    
57
    @Execute
58
    public void execute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection,
59
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
60
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart, EPartService partService,
61
            UISynchronize sync) {
62

    
63
        TaxonNavigatorE4 taxonNavigator = (TaxonNavigatorE4) activePart.getObject();
64

    
65
		Iterator<?> selectionIterator = selection.iterator();
66
		Set<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
67
		TaxonNode taxonNode= null;
68
		Set<UUID> taxonNodeUUIDs = new HashSet<UUID>();
69
		// do not show the current selection
70
		List<UUID> excludeTaxa = new ArrayList<UUID>();
71

    
72
		while (selectionIterator.hasNext()){
73
			Object object = selectionIterator.next();
74
			if(object instanceof TaxonNode){
75
			    taxonNode = HibernateProxyHelper.deproxy(object,TaxonNode.class);
76
				taxonNodes.add(taxonNode);
77
				taxonNodeUUIDs.add(taxonNode.getUuid());
78
				excludeTaxa.add(taxonNode.getTaxon().getUuid());
79
			}
80
		}
81

    
82

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

    
100
				AbstractPostOperation<?> operation = new MoveTaxonOperation
101
						(Messages.MoveTaxonHandler_MOVE_TO_PARENT, NavigationUtil.getUndoContext(), taxonNodeUUIDs, parentTaxonNode, taxonNavigator, taxonNavigator, moveToNewParent); //$NON-NLS-1$
102
				AbstractUtility.executeOperation(operation, sync);
103
				taxonNavigator.refresh();
104

    
105
			}
106
		}
107
	}
108

    
109
    @CanExecute
110
    private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
111
        boolean canExecute = false;
112
        canExecute = selection.getFirstElement() instanceof TaxonNode;
113
        menuItem.setVisible(canExecute);
114
        return canExecute;
115
    }
116

    
117
	/** {@inheritDoc} */
118
	@Override
119
    public boolean postOperation(CdmBase objectAffectedByOperation) {
120
		return true;
121
	}
122

    
123
	@Override
124
    public boolean onComplete() {
125
		return false;
126
	}
127

    
128
}
(8-8/17)