Eclipse RCP Series 3 learn more about Viewer

Source: Internet
Author: User
On the basis of step 2, we need to add a viewer at this time. Here I need to talk about a lot
The concept of viewer (View) is not similar to Windows that are often used in development such as VB. It does not mean that RCP has no window, but uses
The frequency is low, so let's talk about how to add a window and a viewer respectively.

1. Add a dialog box window: Add a class as follows, and then call the open () method where you need to view it.
It is not necessary to inherit from the dialog. Here, in order to get less code, and I also use it to inherit from the dialog.
Package hellorcp;

Import org. Eclipse. SWT. SWT;
Import org. Eclipse. SWT. Widgets. Dialog;
Import org. Eclipse. SWT. Widgets. display;
Import org. Eclipse. SWT. Widgets. shell;

Public class hellodialog extends dialog {

Protected object result;

Protected shell;

Public hellodialog (shell parent, int style ){
Super (parent, style );
}

Public hellodialog (shell parent ){
This (parent, SWT. None );
}
Public object open (){
Createcontents ();
Shell. open ();
Shell. layout ();
Display display = getparent (). getdisplay ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ())
Display. Sleep ();
}
Return result;
}

Protected void createcontents (){
Shell = new shell (getparent (), SWT. dialog_trim | SWT. application_modal );
Shell. setsize (500,375 );
Shell. settext ("SWT dialog ");
//
}

}

2. Add a viewer. First, create a viewer. Below is a viewer automatically generated by designer (a useful plug-in,
That is, the approximate structure of a viewer.
Package hellorcp;

Import org. Eclipse. jface. Action. imenumanager;
Import org. Eclipse. jface. Action. itoolbarmanager;
Import org. Eclipse. SWT. SWT;
Import org. Eclipse. SWT. Widgets. composite;
Import org. Eclipse. UI. Part. viewpart;

Public class helloview extends viewpart {

Public static final string id = "hellorcp. helloview"; // $ NON-NLS-1 $

Public void createpartcontrol (composite parent ){
Composite container = new composite (parent, SWT. None );
//
Createactions ();
Initializetoolbar ();
Initializemenu ();
}

Private void createactions (){
// Create the actions
}
Private void initializetoolbar (){
Itoolbarmanager toolbarmanager = getviewsite (). getactionbars ()
. Gettoolbarmanager ();
}

Private void initializemenu (){
Imenumanager menumanager = getviewsite (). getactionbars ()
. Getmenumanager ();
}

Public void setfocus (){
// Set the focus
}

}
To display the viewer, Each viewer must be loaded to perspective to display the viewer. Therefore, add the following code to perspective. Java:
Public void createinitiallayout (ipagelayout layout ){
Layout. seteditorareavisible (false); // The edit window is not displayed.
String editorarea = layout. geteditorarea ();
// The difference between the following two sentences is that a single page window is displayed and a multi-page window is displayed.
Layout. addstandaloneview (helloviewer. ID, false, ipagelayout. Left, 0.25f, editorarea );
Layout. addview (helloviewer. ID, ipagelayout. Right, 0.75f, editorarea );
}

3. Add controls to the viewer or dialog. If a designer is installed, you can drag and drop them directly. If no programming implementation is available, you can also
Most of them are added to the following functions.
Viewer:
Public void createpartcontrol (composite parent ){
Composite container = new composite (parent, SWT. None );
// Add a button
Final button delbtn = new button (container, SWT. None );
Delbtn. settext ("delete ");
Delbtn. setbounds (10, 83, 44, 22 );
Addlistener2delbtn (delbtn );

Createactions ();
Initializetoolbar ();
Initializemenu ();
}
Dialog:
Protected void createcontents (){
Shell = new shell (getparent (), SWT. dialog_trim | SWT. application_modal );
Shell. setsize (500,375 );
Shell. settext ("SWT dialog ");
}

4. Respond to events. The SWT event response is the same as that of swing. Add listener
Delbtn. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent e ){
// Add what you need to do in response to the event
}
});

5. Layout
In terms of layout, there is nothing new about SWT. Let's give an example of simple layout usage. For more information, see. There are also a lot of la s, but it is not in this article.
Important, temporarily included
Import org. Eclipse. SWT. SWT;
Import org. Eclipse. SWT. Events. selectionevent;
Import org. Eclipse. SWT. Events. selectionlistener;
Import org. Eclipse. SWT. Graphics. Font;
Import org. Eclipse. SWT. Graphics. Point;
Import org. Eclipse. SWT. Graphics. rectangle;
Import org. Eclipse. SWT. layout. formattachment;
Import org. Eclipse. SWT. layout. formdata;
Import org. Eclipse. SWT. layout. formlayout;
Import org. Eclipse. SWT. Widgets. Button;
Import org. Eclipse. SWT. Widgets. display;
Import org. Eclipse. SWT. Widgets. shell;
Import org. Eclipse. SWT. Widgets. Table;
Import org. Eclipse. SWT. Widgets. tablecolumn;
Import org. Eclipse. SWT. Widgets. tableitem;

Public class tabledemo {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Display dispmain = New Display ();

Final shell shellmain = new shell (dispmain, SWT. Title | SWT. min | SWT. Border );

Shellmain. settext ("Table Demo ");

Formlayout = new formlayout ();
Formlayout. marginleft = 10;
Formlayout. marginright = 10;
Formlayout. margintop = 10;
Formlayout. marginbottom = 10;
Formlayout. spacing = 10;
Shellmain. setlayout (formlayout );

Shellmain. setsize (800,600 );
Point size = shellmain. getsize ();
Rectangle rect = dispmain. getbounds ();
Shellmain. setlocation (rect. x + (rect. width-size.x)/2, rect. Y + (rect. height-size.y)/2 );

Table demotable = (table) createcontents (shellmain );
Formdata = new formdata ();
Formdata. Left = new formattachment (0, 0 );
Formdata. Top = new formattachment (0, 0 );
Formdata. Right = new formattachment (100, 0 );
Formdata. Bottom = new formattachment (100,-34 );
Demotable. setlayoutdata (formdata );

Button btnclose = new button (shellmain, SWT. Push | SWT. Flat );
Btnclose. settext ("close ");

Formdata = new formdata ();
Formdata. Right = new formattachment (100, 0 );
Formdata. Top = new formattachment (demotable, 0 );
Formdata. width = 100;
Formdata. Bottom = new formattachment (100, 0 );
Btnclose. setlayoutdata (formdata );

Btnclose. addselectionlistener (
New selectionlistener (){
Public void widgetdefaselecselected (selectionevent e ){
This. widgetselected (E );
}

Public void widgetselected (selectionevent e ){
Shellmain. Close ();
}
}
);
Shellmain. open ();

While (! Shellmain. isdisposed ()){
If (! Dispmain. readanddispatch ()){
Dispmain. Sleep ();
}
}
Dispmain. Dispose ();
Dispmain = NULL;
}

/**
*
* @ Param shellmain
* @ Return
*/
Private Static table createcontents (shell shellmain ){
Table = new table (shellmain, SWT. h_scroll | SWT. v_scroll | SWT. Border );
Table. setheadervisible (true );
Table. setlinesvisible (true );
Table. setfont (new font (shellmain. getdisplay (), "Arial", 11, SWT. Bold ));

Tablecolumn colnumber = new tablecolumn (table, SWT. None );
Tablecolumn colmedname = new tablecolumn (table, SWT. center );
Tablecolumn colprice = new tablecolumn (table, SWT. center );
Tablecolumn colunit = new tablecolumn (table, SWT. center );
Tablecolumn colcount = new tablecolumn (table, SWT. center );

Colnumber. setwidth (25 );

Colmedname. setwidth (150 );
Colmedname. settext ("medicine name ");

Colprice. setwidth (150 );
Colprice. settext ("medicine price ");

Colunit. setwidth (150 );
Colunit. settext ("Medicine Unit ");

Colcount. setwidth (150 );
Colcount. settext ("Medicine count ");

For (INT I = 0; I <10; I ++ ){
Tableitem item = new tableitem (table, SWT. None );
Item. settext (New String [] {I + 1 + "", "4/2", "4/3", "4/4", "4/5", "4/6", "4/7 ", "4/8", "4/9 "});
}
Return table;
}
}
5. Right-click and double-click
Add two listener listeners
// Right-click
Private void hookcontextmenu (){
Menumanager menumgr = new menumanager ("# popupmenu"); // $ NON-NLS-1 $
Menumgr. setremoveallwhenshown (true );
Menumgr. addmenulistener (New imenulistener (){
Public void menuabouttoshow (imenumanager manager ){
Helloview. This. fillcontextmenu (manager );
}
});
Menu = menumgr. createcontextmenu (viewer. getcontrol ());
Viewer. getcontrol (). setmenu (menu );
Getsite (). registercontextmenu (menumgr, viewer );
}
Private void fillcontextmenu (imenumanager manager ){
Manager. Add (addaction );
Manager. Add (modifyaction );
Manager. Add (deleteaction );
Manager. Add (new separator (iworkbenchactionconstants. mb_additions ));
}
// Double-click
Private void hookdoubleclickaction (){
Viewer. adddoubleclicklistener (New idoubleclicklistener (){
Public void DoubleClick (doubleclickevent event ){
// Doubleclickaction. Run ();
}
});
}

6. Use tableviewer
Jface encapsulates controls such as talbeviewer treeviewer to implement many functions. First, let's talk about tableview.
// SWT. full_selection can select an entire row
// SWT. multi can be selected multiple rows
Viewer = new tableviewer (warelistgroup, SWT. Border | SWT. full_selection
| SWT. multi );
Final table = viewer. gettable ();
Table. setheadervisible (true); // display the header
Table. setlinesvisible (true); // display the table

// Automatically sorts the click header.
Final tablecolumn num = new tablecolumn (table, SWT. None );
Num. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent e ){
Resetsort (wareviewersort. Num );
// Resetsort is a self-implemented re-sorting function. You only need to reset the inaccessible viewersort
Tableviewer and refresh
}
});
Num. setalignment (SWT. center );
Num. setwidth (50 );
// Message is used in this place, which can be understood as long as you have made international calls.
Num. settext (messages. getstring ("helloview. warenum"); // $ NON-NLS-1 $

Final tablecolumn name = new tablecolumn (table, SWT. None );
Name. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent e ){
Resetsort (wareviewersort. Name); // same as above
}
});
Name. setwidth (80 );
Name. settext (messages. getstring ("wareview. warename"); // $ NON-NLS-1 $
Name. setalignment (SWT. center );

Final tablecolumn DESC = new tablecolumn (table, SWT. None );
Desc. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent e ){
Resetsort (wareviewersort. DESC );
}
});
Desc. setwidth (110 );
Desc. settext (messages. getstring ("wareview. waredesc"); // $ NON-NLS-1 $

Final tablecolumn price = new tablecolumn (table, SWT. None );
Price. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent e ){
Resetsort (wareviewersort. Price );
}
});
Price. setwidth (70 );
Price. settext (messages. getstring ("wareview. wareprice"); // $ NON-NLS-1 $
Price. setalignment (SWT. Right );

Final tablecolumn upddate = new tablecolumn (table, SWT. None );
Upddate. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent e ){
Resetsort (wareviewersort. upddate );
}
});
Upddate. setwidth (150 );
Upddate. settext (messages. getstring ("wareview. wareupddate"); // $ NON-NLS-1 $
Upddate. setalignment (SWT. center );
// Data changes in a tableviewer mainly depend on the following four sentences
Viewer. setcontentprovider (New warecontentprovider (); // displays the table
Viewer. setlabelprovider (New warelabelprovider (); // data provider of the table

Viewer. setinput (// real data source); // data source, such as arraylist
Viewer. setsorter (New wareviewersort (); // sort

The implementation of the two providers is similar to the following:
Class warecontentprovider implements istructuredcontentprovider {

Public object [] getelements (Object inputelement ){
If (inputelement instanceof node ){
Arraylist list = new arraylist ();
Makewarelist (node) inputelement), list );
Return list. toarray ();
}
If (inputelement instanceof list ){
Return (list) inputelement). toarray ();
}
Return NULL;
}

Public void dispose (){
// Todo auto-generated method stub

}

Public void inputchanged (viewer, object oldinput, object newinput ){
// Todo auto-generated method stub

}

}

Class warelabelprovider extends labelprovider implements
Itablelabelprovider {

Public Image getcolumnimage (object element, int columnindex ){
// Todo auto-generated method stub
Return NULL;
}

Public String getcolumntext (object element, int columnindex ){
If (element instanceof ware ){
Switch (columnindex ){
Case 0:
Return (ware) element). getdisplaynum ();
Case 1:
Return (ware) element). getdisplayname ();
Case 2:
Return (ware) element). getdisplaydesc ();
Case 3:
Return (ware) element). getdisplayprice ();
Case 4:
Return (ware) element). getdisplayupddate ();
Default:
Break;
}
}
Return NULL;
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.