Project

General

Profile

Download (6.87 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 MoveFactualDataSetNameInSourceHandlerE5 extends CdmHandlerE4 {
48

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

    
53
    public MoveFactualDataSetNameInSourceHandlerE5() {
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
        if (menuItem.getElementId().equals("eu.etaxonomy.taxeditor.navigation.handledmenuitem.movefactualdataSetNameInSource")){
120
            setNameInSource = true;
121
        }
122
        return new MoveFactualDataOperation(getTrigger(),
123
                false,
124
                sourceTaxonNode.getTaxonUuid(),
125
                targetTaxonNode.getTaxon().getUuid(), setNameInSource);
126
    }
127

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

    
138
    @Override
139
    public void onComplete() {
140
        Display.getDefault().asyncExec(new Runnable(){
141
            @Override
142
            public void run() {
143

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

    
165
        });
166
    }
167

    
168
    /**
169
     * {@inheritDoc}
170
     */
171
    @Override
172
    protected Object getTrigger() {
173
        return this;
174
    }
175
}
(12-12/19)