...
- Go to your plugin to add a menu contribution. Open
plugin.xml
→ Dependencies and add org.eclipse.ui if not on the list yet. Go to Extensions and add org.eclipse.ui.menus.- Add a menuContribution element to the new extension and set "popup:org.eclipse.ui.popup.any?after=additions" as locationURI (without quotation marks).
- Add a command element to the menuContribution with the following attributes:
- commandId: de.cau.cs.rtprak.login.setSimFile
- label: Set Simulation File
- Add a visibleWhen element to the command, and add an iterate element to the visibleWhen with the following attributes:
- operator: and
- ifEmpty: false
- Add an adapt element to the iterate with the following attribute:
- type: org.eclipse.core.resources.IResource
- Add a test element to the adapt with the following attributes:
- property: org.eclipse.core.resources.extension
- value: brainfuck (or your file extension if you have chosen another one)
- Add a new extension org.eclipse.ui.commands and add a command element to it with the following attributes:
- id: de.cau.cs.rtprak.login.setSimFile
- name: Set Simulation File
- Click on defaultHandler to open a dialog for creation of a new handler class. Name the new class
SetFileHandler
and put it into some package of that plugin. Remove the suggested interface and setorg.eclipse.core.commands.AbstractHandler
as superclass instead.
Use the following method stub for
SetFileHandler
(this requires a plugin dependency to org.eclipse.core.resources):Code Block /** * {@inheritDoc} */ @Override public Object execute(ExecutionEvent event) throws ExecutionException { ISelection selection = HandlerUtil.getCurrentSelection(event); if (selection instanceof IStructuredSelection) { Object element = ((IStructuredSelection) selection).getFirstElement(); if (element instanceof IFile) { IFile file = (IFile) element; // TODO update the static reference to the simulation file } } return null; }language java
Create a command handler that loads the BF model from the selected file, calls the transformation on the model, and saves the newly generated BF program to a file with a different name (but same extension). You can use the following code as a template: (The code requires a dependency to
com.google.inject
to work.)Code Block language java @Override public Object execute(ExecutionEvent event) throws ExecutionException { ISelection selection = HandlerUtil.getCurrentSelection(event); if (selection instanceof IStructuredSelection) { Object element = ((IStructuredSelection) selection).getFirstElement(); if (element instanceof IFile) { IFile bfFile = (IFile) element; // Load Turing Machine Brainfuck bfProgram = loadBrainfuckProgram(bfFile); // Call the transformation Injector injector = Guice.createInjector(); ExtendedBrainfuckTransformation transformation = injector.getInstance(ExtendedBrainfuckTransformation.class); Program newBFProgram = transformation.transformExtendedBrainfuck(bfPRogram); // Save imperative program IFile newBFFile = bfFile.getParent().getFile( new Path(bfFile.getName() + "2" + ".brainfuck")); saveBrainfuckProgram(newBFFile, newBFProgram); // Refresh the parent folder to have the new file show up in the UI try { bfFile.getParent().refreshLocal(IResource.DEPTH_ONE, null); } catch (CoreException e) { // Ignore } } } return null; } /** * Load the Brainfuck program model from the given file. * * @param bfFile the file to load the Brainfuck program model. * @return the Brainfuck model. * @throws ExecutionException if the file couldn't be opened. */ private brainfuck loadBrainfuckProgram(IFile bfFile) throws ExecutionException { // TODO Implement. } /** * Saves the given Brainfuck program. * * @param bfFile the Brainfuck file to save the program to. * @param bfProgram the program to save. * @throws ExecutionException if there was an error saving the file. */ private void saveBrainfuckProgram(IFile bfFile, Program bfProgram) throws ExecutionException { // TODO Implement }