RCP Tutorial

RCP Tutorial

-

English
11 Pages
Read
Download
Downloading requires you to have access to the YouScribe library
Learn all about the services we offer

Description

RCP TutorialLesson 3© Henning V itting 2006, all rights reserved© Henning V itting 2006, all rights reservedLicense: This pa per and t he accompanying m aterials are made available unde r the terms of the Eclipse Public Li cense v1.0 which a ccompanies this distribution. Further information a bout the Eclipse Public License can be found a t http://www.eclipse.org/ legal/epl-v10.ht mlEclipse and Bui lt on E clipse are trademarks of E clipse Foundation, Inc .RCP Tutorial - Lesson 3Table of Contents1Be fore you s tart.................................................................................................................................. 31.1P rerequisites................................................................................................................................ 31.2Conve ntions 31.3S ample Code ............................................................................................................................... 32Advisor(s )........................................................................................................................................... 32.1Advisor ove rview........................................................................................................................ 32.2Adding t he advisors.................................................................................................................... 52.3T he first menu's........................................................... ...

Subjects

Informations

Published by
Reads 14
Language English
Report a problem
RCP Tutorial
Lesson 3
© Henning Vitting 2006, all rights reserved
© Henning Vitting 2006, all rights reserved
License: This paper and the accompanying materials are made available under the terms of theEclipse Public License v1.0which accompanies this distribution. Further information about the Eclipse Public License can be found at http://www.eclipse.org/legal/epl-v10.html Eclipse and Built on Eclipse are trademarks of Eclipse Foundation, Inc.
RCP Tutorial - Lesson 3
Table of Contents 1Before you start.................................................................................................................................. 3 1.1Prerequisites................................................................................................................................3 1.2Conventions................................................................................................................................ 3 1.3Sample Code............................................................................................................................... 3 2Advisor(s)........................................................................................................................................... 3 2.1Advisor overview........................................................................................................................3 2.2Adding the advisors.................................................................................................................... 5 2.3The first menu's...........................................................................................................................7 3Test..................................................................................................................................................... 8 4CVS (optional)....................................................................................................................................9 5Ready for more?................................................................................................................................. 9
1
Before you start
RCP Tutorial - Lesson 3
1.1 Prerequisites You must have completed Lesson 2 DownloadLessonMaterial 3_Post_100.zipcontaining the Source material for the completed lesson.
1.2 Conventions The following formatting conventions are used throughout the tutorial: Italics-BoldUsed for UI actions, field values to type and menu paths e.g.,File->New->Project BoldUsed to highlight names of files, views, fields etc. ItalicsUsed foremphasis and to highlight terminology. /* * Code samples are written on a light gray background *This is a highlighted line of code * */
1.3 Sample Code LessonMaterial 3_Post_100.zip.Unzip the sample code material to a folder on the file system. To enter source code in the your workspace, use cut and paste or useFile->Importthenselect File System . If you use the operating systems explorer to copy files to your project youmust refresh your projectFile->Refreshto resynchronize the workspace to the file system.
2
Advisor(s)
2.1 Advisor overview The Advisor classes are some of the most important classes in the RCP framework. In lesson 2 we created theApplicationWorkbenchAdvisorclass as an extension of the abstract classWorkbenchAdvisor.If you have looked at RCP in Eclipse prior to version 3.1 you have seen that the WorkbenchAdvisor was the base class for configuring the workbench. In version 3.1 the functionality of the WorkbenchAdvisor was split into 3 advisor classes: WorkbenchAdvisor WorkbenchWindowAdvisor ActionBarAdvisor The methods in the 3 advisor classes are called to notify you at various points of the application life cycle.
Last Changed: Tue, Feb 21, 06
Page 3
RCP Tutorial - Lesson 3
To use the advisor classes they must be subclassed, I used the names: ApplicationWorkbenchAdvisor(we have already created this one) ApplicationWorkbenchWindowAdvisor ApplicationActionBarAdvisor
The workbenchadvisor's API Methods are well described in the help system, Help->Help ContentsexpandPlatform-plug-in Developers Guide +Reference +API Reference, and select the packageorg.excipse.ui.applicationin theClass Summery,then select WorkbenchAdvisor.
Last Changed: Tue, Feb 21, 06
Page 4
RCP Tutorial - Lesson 3
I will just mention the API methods used in our application: InApplicationWorkbenchAdvisor initialize(IWorkbenchConfigurer configurer) Called prior to to any windows being opened. Calls configurer.setSaveAndRestore(true)to ensure that the workbench settings are saved on exit. In the completed application error logging is initialized in the data model. createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer) Called before the workbench window opens. Creates ApplicationWorkbenchWindowAdvisor
getInitialWindowPerspectiveId()
Returns the perspective id for the initial perspective.
InApplicationWorkbenchWindowAdvisor
preWindowOpen()
Called before the window is opened. I used it to update the IworkbenchWindowConfigurerproperties initialSize, showMenubar, showCoolbar and showStatusLine. createActionBarAdvisor(IActionBarConfigurer configurer)Created theApplicationActionBarAdvisor
InApplicationActionBarAdvisor makeActions(IWorkbenchWindow window) Instantiates and registers the static actions used in the menus. fillMenuBar(IMenuManager menuBar) Builds the menu tree with the static actions, also adds placeholders for contributed actions and menus.
2.2 Adding the advisors We will add the remaining advisors to our project. You can of course import them from the lesson source material, but as you will see Eclipse will do the most of the work when we start coding. First we will overwrite theinitialize(IWorkbenchConfigurer configurer)method. We will let the content assist do the job. Place the cursor where you want to add the method and typeinitthen presscntrl+space barto activate content assist. In the pop-up menu selectinitialize(......and press enter. Let us add the method doc also. Move the cursor to the top line of the newly created method then pressalt+shift+j;that is it. Now addcreateWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer)in the same way. The completed class should look like this:
Last Changed: Tue, Feb 21, 06
Page 5
RCP Tutorial - Lesson 3
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {  private static final String PERSPECTIVE_ID = "com.vitting.rcpsudoku.perspective";
 /*  * (non-Javadoc)  *  * @see org.eclipse.ui.application.WorkbenchAdvisor....  */  public String getInitialWindowPerspectiveId() {  return PERSPECTIVE_ID;  }
 /* (non-Javadoc)  * @see org.eclipse.ui.application.WorkbenchAdvisor...  */  public void initialize(IWorkbenchConfigurer configurer) {  configurer.setSaveAndRestore(true);  }
 /* (non-Javadoc)  * @see org.eclipse.ui.application.WorkbenchAdvisor...  */  public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {  return new ApplicationWorkbenchWindowAdvisor(configurer);  } } You will get an error, the typeApplicationWorkbenchWindowAdvisoris not known, as in lesson 2 use useQuick-fixto create the missing class definition, and fix the remaining errors. Complete the code forApplicationWorkbenchWindowAdvisor . The completed class should look like this: public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
 public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {  super(configurer);
 }
 /* (non-Javadoc)  * @see org.eclipse.ui.application.WorkbenchWindowAdvisor...  */  public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {  return new ApplicationActionBarAdvisor(configurer);  }  /* (non-Javadoc)  * @see org.eclipse.ui.application.WorkbenchWindowAdvisor...  */  public void preWindowOpen() {  IWorkbenchWindowConfigurer configurer = getWindowConfigurer();  configurer.setInitialSize(new Point(600, 600));  configurer.setShowMenuBar(true);  configurer.setShowCoolBar(false);  configurer.setShowStatusLine(true);
Last Changed: Tue, Feb 21, 06
Page 6
} }
RCP Tutorial - Lesson 3
Again useQuick-fixgenerate import statements and generate the missing type ApplicationActionBarAdvisor.Observe that we have decided our application should have aMenu Barand aStatus Linebut noCool Bar. Complete the code forApplicationActionBarAdvisorfix any errors. Let's try to another method to generate the missing methods: Alt+Shift +sto open theSourcepop-up menu, then selectOverride/Implement Methods...to open theOverride/Implement Methodsdialog. Select thefillMenuBarandmakeActionscheck boxes, also make sure theGenerate method commentscheck box is selected then pressOK The completed class should look like this: public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
 public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) { super(configurer); // TODO Auto-generated constructor stub
}
 /* (non-Javadoc)  * @see org.eclipse.ui.application.ActionBar...  */  protected void fillMenuBar(IMenuManager menuBar) { // TODO Auto-generated method stub super.fillMenuBar(menuBar);  }
 /* (non-Javadoc)  * @see org.eclipse.ui.application.Action...  */  protected void makeActions(IWorkbenchWindow window) { // TODO Auto-generated method stub  super.makeActions(window);  } }
2.3 The first menu's Let us put the advisors to use and add the first menu's. Game->Exit Help->About To define the actions for the menus, add the following 2 lines to the ApplicationActionBarAdvisor private IWorkbenchAction exitAction; private IWorkbenchAction aboutAction;
In the methodmakeActionscreate and register the actions, The actionsexitAction and aboutActionare workbench standard actions, created byorg.eclipse.ui.actions.ActionFactory.
Last Changed: Tue, Feb 21, 06
Page 7
RCP Tutorial - Lesson 3
Change themakeActionsmethod to look like this  protected void makeActions(IWorkbenchWindow window) {  exitAction = ActionFactory.QUIT.create(window);  register(exitAction);  aboutAction = ActionFactory.ABOUT.create(window);  register(aboutAction);  }
Now the actions are created we can add code to thefillMenuBarmethod to actually show the menus.  protected void fillMenuBar(IMenuManager menuBar) {  // Create and add the Game menu  MenuManager gameMenu = new MenuManager("Game", "game");  menuBar.add(gameMenu);  gameMenu.add(exitAction);
 // Create and add the Help menu  MenuManager helpMenu = new MenuManager("Help", "help");  menuBar.add(helpMenu);  helpMenu.add(aboutAction);  }
3 Test Launch the application again, if you have forgotten how, reviewTestin lesson 2.
For this application we have chosen to name the File MenuGame Test the menus, theAbout Boxis not displaying much information, also the windowiconis empty and there is no windowtitlein the title-bar. We will fix that in the next lesson. Try to resize the window and close the application. Open your run configurationRun->run...and make sureClear workspace Data ..... is not selected. PressRunto launch the program again, the window size is remembered between sessions.
In theApplicationWorkbenchAdvisorwe added:  public void initialize(IWorkbenchConfigurer configurer) {  configurer.setSaveAndRestore(true);  }
Which sets the workbench state should be saved on close and restored on subsequent open
Last Changed: Tue, Feb 21, 06
Page 8
RCP Tutorial - Lesson 3
4 CVS (optional) Let us save our work in the repository.Right clickon thecom.vitting.rcpsudokuthen TeamCommit... Just add your comments and pressFinish To be able to retrieve this code from the repository, let us tag the files in the repository as a version Right clickon thecom.vitting.rcpsudokuthenTeam-->Tag as version.Uselesson_3as version tag. Now it will be possible to compare later versions of the code, with this one or reload the tagged code in a workspace.
5 Ready for more? After completing this lesson you should be able to Use the 3 advisor classes Programmatically add workbench standard actions as menus item to the menu bar
In Lesson 4 we will turn our application into a real branded RCP Product.
Last Changed: Tue, Feb 21, 06
Page 9