JFACE Memo

xiaoxiao2021-03-06  41

1. The relationship between JFACE and SWT:

a) Description: JFACE is fully implemented based on SWT. He is mainly by encapsulating a bunch of SWT controls, thereby customizing some programmers will use often, and complicated controls such as Viewer, Wizard, etc.

b) Structure:

2. An example of a JFACE:

a) Description: Different from JFACE and SWT include programmers without manual startup message loops; do not need to create a shell instance.

b) code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test1 {

Public static void main (String [] args) {

ApplicationWindow Win = New ApplicationWindow (NULL);

Win.SetBlockonopen (True);

Win.Open ();

Display.getcurrent (). Dispose ();

}

}

c) Runchart:

3. Customization of the JFACE window

a) Description: Since the JFACE is only packaged for SWT, in order to customize the JFACE ApplicationWindow, we need to get the shell reference in the JFACE. But there is a problem here. When we need to customize the control tree, we must display before the application window, that is, the ApplicationWindow's open () operation is called before, and on the other hand, ApplicationWindow only after the Open () operation is called. Create his shell instance, so this causes us to customize the JFACE window by getting the shell instance.

4. Customize the JFACE window by expanding

a) Description: By subclassification for ApplicationWindow, we can achieve customization of the JFACE window. During the customization process, what we have to do is to implement the callback functions used to create controls.

b) Structure:

c) code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test2 extends ApplicationWindow {

Public test2 () {

Super (NULL);

}

Public Control CreateContents (Composite Parent) {

Parent.getshell (). setText ("test2");

Button B = New Button (PARENT, SWT.PUSH);

B.Settext ("Hello World");

Return B;

}

...

}

d) Runchart:

5. TreeViewer

a) Description: Viewer is one of the most important controls in JFAce. He browses the contents specified by the programmer in the way of tree chart. Viewer is an MVC mode-based control, where TreeViewer is part of the Viewer, which is responsible for displaying the content, we can perform specified for the content you need to display by the setInput () method, but TreeViewer does not process the content you need to display, it Delegate this task to ContentProvider, which is traversed and tracked by him. If there is no labelProvider, TreeViewer will call the toString () method of displaying the content, and make the returned value as the name of the content, but because such readability is not high, we provide a labelProvider, this class The role is to provide a label available for the content displayed. b) Structure:

c) code:

Main program:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test4 extends ApplicationWindow {

...

Public Control CreateContents (Composite Parent) {

TreeViewer TV = New TreeViewer (PARENT);

TV.SetContentProvider (New Test3ContentProvider ());

TV.SetLabelProvider (New Test4LabelProvider ());

TV.setInput (New file ("D: /"));

Return NULL;

}

...

}

ContentProvider:

Public Class Test3ContentProvider Implements ItreeContentProvider {

Public Object [] getChildren (Object element) {

File Parent = (file) ELEMENT;

Object [] children = parent.listfiles ();

if (Children == Null)

Return New Object [0];

Else

Return children;

}

Public Object getParent (Object Child) {

Return (File) .GetParent ();

}

Public Boolean Haschildren (Object Element) {

Object [] children = getChildren (element);

Return Children.Length == 0? false: True;

}

Public object [] getElements (Object element) {

Return getChildren (Element);

}

Public void dispose () {}

Public void InputChanged (Viewer V, Object O1, Object O2) {}

}

LabelProvider:

Public class test4LabelProvider Extends labelprovider {

Public String getText (Object Element) {

Return (File) .GetName ();

}

}

d) Runchart: 6. TableViewer

a) Description: TableViewer is similar to TreeViewer, and their difference is only the way the display is different. In addition, for the TableViewer itself, we need to add some additional custom operations. We need to explicitly add the displayed columns for TableViewer. For ContentProvider, because the form is not necessary to track, in comparison, he is more than ContentProvider in TreeViewer is simple. For LabelProvider, we need to take the index value of the column to take into account the index value of the column. It is necessary to pay special attention to it. Since TableViewer does not track explicit content, in order to update his internal display, we use an external, event handling method, constantly modify the content displayed in the TableViewer.

b) Structure:

c) code:

Main program:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test5 extends ApplicationWindow {

Private Treeviewer TV = NULL;

Private TableViewer Tablev = NULL;

...

Public Control CreateContents (Composite Parent) {

...

Tablev = New TableViewer (FORM);

TableColumn TC = New TableColumn (Tablev.getTable (), SWT.NONE);

Tc.Settext ("Name");

Tc.SetWidth (100);

Tablev.getTable (). setHeadervisible (TRUE);

Tablev.SetContentProvider (New Test5ContentProvider ());

Tablev.setLabelProvider (New Test5TableLabelProvider ());

TV.AddselectionChangedListener (New ISELECTIONCHANGEDLISTENER () {

Public void Selectionchanged (SelectionChangeDevent E) {

IstructureDSelection s = (istructureDselection) E.GetSelection ();

Tablev.setInput (s.getfirstlement ());

}

});

Return NULL;

}

...

}

ContentProvider:

Public class test5contentProvider imports istructuredContentProvider {

Public object [] getElements (Object element) {

File Parent = (file) ELEMENT;

Object [] children = parent.listfiles ();

if (Children == Null)

Return New Object [0];

Else

Return children;

}

Public void dispose () {}

Public void InputChanged (Viewer V, Object O1, Object O2) {}}

LabelProvider:

Public class test5tablelabelprovider imports itablelabelprovider {

Public void AddListener (IlabelProviderListener L) {}

Public void dispose () {}

Public Boolean IslabelProperty (Object O, String S) {

Return False;

}

Public Void Removelistener (IlabelProviderListener L) {}

Public Image getColumnImnImage (Object O, INT I) {

Return NULL;

}

Public String getColumnText (Object O, INT I) {

Return ((file) o) .getname ();

}

}

d) Runchart:

7. Sort and filter

a) Description: While using Viewer, we can sort and filter the content you need to display. The viewersRersorter are responsible for sorting, he has two methods category () and compare (), where category () is responsible for classification, the greater the weight after classification, the more it is displayed, and COMPAR is called after the classification is completed. Method, sorting within the category. Viewerfilter is responsible for filtering, he has a select () method.

b) code:

Main program:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test7 extends ApplicationWindow {

Private Treeviewer TV = NULL;

Private TableViewer Tablev = NULL;

...

Public Control CreateContents (Composite Parent) {

...

TV.addfilter (New Test7filter ());

...

Tablev.setsORTER (New Test 7SORTER ());

...

}

...

}

Viewersorter:

Public class test7sorter extends viewersRer {

Public int Category (Object O) {

File f = (file) O;

RETURN F.ISDIRECTORY ()? 0: 1;

}

Public Int Compare (Viewer V, Object O1, Object O2) {

Return Super.comPare (V, O1, O2);

}

}

Viewerfilter:

Public class test7filter extends viewerfilter {

Public Boolean SELECT (Viewer V, Object Parent, Object E) {

Return (File) .Indirectory ();

}

}

c) Runchart:

8. Picture shows:

a) Description: In SWT, the picture is a heavyweight control, so we need to make explicit release after use. For better management of image resources, iMageRegistry and ImageDescriptor are provided for managing and references images in JFACE. Where imageRegistry is responsible for maintaining the index of the ImageDescriptor instance, ImageDescriptor is responsible for the management of the underlying image resource. b) Structure:

c) code:

Image management:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test6UTIL {

...

Public static imageRegistry GetImageRegistry () {

IF (REG == NULL) {

REG = new imageRegistry ();

Reg.PUT ("Folder", ImageDescriptor

.createFromurl (NewURL ("File: Folder.gif")))))))))

Reg.put ("File", ImageDescriptor

.createFromurl (NewURL ("File: File.gif")))

}

RETURN REG;

}

}

Picture use:

Public class test6tablelabelprovider imports itablelabelprovider {

...

Public Image getColumnImnImage (Object O, INT I) {

IF (i == 0) {

File f = (file) O;

IF (f.IsDirectory ())

Return Test6UTIL.GetImageRegistry (). Get ("Folder");

Else

Return Test6UTIL.GetImageRegistry (). get ("file");

Else

Return NULL;

}

...

}

d) Runchart:

9. Add menu, status bar, toolbar, etc.

a) Description: When adding controls such as a menu and status bar, two steps are mainly needed, one is to call the corresponding notification functions in the constructor, and the other is to implement functions for increasing these controls.

b) code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test8 extends ApplicationWindow {

Private Treeviewer TV = NULL;

Private TableViewer Tablev = NULL;

PRIVATE TEST8OpenAction aopen = NULL;

Private test8editaction aedit = null;

PRIVATE TEST8ABOATION AABOUT = NULL;

Public test8 (shell s) {

Super (s);

AOpen = New Test8openAction (this);

AEDIT = New Test8Editction (this);

Aabout = New Test8aboutAction (this);

AddStatusline (); // Increase status bar

AddMenuBar (); // Add menu bar

AddToolbar (SWT.WRAP | SWT.FLAT); // Add Toolbar

}

Public Control CreateContents (Composite Parent) {

...

/ **

* Create a context menu for TableViewer

* /

Menumanager mm = new menuManager ();

Tablev.getTable (). setmenu (mm.createcontextmenu (tablev.gettable ()))

mm.add (aopen);

mm.add (AEDIT);

mm.add (aabout);

Return NULL;

}

Public menuManager createmenumanager () {

Menumanager mm = new menuManager ();

Menumanager mmfile = new menuManager ("& file");

Menumanager mmedit = new menuManager ("& edit");

Menumanager mmabout = new menuManager ("& about");

MMFile.Add (AOpen);

MMEDIT.ADD (AEDIT);

Mmabout.Add (Aabout);

mm.add (mmfile);

mm.add (mmedit);

mm.add (mmabout);

Return mm;

}

Public ToolbarManager CreateToolbarManager (int style) {

Toolbarmanager TBM = New ToolbarManager (style);

TBM.Add (AOpen);

TBM.Add (AEDIT);

TBM.Add (Aabout);

Return TBM;

}

...

}

Public class test8openaction extends action {

PRIVATE TEST8 T = NULL;

Public Test8openAction (TEST8 T) {

THIS.T = T;

SetText ("& Open @ Ctrl O");

}

Public void run () {

System.out.println ("HEHE Open Action");

}

}

c) Runchart:

10. Wizard

a) Description: JFACE provides classes for generating wizards. Wizard and WizardPage. When we need to display a wizard, we need to create a wizard dialog for him. For Wizard we need to implement two methods, addpages (), used to add a wizard page, performfinish (), is called when the "Finish" button in the wizard is pressed. For WizardPage, we can build a UI control within the wizard by createControl () method. In addition, the setErrorMessage () method provided by WizardPage can be used to display the error information among the inputs, and the setPageComplete () method is used to indicate whether the input of the current page has ended, whether you need to jump to the rear page.

b) code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test9 extends ApplicationWindow {

Private shell s = NULL;

...

Public Control CreateContents (Composite Parent) {Button B = New Button (PARENT, SWT.PUSH);

B.Settext ("open");

B.AddSelectionListener (New SelectionAdapter () {

Public void widgetselected (SelectionEvent E) {

Wizard w = new test9wizard ();

WizardDialog dia = new WizardDialog (NULL, W);

Dia.open ();

}

});

Return NULL;

}

...

}

Wizard:

Public class test9wizard extends wizard {

TEST9PAGE1 P1 = NULL;

TEST9PAGE2 P2 = NULL;

Public Test9wizard () {

Super ();

SetNeedsProgressMonitor (TRUE);

}

Public void addpages () {

P1 = New Test9Page1 ("Info");

P2 = New Test9Page2 ("Other");

AddPage (P1);

AddPage (P2);

}

Public boolean performfinish () {

String name = p1.getname ();

String agn = p1.getage ();

String phone = p2.getphone ();

String address = p2.getaddress ();

Return True;

}

}

Wizard page:

PUBLIC CLASS TEST9PAGE1 EXTENDS WIZARDPAGE {

Private string name = NULL;

PRIVATE STRING AGE = NULL;

PRIVATE TEXT T1 = NULL;

Private text t2 = NULL;

Public Test9Page1 (String Name) {

Super (Name);

SetTitle (name);

SetMessage ("Input Your Info Detail");

Try {

SetimageDescriptor (imageDescriptor.createFromurl (New Url ("File: File.gif"))))))

} catch (exception e) {

E.PrintStackTrace ();

}

}

Public void createControl (Composite Parent) {

Group g = new group (PARENT, SWT.BORDER);

...

T1.AddModifyListener (new modifylistener () {

Public void modifytext (ModifyEvent E) {

Validate ();

}

});

...

SetControl (g);

SetPageComplete (False);

}

Private void validate () {

Name = t1.gettext (). Trim ();

AGE = T2.Gettext (). Trim ();

IF (Name.Equals (")) {

SeterrorMessage ("Name Is Null");

SetPageComplete (False);

} else if ("")) {SeterrorMessage ("age is null";

SetPageComplete (False);

} else {

SETERRORMESSAGE (NULL);

SetPageComplete (TRUE);

}

}

Public string getage () {

Return Age;

}

Public string getname () {

Return Name;

}

}

c) Runchart:

11. References:

Http://www-900.ibm.com/developerWorks/cn/linux/opensource/os-ecgui1/index.shtml

http://www-900.ibm.com/developerWorks/cn/linux/opensource/os-ecgui2/index.shtml

Http://www-900.ibm.com/developerWorks/cn/linux/opensource/os-ecgui3/index.shtml

转载请注明原文地址:https://www.9cbs.com/read-54738.html

New Post(0)