视图可以包含任何多控件,但通常为了界面简洁只包含一个Jface表或树查看器。下面通过一个实例演示如何开发视图。
1定义输入模型,包括元素、模型、内容提供器、标签提供器(也是一个完整的模型-View事件演示)
public class AddressItem {
private String name;
private String address;
private AddressCategory category;
static AddressItem[] NONE = new AddressItem[]{};
}
//AddressCategory为类别信息
public abstract class AddressCategory implements Comparable{
private final String categoryName;
private final int priority;
}
//模型类,模拟事件源
public class AddressManager{
private static AddressManager manager;
private Collection<AddressItem> addresses;
private List listeners = new ArrayList();
public static AddressManager getManager()
{
if(manager == null)
manager = new AddressManager();
return manager;
}
public AddressItem[] getAddresses(){
return (AddressItem[])addresses.toArray(
new AddressItem[addresses.size()]);
}
public void addAddresses(AddressItem[] items){
if(addresses == null)
loadAddresses();
if(addresses.addAll(Arrays.asList(items)))
{
fireAddressesChanged(items, AddressItem.NONE, AddressItem.NONE);
}
}
public void removeAddresses(AddressItem[] items)
{
if(addresses.removeAll(Arrays.asList(items)))
{
fireAddressesChanged(AddressItem.NONE, items, AddressItem.NONE);
}
}
//模拟事件源,注册监听器、注销监听器、引发事件
public void addAddressManagerListener(AddressManagerListener listener)
{
listeners.add(listener);
}
public void removeAddressManagerListener(AddressManagerListener listener)
{
listeners.remove(listener);
}
private void fireAddressesChanged(AddressItem[] itemsAdded,
AddressItem[] itemsRemoved, AddressItem[] itemsModified)
{
AddressManagerEvent event =
new AddressManagerEvent(
this, itemsAdded, itemsRemoved, itemsModified);
for(Iterator iter = listeners.iterator();iter.hasNext();)
{
((AddressManagerListener)iter.next()).addressesChanged(event);
}
}
监听器如下
public interface AddressManagerListener {
public void addressesChanged(AddressManagerEvent event);
}
事件类如下
public class AddressManagerEvent extends EventObject {
private final AddressItem[] added;
private final AddressItem[] removed;
private final AddressItem[] modified;
public AddressManagerEvent(AddressManager source,
AddressItem[] itemsAdded, AddressItem[] itemsRemoved, AddressItem[] itemsModified)
{
super(source);
added = itemsAdded;
removed = itemsRemoved;
modified = itemsModified;
}
public AddressItem[] getItemsAdded()
{
return added;
}
public AddressItem[] getItemsRemoved()
{
return removed;
}
public AddressItem[] getItemsModified()
{
return modified;
}
}
ContentProvider代码如下,实现对模型AddressManager的监听
public class AddressViewContentProvider implements IStructuredContentProvider,
AddressManagerListener {
private TableViewer viewer;
private AddressManager manager;
public Object[] getElements(Object inputElement) {
return manager.getAddresses();
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
this.viewer = (TableViewer) viewer;
if (manager != null)
manager.removeAddressManagerListener(this);
manager = (AddressManager) newInput;
if (manager != null) {
manager.addAddressManagerListener(this);
}
}
public void addressesChanged(AddressManagerEvent event) {
viewer.getTable().setRedraw(false);
try {
viewer.remove(event.getItemsRemoved());
viewer.add(event.getItemsAdded());
viewer.update(event.getItemsModified(), null);
} finally {
viewer.getTable().setRedraw(true);
}
}
}
标签提供器代码如下
public class AddressViewLabelProvider extends LabelProvider implements
ITableLabelProvider {
public Image getColumnImage(Object element, int columnIndex) {
switch (columnIndex) {
// 姓名
case 0:
return null;
// 类别
case 1:
return ((AddressItem) element).getCategory().getImage();
default:
return null;
}
}
public String getColumnText(Object element, int columnIndex) {
switch (columnIndex) {
case 0:
if (element != null)
return ((AddressItem) element).getName();
return "";
case 1:
if (element != null)
return ((AddressItem) element).getCategory().getCategoryName();
return "";
default:
return "";
}
}
}
AddressView类代码如下:
public class AddressView extends ViewPart{
private static TableViewer viewer;
private TableColumn nameColumn;
private TableColumn categoryColumn;
public void createPartControl(Composite parent) {
// 创建viewer
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.FULL_SELECTION);
// 设置表格的显示界面
final Table table = viewer.getTable();
nameColumn = new TableColumn(table, SWT.LEFT);
nameColumn.setText("姓名");
nameColumn.setImage(ImageCache.getInstance().getImage(
ImageKeys.getImageDescriptor(ImageKeys.IMAGE_PEOPLE)));
nameColumn.setWidth(100);
categoryColumn = new TableColumn(table, SWT.LEFT);
categoryColumn.setText("类别");
categoryColumn.setImage(ImageCache.getInstance().getImage(
ImageKeys.getImageDescriptor(ImageKeys.IMAGE_CATEGORY)));
categoryColumn.setWidth(100);
table.setHeaderVisible(true);
table.setLinesVisible(true);
viewer.setContentProvider(new AddressViewContentProvider());
viewer.setLabelProvider(new AddressViewLabelProvider());
viewer.setInput(AddressManager.getManager());
}