This tutorial isn't complete yet! |
This tutorial will teach you the basics of writing plugins that run inside the Eclipse framework. You will learn about editors, views, and extension points by creating one of each yourself.
There's a few things to do before we dive into the tutorial itself. For example, to do Eclipse programming, you will have to get your hands on an Eclipse installation first. Read through the following sections to get ready for the tutorial tasks.
For this tutorial, we need you to have Eclipse and Git installed:
Over the course of this tutorial, you will be writing a bit of code. Here's a few rules we ask you to follow:
de.cau.cs.rtprak.login
, where login
is your login name as used for your email address at the institute. From now on, this rule will apply to all tutorials. Once we start with the actual practical projects, we will choose another package name.During the tutorial, we will cover each topic only briefly, so it is always a good idea to find more information online. Here's some more resources that will prove helpful:
You will find that despite of all of these resources Eclipse is still not as well commented and documented as we'd like it to be. Finding out how stuff works in the world of Eclipse can thus sometimes be a challenge. However, this does not only apply to you, but also to many people who are conveniently connected by something called The Internet. It should go without saying that if all else fails, Google often turns up great tutorials or solutions to problems you may run into. And if it doesn't, Miro and I will be happy to help you as well.
We have created a Git repository for everyone to do his tutorials in. You can access the repository online through our Stash tool over here. You will first have to configure your Stash account:
You should now be able to access the repository. Clone it:
ssh://git@git.rtsys.informatik.uni-kiel.de:7999/PRAK/12ws-eclipse-tutorials.git .
(including the final dot, which tells git to clone the repository into the current directory instead of a subdirectory).You will use this repository for all your tutorial work, along with everyone else. To make sure that you don't interfere with each other, everyone will work on a different branch. This is not exactly how people usually use Git, but goes to demonstrate Git's flexibility... Add a branch for you to work in:
git checkout -b login_name
You have just added and checked out a new branch. Everything you commit will go to this branch. To push your local commits to the server (which you will need to do so we can access your results), do the following:
git push origin login_name
You would usually have to enter git pull
first, but since nobody will mess with your branch this won't be necessary. By the way, you only need to mention origin login_name
with the first git push
, since Git doesn't know where to push the branch yet. After the first time, Git remembers the information and it will be enough to just enter git push
.
OK, with all the preliminaries out of the way let's get working. Fire up Eclipse, choose an empty workspace, close the Welcome panel it will present you with and follow the following steps.
For our text editor to integrate into Eclipse, we need to create a plug-in project for it:
de.cau.cs.rtprak.login.simple
. Uncheck Use default location (which would put the project into your workspace), and put it into your local clone of the Git repository instead (the Location should read something like /path/to/git/repository/de.cau.cs.rtprak.login.simple
). Click Next.Simple (login)
. Also, make sure that Generate an activator and This plug-in will make contributions to the UI are both checked. Click Finish. (Eclipse might ask you whether you want to switch to the Plug-in Development Perspective, which configures Eclipse to provide the views that are important for plug-in development. Choose Yes. Or No. It won't have a big influence on your future...)plugin.xml
and META-INF/MANIFEST.MF
. (By the way, this would be a great time to research the editor and the two files online.) Basically, those two files provide information that tell Eclipse what other plug-ins your plug-in needs and how it works together with other plug-ins by providing extensions and extension points. Our new plug-in will depend on two other plug-ins, so switch to the Dependencies tab of the editor and add dependencies to org.eclipse.ui.editors
and org.eclipse.jface.text
. Save the editor and close it. (You can always reopen it by opening one of the two mentioned files from the Package Explorer.)We will now create the class that implements the simple text editor. There won't be any programming involved here since we're lazy; instead, we will just inherit from an existing simple text editor.
de.cau.cs.rtprak.login.simple.editors
. Name: SimpleEditorPart
. Superclass: org.eclipse.ui.editors.text.TextEditor
. Click Finish.For the editor to be available inside Eclipse, we will have to register it by adding an extension to an extension point.
org.eclipse.ui.editors
extension point and click Finish.de.cau.cs.rtprak.login.simple.editor
, the name to Simple Text Editor
, the icon to icons/turing-file.gif
, the extensions to simple
, the class to de.cau.cs.rtprak.login.simple.editors.SimpleEditorPart
, and the default to true
.It's time to test your new simple editor in a new Eclipse instance.
test
as the project name.test.simple
. This will create a new file with that name and open the file in your newly added text editor. (You can see that it is your editor by looking at the editor icon, which should look like the icon you downloaded and put into the icons folder.)The next task consists of creating a view that is able to display the state of a Turing Machine. We will do this using a table with one column, where each row represents an entry on the tape of the Turing Machine. The tape shall be infinite to one side, and the position of the read/write head shall be movable by two buttons. The content of the tape shall be determined by the currently active instance of our simple text editor.
In this tutorial, we will be making use of the Standard Widget Toolkit (SWT) and JFace to build a user interface. It might be a good idea now to search for an introduction to SWT and JFace concepts on the Internet before you proceed. |
We will start by creating a class that will define the view.
TapeViewPart
in a new package de.cau.cs.rtprak.login.simple.views
that extends the ViewPart class. (make sure that in the New Java Class wizard, the option Inherited abstract methods is checked.)tableViewer
of type TableViewer.Your TableViewPart
contains a still empty method createPartControl
. This method will be responsible for creating the user interface components of your view. Add the following code to create the table we want to display:
Table table = new Table(parent, SWT.BORDER); TableColumn column = new TableColumn(table, SWT.NONE); column.setWidth(80); tableViewer = new TableViewer(table); |
The setFocus
method controls what happens when your part gets the focus. Make sure the focus will then automatically be set to the table by adding the following code:
tableViewer.getControl().setFocus(); |
We will now have to register our new view with Eclipse so that it can be seamlessly integrated into the workbench.
icons
subfolder of your plug-in as you did it before. (You might need to refresh your project again if you did the copying outside of Eclipse.)plugin.xml
file in the Plugin Manifest Editor and switch to the Extensions tab.org.eclipse.ui.views
. Right-click the newly added extension and add a new view
element through the New menu.de.cau.cs.rtprak.login.simple.view
, name Tape
, class de.cau.cs.rtprak.login.simple.views.TapeViewPart
, category org.eclipse.ui
, icon icons/tape_head.gif
.When you start the application, you should now be able to open your view by clicking Window -> Show View -> Other.
The idea of JFace viewers is to abstract a bit from the underlying widget (in our case, the table) and instead work on data models that are to be viewed. Instead of adding items to the table directly, the table viewer is supplied with an input object, a content provider, and a label provider. The content provider allows the viewer to make sense of the input object and basically allows the viewer to access the input object's data. The label provider translates each item of data into text and icons that can be used to present the item to the user in the table.
We will now create content and label providers to do just that.
Create a class TuringTape
in a new package de.cau.cs.rtprak.login.simple.model
with the following fields:
private int headPosition = 1; private StringBuffer text = new StringBuffer(); |
Also add corresponding getter and setter methods. (You can simply right-click somewhere in the class and choose Source -> Generate Getters and Setters.)
Add two constants to the class:
public static final char START_CHAR = '\u25b7'; public static final char BLANK_CHAR = '\u25fb'; |
getCharacter(int pos)
that calculates the tape character at position pos
as follows:pos == 0
, return the character START_CHAR
.pos > text.length()
, return the character BLANK_CHAR
.pos - 1
.TuringTape
to TapeViewPart
and initialize it with a new instance.TapeData
in de.cau.cs.rtprak.login.simple.model
with two fields int index
and char character
, and add a constructor for initialization as well as corresponding getter methods.TapeContentProvider
in the de.cau.cs.rtprak.login.simple.views
package that implements IStructuredContentProvider.dispose()
and inputChanged()
may remain empty.getElements()
must return an array of objects, where each object must contain all necessary data to be displayed in a single row of the table. The number of returned objects corresponds to the number of rows.TuringTape
. The result of getElements()
shall be an array of TapeData
elements. The size of the array shall be one more than the maximum of the tape head position and the length of the tape text. The index and character of each tape data element shall be filled with i
and the result of turingTape.getCharacter(i)
, respectively, where i
is the array index of the element.TapeLabelProvider
in the de.cau.cs.rtprak.login.simple.views
package that extends BaseLabelProvider and implements ITableLabelProvider.TuringTape
that is initialized from the constructor.presentImage
and absentImage
of type Image.Initialize each image using the following code, where path_to_image
is icons/head_present.gif
and icons/head_absent.gif
, respectively:
image = Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "path_to_image").createImage(); |
dispose()
in TapeLabelProvider
to dispose both images after calling super.dispose()
.getColumnImage()
and getColumnText()
, first check whether the element is an instance of TapeData
and the column index is 0, and return null
otherwise. If the check passes, return the following:getColumnImage()
: presentImage
if the index given by the tape data element equals the current value of tape.getHeadPosition()
, absentImage
otherwise.getColumnText()
: a String
containing the character of the tape data element.Add the following lines to createPartControl()
in TapeViewPart
:
tableViewer.setContentProvider(new TapeContentProvider()); tableViewer.setLabelProvider(new TapeLabelProvider(tape)); tableViewer.setInput(tape); |
WRITE THIS SECTION