查看器的框架构成:
* 模型和元素:存储着要显示在控件中的数据模型(由开发者编写)
* 内容提供者和标签提供者:负责将数据模型转化成可显示的图片和文字(由开发者编写)
* 控件:显示内容
* 查看器:负责协调上面各部分的工作
============Demo============
public class User {
private String id;
private String name;
public User(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Vector;
public class ListModel {
public static final String ADD_ELEMENT = "addElement";
public static final String REMOVE_ELEMENT = "removeElement";
private PropertyChangeSupport delegate;
private Vector content;
public ListModel() {
content = new Vector();
delegate = new PropertyChangeSupport(this);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
delegate.addPropertyChangeListener(listener);
}
public void firePropertyChange(PropertyChangeEvent evt) {
delegate.firePropertyChange(evt);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
delegate.removePropertyChangeListener(listener);
}
public void add(Object element) {
if (content.add(element))
firePropertyChange(new PropertyChangeEvent(this, ADD_ELEMENT, null,
element));
}
public void remove(Object element) {
if (content.remove(element))
firePropertyChange(new PropertyChangeEvent(this, REMOVE_ELEMENT,
null, element));
}
public Object[] elements() {
return content.toArray();
}
}
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;
public class ListLabelProvider implements ILabelProvider {
public Image getImage(Object element) {
return null;
}
public String getText(Object element) {
if(element instanceof User)
return ((User)element).getName();
return element.toString();
}
public boolean isLabelProperty(Object element, String property) {
return false;
}
public void dispose() {}
public void addListener(ILabelProviderListener listener) {}
public void removeListener(ILabelProviderListener listener) {}
}
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.Viewer;
public class ListContentProvider implements IStructuredContentProvider,
PropertyChangeListener {
private ListViewer viewer;
private ListModel model;
public Object[] getElements(Object inputElement) {
return model.elements();
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
this.viewer = (ListViewer) viewer;
if (oldInput instanceof ListModel)
((ListModel) oldInput).removePropertyChangeListener(this);
if (newInput instanceof ListModel) {
this.model = (ListModel) newInput;
((ListModel) newInput).addPropertyChangeListener(this);
}
}
public void propertyChange(PropertyChangeEvent evt) {
if (ListModel.ADD_ELEMENT.equals(evt.getPropertyName()))
viewer.add(evt.getNewValue());
if (ListModel.REMOVE_ELEMENT.equals(evt.getPropertyName()))
viewer.remove(evt.getNewValue());
}
}
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class UsingListViewer {
public static void main(String[] args) {
Display display = Display.getDefault();
final Shell shell = new Shell(display);
shell.setSize(400, 400);
shell.setLayout(new FillLayout());
ListViewer viewer = new ListViewer(shell, SWT.BORDER);
viewer.setContentProvider(new ListContentProvider());
viewer.setLabelProvider(new ListLabelProvider());
final ListModel input = new ListModel();
viewer.setInput(input);
shell.open();
shell.layout();
input.add(new User("1","张三"));
input.add(new User("2","李四"));
input.add(new User("3","王五"));
viewer.addDoubleClickListener(new IDoubleClickListener() {
@Override
public void doubleClick(DoubleClickEvent event) {
input.add(new User("4","hzy"));
}
});
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
System.out.println(((User)((StructuredSelection)event.getSelection()).getFirstElement()).getName());
}
});
while (!shell.isDisposed())
if (!display.readAndDispatch())
display.sleep();
}
}