Project

General

Profile

Download (5.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.key.polytomous.e4.handler;
10

    
11
import java.util.Collection;
12

    
13
import javax.inject.Named;
14

    
15
import org.eclipse.core.runtime.IProgressMonitor;
16
import org.eclipse.core.runtime.IStatus;
17
import org.eclipse.core.runtime.Status;
18
import org.eclipse.core.runtime.jobs.Job;
19
import org.eclipse.e4.core.di.annotations.CanExecute;
20
import org.eclipse.e4.core.di.annotations.Execute;
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.e4.ui.workbench.modeling.EPartService;
25
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
26
import org.eclipse.jface.viewers.IStructuredSelection;
27
import org.eclipse.jface.viewers.StructuredSelection;
28
import org.eclipse.swt.widgets.Display;
29

    
30
import eu.etaxonomy.cdm.model.description.PolytomousKey;
31
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
32
import eu.etaxonomy.taxeditor.editor.key.polytomous.e4.PolytomousKeyListEditorE4;
33
import eu.etaxonomy.taxeditor.model.MessagingUtils;
34
import eu.etaxonomy.taxeditor.navigation.key.polytomous.PolytomousKeyViewLabels;
35
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
36

    
37
/**
38
 * @author cmathew
39
 * @date 29 Jun 2015
40
 *
41
 */
42
public class RemotingEditPolytomousKeyNodesHandlerE4 {
43

    
44
    public static final String OPENING_POLYTOMOUS_KEYS = Messages.EditPolytomousKeyNodesHandler_OPEN_KEYS;
45

    
46
    @Execute
47
    public void execute(EPartService partService,
48
            @Named(IServiceConstants.ACTIVE_SELECTION)Object selection) {
49

    
50
        if(selection instanceof StructuredSelection){
51

    
52
            final StructuredSelection structuredSelection = (StructuredSelection) selection;
53

    
54
            Job job = new Job(OPENING_POLYTOMOUS_KEYS){
55

    
56
                @Override
57
                protected IStatus run(IProgressMonitor monitor) {
58
                    monitor.beginTask(OPENING_POLYTOMOUS_KEYS, structuredSelection.size());
59

    
60
                    for(final Object selectedObject : structuredSelection.toArray()){
61
                        if(selectedObject instanceof PolytomousKey){
62

    
63
                            Display.getDefault().asyncExec(new Runnable(){
64

    
65
                                @Override
66
                                public void run() {
67
                                    try {
68
                                        PolytomousKey key = (PolytomousKey) selectedObject;
69
                                        PolytomousKeyEditorInput input = PolytomousKeyEditorInput.NewInstance(key.getUuid());
70

    
71
                                        //check if editor already open
72
                                        Collection<MPart> parts = partService.getParts();
73
                                        MPart part = null;
74
                                        //check if part is already opened
75
                                        for (MPart mPart : parts) {
76
                                            if(mPart.getObject() instanceof PolytomousKeyListEditorE4
77
                                                    && ((PolytomousKeyListEditorE4) mPart.getObject()).getViewerInputKey().equals(input.getKey())){
78
                                                part = mPart;
79
                                                break;
80
                                            }
81
                                        }
82
                                        if(part==null){
83
                                            part = partService.createPart("eu.etaxonomy.taxeditor.editor.key.polytomous.e4.PolytomousKeyListEditorE4");
84
                                            part = partService.showPart(part, PartState.ACTIVATE);
85
                                            PolytomousKeyListEditorE4 editor = (PolytomousKeyListEditorE4) part.getObject();
86
                                            editor.init(input);
87
                                        }
88
                                        part = partService.showPart(part, PartState.ACTIVATE);
89
                                    } catch(Exception ex) {
90
                                        MessagingUtils.warningDialog(PolytomousKeyViewLabels.ERROR_OPENING_KEY_EDITOR_MESSAGE,
91
                                                RemotingEditPolytomousKeyNodesHandlerE4.this,
92
                                                ex.getLocalizedMessage());
93
                                        ex.printStackTrace();
94
                                    }
95
                                }
96

    
97
                            });
98
                            monitor.worked(1);
99
                        }
100
                    }
101
                    monitor.done();
102
                    return Status.OK_STATUS;
103
                }
104

    
105
            };
106

    
107
            job.setPriority(Job.SHORT);
108
            job.schedule();
109

    
110
        }
111
    }
112

    
113
    @CanExecute
114
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
115
            MHandledMenuItem menuItem){
116
        boolean canExecute = false;
117
        canExecute = ((IStructuredSelection)selection).getFirstElement() instanceof PolytomousKey;
118
        menuItem.setVisible(canExecute);
119
        return canExecute;
120
    }
121

    
122
}
(5-5/6)