Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d8fc45d4287d17f150f39459eea6f577e2371fea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*******************************************************************************
 * Copyright (c) 2010-2012, Andras Okros, Tamas Szabo, Istvan Rath and Daniel Varro
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-v20.html.
 * 
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.viatra.query.runtime.ui.modelconnector;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.ui.viewer.IViewerProvider;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.viatra.query.runtime.api.IModelConnectorTypeEnum;
import org.eclipse.viatra.query.runtime.matchers.util.Preconditions;
import org.eclipse.viatra.query.runtime.ui.ViatraQueryRuntimeUIPlugin;
import org.eclipse.viatra.query.runtime.ui.modelconnector.internal.ModelEditorPartListener;

/**
 * Model connector implementation for the default EMF generated model editors.
 */
public class EMFModelConnector implements IModelConnector {

    private static final String LISTENER_NULL_MSG = "Listener cannot be null!";

    protected IEditorPart editorPart;

    protected ILog logger;

    protected IWorkbenchPage workbenchPage;

    private ModelEditorPartListener modelEditorPartListener;
    
    private Set<IModelConnectorListener> listeners;

    public EMFModelConnector(IEditorPart editorPart) {
        super();
        this.logger = ViatraQueryRuntimeUIPlugin.getDefault().getLog();
        this.editorPart = editorPart;
        this.listeners = new HashSet<>();
    }

    @Override
    public void loadModel(IModelConnectorTypeEnum modelConnectorTypeEnum) {
        Notifier notifier = getNotifier(modelConnectorTypeEnum);
        if (notifier != null) {
            workbenchPage = editorPart.getSite().getPage();
            modelEditorPartListener = new ModelEditorPartListener(this);
            workbenchPage.addPartListener(modelEditorPartListener);
        }
    }

    @Override
    public void unloadModel() {
        for (IModelConnectorListener listener : listeners) {
            listener.modelUnloaded(this);
        }
        workbenchPage.removePartListener(modelEditorPartListener);
    }

    @Override
    public void showLocation(Object[] locationObjects) {
        IStructuredSelection preparedSelection = prepareSelection(locationObjects);
        navigateToElements(editorPart, preparedSelection);
        workbenchPage.bringToTop(editorPart);
        if (editorPart instanceof ISelectionProvider) {
            ISelectionProvider selectionProvider = (ISelectionProvider) editorPart;
            selectionProvider.setSelection(preparedSelection);
            
        }
        reflectiveSetSelection(editorPart, preparedSelection);
    }

    @Override
    public Notifier getNotifier(IModelConnectorTypeEnum modelConnectorTypeEnum) {
        Notifier result = null;
        if (IModelConnectorTypeEnum.RESOURCESET.equals(modelConnectorTypeEnum)) {
            if (editorPart instanceof IEditingDomainProvider) {
                IEditingDomainProvider editingDomainProvider = (IEditingDomainProvider) editorPart;
                result = editingDomainProvider.getEditingDomain().getResourceSet();
            }
        } else if (IModelConnectorTypeEnum.RESOURCE.equals(modelConnectorTypeEnum) && editorPart instanceof ISelectionProvider) {
            ISelectionProvider selectionProvider = (ISelectionProvider) editorPart;
            if (selectionProvider.getSelection() instanceof TreeSelection) {
                Object object = ((TreeSelection) selectionProvider.getSelection()).getFirstElement();
                if (object instanceof Resource) {
                    result = (Resource) object;
                } else if (object instanceof EObject) {
                    result = ((EObject) object).eResource();
                }
            }
        }
        return result;
    }

    /**
     * This is a somewhat "hackish" workaround in case the selection setting through the provider doesn't work.
     * 
     * Unfortunately, this seems to be the most reliable way to do this. *sigh*
     * 
     */
    private void reflectiveSetSelection(IEditorPart editorPart, IStructuredSelection preparedSelection) {
        if (editorPart instanceof IViewerProvider 
                && (((IViewerProvider) editorPart).getViewer() instanceof TreeViewer)) {
            try {
                Method m = editorPart.getClass().getMethod("setSelectionToViewer", Collection.class);
                if (m!=null) {
                    m.invoke(editorPart, preparedSelection.toList());
                }
            } catch (Exception e) {
                logger.log(new Status(IStatus.INFO, ViatraQueryRuntimeUIPlugin.PLUGIN_ID, "Error while setting selection. If this is not an EMF Tree editor, consider providing a specialized ModelConnector implementation.", e));
            }
        }
    }

    protected TreeSelection prepareSelection(Object[] locationObjects) {
        List<TreePath> paths = new ArrayList<>();
        for (Object o : locationObjects) {
            if (o instanceof EObject) {
                TreePath path = createTreePath(editorPart, (EObject) o);
                if (path != null) {
                    paths.add(path);
                }
            }
        }

        return paths.isEmpty() ? new TreeSelection() : new TreeSelection(paths.toArray(new TreePath[1]));
    }

    protected void navigateToElements(IEditorPart editorPart, IStructuredSelection selection) {
        // ISelectionProvider selectionProvider = editorPart.getEditorSite().getSelectionProvider();
        ISelectionProvider selectionProvider = editorPart.getSite().getSelectionProvider();
        selectionProvider.setSelection(selection);
    }

    protected TreePath createTreePath(IEditorPart editorPart, EObject obj) {
        List<Object> nodes = new LinkedList<>();
        nodes.add(obj);
        EObject tmp = obj.eContainer();

        while (tmp != null) {
            nodes.add(0, tmp);
            tmp = tmp.eContainer();
        }

        return new TreePath(nodes.toArray());
    }

    @Override
    public IWorkbenchPart getOwner() {
        return this.editorPart;
    }

    @Override
    public Collection<EObject> getSelectedEObjects() {
        return getSelectedEObjects(getCurrentSelection());
    }

    protected ISelection getCurrentSelection() {
        ISelectionProvider selectionProvider = editorPart.getSite().getSelectionProvider();
        return selectionProvider.getSelection();
    }
    
    protected Collection<EObject> getSelectedEObjects(ISelection selection) {
        if (selection instanceof IStructuredSelection) {
            return Arrays.stream(((IStructuredSelection) selection).toArray())
                    .filter(EObject.class::isInstance)
                    .map(EObject.class::cast).collect(Collectors.toList());
        } else {
            return Collections.emptyList();
        }
    }
    
    public boolean addListener(IModelConnectorListener listener) {
        Preconditions.checkArgument(listener != null, LISTENER_NULL_MSG);
        return listeners.add(listener);
    }

    public boolean removeListener(IModelConnectorListener listener) {
        Preconditions.checkArgument(listener != null, LISTENER_NULL_MSG);
        return listeners.remove(listener);
    }
    
}

Back to the top