5.1.3 a basic RCP application of the RCP basic framework has been set up. Although it is still very immature, it has already taken shape. Exploring the basic framework of RCP is of great benefit to understanding RCP. See Figure 5-6 above. Out, eclipse has automatically generated application. Java, applicationactionbaradvisor. Java, Applicationworkbenchadvisor. Java, applicationworkbench- Windowadvisor. Java and perspective. Java class files. It is necessary to analyze these five files to gain a deeper understanding of the basic RCP box. Rack. Before analyzing, let's take a look at several basic concepts. 1. Basic concepts (1) display applications generally only need one display object, which is actually a SWT object, representing the underlying Graph System implementation. The main task of display is event processing. It is responsible for reading the event from the event queue of the operating system and passing the event listener to RCP to complete the specific task. Note: Yes. The Display object does not represent any visual controls in the application window. display is not available until the application main window is opened. After the application main window is opened, you can use Get an object called shell. The Display object is obtained as follows. Display display = New Display (); or: Display display = display. getcurrent (); (2) each shell window has a shell object. Shell Objects represent window frameworks that interact with users and process common activities associated with windows, such as moving and changing the size. How to obtain the shell object: Display display = display. getcurrent (); shell = new shell (Display); or: Shell shell = display. getcurrent (). getactiveshell (); (3) workbenchworkbench is a workbench, which stands for the UI elements of the user interface. There are various windows, icons, buttons, and controls on the workbench. You can perform various operations on the workbench. How to get the workbench object: iworkbench WB = platformui. getworkbench (); two methods of the workbench object may be useful: l WB. Restart (), close the application and immediately restart. L WB. Close (), close the application normally, equivalent to actionfactory. quit_action. (4) advisoradvisor is the author. The user's main interface width, height, icon, menu, toolbar, color, operation and other configuration schemes need advisor to propose "Suggestions ". Eclipse provides three types of advisor: l workbenchadvisor. At the application level, eclipse3.0 introduces a new class. Each application has only one Workbench, workbenchadvisor is responsible for the lifecycle management of the workbench, such as start and close. Workbenchadvisor is also responsible for this Workbench Exception Handling and provides some important parameters to workbench. For example, you can specify the initial perspective of the workbench. L workbenchwindowadvisor. Window Level. Each window has a workbenchwindow-Advisor Instance. Workbenchwindowadvisor Manages the lifecycle of a window, such as the status bar, toolbar, menu, window title, window size, and various controls. It can also process various event routines of the window. L actionbaradvisor. Window Level. Each window has an actionbaradvisor instance. Actionbaradvisor manages the appearance and behavior of the menu bar, Status Bar, and toolbar of the window. (5) view and editor in eclipse In RCP applications, users mainly interact with programs through views and editors. A view is a floating work window that displays the status information of data at a certain level. The editor is also a work window that provides the ability to interact with various operations on data. As a result, there is no strict distinction between the use of views and the use of editors, depending on the needs of developers and users, for example, should You can use all views on the application interface without any obstacles. The Fund calculation system is implemented only by the public parameter setting function module using the editor. Most other interfaces are implemented using views. The view is extended from the viewpart abstract class. Developers usually need to write code in the createpartcontrol () method to build the interface elements of the view. The following is a simple view without any code processing. Package cn.edu. jfcs. ui; import Org. eclipse. SWT. widgets. composite; import Org. eclipse. UI. part. viewpart; public class teachunitname extends viewpart {public teachunitname () {// todo auto-generated constructor stub} public void createpartcontrol (composite parent) {// todo auto-generated method stub} public void setfocus () {// todo auto-generated method stub} (6) the relationship between perspectiveperspective and view and editor is similar. The relationship between schoolbags, books, and pens. There are various books and pens in the bag, the bag is perspective, and the book and pen are Similar to views and editors. Perspective is called a perspective. The RCP application arranges and la s the window content through the perspective. Each application has a default perspective. Workbench windows can contain one or more perspectives, and you can add other UI elements to the perspective. The default perspective is fixed and cannot be moved without a title bar. For example After the billing system is used, click Help> welcome to open the welcome page in the default perspective. When you close the welcome page, a minimum value with no title bar is displayed. An empty interface of the maximize button, which is the default perspective. The perspective must implement the iperspectivefactory interface. Iperspectivefactory can be said to be a worker that generates the initial page layout and visibility perspective. Factory. In eclipse, any new page opened by the workbench requires a perspective to define its initial layout. Specifically, you need to pass an ipagelayout object to the perspective factory, Use this object to arrange the initial layout of the view or editor. By default, the initial layout contains a visible editing area. Of course, the editing area can also be closed. 2. Class File Analysis (1) application. Java for common Java applications, there is always a main () method, and the application always starts to run from main. Application is similar to a Java application. Main (), that is, the RCP application program starts from the application, and the application is the entrance of the RCP application. Application has implemented Iplatformrunnable interface, the run () method of this interface will be executed when RCP is started. You can see from plugin. XML that the Application It is actually eclipse Org. Eclipse. Core. runtime. applications is an entry to the extension point during runtime. The main application code is as follows: cn.edu. jfcs. APP/application. javaimport... Public class application implements iplatformrunnable {public object run (Object ARGs) throws exception {display = platformui. createdisplay (); try {int returncode = platformui. createandrunworkbench (display, new applicationworkbenchadvisor (); If (returncode = platformui. return_restart) {return iplatformrunnable. exit_restart;} return iplatformrunnable. exit_ OK;} finally {display. dispos E () ;}} can use the application as the controller of the RCP application, which is responsible for starting and stopping the application. Application first creates a display object, Call the platformui. createandrunworkbench () method to start the workbench. This will open the main application window and continue to open. Applications Start to process the user's mouse clicks, mouse moves, buttons and other events until the user closes the program to exit, this is called an event loop. Of course, you must destroy the Display object to release Display. Dispose ()). From the above analysis, we can get the following information: The application main window is only available in the statement "int returncode = Platformui. createandrunworkbench (display, new Applicationworkbenchadvisor (); "is opened and visualized only after the call. Developers can use this to make a fuss. Window recording, starting the Derby database server, initialization, and so on. The code is similar to the following format ....... // Start the Derby Database Server // open the logon interface int returncode = platformui. createandrunworkbench (display, new applicationworkbenchadvisor (); If (returncode = platformui. return_restart) {return iplatformrunnable. exit_restart ;}...... The implementation of user login processing in Chapter 6th is based on this approach. Note that platformui. createandrun- In the workbench () method, in addition to the Display object, another object is also passed to it: new application- Workbenchadvisor (), what does applicationworkbenchadvisor do? (2) applicationworkbenchadvisor. javaapplicationworkbenchadvisor class is responsible for application lifecycle management, following From the workbenchadvisor class. Developers can implement some processing when the program is started or closed in this class. The specific implementation of this class only gives the user an initial view interface. Workbenchwindowadvisor and actionbaradvisor must be used together to form a complete user interface. The applicationworkbenchadvisor class mainly contains five important methods, which are arranged in the order of execution, as shown in table 5-1. Table 5-1 main applicationworkbenchadvisor Methods
Method Name |
Fang fa description |
Initialize |
First call. Called before the window is opened. It can be used to handle initialization configuration. |
Prestartup |
Called after initialize and before the first window opens. It can be used for temporary or optional processing. |
Poststartup |
The first window is opened but called before the event loop is started. It can be used to perform tasks that require automatic processing. For example, a prompt window is displayed. |
Preshutdown |
It is called after the event loop ends but before the window is closed. It can be used to store data and close the database server. |
Postshutdown |
When the window is closed, it is called to save the application status and clear the objects created by initialize. |
The main code of applicationworkbenchadvisor. Java is as follows. Cn.edu. jfcs. APP/applicationworkbenchadvisor. javaimport... Public class applicationworkbenchadvisor extends workbenchadvisor {Private Static final string perspective_id = "cn.edu. jfcs. perspective "; Public workbenchwindowadvisor createworkbenchwindowadvisor (iworkbenchwindowconfigurer extends ER) {return New applicationworkbenchwindowadvisor (extends ER);} public void initialize (iworkbenchconfigurer extends ER) {super. initialize (aggreger); aggreger. setsaveandrestore (true);} Public String getinitialwindowperspectiveid () {return perspective_id;} The above program mainly does two things: one is the perspective that needs to be displayed on the main interface during initialization. (Perspective), create and return the applicationworkbenchwindowadvisor object. Second, the code in the initialization method The consumer er. setsaveandrestore (true) is used to set the current window status of the application to be saved every time the application exits and the application is applied at the next startup. Performance is slightly affected. We recommend that you change it to false without saving it. The getinitialwindowperspectiveid () method opens the default perspective. Cn.edu. jfcs. perspective.
|
If you write a large amount of processing code in the initialization initialize event, it will cause a delay in the main window when the application starts, giving you a low system speed. Therefore, try to avoid this problem. |
(3) applicationworkbenchwindowadvisor. javaapplicationworkbenchwindowadvisor class is responsible for the application window Lifecycle management, which is extended from workbenchwindowadvisor. Every application requires a workbenchwindowadvisor to control UI elements of the window interface. Developers can control the size, title, and position of the window during creation, or add methods called when the window is created, opened, restored, or closed. The main methods of applicationworkbenchwindowadvisor are shown in Table 5-2. Table 5-2 main applicationworkbenchwindowadvisor Methods
Method Name |
Fang fa description |
Prewindowopen |
Call the window control before it is created. In this method, you can set the visibility of the window, such as the initial size, Status Bar, and toolbar. |
Postwindowrestore |
This function is called after the window is created based on the last saved status. It can be used to adjust the window recovery status. |
Postwindowcreate |
Called after the window is created, which can be used to adjust the window |
Postwindowopen |
The window has been opened and called. It can be used to register window listening. For example, in this method, the system tray is implemented. |
The main code of applicationworkbenchwindowadvisor. Java automatically generated by eclipse is as follows: cn.edu. jfcs. APP/applicationworkbenchwindowadvisor. javaimport... Public class extends workbenchwindowadvisor {public extends (extends ER) {super (extends ER);} public actionbaradvisor createactionbaradvisor (iactionbarconfigurer extends ER) {return New applicationactionbaradvisor (extends ER);} public void prewindowopen () {iworkbenchwindowconfigurer worker ER = getwindowcon Figurer (); aggreger. setinitialsize (new point (700,550); // defines the window initialization size. setshowcoolbar (false); // The toolbar receiver is not displayed. setshowstatusline (false); // the status bar is not displayed. settitle ("jfcs"); // The window title is jfcs}. This class creates an applicationactionbaradvisor object and uses the prewindowopen () method to configure the window. In In the prewindowopen () method, the volume er object is first generated. The size, toolbar, status bar, title, and other visual elements of the window are configured through this object, that is, the control The appearance of the window. By default, the toolbar and status bar are hidden. Iworkbenchwindowcycler is an interface class that provides many The Set/get method allows you to access the configuration of the workbench window. In fact, you may also notice that each type of advisor has a corresponding aggreger. Iworkbenchconfigurer, Iworkbenchwindowconfigurer in applicationworkbenchwindowadvisor Application- Iactionbarconfigurer in actionbaradvisor, which deals with workbench, window, menu, toolbar, and status bar A convenient bridge. (4) applicationactionbaradvisor. javaapplicationactionbaradvisor. Java class defines window behavior, which is extended from actionbaradvisor. Location This class is used to build the menu bar, toolbar, and status line. Actionbaradvisor is responsible for creating and registering the menu bar, toolbar, and status line. The main methods of applicationactionbaradvisor are shown in Table 5-3. Table 5-3 main applicationactionbaradvisor Methods
Method Name |
Fang fa description |
Void makeactions (iworkbenchwindow window) |
Register menu or toolbar actions |
Void fillmenubar (imenumanager menubar) |
Add menu bar |
Void fillcoolbar (icoolbarmanager coolbar) |
Add Toolbar |
Void fillstatusline (istatuslinemanager statusline) |
Add Status Bar |
The main code of applicationactionbaradvisor. Java is as follows. Cn.edu. jfcs. APP/applicationactionbaradvisor. javaimport... Public class applicationactionbaradvisor extends actionbaradvisor {private iworkbenchaction introaction; Public applicationactionbaradvisor (iactionbarconfigurer extends ER) {super (extends ER);} protected void makeactions (iworkbenchwindow window) {// use actionfactory action factory to create introaction = actionfactory. intro. create (window); Register (introaction);} protected void fillmenubar (imenumanager Menubar) {menumanager helpmenu = new menumanager ("& help", iworkbenchactionconstants. m_help); menubar. add (helpmenu); helpmenu. add (introaction) ;}} the program creates a help menu bar, and the actions executed by its Sub-menu are implemented using the actionfactory built in eclipse. intro. If you perform this operation, the welcome screen of welcome is displayed. (5) The perspective. javaperspective class is mainly responsible for the arrangement of the interface layout. The perspective. Java code is as follows. Cn.edu. jfcs. APP/perspective. javaimport... Public class perspective implements iperspectivefactory {public void createinitiallayout (ipagelayout layout) {string editorarea = layout. geteditorarea (); // obtain the layout of the layout editing area. seteditorareavisible (false); // hide layout in the editing area. setfixed (true); // fixed layout // various views are added here} The createinitiallayout () method is usually used to create an initial interface layout, which is currently empty, therefore, after the Fund calculation system is opened and the welcome screen is closed, the interface will be blank and the main interface view will be added later. |