Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext prefences pages(Create a Preferences pages for my DSL)
Xtext prefences pages [message #1766655] Sat, 24 June 2017 07:00 Go to next message
Yves LEDUC is currently offline Yves LEDUCFriend
Messages: 56
Registered: May 2015
Member
I wrote a DSL and I have a problem to complete it with templates as I wish, ie like this in the Preferences dialog:
Window> Preferences and in the Algorithmic> Templates dialog
By default in an Xtext project this gives something like below based on:
org.eclipse.xtext.ui.editor.templates.XtextTemplatePreferencePage
That I can populate with personal templates.

What I want to do is have three types of templates sorted by category:
• Code
• Comments
• Files
In each of its categories then only the templates of the category are displayed.
I have not found how to filter the output of XtextTemplatePreferencePage to get this.
Thank you in advance
Re: Xtext prefences pages [message #1821226 is a reply to message #1766655] Fri, 07 February 2020 12:43 Go to previous message
Marie-Saphira Flug is currently offline Marie-Saphira FlugFriend
Messages: 21
Registered: January 2019
Junior Member
I know this is old, but maybe someone else is searching for it and would like to have an answer..
This is quite simply solved by adding the wanted template pages to the plugin.xml at the <extension point="org.eclipse.ui.preferencePages">
  <page
        category="org.xtext.example.mydsl.MyDsl.templates"
        class="org.xtext.example.mydsl.ui.MyDslExecutableExtensionFactory:org.xtext.example.mydsl.ui.preferencePages.GreetingTemplatePreferencePage"
        id="org.xtext.example.mydsl.MyDSL.templates.greeting"
        name="Greeting Templates">
     <keywordReference
           id="org.xtext.example.mydsl.ui.keyword_MyDsl">
     </keywordReference>
  </page>
  <page
        category="org.xtext.example.mydsl.MyDsl.templates"
        class="org.xtext.example.mydsl.ui.MyDslExecutableExtensionFactory:org.xtext.example.mydsl.ui.preferencePages.NameTemplatePreferencePage"
        id="org.xtext.example.mydsl.MyDSL.templates.name"
        name="Name Templates">
     <keywordReference
           id="org.xtext.example.mydsl.ui.keyword_MyDsl">
     </keywordReference>
  </page>

With category="org.xtext.example.mydsl.MyDsl.templates" those two pages above will be subpages of the XtextTemplatePreferencePage and displayed as shown in your result picture. They look like:
public class GreetingTemplatePreferencePage extends XtextTemplatePreferencePage {

	@Inject
	public GreetingTemplatePreferencePage(IPreferenceStore preferenceStore, ContextTypeRegistry registry,
			TemplateStore templateStore) {
		super(preferenceStore, registry, templateStore);
	}

	@Override
	protected Control createContents(Composite ancestor) {
		Control result = super.createContents(ancestor);
		ancestor.layout();
		super.getTableViewer().setContentProvider(new MyTemplateContentProvider("org.xtext.example.mydsl.MyDsl.Greeting"));
		return result;
	}
}

For NameTemplatePreferencePage replace "Greeting" with "Name". The content provider:
class MyTemplateContentProvider implements IStructuredContentProvider {

	/** The template store. */
	private TemplateStore fStore;
	private String contextID;

	public MyTemplateContentProvider(String contextID) {
		this.contextID = contextID;
	}

	@Override
	public Object[] getElements(Object input) {
		TemplatePersistenceData[] templateData = fStore.getTemplateData(false);
		List<TemplatePersistenceData> onlyContextTemplateData = new ArrayList<TemplatePersistenceData>();
		for (int i = 0; i < templateData.length; i++) {
			if (templateData[i].getTemplate().getContextTypeId().equalsIgnoreCase(this.contextID)) {
				onlyContextTemplateData.add(templateData[i]);
			}
		}
		return onlyContextTemplateData.toArray();
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		fStore= (TemplateStore) newInput;
	}

	@Override
	public void dispose() {
		fStore= null;
	}
}

There probably is another way using TableViewer.addFilter(ViewerFilter filter), but since I'm not very familiar with jface this was the quickest and easiest solution.
Previous Topic:Customizing content proposal in Xtext 2.20 for web editors
Next Topic:Skiping classes when debugging DSL code
Goto Forum:
  


Current Time: Tue Mar 19 06:58:47 GMT 2024

Powered by FUDForum. Page generated in 0.01999 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top