Project

General

Profile

Download (6.7 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.RemotingMoveFactualDataOperation;
38
import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
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 RemotingMoveFactualDataHandlerE4 extends RemotingCdmHandlerE4 {
48

    
49
    private TaxonNodeDto sourceTaxonNode;
50
    private TaxonNode targetTaxonNode;
51

    
52
    public RemotingMoveFactualDataHandlerE4() {
53
        super(TaxonNavigatorLabels.MOVE_FACTUAL_DATA_LABEL);
54
    }
55

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

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

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

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

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

    
94
        targetTaxonNode = TaxonNodeSelectionDialog.select(shell,
95
//                new ConversationHolderMock(),
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 RemotingMoveFactualDataOperation(getTrigger(),
120
                false,
121
                sourceTaxonNode.getTaxonUuid(),
122
                targetTaxonNode.getTaxon().getUuid());
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
}
(12-12/15)