Project

General

Profile

Download (6.07 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.AppModelId;
35
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
36
import eu.etaxonomy.taxeditor.editor.key.polytomous.e4.PolytomousKeyListEditorE4;
37
import eu.etaxonomy.taxeditor.model.MessagingUtils;
38
import eu.etaxonomy.taxeditor.navigation.key.polytomous.PolytomousKeyViewLabels;
39
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
40
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
41

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

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

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

    
55
        if(selection instanceof StructuredSelection){
56

    
57
            final StructuredSelection structuredSelection = (StructuredSelection) selection;
58

    
59
            Job job = new Job(OPENING_POLYTOMOUS_KEYS){
60

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

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

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

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

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

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

    
114
            };
115

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

    
119
        }
120
    }
121

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

    
131
}
(5-5/6)