Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "VIATRA/Query/DeveloperDocumentation/Model connectors"

< VIATRA‎ | Query
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
For navigating from results of the QueryExplorer to an appropriate location in the editor which presents the particular model elements one must double-click the result in the QueryExplorer.
+
{{caution|Old information|The content from this page was migrated to the new, Github-based wiki system of VIATRA: https://github.com/eclipse-viatra/org.eclipse.viatra/wiki/Creating-Model-Connectors-for-VIATRA-Query}}
If the presenting editor is not a default-EMF editor model connectors are needed. As an example consider textual models created with an editor generated with [http://www.emftext.org/ EMFText] or [http://xtext.org/ Xtext]. The following process demonstrates a model connector for EMFText-based textual models.
+
{{VIATRA}}
 +
For navigating from results of the Query Explorer to an appropriate location in the editor which presents the particular model elements one must double-click the result in the Query Explorer.
 +
If the presenting editor is not a default EMF editor then model connectors are needed. As an example consider the [http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.modisco.infrastructure.doc%2Fmediawiki%2Fmodel_browser%2Fuser.html MoDisco Browser] which provides a more convenient and feature-rich EMF model presentation than the reflective Ecore model editor.
  
 
==Provide an IAdapterFactory==
 
==Provide an IAdapterFactory==
The model connector concept is lightweight and seemlessly integrates as an adapter. Thus, you have to register a new IAdapterFactory which creates the connector. In the following snippet you see the part of the plugin.xml registering the adapter factory:
+
The model connector concept is lightweight and seamlessly integrates as an adapter. Thus, you have to register a new <code>IAdapterFactory</code> which creates the connector in your plugin.xml. See the [http://git.eclipse.org/c/viatra/org.eclipse.viatra.git/tree/integration/plugins/org.eclipse.viatra.integration.modisco/plugin.xml MoDisco integration's plugin.xml].
  
<source lang="xml">
+
The ''adaptableType'' property specifies the kind of editor being supported by this adapter factory (and thus it is supported by the model connector). Determine the adapter factory with the ''class'' property. The children ''adapter'' specifies the returning type of the adapter factory. This is the type which your editor is adapted to. In the case of VIATRA the ''type'' must always refer to ''org.eclipse.viatra.query.tooling.ui.queryexplorer.IModelConnector''.
  <extension
+
        point="org.eclipse.core.runtime.adapters">
+
      <factory
+
          adaptableType="org.eclipse.ui.texteditor.ITextEditor"
+
          class="org.emftext.incquery.modelconnector.ModelConnectorAdapterFactoryForEMFTextEditors">
+
        <adapter
+
              type="org.eclipse.incquery.runtime.api.IModelConnector">
+
        </adapter>
+
      </factory>
+
  </extension>
+
</source>
+
  
The ''adaptableType'' property specifies the kind of editor being supported by this adapter factory (and thus it is supported by the model connector). Determine the adapter factory with the ''class'' property. The children ''adapter'' specifies the returning type of the adapter factory. This is the type which your editor is adapted to. In the case of IncQuery the ''type'' must always refer to ''org.eclipse.incquery.runtime.api.IModelConnector''.
+
Next step is to implement the adapter factory which simply adapts an <code>IEditorPart</code> to your custom <code>IModelConnector</code> implementation. See the [http://git.eclipse.org/c/viatra/org.eclipse.viatra.git/tree/integration/plugins/org.eclipse.viatra.integration.modisco/src/org/eclipse/viatra/integration/modisco/ModelConnectorAdapterFactoryForFacet.java adapter factory for MoDisco].
 
+
Next step is to implement the adapter factory:
+
 
+
<source lang="java">
+
  public class ModelConnectorAdapterFactoryForEMFTextEditors implements IAdapterFactory {
+
   
+
    @Override
+
    public Object getAdapter(Object adaptableObject, Class adapterType) {
+
        if(adapterType.equals(IModelConnector.class) && adaptableObject instanceof ITextEditor){
+
          if(EMFTextAccessProxy.isAccessibleWith(adaptableObject.getClass(), IEditor.class)){
+
            IEditor emftextEditor = (IEditor) EMFTextAccessProxy.get(adaptableObject, IEditor.class);
+
            return new EMFTextModelConnector((ITextEditor) adaptableObject, emftextEditor);
+
          }
+
        }
+
        return null;
+
    }
+
   
+
    @Override
+
    public Class[] getAdapterList() {
+
        return new Class[] { IModelConnector.class };
+
    }
+
  }
+
</source>
+
  
 
==Implement IModelConnector==
 
==Implement IModelConnector==
The last step is to provide the implementation of the IModelConnector interface. In the following you see the model connector which supports navigation to textual model elements in EMFText-based editors:
+
The last step is to provide the implementation of the <code>IModelConnector</code> interface. An abstract <code>EMFModelConnector</code> base class is available for EMF-based editors. See the [http://git.eclipse.org/c/viatra/org.eclipse.viatra.git/tree/integration/plugins/org.eclipse.viatra.integration.modisco/src/org/eclipse/viatra/integration/modisco/FacetModelConnector.java model connector for MoDisco].
 
+
<source lang="java">
+
        public class EMFTextModelConnector extends EMFModelConnector {
+
   
+
        private IEditor emftextEditor;
+
        private ITextEditor textEditor;
+
   
+
        public EMFTextModelConnector(ITextEditor textEditor, IEditor emftextEditor) {
+
                super(textEditor);
+
                this.emftextEditor = emftextEditor;
+
                this.textEditor = textEditor;
+
        }
+
   
+
        @Override
+
        public Notifier getNotifier(IModelConnectorTypeEnum modelConnectorTypeEnum) {
+
                Notifier result = null;
+
                switch (modelConnectorTypeEnum) {
+
                case RESOURCE:
+
                    result = emftextEditor.getResource();
+
                    break;
+
 
+
                case RESOURCESET:
+
                    result = emftextEditor.getResource().getResourceSet();
+
 
+
                }
+
                return result;
+
        }
+
   
+
        @Override
+
        public void showLocation(Object[] locationObjects) {
+
                IResource emftextResource = emftextEditor.getResource();
+
                ILocationMap locationMap = emftextResource.getLocationMap();
+
                if (locationObjects.length > 0 && objectsAreEObjects(locationObjects)) {
+
                    // selects the first object in the array since text editors can only select one
+
                    EObject eObject = (EObject) locationObjects[0];
+
                    int start = locationMap.getCharStart(eObject);
+
                    int end = locationMap.getCharEnd(eObject);
+
                    textEditor.selectAndReveal(start, end - start + 1);
+
                }
+
        }
+
   
+
        private boolean objectsAreEObjects(Object[] locationObjects) {
+
                if(locationObjects == null){
+
                    return false;
+
                }
+
                for (Object object : locationObjects) {
+
                    if(!(object instanceof EObject)){
+
                        return false;
+
                    }
+
                }
+
                return true;
+
        }
+
    }
+
</source>
+
 
+
The above example can be found in the github repository for IncQuery addons: [https://github.com/istvanrath/EMF-IncQuery-Addons/tree/master/emftext https://github.com/istvanrath/EMF-IncQuery-Addons/tree/master/emftext]
+

Latest revision as of 09:50, 2 April 2024

Stop.png
Old information
The content from this page was migrated to the new, Github-based wiki system of VIATRA: https://github.com/eclipse-viatra/org.eclipse.viatra/wiki/Creating-Model-Connectors-for-VIATRA-Query


For navigating from results of the Query Explorer to an appropriate location in the editor which presents the particular model elements one must double-click the result in the Query Explorer. If the presenting editor is not a default EMF editor then model connectors are needed. As an example consider the MoDisco Browser which provides a more convenient and feature-rich EMF model presentation than the reflective Ecore model editor.

Provide an IAdapterFactory

The model connector concept is lightweight and seamlessly integrates as an adapter. Thus, you have to register a new IAdapterFactory which creates the connector in your plugin.xml. See the MoDisco integration's plugin.xml.

The adaptableType property specifies the kind of editor being supported by this adapter factory (and thus it is supported by the model connector). Determine the adapter factory with the class property. The children adapter specifies the returning type of the adapter factory. This is the type which your editor is adapted to. In the case of VIATRA the type must always refer to org.eclipse.viatra.query.tooling.ui.queryexplorer.IModelConnector.

Next step is to implement the adapter factory which simply adapts an IEditorPart to your custom IModelConnector implementation. See the adapter factory for MoDisco.

Implement IModelConnector

The last step is to provide the implementation of the IModelConnector interface. An abstract EMFModelConnector base class is available for EMF-based editors. See the model connector for MoDisco.

Back to the top