SWT/Jface Final evaluation of JFace project on Google Summer of Code 2014

来源:互联网 发布:pc软件下载吧 编辑:程序博客网 时间:2024/05/21 17:09

以后在玩Viewer时,获取selection时,不用强制转换了!

Hi everyone,

It has been a while since my last post entry. We had a lot of work to do to move our project to an acceptable state. In this post, I would like to give you an overview of the project and introduce you to the new changes.

We noticed that many client codes had to perform explicit casts when dealing with JFace Viewers and its related components (e.g. Providers, Selectors, and Editors). The issue of manipulating raw types is the possibility of throwing a ClassCastException to the user at runtime. By adding Generics support, we ensure that the Java compiler will perform type check and expose any improper cast at compiling time.

The major change was to add a generic type about the object to be hold, just like a regular class from Java Collections (e.g. List). Consider the following example:

  • Selection

Previously, we would return a selected content like this:

IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
MyModel obj = (MyModel) sel.getFirstElement();

Now the IStructuredSelection class contains information about what type of object it is holding, so the cast becomes unnecessary:

// There is a better way to get the concrete selection
// I’m using the old style (see this post)
IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
MyModel obj = sel.getFirstElement();

In addition to the object type, Viewers have to know how the elements are organized. For instance, a given class A to be displayed in a ListViewer is organized as a List of objects A. Therefore, all JFace viewers support this information through 2 generic types: E (the type of the element) and I (the type of the input):

final ListViewer<MyModel, List> v =
new ListViewer<MyModel, List>(shell, SWT.H_SCROLL | SWT.V_SCROLL);

v.setLabelProvider(new LabelProvider());
v.setContentProvider(ArrayContentProvider.getInstance(MyModel.class));
v.setInput(createModel());

private List createModel() {
List elements = new ArrayList();
for (int i = 0; i < 10; i++) {
elements.add(new MyModel(i));
}
return elements;
}

There are several classes supporting Generics, including Viewer, ContentViewer, StructuredViewer, AbstractListViewer, ColumnViewer, ComboViewer, ListViewer, AbstractTableViewer, AbstractTreeViewer, TableViewer, CheckboxTableViewer, AbstractTreeViewer, TreeViewer, CheckboxTreeViewer, IContentProvider, ILazyContentProvider, ILazyTreeContentProvider, ILazyTreePathContentProvider, IStructuredContentProvider, ArrayContentProvider, LabelProvider, BaseLabelProvider, IBaseLavelProvider, ILabelDecorator, IViewerLabelProvider, ILabelProvider, IStructuredSelection, ITreeSelection, StructuredSelection, and many other components from JFace.

By now, all tests are working and we have all JFace snippets updated. However, there are several Eclipse Viewers that should be updated to use the new JFace API.

Java collections and arrays do not mix very well, maybe in the future we will think about another version of JFace to work only with Java collection. Now, our priority is to make the new JFace API available to the Eclipse main repository.

Contributing to Eclipse was a valuable experience, so you are welcome to to join us too!


转载自:http://blog.vogella.com/2014/08/23/final-evaluation-of-jface-project-on-google-summer-of-code-2014/

1 0
原创粉丝点击