Bug 395840 - Embeddable Xtext editors
Summary: Embeddable Xtext editors
Status: NEW
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: BPEL (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Robert Brodt CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-12-05 12:11 EST by Lorenzo Bettini CLA
Modified: 2022-10-03 11:11 EDT (History)
2 users (show)

See Also:


Attachments
two expression editors (for the greeting example and fowler dsl example) (168.17 KB, image/png)
2012-12-06 10:25 EST, Lorenzo Bettini CLA
no flags Details
error markers (31.37 KB, image/png)
2012-12-06 10:26 EST, Lorenzo Bettini CLA
no flags Details
code completion (18.14 KB, image/png)
2012-12-06 10:26 EST, Lorenzo Bettini CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Lorenzo Bettini CLA 2012-12-05 12:11:54 EST
I'm working on a new bundle which allows to easily embedd Xtext editors as expression editors in the BPEL designer (expression editors were reintroduced in bug 391913).
As soon as I have a patch I'll push it to gerrit (together with some examples).
Comment 1 Lorenzo Bettini CLA 2012-12-06 10:25:41 EST
Created attachment 224376 [details]
two expression editors (for the greeting example and fowler dsl example)
Comment 2 Lorenzo Bettini CLA 2012-12-06 10:26:06 EST
Created attachment 224377 [details]
error markers
Comment 3 Lorenzo Bettini CLA 2012-12-06 10:26:28 EST
Created attachment 224378 [details]
code completion
Comment 4 Lorenzo Bettini CLA 2012-12-06 10:27:43 EST
I pushed a patch set to Gerrit https://git.eclipse.org/r/#/c/9077

I'll detail what I've done.

I created an abstract subclass of AbstractExpressionAssignCategory (see bug 391913), called AbstractXtextExpressionAssignCategory; this class implements createStyledText by creating an Xtext EmbeddedEditor, and returning its StyledText widget which will then be embedded in the bpel design as an expression editor:

public abstract class AbstractXtextExpressionAssignCategory extends
		AbstractExpressionAssignCategory implements IAssignCategory {

	/**
	 * @param styledTextComposite
	 *            the Composite for the StyledText
	 * @return the created and configured StyledText based on an
	 *         {@link EmbeddedEditor}
	 */
	protected StyledText createStyledText(Composite styledTextComposite) {
		Composite editor = getWidgetFactory().createComposite(this.fEditorArea,
				SWT.BORDER);
		editor.setLayout(new GridLayout());

		GridData layoutData = new GridData(GridData.FILL_BOTH);
		editor.setLayoutData(layoutData);

		EmbeddedEditor embeddedEditor = getInjector().getInstance(
				XtextEmbeddedEditorProvider.class).getXtextEmbeddedEditor(
				editor);
		final ISourceViewer viewer = embeddedEditor.getViewer();
		StyledText textWidget = viewer.getTextWidget();
		// textWidget.setLayoutData(new GridData(GridData.FILL_BOTH));

		return textWidget;
	}

	/**
	 * This must redefined so to return the {@link Injector} for your own Xtext
	 * DSL.
	 * 
	 * You can get the injector from the Activator generated by Xtext for your
	 * DSL in the ui plugin, for instance
	 * 
	 * <pre>
	 * org.xtext.example.mydsl.ui.internal.MyDslActivator.getInstance().getInjector(
	 * 		MyDslActivator.ORG_XTEXT_EXAMPLE_MYDSL_MYDSL);
	 * </pre>
	 * 
	 * @return
	 */
	public abstract Injector getInjector();

	/**
	 * A string to identify your DSL.
	 * 
	 * @see org.eclipse.bpel.ui.properties.AbstractExpressionAssignCategory#getExpressionLanguage()
	 */
	public abstract String getExpressionLanguage();
}

This class relies on XtextEmbeddedEditorProvider which internally implements a IEditedResourceProvider (required by Xtext EmbeddedEditorFactory) which basically creates a synthetic resource that will be used by the EmbeddedEditor.

The AbstractXtextExpressionAssignCategory only needs an Injector to start, and this can be easily retrieved using the Activator automatically created by Xtext for your DSL in the ui plugin (it also needs a name to identify the expression language).

For instance, the programmer who wants to embed an editor for his DSL, say MyDsl, only needs to provide a subclass, like the following (it also contains some derived methods to fill labels and descriptions in the bpel designer property view):

public class FowlerDslXtextExpressionAssignCategory extends
		AbstractXtextExpressionAssignCategory {

	public String getName() {
		return "Xtext FowlerDsl Editor";
	}

	public Injector getInjector() {
		return StatemachineActivator
				.getInstance()
				.getInjector(
						StatemachineActivator.ORG_ECLIPSE_XTEXT_EXAMPLE_FOWLERDSL_STATEMACHINE);
	}
	
	public String getExpressionLanguage() {
		return "fowler";
	}

	protected String getStyledTextLabelString() {
		return "Edit the statemachine expression";
	}

}

And of course declare an extension point

   <extension
         point="org.eclipse.bpel.ui.assignCategoryAdditions">
      <assignSection
            class="org.eclipse.bpel.ui.xtext.examples.FowlerDslXtextExpressionAssignCategory"
            name="Xtext Fowler DSL">
      </assignSection>
   </extension>

In the patch set there's also a project, org.eclipse.bpel.ui.xtext.examples, which defines the embedded editors for some classical Xtext examples: the greeting example, the fowler dsl example, and the arithmetics examples (the corresponding Xtext DSL projects are also in the patch set).

To try the examples one can use the "Xtext Embedded Editors Examples.launch" in the project org.eclipse.bpel.ui.xtext.examples (which also sets the vm argument for PermGen, required by Xtext).

In the screenshots you can see 

- two expression editors (for the greeting example and fowler dsl example)
- error markers
- code completion