Project

General

Profile

Download (6 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.MApplication;
22
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
23
import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
24
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
25
import org.eclipse.e4.ui.services.IServiceConstants;
26
import org.eclipse.e4.ui.workbench.modeling.EModelService;
27
import org.eclipse.e4.ui.workbench.modeling.EPartService;
28
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
29
import org.eclipse.jface.viewers.IStructuredSelection;
30
import org.eclipse.jface.viewers.StructuredSelection;
31
import org.eclipse.swt.widgets.Display;
32

    
33
import eu.etaxonomy.cdm.model.description.PolytomousKey;
34
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
35
import eu.etaxonomy.taxeditor.editor.key.polytomous.e4.PolytomousKeyListEditorE4;
36
import eu.etaxonomy.taxeditor.model.MessagingUtils;
37
import eu.etaxonomy.taxeditor.navigation.key.polytomous.PolytomousKeyViewLabels;
38
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
39
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
40

    
41
/**
42
 * @author cmathew
43
 * @date 29 Jun 2015
44
 *
45
 */
46
public class RemotingEditPolytomousKeyNodesHandlerE4 {
47

    
48
    public static final String OPENING_POLYTOMOUS_KEYS = Messages.EditPolytomousKeyNodesHandler_OPEN_KEYS;
49

    
50
    @Execute
51
    public void execute(EModelService modelService, EPartService partService, MApplication application,
52
            @Named(IServiceConstants.ACTIVE_SELECTION)Object selection) {
53

    
54
        if(selection instanceof StructuredSelection){
55

    
56
            final StructuredSelection structuredSelection = (StructuredSelection) selection;
57

    
58
            Job job = new Job(OPENING_POLYTOMOUS_KEYS){
59

    
60
                @Override
61
                protected IStatus run(IProgressMonitor monitor) {
62
                    monitor.beginTask(OPENING_POLYTOMOUS_KEYS, structuredSelection.size());
63

    
64
                    for(final Object selectedObject : structuredSelection.toArray()){
65
                        if(selectedObject instanceof PolytomousKey){
66

    
67
                            Display.getDefault().asyncExec(new Runnable(){
68

    
69
                                @Override
70
                                public void run() {
71
                                    try {
72
                                        PolytomousKey key = (PolytomousKey) selectedObject;
73
                                        PolytomousKeyEditorInput input = PolytomousKeyEditorInput.NewInstance(key.getUuid());
74

    
75
                                        //check if editor already open
76
                                        Collection<MPart> parts = partService.getParts();
77
                                        MPart part = null;
78
                                        //check if part is already opened
79
                                        for (MPart mPart : parts) {
80
                                            if(mPart.getObject() instanceof PolytomousKeyListEditorE4
81
                                                    && ((PolytomousKeyListEditorE4) mPart.getObject()).getViewerInputKey().equals(input.getKey())){
82
                                                part = mPart;
83
                                                break;
84
                                            }
85
                                        }
86
                                        if(part==null){
87
                                            part = partService.createPart("eu.etaxonomy.taxeditor.editor.key.polytomous.e4.PolytomousKeyListEditorE4");
88
                                            MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
89
                                            if(editorAreaPartStack!=null){
90
                                                editorAreaPartStack.getChildren().add(part);
91
                                            }
92
                                            part = partService.showPart(part, PartState.ACTIVATE);
93
                                            PolytomousKeyListEditorE4 editor = (PolytomousKeyListEditorE4) part.getObject();
94
                                            editor.init(input);
95
                                        }
96
                                        part = partService.showPart(part, PartState.ACTIVATE);
97
                                    } catch(Exception ex) {
98
                                        MessagingUtils.warningDialog(PolytomousKeyViewLabels.ERROR_OPENING_KEY_EDITOR_MESSAGE,
99
                                                RemotingEditPolytomousKeyNodesHandlerE4.this,
100
                                                ex.getLocalizedMessage());
101
                                        ex.printStackTrace();
102
                                    }
103
                                }
104

    
105
                            });
106
                            monitor.worked(1);
107
                        }
108
                    }
109
                    monitor.done();
110
                    return Status.OK_STATUS;
111
                }
112

    
113
            };
114

    
115
            job.setPriority(Job.SHORT);
116
            job.schedule();
117

    
118
        }
119
    }
120

    
121
    @CanExecute
122
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
123
            MHandledMenuItem menuItem){
124
        boolean canExecute = false;
125
        canExecute = ((IStructuredSelection)selection).getFirstElement() instanceof PolytomousKey;
126
        menuItem.setVisible(canExecute);
127
        return canExecute;
128
    }
129

    
130
}
(5-5/6)