Flylib.com

Books Software

 
 
 

6.3 Using Eclipse Plug-ins

     

6.3 Using Eclipse Plug-ins

Many Eclipse plug-ins are available, and many are free. You can find over 400 plug-ins at http://www.eclipse-plugins.2y.net/eclipse/, and the plug-in we'll use for Swing is available from that site. On the other hand, Eclipse plug-ins can change very frequently, so we can't base entire chapters on themthe plug-in you download would be different from the one written about in this book.

But we can look at what specific plug-ins have to offer in general terms, and we'll do that here. In this chapter, we'll take a look at the popular, free V4ALL plug-in to help us write some Swing code, available from http://www.eclipse-plugins.2y.net/eclipse/. Navigate to that page, click the plugins link, then the categories link, and then the UI link to find the Eclipse V4ALL Swing & SWT plug-in. Stop Eclipse if it's running and download that compressed plug-in file to Eclipse's eclipse directory (this is the directory that contains the workspace and plugins directories).

When you download plug-ins, you usually download them to the eclipse directory. They'll come zipped or tarred, and you usually uncompress them in the eclipse directory. When you uncompress them, the support files for plug-ins go automatically into the plugins and features directories, which are subdirectories of the eclipse directory.

Plug-ins are not always designed to be uncompressed in the eclipse directoryif possible, check the plug-in's documentation first. When you're about to unzip a plug-in, open it in an unzip utility and see if it'll unzip files to the plugins directory. If so, unzipping it in the eclipse directory is the right thing to do. Some plug-ins should be uncompressed in the plugins directory directly, however.


Unzip or untar the V4ALL plug-in now, which should automatically load its files into the plugins and features directories. That's all the installation you need. Now start Eclipse again. Eclipse may display a dialog indicating that there are pending configuration changes and may ask you if Eclipse should restart. Restart Eclipse if you're asked.

Congratulationsyou've just installed the V4ALL plug-in. It's time to put it to work.

     

6.4 Using the V4ALL Plug-in

The V4ALL plug-in gives you a palette of Swing components that you can drag to a "whiteboard" to design your application. To see how this works, we'll create a Swing application using this plug-in. Create a new Java project named Ch06_05 , and give it a source folder, src , in the third pane of the New Project dialog as we've done before. Now create a new class, Ch06_05 , in the org.eclipsebook.ch06 package.

6.4.1 Adding a V4ALL Editor to a Project

To add a new V4ALL editor to the project, right-click the org.eclipsebook.ch06 package and select New Other, opening the New dialog you see in Figure 6-8.

Figure 6-8. The New dialog
figs/ecps_0608.gif

You should see V4E Visual Editor as one of the options; select that item and click Next to open the V4E dialog you see in Figure 6-9.

Figure 6-9. The V4E dialog
figs/ecps_0609.gif

The V4E dialog lets you select the name of the V4ALL file that will contain the design information V4ALL will use. Keep the default name , Gui_1.v4all , and click Finish.

6.4.2 Designing the GUI

When you name your V4ALL file of design information and click Finish, the V4ALL perspective that you see in Figure 6-10 will open. You can see the new entry, Gui_1.v4all , in the Package Explorer at left. In the center, you see the V4ALL palette, from which you can drag Swing containers and components. You create your application in the whiteboard, which appears to the right of the palette. Under the palette is the properties view, which lets you set various properties of items in the whiteboard.

Our Swing example will display a button and a text field in a Swing frame, so find the Frame item in the Containers section of the palette and drag it to the whiteboard. You can see the results in Figure 6-10.

Figure 6-10. Using V4ALL
figs/ecps_0610.gif

Next, drag a button and a text field from the Components section of the palette to the Swing frame, as you see in Figure 6-11 (note that we've also resized the frame to fit better around these two controls).

Figure 6-11. Adding a button and a text field
figs/ecps_0611.gif

The default caption for the button is "Button 100"; to change that, select the button and click the Text property in the Properties view. Click the ellipsis (" . . . ") button that appears in the Text property line, enter "Click Me" in the Text Input dialog that appears, and click OK.

6.4.3 Creating a Method

In this application, our goal is to display a message in the text field when the user clicks the button, so our next step is to create a method that will handle the button click. To create a new method, drag a method item from the palette Beans & Factories section (the method item's icon is a pair of parentheses) to the whiteboard. Double-click the new method item and edit its name to read processEvent( ) , as you see in Figure 6-12.

Figure 6-12. Creating a new method
figs/ecps_0612.gif

To connect the Click Me button to this method, select the Event to Method item in the palette, then click the button. Next, move the mouse to the processEvent method item and click that item. This connects the button with the method visually using an arrow, as you see in Figure 6-12.

To indicate what event you want to handle, click Select at the top of the palette and click the arrow that extends from the button to the method. Then select the Action Performed item in the Events property in the Properties view. That's all it takes.

6.4.4 Generating and Editing the Code

We're ready to generate some code. To do that, right-click the whiteboard and select the Code Generation Generate Code for Swing context menu item (or click the left-most V4ALL button in the second toolbar beneath the menu bar, which you can see in Figure 6-12). This creates Gui_1.java , which you can see opened in Figure 6-13.

Figure 6-13. Working on V4ALL-generated code
figs/ecps_0613.gif

This code was generated for us by V4ALL. The processEvent method we've created will be called when the Click Me button is clicked. If you open the processEvent method, you'll see this generated code:

public void processEvent( ){

    // user code begin {1} Swing

    // user code end

    // user code begin {1} SWT

    // user code end

    // user code begin {1} HTML

    // user code end

    // user code begin {1} Eclipse

    // user code end

    // user code begin {1} C#

    // user code end

}

As you can see by looking at the code, the text field is referred to as ivjTextField100 , so we can use that object's setText method to display a message, "No worries!", like this (you can see this code entered in Figure 6-13):

public void processEvent( ){

    // user code begin {1} Swing

ivjTextField100.setText("No worries!");

// user code end

    // user code begin {1} SWT

    // user code end

    // user code begin {1} HTML

    // user code end

    // user code begin {1} Eclipse

    // user code end

    // user code begin {1} C#

    // user code end

}

6.4.5 Running the Result

That's all we need. Save the Gui_1.java file now, select it in the Package Explorer, and select the Run As Java Application menu item, launching the new Swing application, as you see in Figure 6-14. When you click the button, the text field shows the message, as we've designed it to do.

Figure 6-14. The new Swing application
figs/ecps_0614.gif

And that's iteverything works as planned. You've created and launched a Swing application using the V4ALL plug-in.

Our V4ALL example is complete, as is our look at applets, AWT, and Swing in this chapter. In the next chapter, we're going to start working with the new GUI alternative, the Standard Widget Toolkit, that comes with Eclipse.