Project

General

Profile

Download (3.78 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2015 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.taxeditor.navigation.key.polytomous.handler;
11

    
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.IStructuredSelection;
20
import org.eclipse.ui.IEditorInput;
21
import org.eclipse.ui.IEditorReference;
22
import org.eclipse.ui.IWorkbenchPage;
23
import org.eclipse.ui.PartInitException;
24
import org.eclipse.ui.handlers.HandlerUtil;
25

    
26
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
27
import eu.etaxonomy.taxeditor.model.MessagingUtils;
28
import eu.etaxonomy.taxeditor.navigation.key.polytomous.PolytomousKeyViewLabels;
29
import eu.etaxonomy.taxeditor.navigation.key.polytomous.operation.RemotingDeletePolytomousKeyOperation;
30
import eu.etaxonomy.taxeditor.operation.RemotingCdmHandler;
31
import eu.etaxonomy.taxeditor.util.OperationsUtil;
32

    
33
/**
34
 * @author cmathew
35
 * @date 25 Jun 2015
36
 *
37
 */
38
public class RemotingDeletePolytomousKeyHandler extends RemotingCdmHandler {
39

    
40
    List<UUID> keysToDelete;
41
    /**
42
     * @param label
43
     */
44
    public RemotingDeletePolytomousKeyHandler() {
45
        super(PolytomousKeyViewLabels.DELETE_POLYTOMOUS_KEY_LABEL);
46
    }
47

    
48
    /* (non-Javadoc)
49
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#allowOperations(org.eclipse.core.commands.ExecutionEvent)
50
     */
51
    @Override
52
    public IStatus allowOperations(ExecutionEvent event) {
53
        IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
54

    
55
        keysToDelete = OperationsUtil.convertToUuidList(selection.toList());
56

    
57
        if(keysToDelete.isEmpty()){
58
           return Status.CANCEL_STATUS;
59
        }
60

    
61
        boolean confirmation = MessagingUtils.confirmDialog("Confirm deletaion", "Do you want to delete the selected key" + (keysToDelete.size() == 1 ? "" : "s") + "?");
62

    
63
        if(!confirmation) {
64
            return Status.CANCEL_STATUS;
65
        }
66
        closeObsoleteEditor(event);
67
        return Status.OK_STATUS;
68
    }
69

    
70
    /* (non-Javadoc)
71
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#prepareOperation(org.eclipse.core.commands.ExecutionEvent)
72
     */
73
    @Override
74
    public AbstractOperation prepareOperation(ExecutionEvent event) {
75
        return new RemotingDeletePolytomousKeyOperation(event.getTrigger(),
76
                false,
77
                keysToDelete);
78
    }
79

    
80
    /* (non-Javadoc)
81
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#onComplete()
82
     */
83
    @Override
84
    public void onComplete() {
85
        // TODO Auto-generated method stub
86

    
87
    }
88
    protected boolean closeObsoleteEditor(ExecutionEvent event){
89
        boolean result = true;
90
        IWorkbenchPage activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
91

    
92
        for (IEditorReference ref : activePage.getEditorReferences()) {
93
            try {
94
                IEditorInput input = ref.getEditorInput();
95
                if (input instanceof PolytomousKeyEditorInput) {
96
                    UUID uuidForKey = ((PolytomousKeyEditorInput) input).getKey().getUuid();
97
                    for (UUID keyUuid :keysToDelete) {
98
                        if( uuidForKey.equals(keyUuid)){
99
                            result &= activePage.closeEditor(ref.getEditor(false), false);
100
                        }
101
                    }
102

    
103
                }
104
            } catch (PartInitException e) {
105
                continue;
106
            }
107
        }
108
        return result;
109
    }
110

    
111
}
(6-6/8)