当前页面: 开发资料首页 → JSP 专题 → 用视图链接使Eclipse应用程序更丰富
摘要: Eclipse 简化了链接 UI 视图的工作,并提供了将视图链接应用到非 UI 场景的方式。
丰富的 GUI 中的视图可以以各种方式显示信息,从而改善用户体验。非常自然,UI 视图之间是相互依赖的,需要进行交互。Eclipse 简化了链接 UI 视图的工作,并提供了将视图链接应用到非 UI 场景的方式。
简介
Eclipse 平台允许使用可插入组件 —— 插件 —— 帮助创建丰富的图形用户界面(graphical user interface,GUI)应用程序。例如,插件可以向 GUI 提供视图。但是,在现实的应用程序中,UI 视图不能是孤立的。它们需要根据其他视图的状态进行交互和对本身进行更新。
一个简单的例子是描述世界各地的主要旅游目的地的 GUI 应用程序。这个 GUI 可能有一个 Select City 视图,用于显示旅游景点和公共交通信息。
public void selectionChanged(IWorkbenchPart part, ISelection selection);
要使视图能够监听选择改变,视图必须实现 ISelectionListener 接口并必须将自己注册到工作台页面。清单 1 显示一个例子。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!
}
}
getSite().getPage().addSelectionListener("SampleViewId",(ISelectionListener)this);
这种方式可以避免对消费者视图进行多余的回调,如果视图被注册为非特定的监听器,就会出现这种情况。 清单 2 中的代码片段显示一个视图的 createPartControl() 方法,这个方法创建一个 JFace TableViewer 并将它作为选择提供器添加到工作台站点。这些代码使 TableViewer 中的任何 UI 选择改变能够传播到页面,并最终传播到对这种事件感兴趣的消费者视图。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);
}
这个视图将它创建的 JFace TableViewer 注册为选择提供器有两个原因:
这个视图打算使用这个 TableViewer 显示信息,而且用户将与 TableViewer 进行交互。
TableViewer 实现了选择提供器接口并能够向工作台部分站点传播选择事件。
因为 JFace 查看器是选择提供器,所以在大多数情况下就不必创建选择提供器了。视图只需使用众多的 JFace 查看器之一来显示信息,并将 JFace 查看器注册为选择提供器。
另一种链接方式
某些情况需要另一种视图链接方式:
信息量可能太大,由于内存使用量增加,UI 选择对象无法有效地容纳它。
视图可能希望公布其他信息,而不只是公布可视化选择信息。公布的信息可能是根据选择进行某些后期处理的结果。
视图可能希望使用来自另一个插件的信息,而这个插件可能根本没有提供视图(使用包含的 JFace 查看器)。在这种情况下,使用基于 UI 选择的链接是不可能的。
可以使用 org.eclipse.core.runtime.IAdaptable 接口来缓解第一个问题,这个接口使选择对象能够在需要时传播更多信息。第二个和第三个问题需要用手工方式解决,属性改变监听器模式是合适的解决方案。
使用 IAdaptable 接口
实现 IAdaptable 接口的类能够动态地返回某些类型的适配器,然后可以使用这些适配器获取更多信息。如果查看器中的选择对象实现了 IAdaptable 接口,那么根据它们可以返回的适配器类型,可以有效地获取更多信息或相关信息。org.eclipse.core.runtime.IAdaptable 接口如下所示。
public void object getAdapter(Class adapter);
显然,调用者应该知道它期望选择返回的适配器接口类型。考虑一个 JFace TreeViewer,它在一个单层的树中显示城市。代表城市的对象是 CityClass 类型的。CityClass 对象应该包含关于此城市的基本信息,并只在需要时返回详细信息。在清单 3 中要注意,CityClass 支持的适配器类型使调用者能够在需要时获得更多信息。class CityClass implements IAdaptable {
private String cityName;
public CityClass(String name) {
this.name = name;
}
public String getName() {
return name;
}
public CityClass getParent() {
return 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;
}
}
//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);
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(new PropertyChangeEvent(this, \"CitiesWeatherXML" , null , CityWeatherXMLObj));
}
}
}
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
}
}
}