⬆️ ⬇️

Developing a simple Eclipse RCP application.

In this article, I wanted to talk about how to create applications using the Eclipse RCP (Rich Client Platform). The reason for writing was the fact that on Habré there are absolutely no articles describing this platform. As an example, we will create a custom input form; correctness of the data will be checked using the JFace Data Binding framework. For the seed, here is a screenshot of what we should have.









What is Eclipse RCP

Pros and cons of the platform

Getting Started

Install the necessary plugins

Creating a project

Project structure

Launch of the project

Add view

Add extension

Customize the view

Customize view layout

Launching an app with an added view

Links to useful resources

')

What is Eclipse RCP



To begin with, it is still necessary to say what Eclipse RCP is. Eclipse is a development environment written in Java, developed and supported by the Eclipse Foundation (whose members are IBM, SAP, Oracle, companies offering Eclipse RCP-based products and participating in the development of the ecosystem of Google, RedHat, Adobe, Cisco, Intel). Eclipse RCP is a set of plug-ins for creating a so-called rich client application. All that the user sees when opening the Eclipse IDE is a plugin based on this platform. The user can create his plug-ins based on a huge number of existing ones, and if necessary, you can find most popular frameworks, such as Hibernate, Google Guice, Google Guava, JUnit, TestNG in the form of plug-ins. It is also worth noting that the runtime architecture is based on the specification of the OSGI service platform, this specification describes how to create and operate modular applications. Eclipse, as mentioned above, is written in Java and is positioned as a cross-platform product (in 90% of cases, the RCP application will be built on Windows, Linux and Mac). All plug-ins that make up the core of the platform, and most third-party plug-ins are distributed under the EPL license (Eclipse Public License). The user interface of the RCP applications is based on the visual components of the SWT and JFace frameworks, as well as on its own Eclipse widgets. The figures below show the components of an application based on the RCP platform and the structure of the Eclipse platform itself.





Figure 1 - Components used in the RCP application (Figure taken from this site )





Figure 2 - Eclipse architecture (image taken from this site )



Pros and cons of the platform


The main question is why this platform is so good and why to use it for developing desktop applications (by the way, it is possible to develop web applications and applications for mobile platforms). Modularity, cross-platform, multilingual support, free of charge, a huge number of existing plug-ins, libraries and frameworks. All this allows you to create commercial-level applications (a link to the list of existing applications developed on this platform is given at the end of the article). The disadvantages include a fairly high level of occurrence, as to develop a serious application, you need to know at least in general terms how the OSGI framework works, to be able to work with SWT and JFace components and widgets. Also, for Russian-speaking developers, the problem will be to find any materials or books about the frameworks and libraries mentioned above (links to the found resources, including Russian-language ones, are given at the end of the article), although in Europe and the USA there are periodic conferences organized by The Eclipse Foundation, in Germany, publishes a special magazine about new plug-ins and containing a lot of examples of their use, and there is also a whole series of books in German, which describes all the nuances and subtleties of development. In English, you can find a special series of books eclipse series publisher Addison-Wesley, you can also find a couple of books from the publisher Apress. But in our native language materials and books are negligible.



Getting Started



Install the necessary plugins


Let's go straight to creating our first RCP application. To work, we need to download the build: Eclipse for RCP and RAP Developers from the official site. If the Eclipse IDE is already installed, you can use the update manager. Select Help-> Install New Software in the main menu. In the Install window that appears, from the top, select from the drop-down list the update site we need - download.eclipse.org/releases/indigo , if there is no such site in the list, then click the Add button to the right, write Indigo Update Site in the Name field, and in the Location field - the address given above, click OK (if the message appears when adding the address - Duplicate location, then the address already exists in the Available Software Sites list, you can view the list of update sites in the previous Install window by clicking the link Available Software Sites, which is located under the drop-down list). After selecting the update site, a tree will appear below (if the Group items by category checkbox is checked), open the General Purpose Tools item and select the Eclipse Plug-in Development Environment , then open the EclipseRT Target Platform Components and check the plug-in - Eclipse RCP Plug-in Developer Resources , we need these two plugins to create our project. Next, click Next twice, accept the license agreement and click the Finish button; all, installation of necessary plug-ins will begin. After installation, we will be prompted to restart Eclipse, which we will do.





Figure 3 - Plug-in installation window



Creating a project


After the reboot, select in the main menu the item File-> New-> Other, select the item Plug-in Development, then in the drop-down menu we will mark Plug-in Project.





Figure 4 - Menu for selecting the type of project being created



Click Next, we need to give the name of our project, let it be called first.rcp.application, again click Next. In the next window we need to specify the name of the application, in the Name field we will write First RCP Application. Uncheck the box next to Generate an activator, a Java class that controls the plug-in's life cycle; For our simple application, the class activator is not needed. Leave the checkbox on the item - this plug-in, as our application will contain a user interface. Let's leave the third item Enable API Analysis unchecked. To the question - would you like a rich client application? answer Yes.





Figure 5 - Plugin creation window



Click Next, we will be asked to select a template for the future application, select Hello RCP and click Next.





Figure 6 - The RCP project template selection window



In the last window, in the Application window title field we will write - User Form, in the Application class field - MyApplication . Checkbox Add branding leave inactive. Click the Finish button. We will be asked to switch to the perspective of the Plug-in Development perspective, agree with this proposal.





Figure 7 - RCP project template configuration window



Project structure


So before us is the structure of the newly created project.





Figure 8 - Project Structure



The content of the five classes in the first.rcp.application package currently does not iterate us, I can only say that the MyApplication class is in some way the main () method of a regular Java program, this class is responsible for how our plugin will be started and how it will be stopped . In the ApplicationWorkbenchWindowAdvisor class, we can set the size of the application window using the following line of code:

configurer.setInitialSize(new Point(400, 300));

We can also see that the default toolbar and status bar will not be shown:

configurer.setShowCoolBar(false);

configurer.setShowStatusLine(false);


The last line sets the title of the main window:

configurer.setTitle("User Form");

The ApplicationActionBarAdvisor class is responsible for setting up the menu bar of our application. The Perspective class is responsible for the location and size of editors (editors) and views (views) that are in a given perspective (perspective); there must be at least one perspective in the RCP application.



Launch of the project


In order to launch the application we just created, we need to go to the META-INF folder and open the MANIFEST.MF file if you suddenly closed it (this file opens by default when you create a project).





Figure 9 - RCP Application Property Editor



This file allows us to change many of the project settings, connect additional plug-ins, connect and manage extensions, customize the build of our plugin and much more. We are on the Overview tab, in the Testing section, click on the link - Launch an Eclipse application, after a moment our application window will appear on the screen, close it and proceed to the next step.





Figure 10 - Window of our application



Add view



Add extension


Our task is to create a view on which we can place the elements of the graphical interface. Go back to the file MANIFEST.MF , select the tab below - Extensions. We see that by default we have two extensions: org.eclipse.core.runtime.applications and org.eclipse.ui.perspectives. The first extension is associated with the class of our application MyApplication , if we select this extension and expand the tree to the node first.rcp.application.MyApplication (run), we will see that the class field contains the name of this particular class. Returning to the root node of this list, in the Extension Details section on the right we will see two fields, the values ​​of which we can also change (we will not do this now): ID - application identifier and Name - application name.



The second extension is responsible for setting the perspective of our application. The MANIFEST.MF file allows us, without looking at the code of the Perspective class, to specify which types and / or editors the given perspective will contain, their position, size and ratio. Clicking on this extension and going to the child item, we will see that in the Extension Element Details section we can specify the perspective class, the identifier and the name. It should be noted that such changes, as mentioned above, can be made by editing directly the class code that is associated with this extension and editing the plugin.xml file, but I would not like to complicate the creation of our first project.





Figure 11 - Tab with extensions of our project



We need to add a new extension to create a view. To do this, click on the Add button and in the window that appears, enter views into the Extension Point filter field, only one extension should remain - org.eclipse.ui.views, select it and click the Finish button.





Figure 12 - Window for selecting a new extension



In the list of extensions, we should have one more, the third extension.



Customize the view


Click on the extension we added with the right mouse button, a context menu will appear, select New-> view in it, thus we will add an element to our extension (this element is the view we need). The settings for this item will appear on the right. First, create a class for our view. We can do this by clicking on the class * link.





Figure 13 - View Settings (view)



A standard Java class creation dialog will open, give it a name - MainView; as we can see, this class is inherited from the org.eclipse.ui.part.ViewPart class, the parent class for all views. Finish class creation by clicking Finish. It remains quite a bit, after creating a class for our view, we will open its code, create a static variable that will contain an identifier of this type, we will set the canonical name of this class as an identifier. We write the following:



public class MainView extends ViewPart {

public static final String ID = MainView.class.getCanonicalName();



}


Scroll to the file MANIFEST.MF , open the Extension tab, copy the contents of the class * field and paste it into the ID field. Now the name of the class and the identifier of this type coincide, this technique is good practice, as it always allows you to quickly find the desired type and find out its identifier. In the field name * write - User Form. The whole species has been created, it remains to associate it with our perspective, since the species itself cannot be used, in other words, it must belong to some perspective.



Customize view layout


We can perform further manipulations while remaining in the extension editor or open the code of the perspective class - Perspective.java , which we will do. In the Perspective class, go to the method - createInitialLayout (), this method sets the initial position of the views and editors. In the method, we will write the following two lines of code:

layout.setEditorAreaVisible(false);

layout.addStandaloneView(MainView.ID, true, IPageLayout.LEFT, 1.0f, layout.getEditorArea());


The first line indicates to the layout object that we do not need an area for editing, since we only have one type and do not have editors. The second line adds our view, moreover as standalone. The first parameter is our view identifier. The second parameter is a boolean value that is responsible for whether or not the header of our view is displayed (User Form). The third parameter is intended to indicate the orientation of the view in perspective, since we have one view and occupy the entire perspective space, this value can be anything. The fourth parameter determines the position of this view in relation to other types or editors, since, in our case, view one it should occupy the entire perspective space. The last fifth parameter, the identifier of the editing area (editor area). Let's keep our changes.



Launching an app with an added view


Let's go back to the MANIFEST.MF file and run our application again. To do this, go to the Overview tab, in the Testing section, click on the Launch an Eclipse application link. We will not see a big difference with the way the form looked at the previous launch, only the tab with our view, the User Form, was added.





Figure 14 - Window of our application with a view added



For today, all will be continued in the next article.



Archive with the source code of the project



Links to useful resources



Russian speakers:



English speaking:

Source: https://habr.com/ru/post/139340/



All Articles