Java by API/org.eclipse.jface.viewers/ILabelProvider
Версия от 17:43, 31 мая 2010; (обсуждение)
implements ILabelProvider
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass extends ApplicationWindow {
private ItemFilter filter = new ItemFilter();
public MainClass() {
super(null);
setBlockOnOpen(true);
open();
Display.getCurrent().dispose();
}
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText("Food List");
}
protected Control createContents(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
Button filterButton = new Button(composite, SWT.CHECK);
filterButton.setText("&Show only healthy");
final ListViewer lv = new ListViewer(composite);
lv.setContentProvider(new ItemContentProvider());
lv.setLabelProvider(new ItemLabelProvider());
lv.setInput(new ItemList());
filterButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
if (((Button) event.widget).getSelection())
lv.addFilter(filter);
else
lv.removeFilter(filter);
}
});
parent.pack();
return composite;
}
public static void main(String[] args) {
new MainClass();
}
}
class MyItem {
private String name;
private boolean visible;
public MyItem(String name, boolean healthy) {
this.name = name;
this.visible = healthy;
}
public boolean isVisible() {
return visible;
}
public String getName() {
return name;
}
}
class ItemFilter extends ViewerFilter {
public boolean select(Viewer arg0, Object arg1, Object arg2) {
return ((MyItem) arg2).isVisible();
}
}
class ItemList {
private ArrayList foods;
public ItemList() {
foods = new ArrayList();
foods.add(new MyItem("A", true));
foods.add(new MyItem("B", false));
foods.add(new MyItem("C", true));
foods.add(new MyItem("D", false));
foods.add(new MyItem("E", true));
foods.add(new MyItem("F", false));
foods.add(new MyItem("G", true));
foods.add(new MyItem("H", false));
foods.add(new MyItem("I", true));
foods.add(new MyItem("K", false));
}
public List getFoods() {
return Collections.unmodifiableList(foods);
}
}
class ItemLabelProvider implements ILabelProvider {
public Image getImage(Object arg0) {
return null;
}
public String getText(Object arg0) {
return ((MyItem) arg0).getName();
}
public void addListener(ILabelProviderListener arg0) {
}
public void dispose() {
}
public boolean isLabelProperty(Object arg0, String arg1) {
return false;
}
public void removeListener(ILabelProviderListener arg0) {
}
}
class ItemContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object arg0) {
return ((ItemList) arg0).getFoods().toArray();
}
public void dispose() {
}
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
}
}