Make eclipse applications richer with view links

Source: Internet
Author: User
Tags object event listener implement interface key memory usage query require
Programs | links | Views in rich GUI can display information in a variety of ways to improve the user experience. Naturally, the UI views are interdependent and require interaction. Eclipse simplifies the work of the linked UI view and provides a way to apply the view link to a non-UI scenario.

   Brief Introduction

The Eclipse platform allows the use of pluggable components-plug-ins-to help create rich graphical user interface (graphical user Interface,gui) applications. For example, a plug-in can provide a view to the GUI. However, in a real-world application, the UI view cannot be isolated. They need to interact and update themselves based on the state of the other views.

A simple example is a GUI application that describes the major tourist destinations around the world. This GUI may have a Select city view to display tourist attractions and public transport information.


Figure 1. Examples of view links
This article describes how to combine views in Eclipse and how to respond to the state of other views. Also discusses how linked views might be more appropriate than others.

The Eclipse developer can link to the view by relying on the following methods:

Select Provider-Select the Listener (Selection Provider-selection listener) mode so that the view responds to selections in other views
Iadaptable interface, used in conjunction with some events
Property changes the listener, which allows the view to change the properties of the event to the registered listener

   Select provider-Select Listener Paradigm

Select provider-selecting listener mode makes it easy to create views that respond to changes in other views. For example, when a user clicks on a UI item that represents the city name, another view may need to display details of the city's attractions. Such a view can use the UI to select the information contained in the object (possibly a string object representing the city name) and use it to get and display additional information from the model.

Views should be able to identify and take advantage of UI selection events. Org.eclipse.ui.ISelectionListener is the listener interface that receives UI selection events. Select the listener must register to the Workbench page. The Workbench page implements the services defined by the Org.eclipse.ui.ISelectionService interface, thus selecting the UI to tell the listener of the event. Select the listener must be registered to the Select service.

Views that are used to display selectable UI items should also be able to advertise UI selections. The view does this by registering the "select Provider" into their respective workbench sites. Each UI section in Eclipse contacts the Workbench site through a org.eclipse.ui.IWorkbenchPartSite reference. Select the provider to register to the Workbench site.

When you use the Select Provider-select listener Mode link View, the view can add itself as a listener to the Workbench page, and other views that you want to advertise the selection must add the selection provider to their respective workbench site. The Org.eclipse.ui.ISelectionListener interface is shown below.

public void SelectionChanged (Iworkbenchpart part, iselection selection);

To enable the view to listen for selection changes, the view must implement the Iselectionlistener interface and must register itself with the Workbench page. Listing 1 shows an example.

Listing 1. Add a selection listener to the Workbench page

public class MyView extends Viewpart implements iselectionlistener{


public void Createpartcontrol (composite parent) {

Add this view as a selection listener to the Workbench page
Getsite (). GetPage (). Addselectionlistener ((Iselectionlistener) this);

}

Implement the method defined in Iselectionlistener, to consume UI selections
public void SelectionChanged (Iworkbenchpart part, iselection selection) {
Examine selection and act on it!
}

}

A better way to use the UI selection is to register the consumer view as a listener to a specific view section. As you can see in the following example, the view ID of the Source view section is used as a parameter during registration of the selection listener.

Getsite (). GetPage (). Addselectionlistener ("Sampleviewid", (Iselectionlistener) this);

This approach avoids unnecessary callbacks to the consumer view, which can occur if the view is registered as a neutral listener. The code fragment in Listing 2 shows the Createpartcontrol () method of a view that creates a JFace tableviewer and adds it as a selection provider to the Workbench site. This code allows any UI selection changes in Tableviewer to propagate to the page and eventually propagate to the consumer view of interest in such events.

Listing 2. Setting the selection Provider

public void Createpartcontrol (composite parent) {
Set up a JFace Viewer
Viewer = new Tableviewer (parent, SWT. MULTI | Swt. H_scroll | Swt. V_scroll);
Viewer.setcontentprovider (New Viewcontentprovider ());
Viewer.setlabelprovider (New Viewlabelprovider ());
Viewer.setsorter (New Namesorter ());
Viewer.setinput (Getviewsite ());

ADD the JFace Viewer as a Selection Provider to the View site.
Getsite (). Setselectionprovider (Viewer);

}

This view registers the JFace Tableviewer it creates as a selection provider for two reasons:

This view is intended to use this tableviewer to display information, and the user will interact with Tableviewer.

Tableviewer implements the Select provider interface and is able to propagate selection events to the workbench part of the site.

Because the JFace Viewer is a selection provider, in most cases you do not have to create a selection provider. The view simply uses one of the many JFace viewers to display the information and registers the JFace Viewer as the selection provider.

Another way to link

Some situations require a different way of view linking:

The amount of information may be too large, and the UI Selection object cannot effectively accommodate it because of increased memory usage.

The view might want to advertise other information, not just the visual selection information. The published information may be the result of some post-processing based on the selection.

The view may want to use information from another plug-in, which may not provide a view at all (using the included JFace Viewer). In this case, it is not possible to use a link based on the UI selection.

You can use the Org.eclipse.core.runtime.IAdaptable interface to mitigate the first problem, which allows the selected object to propagate more information as needed. The second and third issues need to be addressed manually, and the attribute change listener mode is the appropriate solution.

  Using the Iadaptable interface

Classes that implement the Iadaptable interface can dynamically return certain types of adapters, which can then be used to obtain more information. If the selected objects in the viewer implement the Iadaptable interface, more information or related information can be effectively obtained based on the type of adapter they can return. The Org.eclipse.core.runtime.IAdaptable interface is shown below.

public void Object Getadapter (Class adapter);

Obviously, the caller should know that it expects to choose the adapter interface type returned. Consider a JFace treeviewer, which displays the city in a single layer of trees. The object representing the city is of type Cityclass. The Cityclass object should contain basic information about the city and return details only as needed. Notice in Listing 3 that the type of adapter supported by Cityclass enables callers to get more information when they need it.

Listing 3. Cityclass in the JFace treeviewer

Class Cityclass implements Iadaptable {
Private String CityName;

Public Cityclass (String name) {
THIS.name = name;
}
Public String GetName () {
return name;
}
Public Cityclass GetParent () {
return to parent;
}
Public String toString () {
return GetName ();
}
Public Object Getadapter (Class key) {
if (Key.getname (). Equals ("Itransportationinfo"))
Return Cityplugin.getinstance (). Gettransportadapter ();
Else (Key.getname (). Equals ("Iplacesinfo"))
Return Cityplugin.getinstance (). Getplacesadapter ();
return null;
}
}

Developers familiar with the Eclipse IDE Workbench understand the Outline view, which provides a structured view of the files opened in the editor. This Outline view shows how the Iadaptable interface can be used in conjunction with some event types to effectively initialize the view based on the contents of other views. The editor must create a Content Outline page for the files that the user opens. The Content Outline page conforms to the Icontentoutlinepage interface. The editor must also implement the Iadaptable interface so that you can query the editor for the Icontentoutlinepage type of adapter. Use this adapter to get and display outline information for a file.

Another example of the Iadaptable interface is the Properties view. The Properties view tracks the selection of the active part and invokes the Getadapter method on the currently selected object. The adapter type of the query is Ipropertysource. The Properties view then uses the Ipropertysource adapter to get the information to display.

In these view link examples, the application obtains information through iadaptable when it receives Selection Changed or part activation notifications. Therefore, if the selected object implements Iadaptable, the user can get much more information from the adapter than the amount obtained from the selected object itself.

   property to change listener paradigm

You can use attributes to change the interaction of listener types to address two other issues mentioned earlier: How does a view use information from Plug-ins that do not provide views, and how does the view advertise information generated by some processing after a visual selection?

You can create a plug-in to accept the registration of a property change listener and notify registered listeners when needed. An application can tell a custom event listener, and the event can contain information to share.

Unlike the case of a selection provider, a property change provider does not need to implement a specific interface. You must decide to register the listener with the semantics of the provider. The code fragment in Listing 4 is a method that allows you to add or remove a property change listener in the property provider view or plug-in class.

Listing 4. Add and remove properties change listeners

To add a listener for property changes to this notifier:

public void Addpropertychangelistener (Ipropertychangelistener listener);

To remove the given content change listener from this notifier:

public void Removepropertychangelistener (Ipropertychangelistener listener);

The property provider should use Org.eclipse.jface.util.PropertyChangeEvent to create an event that can be effectively populated and propagated. In addition, the property provider is responsible for maintaining and recalling the list of listeners.

As an example, consider a plug-in that calls the world Weather Web Service hourly to query the weather of a major city to make this information available to other plug-ins and views. Cityweatherplugin can expose a property called Citiesweatherxml, and consumers can register themselves as PropertyChange listeners to Cityweatherplugin. To do this, the listener must understand the registration method in Cityweatherplugin in order to register itself as a listener for meteorological data events. Cityweatherplugin should track and notify listeners. It uses propertychangeevent to provide data to listeners.

Listing 5. Creating a property Provider

Class Citypopulationplugin {
ArrayList mylisteners;

A public method that allows listener registration
public void Addpropertychangelistener (Ipropertychangelistener listener) {
if (!mylisteners.contains (listener))
Mylisteners.add (listener);
}

A public method that allows listener registration
public void Removepropertychangelistener (Ipropertychangelistener listener) {
Mylisteners.remove (listener);
}

Public Citypopulationplugin () {
method to start the thread that invokes the population \
Web Service once every hour
And then notifies the listeners via the PropertyChange () callback method.
Initwebserviceinvokerthread (mylisteners);
}

void Initwebserviceinvokerthread (ArrayList listeners) {
Code to Invoke Web Service periodically, and retrieve information

Post invocation, Inform listeners
for (Iterator iter = Listeners.iterator (); Iter.hasnext ();) {
Ipropertychangelistener element = (iproperty\
ChangeListener) Iter.next ();
Element.propertychange (this, \ "Citiesweatherxml", NULL, Cityweatherxmlobj) (new propertychangeevent);
}
}
}

The property change listener must implement the Org.eclipse.jface.util.IPropertyChangeListener interface to allow the property-change provider to recall it. This interface has a method of public void PropertyChange (Propertychangeevent event).

Listing 6. Implement Ipropertychangelistener

Class MyView implements Ipropertychangelistener {

public void Createpartcontrol () {
Register with a known Plugin that sources Population Data
Citypopulationplugin.getinstance (). Addpropertychangelistener (this);
}

public void PropertyChange (Propertychangeevent event) {

This view is interested in the Population Counts of the cities.
The population data is being sourced by another
Plugin in the background.

if (Event.getproperty (). Equals ("Citiesweatherxml")) {
Object val = Event.getnewvalue ();
Do something with Val
}
}
}

The flexibility of this approach is that applications can notify listeners when needed and pass information to them based on a variety of scenarios. The information passed does not have to be directly related to the UI selection, which can be the result of some post-processing. In addition, it can be related to the state of other background jobs or the latest information that is periodically obtained from the model. For example, City Selector View may not only propagate selected cities, but also use the PropertyChange model to asynchronously disseminate meteorological information from the currently selected city to other consumers.

   Concluding remarks

This article discusses various ways to make views work together and respond. If the UI selection itself is not enough, you can use the Iadaptable interface to strengthen them. The property change listener also provides greater flexibility to satisfy a non-UI scenario.

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.