JFACE Development Wizard

xiaoxiao2021-03-06  71

JFACE Development Wizard

English original

content:

Address Book Application Environmental Demand Installation and Build Address Book Application Using JFACE Wizard Implementation JFACE WizardPage Running Address Book Application Conclusion Reference About the author to this paper

related information:

with the Eclipse Platform Eclipse plug-in development using the Eclipse platform using C Eclipse GUIWorking the Eclipse PlatformGetting started outside the Eclipse Workbench / C development for XML development Subscribe to the developerWorks newsletter developerWorks Toolbox subscription with Eclipse

In the Linux area:

Tutorial Tools & Product Codes & Component Articles

Use a JFACE toolbox

Level: Intermediate Jeff Gunther (Jeff.Gunther@intalgent.com) General Manager, Intalgent Technologies 2003 September

Whether you use a language to develop a GUI application, collecting the interactive mechanism to collect users is indispensable. The JFACE toolbox belonging to the development of the Eclipse project provides a set of powerful user interface components that can easily use these components in a stand-alone application. One of the more interesting components in the JFACE toolbox is support for the wizard. Other user interface components in the JFACE Widget Toolkit, SWT is combined to provide a flexible mechanism to systematically collect user input and execute data verification.

In the past, use Java to develop a highly interactive client application, so that these applications look like this appliance and can operate like this machine application, such an idea is as difficult as the holy grail. . With the emergence of the Eclipse project, developers began to create Java applications that simulated the original native application. Although the Eclipse project is considered to be a Java open source integration development environment (IDE), the project is much wide. In fact, Eclipse is described as "... a universal tool platform" ... A universal tool platform - is an open scalable IDE, which can do anything, not particularly for an aspect. "In Eclipse During the platform development, the project produces two user interface toolboxes: SWT and JFACE. SWT provides an API-independent API that is closely integrated with the operating system's native window environment. The toolbox enables developers to face the trade-offs in many design and implementation when using Java's abstract window toolbox (AWT) or Java Foundation Classes, JFC). The JFACE toolbox is an API-independent API that can interoperate with SWT. The library provides a set of components and assistant utilities, which make many of the common tasks in the development SWT user interface to simplify. Although Eclipse is used when developing the sample code herein, the article itself demonstrates how to use the JFACE wizard in the context of a stand-alone application. For more information or background knowledge about the Eclipse project, see the references later in this article. The address book application allows us to view the use and structure of this sample application before viewing the code and implementation details of the JFACE toolbox. The application included in this article is an address book in a simple memory that allows the user to add a personal address book contact. The app contains a JFACE wizard consisting of two pages, each page contains a domain that describes a specific contact. Users can use the table shown in Figure 1 to browse the contact list. Figure 1. Address Book Application running on Red Hat Linux 8 As shown in Figure 2, New Contact wizards a new contact for users. Figure 2. Note The NEW CONTACT wizard on Microsoft Windows XP illustrates that data verification is like the Eclipse project, and the SWT and JFACE libraries are designed to support a wide variety of operating systems and environments. In order to demonstrate the cross-platform capabilities of these libraries, the included samples have been packaged in Microsoft Windows and Linux. Environmental requirements You can find and download the code used in this article in the Reference Information section. To test the address book application, your environment must meet the following minimum needs: Microsoft Windows XP or Red Hat Linux 8 Operating System Java 2 SDK, Standard Edition 1.4 or higher Apache Ant 1.5.2 or higher Although it is already very careful The Ant build script can be used across platforms, but still test and verify the personal address book application only on Microsoft Windows XP and Red Hat Linux 8 operating system. Let's start with the installation and set sample code. Installing and Building an Address Book Application To install and build an address book application, complete the following steps:

Download the source code package. See the links in the reference. Unzip the AddressBook.zip file into the temporary directory. Run the following command in the newly created directory: Ant clean runs the following command in the newly created directory: Ant If your environment meets the needs and configures correct, then you should see the following: Listing 1 Build address book application buildfile: build.xml

INIT:

[MKDIR] CREATED DIR: D: / Address Book / Dist

Compile-Common:

Compile-module:

[echo] compling ...

[MKDIR] Created Dir: D: / Address Book / Build

[MKDIR] Created Dir: D: / Address Book / Build / Classes

[Javac] Compiling 8 Source Files to D: / Address Book / Build / Classes

Package-Common:

[jar] Building Jar: D: / address book / dist / addressbook.jar

DEFAULT:

Build Successful

Using the JFACE Wizard Since we have already introduced the basics of SWT and JFACE and the behavior of the address book application, let us study how to use the JFACE wizard. Since the purpose of this article is to introduce the JFACE wizard, there is no more background information about how to create a separate application. For more information on how to use SWT / JFACE components outside of the Eclipse Workbench, see the articles of Articles composed of ADRIAN VAN EMMENIS (see Resources for Link). In the address book application, the NewContactAction class handles the user from the File menu (see Figure 3). Figure 3. The code in the New Contact menu item Listing 2 describes the objects and methods required to open a wizard to the user. Listing 2. NewContactAction class

1 Public Class NewContactAction Extends Action

2 {

3 ApplicationWindow window;

4

5 publicnewcontactivity (ApplicationWindow W)

6 {

7 window = W;

8 this.Settext ("New Contact");

9 this.SetTooltiptext ("CREATE New Contact");

10}

11

12 public void run ()

13 {

14 ContactWizard Wizard = New ContactWizard ();

15

16 WizardDialog Dialog = New WizardDialog (Window.getshell (), Wizard

17 dialog.open ();

18}

19}

Let us gradually study this code:

Regardless of when the user selects New Contact from the menu bar, the RUN method in line 12 is executed. As shown in the 14th line, a new instance of the ContactWizard class is required before the GUI wizard is displayed. The various pages of this class setting wizard allow users to provide input to each domain. We will introduce the ContactWizard class later later. The two variables are instantiated for the WizardDialog class: The first variable contains references to the shell, and the second variable contains references to the ContactWizard created in line 14. The WizardDialog class is responsible for displaying the actual wizard and organizing the expression of each page. As shown in FIG. 4, step 17 opens the wizard dialog. Figure 4. Contact Wizard Running on Microsoft Windows XP Implementing the JFACE Wizard To create a ContactWizard class in the address book application, we need to create a subclass of the inheritance wizard basic implementation and overwrite the following methods: public void addpages () - This method provides A hook to add other pages to the wizard before the final user display wizard. To add a new page to the wizard, you can call the AddPage method as shown in Listing 3. Two pages are defined in the ContactWizard class. We will study how to create a new page in the next section. Listing 3. Addpages method in the ContactWizard class

Public void addpages ()

{

Page1 = New BasicContactPage (Selection);

Page2 = New AddressContactPage (Selection);

Addpage (Page1);

Addpage (Page2);

}

Public Boolean PerformFinish () - Perform this method when the user clicks on the finish button. In ContactWizard, use this method to collect data in each page of each page of the wizard and populate them into a domain object. See Listing 4. Listing 4. PerformFiniSH method in the ContactWizard class

Public boolean performfinish ()

{

Contact Contact = new contact ();

Contact.setFamilyName (page1.getfamilyName ());

Contact.setgivenname ());

Contact.setbusinessphone (page1.getbusinessphone ());

Contact.sethomephone (page1.getHomePhone ());

Contact.semailAddress (page1.getemail ());

Addressbook.addcontact (Contact);

Return True;

} Listing 5 shows a complete class with the AddPages and PerformFinish methods. Listing 5. ContactWizard class

...

Public Class ContactWizard Extends Wizard

{

Private BasiccontactPage Page1;

Private addressContactPage Page2;

PRIVATE ISELECTION SELECTION;

Public contactWizard ()

{

Super ();

SetNeedsProgressMonitor (TRUE);

}

Public void addpages ()

{

Page1 = New BasicContactPage (Selection);

Page2 = new addresscontactpage (selection); addpage (page1);

Addpage (Page2);

}

Public boolean performfinish ()

{

Contact Contact = new contact ();

Contact.setFamilyName (page1.getfamilyName ());

Contact.setgivenname ());

Contact.setbusinessphone (page1.getbusinessphone ());

Contact.sethomephone (page1.getHomePhone ());

Contact.semailAddress (page1.getemail ());

Addressbook.addcontact (Contact);

Return True;

}

}

Realizing the class of JFACE WIZARDPAGE does not implement WizardPage class, ContactWizard will not have any behavior. You can view the wizard as a pile of cards, each card has its own layout and design. Each WizardPage is responsible for the layout and behavior of a single page (ie card) in the wizard. To create WizardPage, we need to create subclasses that inherit the basic implementation of WizardPage and implement the CreateControl method to create a specific GUI control for the wizard page. When developing WizardPage, you need to complete the following:

Create a combination using the specified parent. Create a layout of widgets. For the BasicContactPage class, use GridLayout. The layout created in step 2 is constructed and arranged in widgets. For a more detailed documentation for the SWT widget, see the links in the reference. Listing 6 demonstrates the code required to construct and arrange the BasicContactPage in the ContactWizard class. See the screen of the wizard page in Figure 1. Listing 6. CreateControl method in the BasicContactPage class

...

Public void createControl (Composite Parent)

{

CompositeContainer = New Composite (PARENT, SWT.NULL);

GridLayoutLayout = new gridlayout ();

Container.setLayout (layout);

Layout.numcolumns = 2;

Layout.verticalspacing = 9;

Label label = new label (container, swt.null);

Label.setText ("& Given Name:");

Givennametext = new text (container, swt.border | swt.single);

Griddata gd = new griddata (griddata.fill_horizontal);

Givennametext.setlayOutdata (GD);

Givennametext.addmodifyListener (New ModifyListener ()

{

Public void modifytext (ModifyEvent E)

{

Dialogchanged ();

}

});

Label = NewLabel (Container, SWT.NULL);

Label.setText ("& Family Name:");

FamilyNameText = new text (container, swt.border | swt.single); gd = newgriddata (griddata.fill_horizontal);

FamilyNameText.setLayOutdata (GD);

FamilyNameText.addModifyListener (New ModifyListener ()

{

Public void modifytext (ModifyEvent E)

{

Dialogchanged ();

}

});

Label = NewLabel (Container, SWT.NULL);

Label.Settext ("& Nickname:");

NicknameText = New text (container, swt.border | swt.single);

GD = newgriddata (griddata.fill_horizontal);

NicknameText.setLayOutdata (GD);

Createline (Container, Layout.NumColumn);

Label = NewLabel (Container, SWT.NULL);

Label.Settext ("& Business Phone:");

BusinessphoneText = New Text (container, swt.border | swt.single);

GD = newgriddata (griddata.fill_horizontal);

BusinessphoneText.setLayOutdata (GD);

Label = New label (Container, SWT.NULL);

Label.Settext ("& HOME Phone:");

HomephONETEXT = New Text (Container, SWT.BORDER | SWT.SINGLE);

GD = newgriddata (griddata.fill_horizontal);

HomephoneText.setLayOutdata (GD);

Createline (Container, Layout.NumColumn);

Label = NewLabel (Container, SWT.NULL);

Label.Settext ("& E-mail address:");

EmailText = New Text (container, swt.border | swt.single);

GD = newgriddata (griddata.fill_horizontal);

EmailText.setLayOutdata (GD);

EmailText.AddmodifyListener (NewModifyListener ()

{

Public void modifytext (ModifyEvent E)

{

Dialogchanged ();

}

});

// Dialogchanged ();

SetControl (Container);

}

...

Create a listener for any window widget that may require input verification or conversion. For the BasicContactPage class, several ModifyListeners created a specific data domain performed input verification. Whenever you modify the text in a text domain, perform the DialogChanged method. This method is responsible for handling errors and reports the error report to the wizard. Listing 7 demonstrates processing input verification and the code required for any error notification wizard. Listing 7. DialogChanged and UpdateStatus methods for processing input verification classes

...

Private void Dialogchanged () {

IF (this.getgivenname (). Length () == 0)

{

"" "Given Name Must Be Specified.");

Return;

}

IF (this.getfamilyName (). Length () == 0)

{

UpdateStatus ("Family Name Must Be Specified.");

Return;

}

IF (this.getemail (). Length ()> 0)

{

IF (this.getemail (). Indexof ("@") <0)

{

UpdateStatus ("Enter Your Email Address as YourName@yourDomain.com");

Return;

}

}

Updatestatus (null);

}

Private Void Updatestatus (String Message)

{

SETERRORMESSAGE (Message);

SetPageComplete (Message == Null);

...

Run the Address Book Application To test and run the address book application, complete the following steps:

Run the following command in the directory of unpaolded source code: Ant Run will appear after the ANT script will appear. Select New Contact from the FILE menu to launch the JFACE Contact wizard. See Figure 5. Figure 5. Two pages of the Contact Wizard on Microsoft Windows XP end language By introducing the SWT / JFACE user interface toolbox, the Eclipse project finally provides Java developers with a method of creating a highly interactive client application. Using components such as JFACE wizards to study how to create a flexible mechanism to collect user inputs using the least code, it is really exciting. Sample Address Book Application provides a build and packaging framework, you can use this framework to create your own independent SWT / JFACE application. Reference

Download the source code in this article. Download Java 2 SDK from Sun Microsystems, Standard Edition 1.4.1. Download ANT 1.5.2 from the Apache Software Foundation (Apache Software Foundation). The Eclipse project provides the JFACE and SWT toolboxes used in this source code. The Eclipse Technical Overview describes the core components of the Eclipse platform. Find more information about using JFACE and SWT components outside the Eclipse table in the articles of the Eclipse Workbed: Adrian Van Emmenis.

Part 1: Separately use JFACE and SWT (United States, 2003) Part 2: Using JFACE Image Registry (DeveloperWorks, 2003 in February 2003) Part 3: Add Operation, Menu and Toolbar (DeveloperWorks, 2003 March) If you interested in creating a JFACE wizard in the Eclipse plugin, please read "Creating JFace Wizards" on the Eclipse.org website. Please find more information about multiple SWT widgets in Org.eclipse.swt.Widgets Javadoc. Find more information about the SWT event type in Org.eclipse.swt.widgets Javadoc. Find more references to Eclipse users on developerWorks. About the author Jeff Gunther is the general manager and founder of Intalgent Technologies, which is an emerging software product and solution vendor, its products and solutions use Java 2 Enterprise Edition and Lotus Notes / Domino platform. Jeff from the early "Mosaic" (the earliest Internet web browser) from the times to the Internet industry. He has professional experience in all aspects of software life cycle, including special software development expertise in Java / J2EE, DHTML, XML / XSLT, database design, and handheld equipment. You can contact him through jeff.gunther@intalgent.com.

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

New Post(0)