Commit 9cd4a522 authored by Johannes Roith's avatar Johannes Roith
Browse files

Added wizards to create a new CUP file and to create a new Cup Java Project.

These are very basic implementations. The project should be improved to
automatically create a simple CUP file, a JFlex grammar, a "tools" folder with
the CUP compiler jar and an ant build script, which should also be set to run by default.
parent 677deda1
......@@ -17,7 +17,9 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.ui.views;bundle-version="3.7.0",
org.eclipse.core.expressions;bundle-version="3.4.600",
org.eclipse.debug.core;bundle-version="3.9.1",
org.eclipse.debug.ui;bundle-version="3.10.1"
org.eclipse.debug.ui;bundle-version="3.10.1",
org.eclipse.jdt.core;bundle-version="3.10.0",
org.eclipse.jdt.ui;bundle-version="3.10.1"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Bundle-ClassPath: .
......@@ -132,4 +132,26 @@
value="true">
</persistent>
</extension>
<extension
point="org.eclipse.ui.newWizards">
<category
id="de.tum.in.www2.CupPlugin"
name="Cup">
</category>
<wizard
category="de.tum.in.www2.CupPlugin"
class="de.tum.in.www2.cupplugin.wizards.CupFileWizard"
icon="icons/cup_icon.png"
id="de.tum.in.www2.cupplugin.wizards.CupFileWizard"
name="Cup File">
</wizard>
<wizard
category="de.tum.in.www2.CupPlugin"
class="de.tum.in.www2.cupplugin.wizards.CupJavaProjectWizard"
icon="icons/cup_icon.png"
id="de.tum.in.www2.cupplugin.wizards.CupJavaProjectWizard"
name="Cup Java Project"
project="true">
</wizard>
</extension>
</plugin>
package de.tum.in.www2.cupplugin.wizards;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.operation.*;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.CoreException;
import java.io.*;
import org.eclipse.ui.*;
import org.eclipse.ui.ide.IDE;
public class CupFileWizard extends Wizard implements INewWizard {
private CupFileWizardPage page;
private ISelection selection;
public CupFileWizard() {
super();
setNeedsProgressMonitor(true);
}
public void addPages() {
page = new CupFileWizardPage(selection);
addPage(page);
}
public boolean performFinish() {
final String containerName = page.getContainerName();
final String fileName = page.getFileName();
IRunnableWithProgress op = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor)
throws InvocationTargetException {
try {
doFinish(containerName, fileName, monitor);
} catch (CoreException e) {
throw new InvocationTargetException(e);
} finally {
monitor.done();
}
}
};
try {
getContainer().run(true, false, op);
} catch (InterruptedException e) {
return false;
} catch (InvocationTargetException e) {
Throwable realException = e.getTargetException();
MessageDialog.openError(getShell(), "Error",
realException.getMessage());
return false;
}
return true;
}
private void doFinish(String containerName, String fileName,
IProgressMonitor monitor) throws CoreException {
// create a sample file
monitor.beginTask("Creating " + fileName, 2);
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IResource resource = root.findMember(new Path(containerName));
if (!resource.exists() || !(resource instanceof IContainer)) {
throwCoreException("Container \"" + containerName
+ "\" does not exist.");
}
IContainer container = (IContainer) resource;
final IFile file = container.getFile(new Path(fileName));
try {
InputStream stream = openContentStream();
if (file.exists()) {
file.setContents(stream, true, true, monitor);
} else {
file.create(stream, true, monitor);
}
stream.close();
} catch (IOException e) {
}
monitor.worked(1);
monitor.setTaskName("Opening file for editing...");
getShell().getDisplay().asyncExec(new Runnable() {
public void run() {
IWorkbenchPage page = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage();
try {
IDE.openEditor(page, file, true);
} catch (PartInitException e) {
}
}
});
monitor.worked(1);
}
private InputStream openContentStream() {
String contents = "import java_cup.runtime.*;\n" + "parser code {:\n"
+ ":}\n" + "init with {: :}\n" + "scan with {: :}\n"
+ "terminal MYTERMINAL, OTHERTERMINAL;\n" + "\n"
+ "non terminal Integer expr; " + "\n"
+ "expr ::= MYTERMINAL {:\n"
+ " // action code here\n"
+ " :} | OTHERTERMINAL;\n" + "\n";
return new ByteArrayInputStream(contents.getBytes());
}
private void throwCoreException(String message) throws CoreException {
IStatus status = new Status(IStatus.ERROR, "de.tum.in.www2.CupPlugin",
IStatus.OK, message, null);
throw new CoreException(status);
}
public void init(IWorkbench workbench, IStructuredSelection selection) {
this.selection = selection;
}
}
\ No newline at end of file
package de.tum.in.www2.cupplugin.wizards;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;
public class CupFileWizardPage extends WizardPage {
private Text containerText;
private Text fileText;
private ISelection selection;
public CupFileWizardPage(ISelection selection) {
super("wizardPage");
setTitle("New CUP File");
setDescription("Create a simple CUP specification.");
this.selection = selection;
}
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
container.setLayout(layout);
layout.numColumns = 3;
layout.verticalSpacing = 9;
Label label = new Label(container, SWT.NULL);
label.setText("&Container:");
containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
containerText.setLayoutData(gd);
containerText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
dialogChanged();
}
});
Button button = new Button(container, SWT.PUSH);
button.setText("Browse...");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
handleBrowse();
}
});
label = new Label(container, SWT.NULL);
label.setText("&File name:");
fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
gd = new GridData(GridData.FILL_HORIZONTAL);
fileText.setLayoutData(gd);
fileText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
dialogChanged();
}
});
initialize();
dialogChanged();
setControl(container);
}
private void initialize() {
if (selection != null && selection.isEmpty() == false
&& selection instanceof IStructuredSelection) {
IStructuredSelection ssel = (IStructuredSelection) selection;
if (ssel.size() > 1)
return;
Object obj = ssel.getFirstElement();
if (obj instanceof IResource) {
IContainer container;
if (obj instanceof IContainer)
container = (IContainer) obj;
else
container = ((IResource) obj).getParent();
containerText.setText(container.getFullPath().toString());
}
}
fileText.setText("NewParser.cup");
}
private void handleBrowse() {
ContainerSelectionDialog dialog = new ContainerSelectionDialog(
getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
"Select new file container");
if (dialog.open() == ContainerSelectionDialog.OK) {
Object[] result = dialog.getResult();
if (result.length == 1) {
containerText.setText(((Path) result[0]).toString());
}
}
}
private void dialogChanged() {
IResource container = ResourcesPlugin.getWorkspace().getRoot()
.findMember(new Path(getContainerName()));
String fileName = getFileName();
if (getContainerName().length() == 0) {
updateStatus("File container must be specified");
return;
}
if (container == null
|| (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
updateStatus("File container must exist");
return;
}
if (!container.isAccessible()) {
updateStatus("Project must be writable");
return;
}
if (fileName.length() == 0) {
updateStatus("File name must be specified");
return;
}
if (fileName.replace('\\', '/').indexOf('/', 1) > 0) {
updateStatus("File name must be valid");
return;
}
int dotLoc = fileName.lastIndexOf('.');
if (dotLoc != -1) {
String ext = fileName.substring(dotLoc + 1);
if (ext.equalsIgnoreCase("cup") == false) {
updateStatus("File extension must be \"cup\"");
return;
}
}
updateStatus(null);
}
private void updateStatus(String message) {
setErrorMessage(message);
setPageComplete(message == null);
}
public String getContainerName() {
return containerText.getText();
}
public String getFileName() {
return fileText.getText();
}
}
\ No newline at end of file
package de.tum.in.www2.cupplugin.wizards;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.jdt.ui.wizards.NewJavaProjectWizardPageOne;
import org.eclipse.jdt.ui.wizards.NewJavaProjectWizardPageTwo;
public class CupJavaProjectWizard extends Wizard implements INewWizard {
private NewJavaProjectWizardPageOne fFirstPage;
private NewJavaProjectWizardPageTwo fSecondPage;
public CupJavaProjectWizard() {
super();
setNeedsProgressMonitor(true);
}
public void addPages() {
if (fFirstPage == null)
fFirstPage = new NewJavaProjectWizardPageOne();
fFirstPage.setTitle("Cup new project title");
fFirstPage.setDescription("cup new project description");
addPage(fFirstPage);
if (fSecondPage == null)
fSecondPage = new NewJavaProjectWizardPageTwo(fFirstPage);
fSecondPage.setTitle("Cup new project title2");
fSecondPage.setDescription("cup new project description2");
addPage(fSecondPage);
}
/**
* This method is called when 'Finish' button is pressed in the wizard. We
* will create an operation and run it using wizard as execution context.
*/
public boolean performFinish() {
return true;
}
public void init(IWorkbench workbench, IStructuredSelection selection) {
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment