Eclipse rcp+spring Build a FAT client Web program

Source: Internet
Author: User
Tags format copy implement string window root directory tomcat log4j
Web| Program | client

   SummaryEclipse's rich Client platform (RCP) is rapidly becoming a framework choice for building fat client applications. This article will give you a detailed description of how to use Eclipse RCP for spring web development.

   I. Introduction

While Web 2.0 and Rich Internet Applications (RIA) are now extremely popular, building a rich web front end when you really need the FAT client functionality may not really meet your requirements.

But if you really want to avoid the so-called RIA Mania and choose a real fat client solution, what do you do? The answer is: You can choose a rich client platform (RCP) to handle most of the work for you. In essence, this RCP concept provides a new framework for the Java Desktop Application world.

An RCP provides a framework/shell for an application, as well as a set of module based APIs that you can build your own application based on this shell. This RCP is responsible for all the heavy tasks, such as adding menus, toolbars, different views, and so on, and you don't have to repeat the work.

This article will guide you through the detailed construction of a rich client interface to connect to the server built in the previous article. You will build a FAT client based on Eclipse's rich client platform and integrate Eclipse RCP with spring.

"Ready to Work"

· Eclipse 3.1.2

· MyEclipse 4.1.1

· Java SE 5

• A servlet container or Java EE server (this article uses Tomcat 5.5+)

· Spring 1.2+

   two. Why use Eclipse RCP?

Now that more and more applications are being developed based on Eclipse RCP (and, of course, the eclipse's thick development background), we can safely assume that this framework has been tested more extensively than any other framework.

Below, let's start.

(i) Creation of a new Eclipse plug-in project

Follow these steps to create a new Eclipse plug-in project for your rich client application:

1. Create a new plug-in project in Eclipse and name the project as Eclipsetradeclient. Position the application target of the plugin in Eclipse version 3.1 and make sure to click "Create an OSGi bundle manifest" (see Figure 1) and click Next.


Figure 1. " The New Plug-in Project dialog box creates an additional plug-in engineering eclipsetradeclient in Eclipse.

2. On the Plug-in Content screen, keep the default settings, but make sure Yes is selected-Create a rich client application (see Figure 2) and click Next.


Figure 2. Choose to create a rich client application on the Plug-in Content screen.


• As for the template, select "RCP application with a view" and click Next.

• Fill out the RCP application properties shown in Figure 3 and click Finish. After that, you will be prompted to go to the "Plug-in Development" view and click Yes.


Figure 3. RCP plug-in Engineering Wizard final Results screen

• Now, you have created your project and then opened the Plugin.xml. You will see the screenshot shown in Figure 4 below.


Figure 4. Plugin.xml Overview

If you are new to Eclipse plug-in development, you may often need to use the Plugin.xml tab at the bottom. As you can see from the Overview tab, you will be able to run/debug your Eclipse-rich client application.

• Expand the Eclipsetradeclient/src/eclipsetradeclient package to observe the classes created for you by the Eclipse's RCP wizard. In the Eclipse Editor, click the "All Extensions" tab and expand each top-level node, as shown in Figure 5.


Figure 5. Eclipse-Generated classes and all extensions

Notice the extension entry for your application class, perspective class, and view class. Since Eclipse's rich client platform includes plugin.xml files, you can simply add new components-add them through the "Add ..." button in the "Extensions" tab.

(ii) refactoring the default view class

As you can see, the Eclipse Wizard has created a class called View for you. It's not very useful, is it? Use the following procedure to refactor the default view class:

1. Let's rename it-right-click the View.java in Package Explorer. Go to Refactor->rename, enter the new name Explorerview and click Preview. On the panel that pops up later, you'll see that the perspective class is refactored-using explorerview.id instead of view.id (see Figure 6). Click OK.


Figure 6. Refactoring the view class for Explorerview

2. Unfortunately, Eclipse has a somewhat weak refactoring capability-particularly compared to IntelliJ. For refactoring like this, IntelliJ will not only change the class as expected, but it will apply refactoring to your. xml file! This is a very useful feature, especially in the case of a spring/hibernate/xml configuration with a large proportion of operations.

You must manually update the refactoring of the Plugin.xml. Open the Plugin.xml and click the Plugin.xml tab. Locate the extension that corresponds to the view, and make the following update:

Name= "Explorerview"
Class= "Eclipsetradeclient.explorerview"
Id= "Eclipsetradeclient.explorerview" >
After that, save it (see Figure 7).


Figure 7. Further refactoring-manual update Plugin.xml


3. For this simple refactoring, this is the case, right? Yes, but unfortunately, you're not done yet. Open the class Explorerview, change the static variable id-initialize it to Eclipsetradeclient.explorerview. This corresponds to the ID you just set in Plugin.xml.

4. Finally, you complete the refactoring. Now, let's test if everything changes properly. Switch back to the Plugin.xml in the editor and click the "Overview" tab. Click "Launch an Eclipse application", which should result in the results shown in Figure 8.


Figure 8. Start Eclipse Trade client program


5. Now let's change the name of the node. Open the class Explorerview. Locate the inner class Viewcontentprovider and change the method "Object GetElements" to return an array of strings ({"Watch List", "Order History"}).

   three. Add spring remoting to your application

Next, we add spring to your eclipse-rich client so that it can make a request for the Stocktradeserver project in the forward article.

First, when developing the Eclipse plug-in/RCP application, the recommended way to add a third-party library is through another plug-in. After doing so, you will not need to add these third-party jars to every project you create. Instead, you only build a dependency between your plug-in/RCP project and the Third-party Library project. First, let's assume you're familiar with Eclipse's ClassLoader. The point is that you have to take some extra steps to make sure that your class can find each other between plug-in dependencies:

1. Create a new plug-in project in Eclipse. and named this new project for Springclient. Then, set the following values:

ID = Springclient
Class = Springclient.springclientplugin
After populating the Plug-in property, click Finish. For the Springclient plug-in project, you don't need any templates because the main purpose is to store the spring library and any spring-related service classes.

2. In your unpacked spring-framework-1.2.8 release package, you will find the following jar files:

spring-aop.jar-under Folder Dist

spring-beans.jar-under Folder Dist

spring-context.jar-under Folder Dist

spring-core.jar-under Folder Dist

spring-remoting.jar-under Folder Dist

commons-logging.jar-under Folder lib\jakarta-commons

log4j-1.2.13.jar-under Folder lib\log4j

Then, copy all of them to your springclient root directory.

3. In Eclipse's Package Explorer, right-click springclient to open the engineering properties. Select Java Build Path, click on the "Libraries" tab, and add all those jar files that you just added through the button "add JARs". Make sure you also import these libraries! Click on the "Order and Export" tab and check all libraries (see Figure 9). By doing this, you can ensure that spring and log jars are also available when you create a working dependency on springclient. After that, click OK.


Fig. 9. Output Third Party library


4. Now, you want to modify the springclient manifest. In Package Explorer, expand Springclient->meta-inf and open MANIFEST.MF. Click the "Runtime" tab and click "Add" in the Classpath section. Select all spring jars and logging jars and click OK. Now, in the "Exported Packages" section, click Add. Select all packages for export and click OK (see Figure 10).


Figure 10. Add a Third-party library to the plug-in classpath and export the package


As I've mentioned before, Eclipse's classloader often cause problems. To remedy this, you can click the Manifest.mf tab and add the following line:

Eclipse-buddypolicy:registered

5. Now, let's add the spring configuration file. In Package Explorer, go to the SRC directory, create a new file Applicationcontext.xml, and add the following:

In the SRC directory, create another new file Beanreffactory.xml and add the following:

Needless to be surprised, these profiles are the same as the spring configuration file you used for unit testing of the Stocktradeserver project, except that you renamed Applicationcontext.xml.

6. To simplify the problem, you can copy the class from the Stocktradeserver project to the springclient src directory. Under Springclient's SRC directory, create packages Stephenlum.services.stock and Stephenlum.services.stock.dto.

If you are not ready, you can download the source code of this article or refer to my previous article and copy the Stephenlum.services.stock class Stockservice.java. Then, copy the class Stockdto.java under Stephenlum.services.stock.dto (see Figure 11).


Figure 11. Package Explorer After completing the above operation looks like


7. Add code to class Springclientplugin to load spring's application context. Notice that you want to add the code to the constructor. Here is the new Springclientplugin class:

Package springclient;
Import Org.eclipse.jface.resource.ImageDescriptor;
Import Org.eclipse.ui.plugin.AbstractUIPlugin;
Import Org.osgi.framework.BundleContext;
Import Org.springframework.beans.factory.BeanFactory;
Import Org.springframework.beans.factory.access.BeanFactoryLocator;
Import org.springframework.beans.factory.access.BeanFactoryReference;
Import Org.springframework.beans.factory.access.SingletonBeanFactoryLocator;
/**
* The main plug-in class that is applied to the desktop.
*/
public class Springclientplugin extends Abstractuiplugin {
Private Beanfactory beanfactory;
Share an instance.
private static Springclientplugin plugin;
/**
* Builder.
*/
Public Springclientplugin () {
plugin = this;
Beanfactorylocator beanfactorylocator = Singletonbeanfactorylocator.getinstance ();
Beanfactoryreference beanfactoryreference = beanfactorylocator.usebeanfactory ("CTX");
Beanfactory = Beanfactoryreference.getfactory ();
}
/**
* Call this method when the plug-in is activated
*/
public void Start (Bundlecontext context) throws Exception {
Super.start (context);
}
/**
* When the plugin is stopped, this method is called
*/
public void Stop (Bundlecontext context) throws Exception {
Super.stop (context);
plugin = null;
}
/**
* Returns a shared instance.
*/
public static Springclientplugin Getdefault () {
return plugin;
}
/**
* Returns an image descriptor for the image file under the given plugin relative path
* @param path-Path
* @ return Image descriptor
*/
public static Imagedescriptor Getimagedescriptor (String path) {
Return Abstractuiplugin.imagedescriptorfromplugin ("Springclient", Path);
}
Public Beanfactory getbeanfactory () {
return beanfactory;
}
}
8. Finally, adding dependencies to achieve engineering eclipsetradeclient relies on your new plug-in engineering springclient. In the engineering eclipsetradeclient, open the Plugin.xml and click on the "Dependencies" tab. In the "Required plug-ins" section, click Add, select "Springclient (1.0.0)" and click OK (see Figure 12).


Figure 12. Add Springclient as a required plugin


Now you must add content related to Eclipse's partner policy in the eclipsetradeclient manifest file. In file Plugin.xml, click the Manifest.mf tab and add the following entry:

Eclipse-registerbuddy:springclient

  Four. Create a new Watchlistview

Now, you can start creating your own view class. First, you create a watchlistview that will send a request to the Stockdataservice of the application server:

1. In Plugin.xml, go to the Extensions tab.

2. Select Org.eclipse.ui.views in the All extensions tree and click Add.

3. Then a New dialog box window appears. Scroll through the extension points tree and select Org.eclipse.ui.views. In the available templates that correspond to Org.eclipse.ui.views, select Sampleview, and then click Next (see Figure 13).


Figure 13. New Extension dialog box


4. In the Main View Settings window, fill in the following:

Java Package Name = eclipseTradeClient.views.watchlist
View Class Name = Watchlistview
View Name = Watch List View
View Category ID = eclipsetradeclient
View Category Name = watchlist Category
Keep the Table Viewer selected and click Add the view to the resource perspective checked (see Figure 14). Click Next.


Figure 14. Setting "Main View Settings" For the Watch list view


5. Under "View Features", keep the default settings and click Finish.

6. You will now see the new view and Category in the All Extensions tab of Plugin.xml.

7. Now you can start writing your watch list view. This watch list is a table, so you first implement an interface Itablelabelprovider for this table. Create a new class Watchlisttablelabelprovider under the package eclipseTradeClient.views.watchlist. You can think of a itablelabelprovider as the tablecellrenderer of the jface equivalent-swing. The following is the code section for Watchlisttablelabelprovider:

Package eclipseTradeClient.views.watchlist;
Import Java.text.NumberFormat;
Import Org.eclipse.jface.viewers.ITableLabelProvider;
Import Org.eclipse.jface.viewers.LabelProvider;
Import Org.eclipse.swt.graphics.Image;
Import Stephenlum.services.stock.dto.StockDTO;
public class Watchlisttablelabelprovider extends Labelprovider implements
Itablelabelprovider {
private static NumberFormat NumberFormat = Numberformat.getinstance ();
Public Image Getcolumnimage (Object element, int columnindex) {
return null;
}
Public String Getcolumntext (Object element, int columnindex) {
if (element!= null) {
Switch (columnindex) {
Case 0:
Return ((stockdto) Element). Gettickersymbol ();
Case 1:
Return ((stockdto) Element). Getlasttrade (). toString ();
Case 2:
Return Numberformat.format ((stockdto) Element). Getvolume ());
Case 3:
Return ((stockdto) Element). Getdaysrange ();
Case 4:
Return Numberformat.format ((stockdto) Element). Getavgvol ());
Case 5:
Return ((stockdto) Element). Getdaysrange ();
Case 6:
Return ((stockdto) Element). Getfiftytwoweekrange ();
Case 7:
Return ((stockdto) Element). Getmarketcap ();
}
}
Return "";
}
}
8. Finally, you add your Watchlistview to the perspective class. Open the class perspective in Package Explorer and make the following modifications so that Watchlistview will appear at the bottom of the page:

Package eclipsetradeclient;
Import Org.eclipse.ui.IPageLayout;
Import Org.eclipse.ui.IPerspectiveFactory;
Import Org.eclipse.ui.IFolderLayout;
Import EclipseTradeClient.views.WatchListView;
Public class perspective implements Iperspectivefactory {
public void Createinitiallayout (Ipagelayout layout) {
String Editorarea = Layout.geteditorarea ();
Layout.seteditorareavisible (FALSE);
Layout.setfixed (FALSE);
Layout.addstandaloneview (Explorerview.id,
False
Ipagelayout.left,
0.25f,
Editorarea);
Ifolderlayout TopLeft = Layout.createfolder ("Top",
Ipagelayout.top,
0.50F,
Editorarea);
Layout.addview (Watchlistview.id,ipagelayout.bottom, 0.25f,editorarea);
}
}
9. Now, you can add in the class Watchlistview. I try to keep the code generated by the template from moving so that you are free to add your code. Essentially, you are adding a table-it will show all the information contained in a class Stockdto instance. Therefore, the columns in the table are also based on Stockdto members. I've renamed two generated action-now Action1 can retrieve a list of stocks from Stocktradeserver and display them in a table. And Action2 removes all elements from the table (refer to the list in the source code 1.eclipsetradeclient.views.watchlist).

  Five. Running the application

Now, you can run your application again. If you are not ready, you can import the Stocktradeserver project into eclipse by following these steps:

1. In Eclipse, click the toolbar button "Deploy myeclipse Java Project to Server" (see Figure 15).


Figure 15. Publish MyEclipse Java Server button

Make sure the project under the list is stocktradeserver. Click Add, select Tomcat 5 as your server, and click Finish. When you see a message "Successfully deployed", click OK (see Figure 16).


Figure 16. Stocktradeserver was successfully released

Now, start the Tomcat server (see Figure 17). Tomcat should be launched successfully.


Figure 17. Start Tomcat through the MyEclipse plug-in

2. Launch "Eclipse Rich Client". Open the Eclipsetradeclient ' s Plugin.xml file, click on the "Overview" tab and click "Launch an Eclipse application". When the application starts, press the red button in Figure 18 to get a list of stocks. This behavior will use spring Httpinvoker to retrieve the stock list from the application server. You can clear the stock list by pressing the button to the right of the red circle (see Figure 18).


Figure 18. Eclipsetradeclient Run successfully

Everything is going well! You have successfully built a small, eclipse-rich client program and connected it to an application server using the Spring Remoting technology.

   Six. Summary

In short, building your rich client program based on Eclipse RCP will significantly reduce the heavy code required to build the GUI framework for this program. In addition, the use of spring remoting as a client/server communication mechanism also allows you to easily implement protocol switching, while providing all other spring benefits on the server side.

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.