...
Add a class
HeadControllers
to the packagede.cau.cs.rtprak.login.simple.controller
. Add the following code, replacinglogin
with your login name inEXTENSION_POINT_ID
as usual:Code Block language java /** * Class that gathers extension data from the 'headControllers' extension point * and publishes this data using the singleton pattern. * @author msp */ public class HeadControllers { /** Identifier of the extension point */ public final static String EXTENSION_POINT_ID = "de.cau.cs.rtprak.groupxlogin.simple.headControllers"; /** The singleton instance of the {@code HeadControllers} class */ public final static HeadControllers INSTANCE = new HeadControllers(); /** list of head controller ids with associated names. */ private List<String[]> controllerNames = new LinkedList<String[]>(); /** map of controller ids to their runtime instances. */ private Map<String, IHeadController> controllerMap = new HashMap<String, IHeadController>(); /** * Creates an instance of this class and gathers extension data. */ HeadControllers() { IConfigurationElement[] elements = Platform.getExtensionRegistry() .getConfigurationElementsFor(EXTENSION_POINT_ID); for (IConfigurationElement element : elements) { if ("controller".equals(element.getName())) { String id = element.getAttribute("id"); String name = element.getAttribute("name"); if (id != null && name != null) { try { IHeadController controller = (IHeadController)element .createExecutableExtension("class"); controllerNames.add(new String[] {id, name}); controllerMap.put(id, controller); } catch (CoreException exception) { StatusManager.getManager().handle(exception, Activator.PLUGIN_ID); } } } } } /** * Returns a list of controller ids and names. The arrays in the list are * all of size 2: the first element is an id, and the second element is the * associated name. The controller name is a user-friendly string to be * displayed in the UI. * @return a list of controller ids and names */ public List<String[]> getControllerNames() { return controllerNames; } /** * Returns the head controller instance for the given id. * @param id identifier of a head controller * @return the associated controller */ public IHeadController getController(final String id) { return controllerMap.get(id); } }
...
- Open the
TapeViewPart
class and add the private fieldscheckedControllerAction
of type IAction andcurrentController
of typeIHeadController
. Add a list of registered head controllers to the view's menu (which can be opened using the small white triangle) in the
createPartControl()
method:Code Block language java IMenuManager menuManager = getViewSite().getActionBars().getMenuManager(); for (String[] controllerName : HeadControllers.INSTANCE.getControllerNames()) { final String id = controllerName[0]; String name = controllerName[1]; Action action = new Action(name, IAction.AS_RADIO_BUTTON) { public void run() { if (checkedControllerAction != null) { checkedControllerAction.setChecked(false); } this.setChecked(true); checkedControllerAction = this; currentController = HeadControllers.INSTANCE.getController(id); } }; if (checkedControllerAction == null) { action.run(); } menuManager.add(action); }
Implement the following method in the
TuringTape
class:Code Block language java public void execute(final IHeadController controller)
The method shall have the following properties:
- Determine the character at the current head position using
getCharacter(getHeadPosition())
. - Call
controller.nextCommand()
with the current character as parameter. - Depending on the action in the returned head command, either write the returned new character to the current position in text (
WRITE
), or write the blank symbol (ERASE
), or do nothing. If the current position exceeds the end of the text, append enough blank characters up to the current position, then append the new character. - Depending on the direction in the returned head command, either move the head to the left (but no further than position 0), or to the right, or do nothing.
- Determine the character at the current head position using
- Copy the files step.gif and reset.gif to the icons folder.
- Add an action to the toolbar of the Tape view with text
Step
and iconstep.png
which does the following:- Check whether the current head controller is not
null
, than calltape.execute(currentController)
. - Refresh the table viewer with its
refresh()
method. Note: actions don't need images, but only image descriptors. Thus, to set the action's icon to
step.png
, you can use something like the following:Code Block language java Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "path_to_icon");
- Check whether the current head controller is not
- Add another action with text Reset and icon reset.png which does the following:
- Check whether the current head controller is not
null
, then call thereset()
method oncurrentController
. - Set the current head position to 1.
- Refresh the table viewer with its
refresh()
method.
- Check whether the current head controller is not
...
Adding a Test Head Controller
Before creating a proper head controller in another plug-in, we will add a test controller to check whether all this stuff works.
Add a new class
NullController
to thede.cau.cs.rtprak.login.simple.controllers
package:Code Block language java /** * Head controller that does nothing, for testing. * @author msp */ public class NullController implements IHeadController { /** * {@inheritDoc} */ public HeadCommand nextCommand(final char character) { return new HeadCommand(Action.NULL, Direction.NONE, '_'); } /** * {@inheritDoc} */ public void reset() { } }
- Open the Plugin Manifest Editor and switch to the Extensions tab. Add your
de.cau.cs.rtprak.login.simple.headControllers
extension point. Add acontroller
element with IDde.cau.cs.rtprak.login.simple.nullController
, nameNull Controller
, and classde.cau.cs.rtprak.login.simple.controller.NullController
. - Start the application and observe how your program behaves if you change the action and direction in the
NullController
class. You can actually change both while the application is running, but only if you have started it in the Debug mode. In that case, Eclipse will actually hot-swap your changes into the running application. Sorcery!
Implementing Your Own Head Controller
We will now create a new plug-in with a new head controller:
- Create a new plug-in
de.cau.cs.rtprak.login.simple.extension
. In the Plugin Manifest Editor, addde.cau.cs.rtprak.login.simple
to the dependencies of the new plug-in. - Create a new class that implements
IHeadController
:- Assuming that the initial head position is 1, the controller shall copy the input text infinitely often. So if the tape initially contains the word
hello
, the controller shall generatehellohellohellohe...
. - Your class needs some private fields to store the internal state of the controller, and you may need some special character as marker. Imagine how a Turing Machine would do this.
- It is not allowed to store data that can grow infinitely, since a Turing Machine may only have a finite number of states. This means that you may store single characters or numbers, but you must not store Strings, StringBuffers, arrays, lists, or sets.
- Assuming that the initial head position is 1, the controller shall copy the input text infinitely often. So if the tape initially contains the word
- Register the new controller class using an extension in the new plug-in.
- Test your controller.
Congratulations!
Congratulations, you just made a big step towards understanding how Eclipse works. Plus, you've refreshed your knowledge on Turing Machines along the way. Eclipse is an industry standard technology, and having experience programming against it is a valuable skill for you.
If you have any comments and suggestions for improvement concerning this tutorial, please don't hesitate to tell us about them!