Project

General

Profile

Download (5.92 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.handler;
10

    
11
import java.util.ArrayList;
12
import java.util.List;
13
import java.util.UUID;
14

    
15
import org.eclipse.core.commands.ExecutionEvent;
16
import org.eclipse.core.commands.operations.AbstractOperation;
17
import org.eclipse.core.runtime.IStatus;
18
import org.eclipse.core.runtime.Status;
19
import org.eclipse.jface.viewers.TreeSelection;
20
import org.eclipse.swt.widgets.Display;
21
import org.eclipse.ui.PartInitException;
22
import org.eclipse.ui.handlers.HandlerUtil;
23

    
24
import eu.etaxonomy.cdm.api.conversation.ConversationHolderMock;
25
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
26
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
27
import eu.etaxonomy.taxeditor.editor.EditorUtil;
28
import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
29
import eu.etaxonomy.taxeditor.model.AbstractUtility;
30
import eu.etaxonomy.taxeditor.model.MessagingUtils;
31
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
32
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
33
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
34
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveFactualDataOperation;
35
import eu.etaxonomy.taxeditor.operation.RemotingCdmHandler;
36
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
37

    
38
/**
39
 * @author cmathew
40
 * @date 19 Jun 2015
41
 *
42
 */
43
public class RemotingMoveFactualDataHandler extends RemotingCdmHandler {
44

    
45

    
46
    private TaxonNode sourceTaxonNode;
47
    private TaxonNode targetTaxonNode;
48
    /**
49
     * @param label
50
     */
51
    public RemotingMoveFactualDataHandler() {
52
        super(TaxonNavigatorLabels.MOVE_FACTUAL_DATA_LABEL);
53
    }
54

    
55
    /* (non-Javadoc)
56
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#allowOperations(org.eclipse.core.commands.ExecutionEvent)
57
     */
58
    @Override
59
    public IStatus allowOperations(ExecutionEvent event) {
60
        TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
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 ITaxonTreeNode) {
78
            sourceTaxonNode = (TaxonNode)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)) {
86
            return new Status(IStatus.ERROR,
87
                    "unknown", //$NON-NLS-1$
88
                    TaxonNavigatorLabels.UNSAVED_CHANGES_MESSAGE);
89
        }
90

    
91
        List<UUID> excludeTaxa = new ArrayList<UUID>();
92
        excludeTaxa.add(sourceTaxonNode.getTaxon().getUuid());
93

    
94
        targetTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event),
95
                new ConversationHolderMock(),
96
                Messages.RemotingMoveFactualDataHandler_CHOOSE_TAXA,
97
                excludeTaxa,
98
                null,
99
                sourceTaxonNode.getClassification());
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)){
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
    /* (non-Javadoc)
115
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#prepareOperation(org.eclipse.core.commands.ExecutionEvent)
116
     */
117
    @Override
118
    public AbstractOperation prepareOperation(ExecutionEvent event) {
119
        return new RemotingMoveFactualDataOperation(event.getTrigger(),
120
                false,
121
                sourceTaxonNode.getTaxon().getUuid(),
122
                targetTaxonNode.getTaxon().getUuid());
123
    }
124

    
125
    /* (non-Javadoc)
126
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#onComplete()
127
     */
128
    @Override
129
    public void onComplete() {
130
        Display.getDefault().asyncExec(new Runnable(){
131
            @Override
132
            public void run() {
133
                try {
134
                    //close and re-open to refresh factual data view
135
                    MultiPageTaxonEditor sourceEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(sourceTaxonNode.getUuid());
136
                    MultiPageTaxonEditor targetEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(targetTaxonNode.getUuid());
137
                    if(targetEditor != null){
138
                        AbstractUtility.close(sourceEditor);
139
                        AbstractUtility.close(targetEditor);
140
                    }
141
                    EditorUtil.openTaxonNode(sourceTaxonNode.getUuid());
142
                    EditorUtil.openTaxonNode(targetTaxonNode.getUuid());
143
                } catch (PartInitException e) {
144
                    MessagingUtils.error(this.getClass(), e);
145
                    throw new RuntimeException(e);
146
                } catch (Exception e) {
147
                    MessagingUtils.warningDialog(Messages.RemotingMoveFactualDataHandler_CREATE_FAILED, this, e.getMessage());
148
                }
149
            }
150

    
151
        });
152

    
153
    }
154

    
155
}
(14-14/17)