RCP Text editor
short tutorial to build a text editor application on Eclipse RCP
Eclipse, RCP, text, editor, tutorial
Text Editor Application
We start from creating an eclipse application that will contain the editor.
Open eclipse and create a new Plug-in project named my.rcp.texteditor
,
Choose to build a new RCP application.
Select to use the Hello RCP Template
Once built the application we proceed build the basic infrastructure:
- add an Open File Command,
- write the Open File Handler,
- add a visible Menu containing Open, Save and Exit
And finally we test the application
Dependencies
Before to start the implementation, we need to add some dependencies from out project to external plugins.
Open the Dependencies tab, of the plugin.xml, then add some dependencies that we will use during coding:
- org.eclipse.core.filesystem
- org.eclipse.ui.ide
- org.eclipse.ui.editors
At the end the list of dependencies looks like the image
Command
Open the plugin.xml, select the extension tab, then proceed for adding a new “Open File” command.
Click on Add.., type *comman and select org.eclipse.ui.commands
to add the extension.
Add a command by right clicking on org.eclipse.ui.commands
node and select New > command.
Finally Fill fields for command as shown in the right side of the image below.
For our command we defined an id: my.rcp.texteditor.commands.OpenFile
. Take note of this, since will be used further.
Note: to use the %property notation
, we need to:
- edit the MANIFEST.MF, by adding the line
Bundle-Localization: plugin
- add the file
plugin.properties
to the root of project, and add property vlaues likeproperty= PROP VALUE
Handler
We want that our Open File Command is handled by our code, thus we need to write an handler for the OpenFile.
Click on the default handler hyperlink to open the Wizard for creating a class that extends IHandler
Write my.rcp.texteditor.handlers
as package and create the class OpenFile
extending AbstractHandler
and implementing IHandler
.
The code of the class follows
public class FileOpen extends AbstractHandler implements IHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { File file = openFile(); if( file!=null ){ IWorkbenchPage page = PlatformUI.getWorkbench() .getActiveWorkbenchWindow().getActivePage(); try{ IFileStore fileStore = EFS.getStore( file.toURI() ); IEditorInput input = new FileStoreEditorInput(fileStore); page.openEditor(input, EditorsUI.DEFAULT_TEXT_EDITOR_ID); } catch (CoreException e){ e.printStackTrace(); } } return null; } /** Show OpenFile dialog to select a file */ private File openFile(){ Shell shell = PlatformUI.getWorkbench() .getActiveWorkbenchWindow().getShell(); // opens dialog to select file FileDialog dialog = new FileDialog(shell, SWT.OPEN); dialog.setFilterExtensions(new String[]{"*.*"}); dialog.setFilterNames(new String[]{"All files"}); String path = dialog.open(); if( path!=null && path.length() > 0 ){ // Perform action, opens the file System.out.println("Selected file: " + path); return new File(path); } return null; } }
The Open File Command first Opens a file selection dialog, for selecting a file, then ask the workbench to open the file with the default editor.
page.openEditor(input, EditorsUI.DEFAULT_TEXT_EDITOR_ID);
Substantially The Eclipse Text package contains many editors, and we use the EditorUI
to get the identifier for the default editor.
Menu
Now we add a main menu to our application. The menu will have three commands:
- Open File : executes the Command written before
- Save File execute a predefined Action for saving editor content
- Exit executes a predefined Action that exits the application
To add the menu:
From extension tab of the plugin.xml, we add the extension org.eclipse.ui.menus
.
Right click on the node, add a new menuContribution child, and fill the locationURI field with menu:org.eclipse.ui.main.menu
Now select the menuContribution node, and add the menu, by right clicking and selecting New > menu.
To the menu we add the Open command, by right clicking on menu node and selecting New > command.
- Open is a command implemented by us, from scratch. so we insert the command id
my.rcp.texteditor.commands.OpenFile
, defined above
The Menu node should looks like the following image.
Note: the location URI menu:org.eclipse.ui.main.menu
says two things
- the item will be treated as a menu
- its entry point is org.eclipse.ui.main.menu, that is the default entry point for eclipse main menu
Now that the mechanism is clear, we add other two actions, a separator and icons, to get a result like the following image
The Open File menu command correspond to a command that we writed by our hands, while the Save File and the Exit Command corresponds to predefined commands. So, getting over the different icons, we have different command ids
- Open File command id:
my.rcp.texteditor.commands.OpenFile
- Save File command id:
org.eclipse.ui.file.save
- Exit command id:
org.eclipse.ui.file.exit
Test
To test the application, go to the first page of the plugin.xml, and click on the “Launch an eclipse application” hyperlink. You could see the application running.
By clicking on “My Menu > Open File” you can select a file and edit it.
When the file is changed, you see the “Save File” command is enabled
Moreover you’re full of RCP stuffs, like opening more files, etc..
Source Code
You can download the source code of the example
- my.rcp.texteditor.zip zipped eclipse project
References
Further readings
- Eclipsecon 2006, Text editor recipes (pdf)
- Java Forum Stuttgart 2006, Season’s Text editor recipes (pdf)
- Eclipsecon 2004 Text editor and how to implement your own
- IBM developerworks Create a Commercial Quality Eclpse IDE, Part2 and Part 3
Other resources
- icons from softicons.com free toolbar icons