Many eclipse applicationsProgramWhile providing an editor, some views are also provided. These views monitor the selection of the editor and provide context-related information. The propertysheet (attribute view) provided by eclipse is such an example. The outline (Outline View) is also like this. In terms of functions, you may call this type of view as "attribute view in a broad sense ".
In the past, I directly inherited viewpart to implement my own attribute view, but I found it takes a lot of effort to process the coordination between view and editor. For example, when the editor is closed, the information in the view should also be hidden, and the editor should also be switched to be processed. RecentlyCodeThey inherited Org. eclipse. UI. part. pagebookview achieves the same functions, just like propertysheet does. It works very well and requires less code to be written by yourself, because most of the functions mentioned above have been processed in pagebookview. The following describes how to use pagebookview for your reference.
As the name suggests, pagebookview is such a view. It is like a book that can contain multiple pages, but only displays one page at a time. Pagebookview is responsible for switching to a suitable Page Based on the workbenchpart of the current activity. The actual displayed content is mainly provided by the page, which is generally provided through the getadapter () method of workbenchpart.
When inheriting pagebookview to implement its own attribute view, the implementation of the pagebookview subclass is almost fixed. If your view is related to the selection in workbenchpart (context sensitive ), when inheriting the pagebookview, you should implement iselectionlistener By The Way, then register yourself as the workbenchpage selection listener in the init () method, and in selectionchanged () the following code is a typical context-sensitive pagebookview subclass:
Import Org. Eclipse. jface. Viewers. iselection; Import Org. Eclipse. SWT. Widgets. composite; Import Org. Eclipse. UI. ieditorpart; Import Org. Eclipse. UI. iselectionlistener; Import Org. Eclipse. UI. iviewsite; Import Org. Eclipse. UI. iworkbenchpage; Import Org. Eclipse. UI. iworkbenchpart; Import Org. Eclipse. UI. partinitexception; Import Org. Eclipse. UI. Part. ipage; Import Org. Eclipse. UI. Part. ipagebookviewpage; Import Org. Eclipse. UI. Part. messagepage; Import Org. Eclipse. UI. Part. pagebook; Import Org. Eclipse. UI. Part. pagebookview; Public Class ExampleviewExtends Pagebookview Implements Iselectionlistener { Public Exampleview () {}@ override Protected Ipage createdefaultpage (pagebook book) {messagepage page = New Messagepage (); initpage (PAGE); page. createcontrol (book); page. setmessage ( "An example view is not available ." ); Return Page;} @ override Public Void Init (iviewsite site) Throws Partinitexception { Super . INIT (SITE); site. getpage (). addselectionlistener ( This ) ;}@ Override Public Void Dispose () {getsite (). getpage (). removeselectionlistener ( This ); Super . Dispose () ;}@ override Protected Pagerec docreatepage (iworkbenchpart part ){ // Try to get a custom page Object OBJ = part. getadapter (iexamplepage. Class ); If (OBJ Instanceof Iexamplepage) {iexamplepage = (Iexamplepage) OBJ; If (Page Instanceof Ipagebookviewpage) initpage (ipagebookviewpage); page. createcontrol (getpagebook ()); Return New Pagerec (part, page );} // Use the default page Return Null ;} @ Override Protected Void Dodestroypage (iworkbenchpart part, pagerec pagerecord) {iexamplepage = (Iexamplepage) pagerecord. Page; page. Dispose (); pagerecord. Dispose () ;}@ override Protected Iworkbenchpart getbootstrappart () {iworkbenchpage = Getsite (). getpage (); If (Page! = Null ) Return Page. getactiveeditor (); Else Return Null ;} @ Override Protected Boolean Isimportant (iworkbenchpart part ){ Return (Part Instanceof Ieditorpart);} @ override Public Void Createpartcontrol (composite parent ){ Super . Createpartcontrol (parent );} Public Void Selectionchanged (iworkbenchpart part, iselection selection ){ // We ignore our own selection or null Selection If (Part = This | Selection = Null ){ Return ;} // Pass the selection to the page If (! (Getcurrentpage () Instanceof Iexamplepage )) Return ; Iexamplepage = (Iexamplepage) getcurrentpage (); If (Page! = Null ) {Page. selectionchanged (part, selection );}}}
In the view above, the page created by the createdefapage page () method is the information displayed when the active workbenchpart is not of the type we are concerned about, implemented by messagepage; isimportant () defines what kind of workbenchpart this view is interested in. In the above example, it only processes ieditorpart. As a comparison, the propertysheet of eclipse is interested in all types of workbenchpart except itself; if the active workbenchpart is "important" and Its getadapter () method is used to obtain the page we need, the page will be displayed in this view, otherwise, the default messagepage is displayed.
Now let's take a look at how to implement your own page. Generally, you must first define an interface that inherits from the ipage. One of its functions is to use the workbenchpart # getadapter () method as a parameter to identify the required page type, if the previous pagebookview subclass has implemented iselectionlistener, most of them need to implement this interface, as shown below:
ImportOrg. Eclipse. UI. iselectionlistener;ImportOrg. Eclipse. UI. Part. ipage;Public InterfaceIexamplepageExtendsIpage, iselectionlistener {}
Then there is a specific page class. This class is actually similar to the code that we used to implement viewpart. It inherits from Org. eclipse. UI. part. page and implement the interface just defined. The most important thing is the createcontrol () method. If you need the selectionchanged () method:
Import Org. Eclipse. SWT. SWT; Import Org. Eclipse. SWT. Widgets. composite; Import Org. Eclipse. SWT. Widgets. Control; Import Org. Eclipse. UI. Part. page; /** * @ Author Zhanghao * */ Public Class Examplepage Extends Page Implements Iexamplepage {text txtname; Public Examplepage (){ Super () ;}@ Override Public Void Createcontrol (composite parent ){ // Create your autual view UI here Txtname = New Text (parent, SWT. Border) ;}@ override Public Control getcontrol (){ Return Txtname ;}@ override Public Void Setfocus (){} Public Void Selectionchanged (iworkbenchpart part, iselection selection) {string name =; // Get information from selection Txtname. settext (name );}}
In addition, each page has an independent actionbar, that is, to add a menu or toolbar for pagebookview, it should be completed in the page, which is generally implemented in the init () method.
There is no step yet, that is, to respond to this view in the getadapter () method of workbenchpart (this requires that workbenchpart can access the custom page class ), in typical cases, we use our own page interface as the parameter. The code is similar to the following:
PublicObject getadapter (class type ){If(Type = iexamplepage.Class){Return NewExamplepage ();}Return Super. Getadapter (type );}
Of course, as before, you still need to register this view in plugin. xml so that you can use the "window-> show View" menu command in eclipse to display it.
Finally, compared with propertysheet, this implementation has a small defect, that is, when this view is opened for the first time, the information selected in the editor cannot be displayed. You may try to solve this problem. (NOTE: Refer to the partactivated () method of propertysheet)