jface

52
A simple, productive path to effective cross-platform interfaces

Upload: rahul-shukla

Post on 15-Aug-2015

34 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Jface

A simple, productive path to effective

cross-platform interfaces

Page 2: Jface

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

Page 3: Jface

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

Page 4: Jface

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

Page 5: Jface

Hello, JFace

Introduction to Jface| By Rahul Shukla 5

Page 6: Jface

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

Page 7: Jface

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

Page 8: Jface

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

Page 9: Jface

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

Page 10: Jface

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

Page 11: Jface

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

Page 12: Jface

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

Page 13: Jface

Suppose we really want to build a “To-do” list...

Introduction to Jface| By Rahul Shukla 13

Page 14: Jface

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

Page 15: Jface

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

Page 16: Jface

JFace TableViewer usage

TableViewer TodoList

TreeMap Todo

“input”

Introduction to Jface| By Rahul Shukla 16

Page 17: Jface

JFace TableViewer usage

TableViewer TodoList

Todo

“input”

ContentProvider

LabelProvider

RowSorter

CellEditor

CellModifier

Introduction to Jface| By Rahul Shukla 17

Page 18: Jface

● 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

Page 19: Jface

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

Page 20: Jface

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

Page 21: Jface

Add TableViewer object to layout

protected Control createContents(Composite parent)

{

return contents;

}

Introduction to Jface| By Rahul Shukla 21

Page 22: Jface

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

Page 23: Jface

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

Page 24: Jface

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

Page 25: Jface

JFace TableViewer usage

TableViewer TodoList

TreeMap Todo

“input”

ContentProvider

LabelProvider

RowSorter

CellEditor

CellModifier

Introduction to Jface| By Rahul Shukla 25

Page 26: Jface

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

Page 27: Jface

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

Page 28: Jface

Set TodoList as the TableViewer's input

private void init() { addColumns(); addProviders(); addEditors(); viewer.setInput(TodoList.list); }

Introduction to Jface| By Rahul Shukla 29

Page 29: Jface

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

Page 30: Jface

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

Page 31: Jface

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

Page 32: Jface

JFace TableViewer usage

TableViewer TodoList

TreeMap Todo

“input”

ContentProvider

LabelProvider

RowSorter

CellEditor

CellModifier

Introduction to Jface| By Rahul Shukla 33

Page 33: Jface

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

Page 34: Jface

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

Page 35: Jface

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

Page 36: Jface

JFace TableViewer usage

TableViewer TodoList

TreeMap Todo

“input”

ContentProvider

LabelProvider

RowSorter

CellEditor

CellModifier

Introduction to Jface| By Rahul Shukla 37

Page 37: Jface

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

Page 38: Jface

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

Page 39: Jface

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

Page 40: Jface

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

Page 41: Jface

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

Page 42: Jface

JFace TableViewer usage

TableViewer TodoList

TreeMap

Todo

“input”

ContentProvider

LabelProvider

RowSorter

CellEditor

CellModifier

Introduction to Jface| By Rahul Shukla 43

Page 43: Jface

JFace TableViewer usage

TableViewer TodoList

Todo

“input”

ContentProvider

LabelProvider

RowSorter

CellEditor

CellModifier

The visible editor

Introduction to Jface| By Rahul Shukla 44

Page 44: Jface

JFace TableViewer usage

TableViewer TodoList

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

Page 45: Jface

Implementing CellModifier

private class CellModifier

implements ICellModifier

{

public boolean canModify(Object element, String property) { return true; } // <continued/>...

Introduction to Jface| By Rahul Shukla 46

Page 46: Jface

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

Page 47: Jface

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

Page 48: Jface

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

Page 49: Jface

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

Page 50: Jface

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

Page 51: Jface

So here's what we just built...

Introduction to Jface| By Rahul Shukla 52

Page 52: Jface

Any questions?

Question................... ?