Project

General

Profile

Download (6.67 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2015 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
package eu.etaxonomy.taxeditor.navigation.navigator.e4.handler;
10

    
11
import java.util.HashSet;
12
import java.util.Set;
13
import java.util.UUID;
14

    
15
import javax.inject.Named;
16

    
17
import org.eclipse.core.commands.operations.AbstractOperation;
18
import org.eclipse.core.runtime.IStatus;
19
import org.eclipse.core.runtime.Status;
20
import org.eclipse.e4.core.di.annotations.CanExecute;
21
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
22
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
23
import org.eclipse.e4.ui.services.IServiceConstants;
24
import org.eclipse.jface.viewers.IStructuredSelection;
25
import org.eclipse.jface.viewers.TreeSelection;
26
import org.eclipse.swt.widgets.Display;
27
import org.eclipse.swt.widgets.Shell;
28

    
29
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
30
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
31
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
32
import eu.etaxonomy.taxeditor.event.EventUtility;
33
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
34
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
35
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
36
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
37
import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveFactualDataOperation;
38
import eu.etaxonomy.taxeditor.operation.e4.CdmHandlerE4;
39
import eu.etaxonomy.taxeditor.store.CdmStore;
40
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
41

    
42
/**
43
 * @author cmathew
44
 * @date 19 Jun 2015
45
 *
46
 */
47
public class MoveFactualDataHandlerE4 extends CdmHandlerE4 {
48

    
49
    private TaxonNodeDto sourceTaxonNode;
50
    private TaxonNode targetTaxonNode;
51
    private boolean setNameInSource = false;
52

    
53
    public MoveFactualDataHandlerE4() {
54
        super(TaxonNavigatorLabels.MOVE_FACTUAL_DATA_LABEL);
55
    }
56

    
57
    @Override
58
    public IStatus allowOperations(IStructuredSelection selection,
59
            Shell shell,
60
            MPart activePart,
61
            MHandledMenuItem menuItem) {
62
        // check that only a single taxon tree node has been selected
63
        if(selection.size() > 1) {
64
            return new Status(IStatus.ERROR,
65
                    "unknown", //$NON-NLS-1$
66
                    TaxonNavigatorLabels.SINGLE_TAXON_SELECTION_MESSAGE);
67
        }
68

    
69
        // check for no taxon tree node selected
70
        if(selection.size() == 0) {
71
            return new Status(IStatus.ERROR,
72
                    "unknown", //$NON-NLS-1$
73
                    TaxonNavigatorLabels.NO_TAXON_SELECTION_MESSAGE);
74
        }
75

    
76
        // check that selected object is a taxon node
77
        Object obj = selection.iterator().next();
78
        if(obj instanceof TaxonNodeDto) {
79
            sourceTaxonNode = (TaxonNodeDto)obj;
80
        } else {
81
            return new Status(IStatus.ERROR,
82
                    "unknown", //$NON-NLS-1$
83
                    TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
84
        }
85

    
86
        if(NavigationUtil.isDirty(sourceTaxonNode, partService)) {
87
            return new Status(IStatus.ERROR,
88
                    "unknown", //$NON-NLS-1$
89
                    TaxonNavigatorLabels.UNSAVED_CHANGES_MESSAGE);
90
        }
91

    
92
        Set<UUID> excludeTaxa = new HashSet<>();
93
        excludeTaxa.add(sourceTaxonNode.getUuid());
94

    
95
        targetTaxonNode = TaxonNodeSelectionDialog.select(shell,
96
                Messages.RemotingMoveFactualDataHandler_CHOOSE_TAXA,
97
                excludeTaxa,
98
                CdmStore.getService(ITaxonNodeService.class).find(sourceTaxonNode.getUuid()),
99
                sourceTaxonNode.getClassificationUUID());
100

    
101
        if(targetTaxonNode == null) {
102
            return new Status(IStatus.CANCEL,
103
                    "unknown", //$NON-NLS-1$
104
                    ""); //$NON-NLS-1$
105
        }
106
        if(NavigationUtil.isDirty(targetTaxonNode, partService)){
107
            return new Status(IStatus.ERROR,
108
                    "unknown", //$NON-NLS-1$
109
                    TaxonNavigatorLabels.UNSAVED_CHANGES_MESSAGE);
110
        }
111
        return Status.OK_STATUS;
112
    }
113

    
114
    @Override
115
    public AbstractOperation prepareOperation(IStructuredSelection selection,
116
            Shell shell,
117
            MPart activePart,
118
            MHandledMenuItem menuItem) {
119
        return new MoveFactualDataOperation(getTrigger(),
120
                false,
121
                sourceTaxonNode.getTaxonUuid(),
122
                targetTaxonNode.getTaxon().getUuid(), setNameInSource);
123
    }
124

    
125
    @CanExecute
126
    private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
127
        boolean canExecute = false;
128
        canExecute = selection.size()==1
129
                && selection.getFirstElement() instanceof TaxonNodeDto
130
                && ((TaxonNodeDto)selection.getFirstElement()).getTaxonUuid()  != null;
131
        menuItem.setVisible(canExecute);
132
        return canExecute;
133
    }
134

    
135
    @Override
136
    public void onComplete() {
137
        Display.getDefault().asyncExec(new Runnable(){
138
            @Override
139
            public void run() {
140

    
141
                EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAME_EDITOR, sourceTaxonNode.getTaxonUuid());
142
                EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAME_EDITOR, targetTaxonNode.getTaxon().getUuid());
143
                //FIXME E4 refresh factual view via events or similar
144
//                try {
145
//                    //close and re-open to refresh factual data view
146
//                    MultiPageTaxonEditor sourceEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(sourceTaxonNode.getUuid());
147
//                    MultiPageTaxonEditor targetEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(targetTaxonNode.getUuid());
148
//                    if(targetEditor != null){
149
//                        AbstractUtility.close(sourceEditor);
150
//                        AbstractUtility.close(targetEditor);
151
//                    }
152
//                    EditorUtil.openTaxonNodeE4(sourceTaxonNode.getUuid());
153
//                    EditorUtil.openTaxonNodeE4(targetTaxonNode.getUuid());
154
//                } catch (PartInitException e) {
155
//                    MessagingUtils.error(this.getClass(), e);
156
//                    throw new RuntimeException(e);
157
//                } catch (Exception e) {
158
//                    MessagingUtils.warningDialog(Messages.RemotingMoveFactualDataHandler_CREATE_FAILED, this, e.getMessage());
159
//                }
160
            }
161

    
162
        });
163
    }
164

    
165
    /**
166
     * {@inheritDoc}
167
     */
168
    @Override
169
    protected Object getTrigger() {
170
        return this;
171
    }
172
}
(11-11/19)