Java/SWT JFace Eclipse/List Viewer
Demonstrates ListViewer
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;
/**
* This class demonstrates ListViewer.
*/
public class FoodList extends ApplicationWindow {
// The healthy filter
private HealthyFilter filter = new HealthyFilter();
/**
* FoodList constructor
*/
public FoodList() {
super(null);
}
/**
* Runs the application
*/
public void run() {
// Don"t return from open() until window closes
setBlockOnOpen(true);
// Open the main window
open();
// Dispose the display
Display.getCurrent().dispose();
}
/**
* Configures the shell
*
* @param shell
* the shell
*/
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText("Food List");
}
/**
* Creates the main window"s contents
*
* @param parent
* the main window
* @return Control
*/
protected Control createContents(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
// Add a checkbox to toggle filter
Button filterHealthy = new Button(composite, SWT.CHECK);
filterHealthy.setText("&Show only healthy");
final ListViewer lv = new ListViewer(composite);
lv.setContentProvider(new FoodContentProvider());
lv.setLabelProvider(new FoodLabelProvider());
lv.setInput(new GroceryList());
// When user checks the checkbox, toggle the filter
filterHealthy.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
if (((Button) event.widget).getSelection())
lv.addFilter(filter);
else
lv.removeFilter(filter);
}
});
parent.pack();
return composite;
}
/**
* The application entry point
*
* @param args
* the command line arguments
*/
public static void main(String[] args) {
new FoodList().run();
}
}
/**
* This class filters only healthy items from the grocery list
*/
class HealthyFilter extends ViewerFilter {
/**
* Returns whether the specified element passes this filter
*
* @param arg0
* the viewer
* @param arg1
* the parent element
* @param arg2
* the element
* @return boolean
*/
public boolean select(Viewer arg0, Object arg1, Object arg2) {
return ((Food) arg2).isHealthy();
}
}
/**
* This class represents a type of food
*/
class Food {
// The name of the food
private String name;
// Is it healthy?
private boolean healthy;
/**
* Food constructor
*
* @param name
* the name
* @param healthy
* whether or not it"s healthy
*/
public Food(String name, boolean healthy) {
this.name = name;
this.healthy = healthy;
}
/**
* Gets whether this is healthy
*
* @return boolean
*/
public boolean isHealthy() {
return healthy;
}
/**
* Gets the name
*
* @return String
*/
public String getName() {
return name;
}
}
/**
* This class provides the labels for the FoodList application
*/
class FoodLabelProvider implements ILabelProvider {
/**
* ListViewers don"t support images
*
* @param arg0
* the element
* @return Image
*/
public Image getImage(Object arg0) {
return null;
}
/**
* Gets the text for an element
*
* @param arg0
* the element
* @return String
*/
public String getText(Object arg0) {
return ((Food) arg0).getName();
}
/**
* Adds a listener
*
* @param arg0
* the listener
*/
public void addListener(ILabelProviderListener arg0) {
// Throw it away
}
/**
* Disposes any resources
*/
public void dispose() {
// Nothing to dispose
}
/**
* Returns whether changing the specified property for the specified element
* affect the label
*
* @param arg0
* the element
* @param arg1
* the property
* @return boolean
*/
public boolean isLabelProperty(Object arg0, String arg1) {
return false;
}
/**
* Removes a listener
*
* @param arg0
* the listener
*/
public void removeListener(ILabelProviderListener arg0) {
// Ignore
}
}
/**
* This class contains all the foods on the "grocery list"
*/
class GroceryList {
// Holds the foods
private List foods;
/**
* Constructs a grocery list
*/
public GroceryList() {
foods = new ArrayList();
// Add some foods
foods.add(new Food("Broccoli", true));
foods.add(new Food("Bundt Cake", false));
foods.add(new Food("Cabbage", true));
foods.add(new Food("Candy Canes", false));
foods.add(new Food("Eggs", true));
foods.add(new Food("Potato Chips", false));
foods.add(new Food("Milk", true));
foods.add(new Food("Soda", false));
foods.add(new Food("Chicken", true));
foods.add(new Food("Cinnamon Rolls", false));
}
/**
* Returns the foods in this grocery list
*
* @return List
*/
public List getFoods() {
return Collections.unmodifiableList(foods);
}
}
/**
* This class provides the content for the FoodList application
*/
class FoodContentProvider implements IStructuredContentProvider {
/**
* Gets the food items for the list
*
* @param arg0
* the data model
* @return Object[]
*/
public Object[] getElements(Object arg0) {
return ((GroceryList) arg0).getFoods().toArray();
}
/**
* Disposes any created resources
*/
public void dispose() {
// Do nothing
}
/**
* Called when the input changes
*
* @param arg0
* the viewer
* @param arg1
* the old input
* @param arg2
* the new input
*/
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
// Do nothing
}
}
Sample ListViewer
/******************************************************************************
* All Right Reserved.
* Copyright (c) 1998, 2004 Jackwind Li Guojie
*
* Created on Feb 15, 2004 10:57:32 AM by JACK
* $Id$
*
*****************************************************************************/
import java.util.Iterator;
import java.util.Vector;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.viewers.ViewerSorter;
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.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class SampleListViewer {
Display display = new Display();
Shell shell = new Shell(display);
ListViewer listViewer;
/**
* Represents programming languages.
*
*/
public static class Language {
public String genre;
public boolean isObjectOriented;
public Language() { }
public Language(String genre, boolean isObjectOriented) {
this.genre = genre;
this.isObjectOriented = isObjectOriented;
}
public String toString() {
return "Lang: " + genre + " [" + (isObjectOriented ? "Object-oriented" : "Procedural") + "]";
}
}
Vector languages = new Vector();
private void init() {
languages.add(new Language("Java", true));
languages.add(new Language("C", false));
languages.add(new Language("C++", true));
languages.add(new Language("SmallTalk", true));
listViewer = new ListViewer(shell);
listViewer.setContentProvider(new IStructuredContentProvider() {
public Object[] getElements(Object inputElement) {
Vector v = (Vector)inputElement;
return v.toArray();
}
public void dispose() {
System.out.println("Disposing ...");
}
public void inputChanged(
Viewer viewer,
Object oldInput,
Object newInput) {
System.out.println("Input changed: old=" + oldInput + ", new=" + newInput);
}
});
//listViewer.setContentProvider(new ArrayContentProvider());
listViewer.setInput(languages);
listViewer.setLabelProvider(new LabelProvider() {
public Image getImage(Object element) {
return null;
}
public String getText(Object element) {
return ((Language)element).genre;
}
});
listViewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection selection = (IStructuredSelection)event.getSelection();
StringBuffer sb = new StringBuffer("Selection - ");
sb.append("tatal " + selection.size() + " items selected: ");
for(Iterator iterator = selection.iterator(); iterator.hasNext(); ) {
sb.append(iterator.next() + ", ");
}
System.out.println(sb);
}
});
listViewer.addFilter(new ViewerFilter() {
public boolean select(
Viewer viewer,
Object parentElement,
Object element) {
if(((Language)element).isObjectOriented)
return true;
else
return false;
}
});
listViewer.setSorter(new ViewerSorter(){
public int compare(Viewer viewer, Object e1, Object e2) {
return ((Language)e1).genre.rupareTo(((Language)e2).genre);
}
});
// Object[] toBeSelectedItems = new Object[2];
// toBeSelectedItems[0] = languages.elementAt(languages.size()-1);
// toBeSelectedItems[1] = languages.elementAt(languages.size()-2);
// IStructuredSelection selection = new StructuredSelection(toBeSelectedItems);
//
// listViewer.setSelection(selection);
//
// Vector v = new Vector();
// v.addElement(new Language("VB", true));
//
// listViewer.setInput(v);
//
// listViewer.add(new Language("C#", true));
}
Button buttonAdd;
Button buttonRemove;
Button buttonModify;
private void addButtons() {
Composite composite = new Composite(shell, SWT.NULL);
FillLayout fillLayout = new FillLayout(SWT.VERTICAL);
fillLayout.spacing = 2;
composite.setLayout(fillLayout);
buttonAdd = new Button(composite, SWT.PUSH);
buttonAdd.setText("Add");
buttonModify = new Button(composite, SWT.PUSH);
buttonModify.setText("Modify");
buttonRemove = new Button(composite, SWT.PUSH);
buttonRemove.setText("Remove");
buttonAdd.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// String text = OptionPane.showInputDialog(shell, "New language genre: ", "Add new element", null);
// if(text != null) {
// languages.add(new Language(text, true));
// }
listViewer.refresh(false);
}
});
buttonModify.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
IStructuredSelection selection = (IStructuredSelection)listViewer.getSelection();
Language language = (Language)selection.getFirstElement();
if(language == null) {
System.out.println("Please select a language first.");
return;
}
// String text = OptionPane.showInputDialog(shell, "Rename: ", "Modify genre", language.genre);
// if(text != null) {
// language.genre = text;
// }
listViewer.update(language, null);
}
});
buttonRemove.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
IStructuredSelection selection = (IStructuredSelection)listViewer.getSelection();
Language language = (Language)selection.getFirstElement();
if(language == null) {
System.out.println("Please select a language first.");
return;
}
languages.remove(language);
System.out.println("Removed: " + language);
listViewer.refresh(false);
}
});
}
public SampleListViewer() {
RowLayout rowLayout = new RowLayout();
shell.setLayout(rowLayout);
(new Label(shell, SWT.NULL)).setText("What programming languages are you proficient in? ");
init();
addButtons();
shell.pack();
shell.open();
//textUser.forceFocus();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new SampleListViewer();
}
}