Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
[#1259] Statemachine example: add New Project Wizard/New File Wizard.
- Extend the
org.eclipse.xtext.ui.wizard.template.AbstractProjectTemplate class by
the List<Pair<String, Image>> getImages API method to be able toto
register images that can be used in the template descriptions.
- Add new project wizard / new file wizard to the Xtext Statemachine
Example to demonstrate the usage of the API.

Signed-off-by: Tamas Miklossy <miklossy@itemis.de>
  • Loading branch information
miklossy committed Nov 18, 2019
1 parent b43d787 commit 92a6062
Show file tree
Hide file tree
Showing 22 changed files with 665 additions and 6 deletions.
Expand Up @@ -14,6 +14,8 @@
import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.graphics.Image;
import org.eclipse.xtext.xbase.lib.Pair;

import com.google.common.annotations.Beta;

Expand Down Expand Up @@ -215,4 +217,18 @@ protected ProjectTemplate getProjectTemplateAnnotation() {
return projectTemplateAnnotation;
}

/**
* Subclasses should override. The method is called after the {@link org.eclipse.ui.forms.widgets.FormText} widget is created
* to register template images that can be used in the template descriptions within the href attribute of the &lt;img&gt; tags.
*
* @return each image is defined by a pair of an id and the image itself.
*
* See also {@link org.eclipse.ui.forms.widgets.FormText#setImage(String, Image)}
*
* @since 2.20
*/
protected List<Pair<String, Image>> getImages() {
return new ArrayList<Pair<String, Image>>();
}

}
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2018 itemis AG (http://www.itemis.de) and others.
* Copyright (c) 2017, 2019 itemis AG (http://www.itemis.de) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -25,11 +25,13 @@
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.widgets.FormText;
import org.eclipse.xtext.xbase.lib.Pair;

import com.google.common.annotations.Beta;

Expand Down Expand Up @@ -78,12 +80,20 @@ public void createControl(Composite parent) {
TableViewer templateTable = new TableViewer(sash, SWT.BORDER);
templateTable.setContentProvider(new ArrayContentProvider());
templateTable.setLabelProvider(labelProvider);
templateTable.setInput(loadTemplatesFromExtensionPoint());
AbstractProjectTemplate[] templates = loadTemplatesFromExtensionPoint();
templateTable.setInput(templates);

FormText text = new FormText(sash, SWT.BORDER);
text.setText("", false, false); //$NON-NLS-1$
text.setBackground(templateTable.getTable().getBackground());

// register images
for (AbstractProjectTemplate template : templates) {
for (Pair<String, Image> image : template.getImages()) {
text.setImage(image.getKey(), image.getValue());
}
}

templateTable.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
Expand Down
@@ -1,8 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry exported="true" kind="con" path="org.eclipse.pde.core.requiredPlugins">
<accessrules>
<accessrule kind="accessible" pattern="org/eclipse/xtext/ui/wizard/template/**"/>
</accessrules>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="xtend-gen"/>
<classpathentry kind="src" path="src-gen">
<attributes>
<attribute name="ignore_optional_problems" value="true"/>
Expand Down
Expand Up @@ -7,8 +7,13 @@ Bundle-SymbolicName: org.eclipse.xtext.example.fowlerdsl.ui; singleton:=true
Bundle-ActivationPolicy: lazy
Require-Bundle: org.antlr.runtime;bundle-version="[3.2.0,3.2.1)",
org.eclipse.compare;bundle-version="2.1.0",
org.eclipse.core.resources,
org.eclipse.core.runtime,
org.eclipse.jdt.core,
org.eclipse.pde.core,
org.eclipse.ui;bundle-version="3.6.0",
org.eclipse.ui.editors;bundle-version="3.6.0",
org.eclipse.ui.forms,
org.eclipse.ui.ide;bundle-version="3.6.0",
org.eclipse.xtend.lib;bundle-version="2.14.0";resolution:=optional,
org.eclipse.xtext.builder,
Expand All @@ -27,6 +32,7 @@ Export-Package: org.eclipse.xtext.example.fowlerdsl.ui;x-internal:=true,
org.eclipse.xtext.example.fowlerdsl.ui.internal;x-internal:=true,
org.eclipse.xtext.example.fowlerdsl.ui.labeling;x-internal:=true,
org.eclipse.xtext.example.fowlerdsl.ui.outline;x-internal:=true,
org.eclipse.xtext.example.fowlerdsl.ui.quickfix;x-internal:=true
org.eclipse.xtext.example.fowlerdsl.ui.quickfix;x-internal:=true,
org.eclipse.xtext.example.fowlerdsl.ui.wizard;x-internal:=true
Bundle-Activator: org.eclipse.xtext.example.fowlerdsl.ui.internal.FowlerdslActivator
Automatic-Module-Name: org.eclipse.xtext.example.fowlerdsl.ui
@@ -1,6 +1,8 @@
source.. = src/,\
src-gen/
src-gen/,\
xtend-gen/
bin.includes = META-INF/,\
.,\
plugin.xml,\
icons/,\
templates/
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Expand Up @@ -422,4 +422,55 @@
type="text">
</fileTypes>
</extension>
<extension
point="org.eclipse.ui.newWizards">
<category id="org.eclipse.xtext.example.fowlerdsl.ui.category" name="Statemachine">
</category>
<wizard
category="org.eclipse.xtext.example.fowlerdsl.ui.category"
class="org.eclipse.xtext.example.fowlerdsl.ui.StatemachineExecutableExtensionFactory:org.eclipse.xtext.ui.wizard.template.TemplateNewProjectWizard"
id="org.eclipse.xtext.example.fowlerdsl.ui.wizard.StatemachineNewProjectWizard"
name="Statemachine Project"
icon="icons/new_Statemachine_proj.gif"
project="true">
</wizard>
</extension>
<extension
point="org.eclipse.xtext.ui.projectTemplate">
<projectTemplateProvider
class="org.eclipse.xtext.example.fowlerdsl.ui.StatemachineExecutableExtensionFactory:org.eclipse.xtext.example.fowlerdsl.ui.wizard.StatemachineProjectTemplateProvider"
grammarName="org.eclipse.xtext.example.fowlerdsl.Statemachine">
</projectTemplateProvider>
</extension>
<extension
point="org.eclipse.ui.perspectiveExtensions">
<perspectiveExtension targetID="org.eclipse.ui.resourcePerspective">
<newWizardShortcut id="org.eclipse.xtext.example.fowlerdsl.ui.wizard.StatemachineNewProjectWizard"/>
</perspectiveExtension>
<perspectiveExtension targetID="org.eclipse.jdt.ui.JavaPerspective">
<newWizardShortcut id="org.eclipse.xtext.example.fowlerdsl.ui.wizard.StatemachineNewProjectWizard"/>
</perspectiveExtension>
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar
id="org.eclipse.xtext.example.fowlerdsl.ui.toolbar">
<!--
For some reason the tooltip is not shown when hovering over the toolbar button
See also https://www.eclipse.org/forums/index.php/t/1079111/
-->
<command
commandId="org.eclipse.ui.newWizard"
tooltip="Create a statemachine project">
<parameter
name="newWizardId"
value="org.eclipse.xtext.example.fowlerdsl.ui.wizard.StatemachineNewProjectWizard">
</parameter>
</command>
</toolbar>
</menuContribution>
</extension>
</plugin>
Expand Up @@ -78,6 +78,8 @@
import org.eclipse.xtext.ui.refactoring.ui.RefactoringPreferences;
import org.eclipse.xtext.ui.resource.ResourceServiceDescriptionLabelProvider;
import org.eclipse.xtext.ui.shared.Access;
import org.eclipse.xtext.ui.wizard.IProjectCreator;
import org.eclipse.xtext.ui.wizard.template.DefaultTemplateProjectCreator;

/**
* Manual modifications go to {@link StatemachineUiModule}.
Expand Down Expand Up @@ -290,4 +292,9 @@ public void configureCompareViewerTitle(Binder binder) {
binder.bind(String.class).annotatedWith(Names.named(UIBindings.COMPARE_VIEWER_TITLE)).toInstance("Statemachine Compare");
}

// contributed by org.eclipse.xtext.xtext.generator.ui.projectWizard.TemplateProjectWizardFragment
public Class<? extends IProjectCreator> bindIProjectCreator() {
return DefaultTemplateProjectCreator.class;
}

}
@@ -0,0 +1,26 @@
package org.eclipse.xtext.example.fowlerdsl.ui.wizard;

import org.eclipse.osgi.util.NLS;

public class Messages extends NLS {
private static final String BUNDLE_NAME = "org.eclipse.xtext.example.fowlerdsl.ui.wizard.messages"; //$NON-NLS-1$

public static String StatemachineProject_Label;
public static String StatemachineProject_Description;
public static String MrsGrantsSecretCompartments_Label;
public static String MrsGrantsSecretCompartments_Description;
public static String MrsGrantsSecretCompartmentsFile_Label;
public static String MrsGrantsSecretCompartmentsFile_Description;
public static String MrsGrantsSecretCompartmentsProject_Label;
public static String MrsGrantsSecretCompartmentsProject_Description;
public static String EmptyFile_Label;
public static String EmptyFile_Description;

static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
}

private Messages() {
}
}
@@ -0,0 +1,41 @@
/*******************************************************************************
* Copyright (c) 2019 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.example.fowlerdsl.ui.wizard

import org.eclipse.xtext.ui.wizard.template.FileTemplate
import org.eclipse.xtext.ui.wizard.template.IFileGenerator
import org.eclipse.xtext.ui.wizard.template.IFileTemplateProvider

/**
* Create a list with all file templates to be shown in the template new file wizard.
*
* Each template is able to generate one or more files.
*
* @author miklossy - Initial contribution and API
*/
class StatemachineFileTemplateProvider implements IFileTemplateProvider {
override getFileTemplates() {
#[new EmptyFile, new MrsGrantsSecretCompartmentsFile]
}
}

@FileTemplate(label="Empty Statemachine", icon="file_template.png", description="Empty Statemachine")
final class EmptyFile {

override generateFiles(IFileGenerator generator) {
generator.generate('''«folder»/«name».statemachine''', '''''')
}
}

@FileTemplate(label="Mrs Grant's Secret Compartments", icon="file_template.png", description="Mrs Grant's Secret Compartments")
final class MrsGrantsSecretCompartmentsFile {

override generateFiles(IFileGenerator generator) {
generator.generate('''«folder»/«name».statemachine''', TemplateContent.MrsGrantsSecretCompartments)
}
}
@@ -0,0 +1,80 @@
/*******************************************************************************
* Copyright (c) 2019 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.example.fowlerdsl.ui.wizard

import java.util.List
import org.eclipse.core.runtime.Status
import org.eclipse.jdt.core.JavaCore
import org.eclipse.swt.graphics.Image
import org.eclipse.xtext.example.fowlerdsl.ui.internal.FowlerdslActivator
import org.eclipse.xtext.ui.PluginImageHelper
import org.eclipse.xtext.ui.XtextProjectHelper
import org.eclipse.xtext.ui.util.PluginProjectFactory
import org.eclipse.xtext.ui.wizard.template.IProjectGenerator
import org.eclipse.xtext.ui.wizard.template.IProjectTemplateProvider
import org.eclipse.xtext.ui.wizard.template.ProjectTemplate

import static org.eclipse.core.runtime.IStatus.*

/**
* Create a list with all project templates to be shown in the template new project wizard.
*
* Each template is able to generate one or more projects. Each project can be configured such that any number of files are included.
*
* @author miklossy - Initial contribution and API
*/
class StatemachineProjectTemplateProvider implements IProjectTemplateProvider {

override getProjectTemplates() {
#[new MrsGrantsSecretCompartmentsProject]
}
}

@ProjectTemplate(label="Mrs Grant's Secret Compartments", icon="project_template.png", description="<p><b>Mrs Grant's Secret Compartments</b></p>
<p>A Fowler Statemachine representing Mrs Grant's Secret Compartments.</p><p><img href=\"mrsgrantssecretcompartment.png\"/></p>")
final class MrsGrantsSecretCompartmentsProject {

val advanced = check("Advanced:", false)
val advancedGroup = group("Properties")
val path = text("Package:", "mydsl", "The package path to place the files in", advancedGroup)

override protected updateVariables() {
path.enabled = advanced.value
if (!advanced.value) {
path.value = "statemachine"
}
}

override protected validate() {
if (path.value.matches('[a-z][a-z0-9_]*(/[a-z][a-z0-9_]*)*'))
null
else
new Status(ERROR, "Wizard", "'" + path + "' is not a valid package name")
}

override generateProjects(IProjectGenerator generator) {
generator.generate(new PluginProjectFactory => [
projectName = projectInfo.projectName
location = projectInfo.locationPath
projectNatures += #[JavaCore.NATURE_ID, "org.eclipse.pde.PluginNature", XtextProjectHelper.NATURE_ID]
builderIds += #[JavaCore.BUILDER_ID, XtextProjectHelper.BUILDER_ID]
folders += "src"
folders += "src-gen"
addFile('''src/«path»/MrsGrantsSecretCompartments.statemachine''', TemplateContent.MrsGrantsSecretCompartments)
])
}

protected override List<Pair<String, Image>> getImages() {
#["mrsgrantssecretcompartment.png".image]
}

private def image(String id) {
id -> new PluginImageHelper().getImage('''platform:/plugin/«FowlerdslActivator.PLUGIN_ID»/«id»''')
}

}

0 comments on commit 92a6062

Please sign in to comment.