This document provides an overview of creating a basic Eclipse Rich Client Platform (RCP) application, including setting up the necessary plug-in project, defining perspectives and views, and implementing core classes like Application, WorkbenchAdvisor, and Perspective. It describes how to add new views to a perspective and launch the RCP application. The key classes involved in startup and initialization of the Eclipse workbench are also explained.
MS4 level being good citizen -imperative- (1) (1).pdf
L0020 - The Basic RCP Application
1. The Basic RCP Application
An overview of the code that makes up the skeleton of a basic RCP
application. This includes the basics for advisors and perspectives. This
module also describes the basics of how to launch and debug an RCP
application.
Redistribution and other use of this material requires written permission from The RCP Company.
L0001 - 2010-11-27
2. Creating an Eclipse RCP Application
Use the “Plug-in Project” wizard!
Project Page:
Specify the plug-in name:
com.rcpcompany.cex (or whatever)
2
L0001 - 2010-11-27
3. Creating an Eclipse RCP Application
Content Page
Change information as appropriate
Activator generated
We contribute to the UI
It is an RCP Application!
3
L0001 - 2010-11-27
4. Creating an Eclipse RCP Application
Templates Page:
Use “Hello RCP” template
4
L0001 - 2010-11-27
5. Launch the Application
The launch configuration for the application can
be created in several ways
E.g. use the “Launch an RCP Application”
in the Overview page of the plugin.xml
editor
Change the name of the application if needed
Use “Run” → “Open Run Dialog…”
5
L0001 - 2010-11-27
6. Update the Launch configuration
The arguments of the launch configuration for the application should be
updated with the following extra arguments:
-clean – cleans all internal caches
-console – starts the OSGi console
-consoleLog – prints all log messages on standard output
6
L0001 - 2010-11-27
7. Eclipse Workbench Start-Up Sequence
The Eclipse workbench is started in the following steps
From the user code the important classes are
Application (IApplication):
Handles the initialization of the display
Any login dialog
Creates and runs the workbench using the workbench advisor
WorkbenchAdvisor:
Names the initial perspective
Specifies the window advisor to use
Provides call-outs that is invoked during the life cycle of the workbench
– e.g. postStartup and preShutdown
WorkbenchWindowAdvisor:
Specifies the action bar advisor to use
Provides call-outs that is invoked during the life cycle of the workbench
window – e.g. postWindowCreate
7
L0001 - 2010-11-27
8. Eclipse Workbench Start-Up Sequence
ActionBarAdvisor:
In 3.2: Created all actions (if hard-coded) - filled in the actions in the
menu, tool bar, and status line as needed
In 3.3: Registers all built-in implementations for standard commands
In 3.4 and later: Empty!
8
L0001 - 2010-11-27
9. The Application (IApplication) Object (3.3 and later edition)
Identified via the extension point org.eclipse.core.runtime.applications
Simply creates a new SWT display and launches the application code
The place to handle login dialogs and similar
Eclipse 3.3 has added a stop() method to gracefully stop an application
public class Application implements IApplication {
public Object start(IApplicationContext context) throws Exception {
Display display = PlatformUI.createDisplay();
try {
int returnCode = PlatformUI.createAndRunWorkbench(display,
new ApplicationWorkbenchAdvisor());
if (returnCode == PlatformUI.RETURN_RESTART)
return IApplication.EXIT_RESTART;
else
return IApplication.EXIT_OK;
} finally {
display.dispose();
}
}
public void stop() {
…
}
}
9
L0001 - 2010-11-27
10. The WorkbenchAdvisor Object
Specified by the Application object
Identifies the initial perspective to be used in the application – can be
overridden by saved state
Specifies the WorkbenchWindowAdvisor to be used when SWT windows are
created
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
private static final String PERSPECTIVE_ID = "com.rcpcompany.cex.perspective";
public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
return new ApplicationWorkbenchWindowAdvisor(configurer);
}
public String getInitialWindowPerspectiveId() {
return PERSPECTIVE_ID;
}
}
10
L0001 - 2010-11-27
11. The WorkbenchWindowAdvisor Object
Specified by the WorkbenchAdvisor object
Specifies the ActionBarAdvisor to use
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
super(configurer);
}
@Override
public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
return new ApplicationActionBarAdvisor(configurer);
}
....
}
11
L0001 - 2010-11-27
12. The WorkbenchWindowAdvisor Object
Configures the basic window properties
Name
Size
Presence of basic UI elements
Menu bar, toolbar, status Line, fast views, and perspective switcher
Progress indicators
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
....
@Override
public void preWindowOpen() {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
configurer.setInitialSize(new Point(800, 600));
configurer.setShowCoolBar(false);
configurer.setShowMenuBar(true);
configurer.setShowStatusLine(false);
configurer.setTitle("Hello Contact Manager");
}
}
12
L0001 - 2010-11-27
13. The ActionBarAdvisor Object
Specified by the WorkbenchWindowAdvisor object
In 3.2: Created all actions (if hard-coded) - filled in the actions in the menu,
tool bar, and status line as needed
In 3.3: Registers all built-in implementations for standard commands
In 3.4 and later: Empty!
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
super(configurer);
}
protected void makeActions(IWorkbenchWindow window) {
}
protected void fillMenuBar(IMenuManager menuBar) {
}
}
13
L0001 - 2010-11-27
14. The Perspective Object
Specified by the WorkbenchAdvisor object
Creates the initial perspective in terms of
Views
Global actions
Shortcuts on relevant menus
public class Perspective implements IPerspectiveFactory {
public void createInitialLayout(IPageLayout layout) {
}
}
14
L0001 - 2010-11-27
15. Lab Exercise
Create the application.
If you have not used the Eclipse debugger before, then introduce an error –
e.g. divide-by-zero – and find the error in the debugger
15
L0001 - 2010-11-27
16. Adding a View
A new view can easily be added using the
extensions page for the plugin.xml file
Add a new extension with the name
“org.eclipse.ui.views”
Select the new line and add a
“New”→”view” using the context menu
Specify id, name and class as shown here
16
L0001 - 2010-11-27
17. Adding a View, cont’
Click on the “class” link to create the new View class automatically
Two methods must be implemented
createPartControl(Composite): Creates the visual content of the view
setFocus(): Sets the initial focus of the view
public class FirstView extends ViewPart {
@Override
public void createPartControl(Composite parent) {
}
@Override
public void setFocus() {
}
}
17
L0001 - 2010-11-27
18. Adding a View, cont’
Adding the view to the perspective
No editor is used
Views are positioned relatively to other views (or the editor)
The new view is placed to the left of the (non-existing) editor and will
take all of the room
public class Perspective implements IPerspectiveFactory {
public void createInitialLayout(IPageLayout layout) {
final String area = layout.getEditorArea();
layout.setEditorAreaVisible(false);
layout.addView(”com.rcpcompany.ex.views.FirstView”, IPageLayout.LEFT, 1.0f, area);
}
}
18
L0001 - 2010-11-27
19. Re-Launch the Application
Now the application includes the new view
Note that
The are no editor area
The view tab is present
The view can be closed
The view can be minimized
19
L0001 - 2010-11-27
20. Adding a File→Exit Command (3.3 and later edition)
Added in the plugin.xml file
<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="menu:org.eclipse.ui.main.menu">
<menu label="File” id=”file” mnemonic="F">
<command commandId="org.eclipse.ui.file.exit" />
</menu>
</menuContribution>
</extension>
And to the ActionBarAdvisor object (3.3 edition only)
Uses predefined exit action
public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
super(configurer);
}
protected void makeActions(IWorkbenchWindow window) {
register(ActionFactory.QUIT.create(window));
}
protected void fillMenuBar(IMenuManager menuBar) {
}
This is not needed for Eclipse 3.4 or later, where the behavior is
defined via the handlers extension point
20
L0001 - 2010-11-27
22. Lab Exercise
Create 4 views altogether
Add the views to the perspective
The way to position views in a perspective have not be discussed in
details, but…
Create the actions
Help→About
Hint: the ID is org.eclipse.ui.help.aboutAction
Extras:
Add the “Show View” command
22
L0001 - 2010-11-27
23. More Information
“Eclipse Rich Client Platform: Designing, Coding, and Packaging Java(TM)
Applications” by Jeff McAffer and Jean-Michel Lemieux (ISBN
978-0321334619)
The essential bible for everybody involved in Eclipse RCP. Includes
numerous examples with best practices for RCP applications
23
L0001 - 2010-11-27
Notas do Editor
\n
Creating a new Eclipse RCP is quite easy: just use the supplied wizard. \n
Enabling the API Analysis does not matter in this case. It is used to test for incompatible changes between releases of a plug-in.\n
Don&#x2019;t use any of thee other templates &#x2013; that would be cheating!\n
A launch configuration can be saved in the files system making it easy to share the configuration. Simply click the Shared option on the &#x201C;Common&#x201D; tab of the launch configuration and specify the name of the launch file. The shared launch configurations are automatically picked up by Eclipse IDE.\n
\n
The different advisors are described in details in the module &#x201C;L0038 - The Workbench Configuration&#x201D;.\n
The different advisors are described in details in the module &#x201C;L0038 - The Workbench Configuration&#x201D;.\n
\n
The changes made to way the application is started (and stopped), is primary due to better support servers. In server multiple applications can be running in the same process space and the start and stop methods are therefore important. \n
The generated code above is not really optional when scaling up the application. The PERSPECTIVE_ID should be moved to the Perspective class as ID.\n
\n
\n
The fact that the ActionBarAdvisor is specified by the WorkbenchWindowAdvisor means that each window will have its own set of actions, menus, etc. In most cases, this is overkill and could be avoided by some careful programming. Note though that if the windows are placed on different monitors (with different properties such as size, resolution and color-depth) then it is necessary to have different font and image resources for the different windows&#x2026;\nThe configurer is used to retrieve the different managers for the menu, tool bar, status line, etc. It is also used to register global actions if needed.\n
\n
Now it&#x2019;s time for the lab.\n
\n
\n
The exact possibilities when positioning views is covered in the module &#x201C;L0011 - Contributing to the Eclipse User Interface&#x201D;.\n
The tab can easily be avoided by using addStandaloneView(&#x2026;) instead of addView(&#x2026;). This is useful when certain view may never be moved or closed.\n
This is basically the same as on the previous slide &#x2013; just declared instead of programmed&#x2026;\n