A simple, productive path to effective
cross-platform interfaces
 A bunch of convenience classes for
building SWT applications
◦ ApplicationWindow
◦ Dialog
◦ Wizard
 A framework for displaying and editing
Java business model objects using SWT
◦ TableViewer
◦ TreeViewer
Introduction to Jface| By Rahul Shukla 2
 A bunch of convenience classes for
building SWT applications
◦ ApplicationWindow
◦ Dialog
◦ Wizard
 A framework for displaying and editing
Java business model objects using SWT
◦ TableViewer
◦ TreeViewer
Introduction to Jface| By Rahul Shukla 3
 Hello, JFace
public class HelloJFace extends ApplicationWindow {
public HelloJFace(Shell parentShell) {
super(parentShell);
setBlockOnOpen(true);
}
public static void main(String[] args) {
(new HelloJFace(null)).open();
}
}
Introduction to Jface| By Rahul Shukla 4
 Hello, JFace
Introduction to Jface| By Rahul Shukla 5
 Ways to add features to your JFace
application:
◦ Add code to constructor
◦ Override protected methods
◦ Add new methods or classes
Introduction to Jface| By Rahul Shukla 6
 Add menu bar, tool bar, status line
public class HelloJFace extends ApplicationWindow
{
public HelloJFace(Shell parentShell) {
super(parentShell);
setBlockOnOpen(true);
}
public static void main(String[] args) {
(new HelloJFace(null)).open();
}
}
Introduction to Jface| By Rahul Shukla 7
 Add menu bar, tool bar, status line
public class HelloJFace extends ApplicationWindow {
public HelloJFace(Shell parentShell) {
super(parentShell);
setBlockOnOpen(true);
addMenuBar();
addToolBar(SWT.FLAT);
addStatusLine();
}
public static void main(String[] args) {
(new HelloJFace(null)).open();
}
}
Introduction to Jface| By Rahul Shukla 8
 Add application title and icon
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText("Hello, JFace");
shell.setImage(
ImageDescriptor.createFromFile(
HelloJFace.class,
"icons/app.png").createImage());
}
Introduction to Jface| By Rahul Shukla 9
 Add menu to menu bar
protected MenuManager createMenuManager() {
MenuManager menuManager = new MenuManager();
menuManager.add(createFileMenu());
return menuManager;
}
private MenuManager createFileMenu() {
MenuManager menu = new MenuManager("&File");
menu.add(new Action() {
public String getText() {
return "E&xit";
}
public void run() {
getShell().close();
} });
return menu;
}
Introduction to Jface| By Rahul Shukla 10
 Add button to tool bar
protected ToolBarManager createToolBarManager(
int style)
{
ToolBarManager toolBar = new
ToolBarManager(style);
toolBar.add(new NewAction());
return toolBar;
}
Introduction to Jface| By Rahul Shukla 11
 Add private class NewAction for button
private class NewAction extends Action {
public String getText() { return "New"; }
public String getToolTipText() { return "New"; }
public ImageDescriptor getImageDescriptor() {
ImageDescriptor imageDesc =
ImageDescriptor.createFromFile(
HelloJFace.class, "icons/new.png");
return imageDesc;
}
public ImageDescriptor getHoverImageDescriptor() {
ImageDescriptor imageDesc =
ImageDescriptor.createFromFile(
HelloJFace.class, "icons/new-h.png");
return imageDesc;
}
public void run() { // add action code here
}
}
Introduction to Jface| By Rahul Shukla 12
 Suppose we really want to build a “To-do”
list...
Introduction to Jface| By Rahul Shukla 13
 A bunch of convenience classes for
building SWT applications
◦ ApplicationWindow
◦ Dialog
◦ Wizard
 A framework for displaying and editing
Java business model objects using SWT
◦ TableViewer
◦ TreeViewer
Introduction to Jface| By Rahul Shukla 14
 Steps to add to-do list editor to
application
◦ Create TodoList “model” objects
◦ Add TableViewer object to layout
◦ Initialize TableViewer
◦ Set TodoList object as the TableViewer's input
◦ Write (or reuse existing classes) to specify the
following TableViewer event handler objects
 ContentProvider, LabelProvider
 CellEditor, CellModifier
 RowSorter
Introduction to Jface| By Rahul Shukla 15
 JFace TableViewer usage
TableViewerTodoList
TreeMapTodo
“input”
Introduction to Jface| By Rahul Shukla 16
 JFace TableViewer usage
TableViewerTodoList
Todo
“input”
ContentProvider
LabelProvider
RowSorter
CellEditor
CellModifier
Introduction to Jface| By Rahul Shukla 17
● Steps to add to-do list editor to application
– Create TodoList “model” objects
– Add TableViewer object to layout
– Initialize TableViewer
– Set TodoList object as the TableViewer's input
– Write (or reuse existing classes) to specify the
following TableViewer event handler objects
● ContentProvider, LabelProvider
● CellEditor, CellModifier
● RowSorter
Introduction to Jface| By Rahul Shukla 18
 Create TodoList “model” object
public class TodoList implements Serializable {
public static List<Todo> list = new ArrayList<Todo>();
public static void addTodo(Todo todo) {
list.add(todo);
}
}
Introduction to Jface| By Rahul Shukla 19
 Steps to add to-do list editor to
application
◦ Create TodoList “model” object
◦ Add TableViewer object to layout
◦ Initialize TableViewer
◦ Set TodoList object as the TableViewer's input
◦ Write (or reuse existing classes) to specify the
following TableViewer event handler objects
 ContentProvider, LabelProvider
 CellEditor, CellModifier
 RowSorter
Introduction to Jface| By Rahul Shukla 20
 Add TableViewer object to layout
protected Control createContents(Composite parent)
{
return contents;
}
Introduction to Jface| By Rahul Shukla 21
 Add TableViewer object to layout
protected Control createContents(Composite parent) {
GridLayout layout = new GridLayout(1, false);
layout.marginHeight = 20;
layout.marginWidth = 20;
contents.setLayout(layout);
TableViewer viewer = new TableViewer(contents, SWT.BORDER |
SWT.H_SCROLL | SWT.V_SCROLL |
SWT.FULL_SELECTION);
viewer.getTable().setLayoutData(
new GridData(GridData.FILL_BOTH));
return contents;
}
Introduction to Jface| By Rahul Shukla 22
 Steps to add to-do list editor to
application
◦ Create TodoList “model” object
◦ Add TableViewer object to layout
◦ Initialize TableViewer
◦ Set TodoList object as the TableViewer's input
◦ Write (or reuse existing classes) to specify the
following TableViewer event handler objects
 ContentProvider, LabelProvider
 CellEditor, CellModifier
 RowSorter
Introduction to Jface| By Rahul Shukla 23
 Initialize TableViewer...
protected Control createContents(Composite parent) {
Composite contents = new Blotter(parent, SWT.NULL);
GridLayout layout = new GridLayout(1, false);
layout.marginHeight = 20;
layout.marginWidth = 20;
contents.setLayout(layout);
viewer = new TableViewer(contents, SWT.BORDER |
SWT.H_SCROLL
| SWT.V_SCROLL | SWT.FULL_SELECTION);
viewer.getTable().setLayoutData(new
GridData(GridData.FILL_BOTH));
init();
return contents;
} Introduction to Jface| By Rahul Shukla 24
 JFace TableViewer usage
TableViewerTodoList
TreeMapTodo
“input”
ContentProvider
LabelProvider
RowSorter
CellEditor
CellModifier
Introduction to Jface| By Rahul Shukla 25
 Implement addProvidersAndEditors()
private void addProvidersAndEditors() {
table.setContentProvider(
new ContentProvider());
table.setLabelProvider(new LabelProvider());
CellEditor[] editors = {
new CheckboxCellEditor(table.getTable()),
new TextCellEditor(table.getTable()),
new TextCellEditor(table.getTable())};
table.setCellEditors(editors);
table.setCellModifier(new CellModifier());
table.setSorter(new RowSorter());
table.setColumnProperties(colNames);
}
Introduction to Jface| By Rahul Shukla 27
 Steps to add to-do list editor to
application
◦ Create TodoList “model” object
◦ Add TableViewer object to layout
◦ Initialize TableViewer
◦ Set TodoList object as the TableViewer's input
◦ Write (or reuse existing classes) to specify the
following TableViewer event handler objects
 ContentProvider, LabelProvider
 CellEditor, CellModifier
 RowSorter
Introduction to Jface| By Rahul Shukla 28
 Set TodoList as the TableViewer's input
private void init() {
addColumns();
addProviders();
addEditors();
viewer.setInput(TodoList.list);
}
Introduction to Jface| By Rahul Shukla 29
 Set TodoList as the TableViewer's input
private void initTable() {
addColumns();
addProvidersAndEditors();
table.setInput(TodoList.theList);
}
private static final String[] colNames =
{ "Done", "Priority", "Description" };
private static final int[] colWeight =
{ 5, 5, 90 };
private static final int COL_DONE = 0;
private static final int COL_PRIORITY = 1;
private static final int COL_DESC = 2;
Introduction to Jface| By Rahul Shukla 30
 Steps to add to-do list editor to
application
◦ Create TodoList “model” object
◦ Add TableViewer object to layout
◦ Initialize TableViewer
◦ Set TodoList object as the TableViewer's input
◦ Write (or reuse existing classes) to specify the
following TableViewer event handler objects
 ContentProvider, LabelProvider
 CellEditor, CellModifier
 RowSorter
Introduction to Jface| By Rahul Shukla 31
 Implement addProvidersAndEditors()
private void addProviders() {
viewer.setContentProvider(new MyContentProvider());
viewer.setLabelProvider(new MyTableLabelProvider());
}
private void addEditors() {
Table table = viewer.getTable();
CellEditor[] editors = { new
CheckboxCellEditor(table),
new TextCellEditor(table), new TextCellEditor(table)
};
viewer.setCellEditors(editors);
viewer.setCellModifier(new CellModifier(columns,
this));
viewer.setColumnProperties(columns);Introduction to Jface| By Rahul Shukla 32
 JFace TableViewer usage
TableViewerTodoList
TreeMapTodo
“input”
ContentProvider
LabelProvider
RowSorter
CellEditor
CellModifier
Introduction to Jface| By Rahul Shukla 33
 A closer look at JFace “event handler
objects”
Event Handler When Triggered
ContentProvider On setInput(), refresh(), update()
LabelProvider A cell needs redrawing
CellEditor Edit cell value
CellModifier Fetch editable value / store value
RowSorter On table redraw
Introduction to Jface| By Rahul Shukla 34
 A closer look at JFace “event handler
objects”
Event Handler When Triggered
ContentProvider On setInput(), refresh(), update()
LabelProvider A cell needs redrawing
CellEditor Edit cell value
CellModifier Fetch editable value / store value
RowSorter On table redraw
Introduction to Jface| By Rahul Shukla 35
 Implementing ContentProvider
private class ContentProvider
implements IStructuredContentProvider
{
public Object[] getElements(
Object inputElement)
{
return ((TodoList)inputElement).toArray();
}
public void dispose() {}
public void inputChanged(Viewer viewer,
Object oldInput, Object newInput) {}
}
Introduction to Jface| By Rahul Shukla 36
 JFace TableViewer usage
TableViewerTodoList
TreeMapTodo
“input”
ContentProvider
LabelProvider
RowSorter
CellEditor
CellModifier
Introduction to Jface| By Rahul Shukla 37
 A closer look at JFace “event handler
objects”
Event Handler When Triggered
ContentProvider On setInput(), refresh(), update()
LabelProvider A cell needs redrawing
CellEditor Edit cell value
CellModifier Fetch editable value / store value
RowSorter On table redraw
Introduction to Jface| By Rahul Shukla 38
 Implementing LabelProvider
private class LabelProvider
implements ITableLabelProvider {
private Image done;
private Image notdone;
public LabelProvider() {
done = ImageDescriptor.createFromFile(
TodoListWindow.class,
"icons/task-done.png").createImage();
notdone = ImageDescriptor.createFromFile(
TodoListWindow.class,
"icons/task-open.png").createImage();
}
// <continued/>...
Introduction to Jface| By Rahul Shukla 39
 Implementing LabelProvider
public Image getColumnImage(Object element, int
columnIndex) {
Todo todo = (Todo) element;
switch (columnIndex) {
case 0:
if (todo.isDone())
return ImageDescriptor.createFromFile(HelloJFace.class,
"icons/done.gif").createImage();
else
return null;
default:
return null;
}
}
// <continued/>...
Introduction to Jface| By Rahul Shukla 40
 Implementing LabelProvider
public String getColumnText(Object element, int
columnIndex) {
Todo todo = (Todo) element;
switch (columnIndex) {
case 1:
return String.valueOf(todo.getPriority());
case 2:
return String.valueOf(todo.getDesc());
default:
return null;
}
} // <continued/>...
Introduction to Jface| By Rahul Shukla 41
 A closer look at JFace “event handler
objects”
Event Handler When Triggered
ContentProvider On setInput(), refresh(), update()
LabelProvider A cell needs redrawing
CellEditor Edit cell value
CellModifier Fetch editable value / store value
RowSorter On table redraw
Introduction to Jface| By Rahul Shukla 42
 JFace TableViewer usage
TableViewerTodoList
TreeMap
Todo
“input”
ContentProvider
LabelProvider
RowSorter
CellEditor
CellModifier
Introduction to Jface| By Rahul Shukla 43
 JFace TableViewer usage
TableViewerTodoList
Todo
“input”
ContentProvider
LabelProvider
RowSorter
CellEditor
CellModifier
The visible editor
Introduction to Jface| By Rahul Shukla 44
 JFace TableViewer usage
TableViewerTodoList
Todo
“input”
ContentProvider
LabelProvider
RowSorter
CellEditor
CellModifier
Translates between
visible editor's
data type and the
model's storage
data type
Introduction to Jface| By Rahul Shukla 45
 Implementing CellModifier
private class CellModifier
implements ICellModifier
{
public boolean canModify(Object element, String
property) {
return true;
}
// <continued/>...
Introduction to Jface| By Rahul Shukla 46
 Implementing CellModifier
public Object getValue(Object element, String property) {
Todo todo = (Todo) element;
if (property.equals(columns[0])) {
return new Boolean(todo.isDone());
} else if (property.equals(columns[1])) {
return Integer.toString(todo.getPriority());
} else if (property.equals(columns[2])) {
return todo.getDesc();
}
return null;
}
Introduction to Jface| By Rahul Shukla 47
 Implementing CellModifier
public void modify(Object element, String property, Object value) {
Item item = (Item) element;
Todo todo = (Todo) item.getData();
if (property.equals(columns[0])) {
boolean val = ((Boolean) value).booleanValue();
todo.setDone(val);
} else if (property.equals(columns[1])) {
int parseInt;
try {
parseInt = Integer.parseInt((String) value);
todo.setPriority(parseInt);
} catch (Exception e) {
MessageDialog.openError(window.getShell(), "Error", value
+ " is not a number");
}
} else if (property.equals(columns[2])) {
todo.setDesc((String) value);
}
window.refresh();
} Introduction to Jface| By Rahul Shukla 48
 A closer look at JFace “event handler
objects”
Event Handler When Triggered
ContentProvider On setInput(), refresh(), update()
LabelProvider A cell needs redrawing
CellEditor Edit cell value
CellModifier Fetch editable value / store value
RowSorter On table redraw
Introduction to Jface| By Rahul Shukla 49
 Implementing RowSorter
private class RowSorter extends ViewerSorter {
public int compare(Viewer viewer,
Object element1, Object element2) {
Todo t1 = (Todo) element1;
Todo t2 = (Todo) element2;
return t1.getPriority() -
t2.getPriority();
}
}
Introduction to Jface| By Rahul Shukla 50
 A closer look at JFace “event handler
objects”
Event Handler When Required
ContentProvider Always
LabelProvider Always
CellEditor If grid is not read-only
CellModifier If grid is not read-only
RowSorter If grid is sorted
Introduction to Jface| By Rahul Shukla 51
 So here's what we just built...
Introduction to Jface| By Rahul Shukla 52
Any questions?
Question................... ?

Jface

  • 1.
    A simple, productivepath to effective cross-platform interfaces
  • 2.
     A bunchof convenience classes for building SWT applications ◦ ApplicationWindow ◦ Dialog ◦ Wizard  A framework for displaying and editing Java business model objects using SWT ◦ TableViewer ◦ TreeViewer Introduction to Jface| By Rahul Shukla 2
  • 3.
     A bunchof convenience classes for building SWT applications ◦ ApplicationWindow ◦ Dialog ◦ Wizard  A framework for displaying and editing Java business model objects using SWT ◦ TableViewer ◦ TreeViewer Introduction to Jface| By Rahul Shukla 3
  • 4.
     Hello, JFace publicclass HelloJFace extends ApplicationWindow { public HelloJFace(Shell parentShell) { super(parentShell); setBlockOnOpen(true); } public static void main(String[] args) { (new HelloJFace(null)).open(); } } Introduction to Jface| By Rahul Shukla 4
  • 5.
     Hello, JFace Introductionto Jface| By Rahul Shukla 5
  • 6.
     Ways toadd features to your JFace application: ◦ Add code to constructor ◦ Override protected methods ◦ Add new methods or classes Introduction to Jface| By Rahul Shukla 6
  • 7.
     Add menubar, tool bar, status line public class HelloJFace extends ApplicationWindow { public HelloJFace(Shell parentShell) { super(parentShell); setBlockOnOpen(true); } public static void main(String[] args) { (new HelloJFace(null)).open(); } } Introduction to Jface| By Rahul Shukla 7
  • 8.
     Add menubar, tool bar, status line public class HelloJFace extends ApplicationWindow { public HelloJFace(Shell parentShell) { super(parentShell); setBlockOnOpen(true); addMenuBar(); addToolBar(SWT.FLAT); addStatusLine(); } public static void main(String[] args) { (new HelloJFace(null)).open(); } } Introduction to Jface| By Rahul Shukla 8
  • 9.
     Add applicationtitle and icon protected void configureShell(Shell shell) { super.configureShell(shell); shell.setText("Hello, JFace"); shell.setImage( ImageDescriptor.createFromFile( HelloJFace.class, "icons/app.png").createImage()); } Introduction to Jface| By Rahul Shukla 9
  • 10.
     Add menuto menu bar protected MenuManager createMenuManager() { MenuManager menuManager = new MenuManager(); menuManager.add(createFileMenu()); return menuManager; } private MenuManager createFileMenu() { MenuManager menu = new MenuManager("&File"); menu.add(new Action() { public String getText() { return "E&xit"; } public void run() { getShell().close(); } }); return menu; } Introduction to Jface| By Rahul Shukla 10
  • 11.
     Add buttonto tool bar protected ToolBarManager createToolBarManager( int style) { ToolBarManager toolBar = new ToolBarManager(style); toolBar.add(new NewAction()); return toolBar; } Introduction to Jface| By Rahul Shukla 11
  • 12.
     Add privateclass NewAction for button private class NewAction extends Action { public String getText() { return "New"; } public String getToolTipText() { return "New"; } public ImageDescriptor getImageDescriptor() { ImageDescriptor imageDesc = ImageDescriptor.createFromFile( HelloJFace.class, "icons/new.png"); return imageDesc; } public ImageDescriptor getHoverImageDescriptor() { ImageDescriptor imageDesc = ImageDescriptor.createFromFile( HelloJFace.class, "icons/new-h.png"); return imageDesc; } public void run() { // add action code here } } Introduction to Jface| By Rahul Shukla 12
  • 13.
     Suppose wereally want to build a “To-do” list... Introduction to Jface| By Rahul Shukla 13
  • 14.
     A bunchof convenience classes for building SWT applications ◦ ApplicationWindow ◦ Dialog ◦ Wizard  A framework for displaying and editing Java business model objects using SWT ◦ TableViewer ◦ TreeViewer Introduction to Jface| By Rahul Shukla 14
  • 15.
     Steps toadd to-do list editor to application ◦ Create TodoList “model” objects ◦ Add TableViewer object to layout ◦ Initialize TableViewer ◦ Set TodoList object as the TableViewer's input ◦ Write (or reuse existing classes) to specify the following TableViewer event handler objects  ContentProvider, LabelProvider  CellEditor, CellModifier  RowSorter Introduction to Jface| By Rahul Shukla 15
  • 16.
     JFace TableViewerusage TableViewerTodoList TreeMapTodo “input” Introduction to Jface| By Rahul Shukla 16
  • 17.
     JFace TableViewerusage TableViewerTodoList Todo “input” ContentProvider LabelProvider RowSorter CellEditor CellModifier Introduction to Jface| By Rahul Shukla 17
  • 18.
    ● Steps toadd to-do list editor to application – Create TodoList “model” objects – Add TableViewer object to layout – Initialize TableViewer – Set TodoList object as the TableViewer's input – Write (or reuse existing classes) to specify the following TableViewer event handler objects ● ContentProvider, LabelProvider ● CellEditor, CellModifier ● RowSorter Introduction to Jface| By Rahul Shukla 18
  • 19.
     Create TodoList“model” object public class TodoList implements Serializable { public static List<Todo> list = new ArrayList<Todo>(); public static void addTodo(Todo todo) { list.add(todo); } } Introduction to Jface| By Rahul Shukla 19
  • 20.
     Steps toadd to-do list editor to application ◦ Create TodoList “model” object ◦ Add TableViewer object to layout ◦ Initialize TableViewer ◦ Set TodoList object as the TableViewer's input ◦ Write (or reuse existing classes) to specify the following TableViewer event handler objects  ContentProvider, LabelProvider  CellEditor, CellModifier  RowSorter Introduction to Jface| By Rahul Shukla 20
  • 21.
     Add TableViewerobject to layout protected Control createContents(Composite parent) { return contents; } Introduction to Jface| By Rahul Shukla 21
  • 22.
     Add TableViewerobject to layout protected Control createContents(Composite parent) { GridLayout layout = new GridLayout(1, false); layout.marginHeight = 20; layout.marginWidth = 20; contents.setLayout(layout); TableViewer viewer = new TableViewer(contents, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION); viewer.getTable().setLayoutData( new GridData(GridData.FILL_BOTH)); return contents; } Introduction to Jface| By Rahul Shukla 22
  • 23.
     Steps toadd to-do list editor to application ◦ Create TodoList “model” object ◦ Add TableViewer object to layout ◦ Initialize TableViewer ◦ Set TodoList object as the TableViewer's input ◦ Write (or reuse existing classes) to specify the following TableViewer event handler objects  ContentProvider, LabelProvider  CellEditor, CellModifier  RowSorter Introduction to Jface| By Rahul Shukla 23
  • 24.
     Initialize TableViewer... protectedControl createContents(Composite parent) { Composite contents = new Blotter(parent, SWT.NULL); GridLayout layout = new GridLayout(1, false); layout.marginHeight = 20; layout.marginWidth = 20; contents.setLayout(layout); viewer = new TableViewer(contents, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION); viewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH)); init(); return contents; } Introduction to Jface| By Rahul Shukla 24
  • 25.
     JFace TableViewerusage TableViewerTodoList TreeMapTodo “input” ContentProvider LabelProvider RowSorter CellEditor CellModifier Introduction to Jface| By Rahul Shukla 25
  • 26.
     Implement addProvidersAndEditors() privatevoid addProvidersAndEditors() { table.setContentProvider( new ContentProvider()); table.setLabelProvider(new LabelProvider()); CellEditor[] editors = { new CheckboxCellEditor(table.getTable()), new TextCellEditor(table.getTable()), new TextCellEditor(table.getTable())}; table.setCellEditors(editors); table.setCellModifier(new CellModifier()); table.setSorter(new RowSorter()); table.setColumnProperties(colNames); } Introduction to Jface| By Rahul Shukla 27
  • 27.
     Steps toadd to-do list editor to application ◦ Create TodoList “model” object ◦ Add TableViewer object to layout ◦ Initialize TableViewer ◦ Set TodoList object as the TableViewer's input ◦ Write (or reuse existing classes) to specify the following TableViewer event handler objects  ContentProvider, LabelProvider  CellEditor, CellModifier  RowSorter Introduction to Jface| By Rahul Shukla 28
  • 28.
     Set TodoListas the TableViewer's input private void init() { addColumns(); addProviders(); addEditors(); viewer.setInput(TodoList.list); } Introduction to Jface| By Rahul Shukla 29
  • 29.
     Set TodoListas the TableViewer's input private void initTable() { addColumns(); addProvidersAndEditors(); table.setInput(TodoList.theList); } private static final String[] colNames = { "Done", "Priority", "Description" }; private static final int[] colWeight = { 5, 5, 90 }; private static final int COL_DONE = 0; private static final int COL_PRIORITY = 1; private static final int COL_DESC = 2; Introduction to Jface| By Rahul Shukla 30
  • 30.
     Steps toadd to-do list editor to application ◦ Create TodoList “model” object ◦ Add TableViewer object to layout ◦ Initialize TableViewer ◦ Set TodoList object as the TableViewer's input ◦ Write (or reuse existing classes) to specify the following TableViewer event handler objects  ContentProvider, LabelProvider  CellEditor, CellModifier  RowSorter Introduction to Jface| By Rahul Shukla 31
  • 31.
     Implement addProvidersAndEditors() privatevoid addProviders() { viewer.setContentProvider(new MyContentProvider()); viewer.setLabelProvider(new MyTableLabelProvider()); } private void addEditors() { Table table = viewer.getTable(); CellEditor[] editors = { new CheckboxCellEditor(table), new TextCellEditor(table), new TextCellEditor(table) }; viewer.setCellEditors(editors); viewer.setCellModifier(new CellModifier(columns, this)); viewer.setColumnProperties(columns);Introduction to Jface| By Rahul Shukla 32
  • 32.
     JFace TableViewerusage TableViewerTodoList TreeMapTodo “input” ContentProvider LabelProvider RowSorter CellEditor CellModifier Introduction to Jface| By Rahul Shukla 33
  • 33.
     A closerlook at JFace “event handler objects” Event Handler When Triggered ContentProvider On setInput(), refresh(), update() LabelProvider A cell needs redrawing CellEditor Edit cell value CellModifier Fetch editable value / store value RowSorter On table redraw Introduction to Jface| By Rahul Shukla 34
  • 34.
     A closerlook at JFace “event handler objects” Event Handler When Triggered ContentProvider On setInput(), refresh(), update() LabelProvider A cell needs redrawing CellEditor Edit cell value CellModifier Fetch editable value / store value RowSorter On table redraw Introduction to Jface| By Rahul Shukla 35
  • 35.
     Implementing ContentProvider privateclass ContentProvider implements IStructuredContentProvider { public Object[] getElements( Object inputElement) { return ((TodoList)inputElement).toArray(); } public void dispose() {} public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {} } Introduction to Jface| By Rahul Shukla 36
  • 36.
     JFace TableViewerusage TableViewerTodoList TreeMapTodo “input” ContentProvider LabelProvider RowSorter CellEditor CellModifier Introduction to Jface| By Rahul Shukla 37
  • 37.
     A closerlook at JFace “event handler objects” Event Handler When Triggered ContentProvider On setInput(), refresh(), update() LabelProvider A cell needs redrawing CellEditor Edit cell value CellModifier Fetch editable value / store value RowSorter On table redraw Introduction to Jface| By Rahul Shukla 38
  • 38.
     Implementing LabelProvider privateclass LabelProvider implements ITableLabelProvider { private Image done; private Image notdone; public LabelProvider() { done = ImageDescriptor.createFromFile( TodoListWindow.class, "icons/task-done.png").createImage(); notdone = ImageDescriptor.createFromFile( TodoListWindow.class, "icons/task-open.png").createImage(); } // <continued/>... Introduction to Jface| By Rahul Shukla 39
  • 39.
     Implementing LabelProvider publicImage getColumnImage(Object element, int columnIndex) { Todo todo = (Todo) element; switch (columnIndex) { case 0: if (todo.isDone()) return ImageDescriptor.createFromFile(HelloJFace.class, "icons/done.gif").createImage(); else return null; default: return null; } } // <continued/>... Introduction to Jface| By Rahul Shukla 40
  • 40.
     Implementing LabelProvider publicString getColumnText(Object element, int columnIndex) { Todo todo = (Todo) element; switch (columnIndex) { case 1: return String.valueOf(todo.getPriority()); case 2: return String.valueOf(todo.getDesc()); default: return null; } } // <continued/>... Introduction to Jface| By Rahul Shukla 41
  • 41.
     A closerlook at JFace “event handler objects” Event Handler When Triggered ContentProvider On setInput(), refresh(), update() LabelProvider A cell needs redrawing CellEditor Edit cell value CellModifier Fetch editable value / store value RowSorter On table redraw Introduction to Jface| By Rahul Shukla 42
  • 42.
     JFace TableViewerusage TableViewerTodoList TreeMap Todo “input” ContentProvider LabelProvider RowSorter CellEditor CellModifier Introduction to Jface| By Rahul Shukla 43
  • 43.
     JFace TableViewerusage TableViewerTodoList Todo “input” ContentProvider LabelProvider RowSorter CellEditor CellModifier The visible editor Introduction to Jface| By Rahul Shukla 44
  • 44.
     JFace TableViewerusage TableViewerTodoList Todo “input” ContentProvider LabelProvider RowSorter CellEditor CellModifier Translates between visible editor's data type and the model's storage data type Introduction to Jface| By Rahul Shukla 45
  • 45.
     Implementing CellModifier privateclass CellModifier implements ICellModifier { public boolean canModify(Object element, String property) { return true; } // <continued/>... Introduction to Jface| By Rahul Shukla 46
  • 46.
     Implementing CellModifier publicObject getValue(Object element, String property) { Todo todo = (Todo) element; if (property.equals(columns[0])) { return new Boolean(todo.isDone()); } else if (property.equals(columns[1])) { return Integer.toString(todo.getPriority()); } else if (property.equals(columns[2])) { return todo.getDesc(); } return null; } Introduction to Jface| By Rahul Shukla 47
  • 47.
     Implementing CellModifier publicvoid modify(Object element, String property, Object value) { Item item = (Item) element; Todo todo = (Todo) item.getData(); if (property.equals(columns[0])) { boolean val = ((Boolean) value).booleanValue(); todo.setDone(val); } else if (property.equals(columns[1])) { int parseInt; try { parseInt = Integer.parseInt((String) value); todo.setPriority(parseInt); } catch (Exception e) { MessageDialog.openError(window.getShell(), "Error", value + " is not a number"); } } else if (property.equals(columns[2])) { todo.setDesc((String) value); } window.refresh(); } Introduction to Jface| By Rahul Shukla 48
  • 48.
     A closerlook at JFace “event handler objects” Event Handler When Triggered ContentProvider On setInput(), refresh(), update() LabelProvider A cell needs redrawing CellEditor Edit cell value CellModifier Fetch editable value / store value RowSorter On table redraw Introduction to Jface| By Rahul Shukla 49
  • 49.
     Implementing RowSorter privateclass RowSorter extends ViewerSorter { public int compare(Viewer viewer, Object element1, Object element2) { Todo t1 = (Todo) element1; Todo t2 = (Todo) element2; return t1.getPriority() - t2.getPriority(); } } Introduction to Jface| By Rahul Shukla 50
  • 50.
     A closerlook at JFace “event handler objects” Event Handler When Required ContentProvider Always LabelProvider Always CellEditor If grid is not read-only CellModifier If grid is not read-only RowSorter If grid is sorted Introduction to Jface| By Rahul Shukla 51
  • 51.
     So here'swhat we just built... Introduction to Jface| By Rahul Shukla 52
  • 52.