Project

General

Profile

Download (6.36 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.navigation.NavigationUtil;
33
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
34
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
35
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveFactualDataOperation;
36
import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
37
import eu.etaxonomy.taxeditor.store.CdmStore;
38
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
39

    
40
/**
41
 * @author cmathew
42
 * @date 19 Jun 2015
43
 *
44
 */
45
public class RemotingMoveFactualDataHandlerE4 extends RemotingCdmHandlerE4 {
46

    
47
    private TaxonNodeDto sourceTaxonNode;
48
    private TaxonNode targetTaxonNode;
49

    
50
    public RemotingMoveFactualDataHandlerE4() {
51
        super(TaxonNavigatorLabels.MOVE_FACTUAL_DATA_LABEL);
52
    }
53

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

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

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

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

    
89
        Set<UUID> excludeTaxa = new HashSet<>();
90
        excludeTaxa.add(sourceTaxonNode.getTaxonUuid());
91

    
92
        targetTaxonNode = TaxonNodeSelectionDialog.select(shell,
93
//                new ConversationHolderMock(),
94
                Messages.RemotingMoveFactualDataHandler_CHOOSE_TAXA,
95
                excludeTaxa,
96
                CdmStore.getService(ITaxonNodeService.class).find(sourceTaxonNode.getUuid()),
97
                sourceTaxonNode.getClassificationUUID());
98

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

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

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

    
133
    @Override
134
    public void onComplete() {
135
        Display.getDefault().asyncExec(new Runnable(){
136
            @Override
137
            public void run() {
138
                //FIXME E4 refresh factual view via events or similar
139
//                try {
140
//                    //close and re-open to refresh factual data view
141
//                    MultiPageTaxonEditor sourceEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(sourceTaxonNode.getUuid());
142
//                    MultiPageTaxonEditor targetEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(targetTaxonNode.getUuid());
143
//                    if(targetEditor != null){
144
//                        AbstractUtility.close(sourceEditor);
145
//                        AbstractUtility.close(targetEditor);
146
//                    }
147
//                    EditorUtil.openTaxonNodeE4(sourceTaxonNode.getUuid());
148
//                    EditorUtil.openTaxonNodeE4(targetTaxonNode.getUuid());
149
//                } catch (PartInitException e) {
150
//                    MessagingUtils.error(this.getClass(), e);
151
//                    throw new RuntimeException(e);
152
//                } catch (Exception e) {
153
//                    MessagingUtils.warningDialog(Messages.RemotingMoveFactualDataHandler_CREATE_FAILED, this, e.getMessage());
154
//                }
155
            }
156

    
157
        });
158
    }
159

    
160
    /**
161
     * {@inheritDoc}
162
     */
163
    @Override
164
    protected Object getTrigger() {
165
        return this;
166
    }
167
}
(11-11/14)