站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Eclipse 专题在定制Eclipse SWT组件中实现MVC

在定制Eclipse SWT组件中实现MVC

摘要: 作者简要介绍了 MVC架构,以结构化查看器的形式解释了 MVC 的当前实现
  Eclipse SWT(标准部件工具包)提供了丰富的 API 集来实现定制部件(widget)。在这篇文章中,作者简要介绍了 MVC(模型-视图-控制器)架构,以结构化查看器的形式解释了 MVC 的当前实现,并介绍了一种使用定制 SWT 部件的实现。

  什么是 MVC?

  MVC 架构(或设计模式)是图形用户界面(GUI)的设计样式,由三部分构成:模型、视图和控制器。MVC 把表示层从数据解耦出来,也把表示从数据的操作解耦出来。

  实现 MVC 架构与其他类型的应用程序有所不同。主要的区别来自如何放置和实现业务逻辑或查看呈现逻辑。与典型的 Web 应用程序不同,在这类程序中,程序员必须设计和实现所有 MVC 组件,而 Eclipse 提供的 API 可以替您做大部分控制或呈现工作。所以,不能严格地把 Eclipse 的 MVC 实现与 Web 或其他应用程序类型的 MVC 进行比较。

  Eclipse JFace

  Eclipse JFace 用内容提供者和标签提供者实现 MVC 架构。JFace API 包装了标准(并非不重要的)部件,例如表和树,并实现了结构化内容提供者和标签提供者。可以根据部件类型实现不同的内容提供者。面向列表的查看器会实现结构化查看器,而内容则以结构化(列表的)方式映射到部件条目上。

  基类叫做 Viewer,它是结构化查看器的一个扩展。查看器充当部件容器。内容提供者以结构化的方式得到数据;类似地,标签提供者获得对应的标签。JFace 查看器实现检索该数据,设置对应的关联,并用数据集更新用户界面(UI)组件。它还执行选择、过滤和排序。

  如何实现 JFace

  Eclipse View 和 Viewer 负责执行大部分 JFace 控制功能。Viewer 或者说 MVC 的视图部分,也充当部件容器;这是表示组件。

  Eclipse View 实例化 Viewer、内容提供者和标签提供者,并充当模型,容纳值对象,并在 Viewer 中把它们设置为 inputElement。

  要创建 View,请用 createPartControl() 方法实例化 Viewer。清单 1 实例化一个默认的树查看器;您也可以定制树,并用树对象作为参数,用构造函数实例化树查看器。

  清单 1. ExampleView 的 CreatePartControl 方法

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>
 

public class ExampleView extends ViewPart  

{ ... public void createPartControl(Composite parent)  

{ // define a grid layout  

GridLayout layout = new GridLayout();  

layout.numColumns = 1;  

layout.marginHeight = 0;  

layout.marginWidth = 0; l 

ayout.horizontalSpacing = 0;  

layout.verticalSpacing = 1;  

parent.setLayout(layout);  

// create widgets createActionBar(parent); 

 createTree(parent);  

// add context menu and listeners 

viewer.addDoubleClickListener(this); viewer.addSelectionChangedListener(openAction); 

 // register viewer so actions respond to selection getSite().setSelectionProvider(viewer); 

 hookContextMenu();  

} 

private void createTree(Composite parent)  

{  

viewer = new TreeViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); 

viewer.setContentProvider(new ExampleViewContentProvider()); viewer.setLabelProvider 

(new ExampleViewLabelProvider());  

viewer.setSorter(new ViewerSorter());  

viewer.setInput(ModelManager.getExampleModel());  

viewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));  

} ... }  

</td></tr></table>
  在另一个独立类中实现 ContentProvider,它是一个对象,用适合查看器类型的接口向视图提供数据。例如,您可以实现 IStructuredContentProvider 或 ITreeContentProvider 查看器。

  请在 ContentProvider 代码中实现以下一个方法,把 ContentProvider 与 Viewer 相关联:
  • getElements(Object parent)
  • getChildren(Object element)

  注意:JFace 框架将调用这些方法。

  清单 2. 创建定制的 ContentProvider

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>
 

  public class ExampleViewContentprovide implements ITreeContentProvide { 

</td></tr></table>
  MVC 架构通常包含多个视图和一个数据源。目前在 Eclipse 平台上,只能把一个视图与一个模型相关联。但是,也可以创建多个视图,用适配器视图访问同一数据。只要把 inputChanged() 方法包含在 ContentProvider 类中即可。只要 Viewer 有新的输入集,就会使用 inputChanged() 方法通知 ContentProvider。inputChanged() 方法接受 Viewer 作为输入参数,所以多个视图可以使用一个 ContentProvider。

  清单 3. 将 inputChanged 方法用于不同的查看器

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>
 

/** * Register content provider with model. */ 

 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) 

 { 

 if (newInput != null) 

    {  

this.viewer = viewer; 

this.model = (ExampleDelegate)newInput; this.model.addModelListener(this);  

} 

 } 

</td></tr></table>



↑返回目录
前一篇: 在Java中使用Draw2D和SWT绘图
后一篇: 基于Eclipse 3.0的SWT编程