SlideShare a Scribd company logo
1 of 52
Download to read offline
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................... ?

More Related Content

What's hot

Quick start with React | DreamLab Academy #2
Quick start with React | DreamLab Academy #2Quick start with React | DreamLab Academy #2
Quick start with React | DreamLab Academy #2DreamLab
 
Aggregation and Awareness or How to Reduce the Amount of your FrontEnd Code ...
Aggregation and Awareness or How to Reduce the Amount of  your FrontEnd Code ...Aggregation and Awareness or How to Reduce the Amount of  your FrontEnd Code ...
Aggregation and Awareness or How to Reduce the Amount of your FrontEnd Code ...ISS Art, LLC
 
Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3 Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3 DreamLab
 
Automated%20testing%20with%20Espresso2.x
Automated%20testing%20with%20Espresso2.xAutomated%20testing%20with%20Espresso2.x
Automated%20testing%20with%20Espresso2.xTatsuya Maki
 
React table tutorial project setup, use table, and usefilter
React table tutorial project setup, use table, and usefilterReact table tutorial project setup, use table, and usefilter
React table tutorial project setup, use table, and usefilterKaty Slemon
 
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...Naresha K
 
A (very) opinionated guide to MSBuild and Project Files
A (very) opinionated guide to MSBuild and Project FilesA (very) opinionated guide to MSBuild and Project Files
A (very) opinionated guide to MSBuild and Project FilesDavid Wengier
 
Your IDE Deserves Better
Your IDE Deserves BetterYour IDE Deserves Better
Your IDE Deserves BetterBoris Litvinsky
 
The secret unit testing tools no one has ever told you about
The secret unit testing tools no one has ever told you aboutThe secret unit testing tools no one has ever told you about
The secret unit testing tools no one has ever told you aboutDror Helper
 
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...탑크리에듀(구로디지털단지역3번출구 2분거리)
 
How to implement g rpc services in nodejs
How to implement g rpc services in nodejsHow to implement g rpc services in nodejs
How to implement g rpc services in nodejsKaty Slemon
 
Testing Android apps based on Dagger and RxJava Droidcon UK
Testing Android apps based on Dagger and RxJava Droidcon UKTesting Android apps based on Dagger and RxJava Droidcon UK
Testing Android apps based on Dagger and RxJava Droidcon UKFabio Collini
 
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo Ali Parmaksiz
 
Lowering in C#: What really happens with your code?, from NDC Oslo 2019
Lowering in C#: What really happens with your code?, from NDC Oslo 2019Lowering in C#: What really happens with your code?, from NDC Oslo 2019
Lowering in C#: What really happens with your code?, from NDC Oslo 2019David Wengier
 
Java8 tgtbatu javaone
Java8 tgtbatu javaoneJava8 tgtbatu javaone
Java8 tgtbatu javaoneBrian Vermeer
 

What's hot (20)

Quick start with React | DreamLab Academy #2
Quick start with React | DreamLab Academy #2Quick start with React | DreamLab Academy #2
Quick start with React | DreamLab Academy #2
 
Aggregation and Awareness or How to Reduce the Amount of your FrontEnd Code ...
Aggregation and Awareness or How to Reduce the Amount of  your FrontEnd Code ...Aggregation and Awareness or How to Reduce the Amount of  your FrontEnd Code ...
Aggregation and Awareness or How to Reduce the Amount of your FrontEnd Code ...
 
React lecture
React lectureReact lecture
React lecture
 
Mpg Dec07 Gian Lorenzetto
Mpg Dec07 Gian Lorenzetto Mpg Dec07 Gian Lorenzetto
Mpg Dec07 Gian Lorenzetto
 
Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3 Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3
 
Automated%20testing%20with%20Espresso2.x
Automated%20testing%20with%20Espresso2.xAutomated%20testing%20with%20Espresso2.x
Automated%20testing%20with%20Espresso2.x
 
JS and patterns
JS and patternsJS and patterns
JS and patterns
 
React table tutorial project setup, use table, and usefilter
React table tutorial project setup, use table, and usefilterReact table tutorial project setup, use table, and usefilter
React table tutorial project setup, use table, and usefilter
 
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
 
guice-servlet
guice-servletguice-servlet
guice-servlet
 
A (very) opinionated guide to MSBuild and Project Files
A (very) opinionated guide to MSBuild and Project FilesA (very) opinionated guide to MSBuild and Project Files
A (very) opinionated guide to MSBuild and Project Files
 
Your IDE Deserves Better
Your IDE Deserves BetterYour IDE Deserves Better
Your IDE Deserves Better
 
The secret unit testing tools no one has ever told you about
The secret unit testing tools no one has ever told you aboutThe secret unit testing tools no one has ever told you about
The secret unit testing tools no one has ever told you about
 
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
 
How to implement g rpc services in nodejs
How to implement g rpc services in nodejsHow to implement g rpc services in nodejs
How to implement g rpc services in nodejs
 
Testing Android apps based on Dagger and RxJava Droidcon UK
Testing Android apps based on Dagger and RxJava Droidcon UKTesting Android apps based on Dagger and RxJava Droidcon UK
Testing Android apps based on Dagger and RxJava Droidcon UK
 
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
 
Lowering in C#: What really happens with your code?, from NDC Oslo 2019
Lowering in C#: What really happens with your code?, from NDC Oslo 2019Lowering in C#: What really happens with your code?, from NDC Oslo 2019
Lowering in C#: What really happens with your code?, from NDC Oslo 2019
 
Firebase ng2 zurich
Firebase ng2 zurichFirebase ng2 zurich
Firebase ng2 zurich
 
Java8 tgtbatu javaone
Java8 tgtbatu javaoneJava8 tgtbatu javaone
Java8 tgtbatu javaone
 

Similar to Jface

Android Design Patterns
Android Design PatternsAndroid Design Patterns
Android Design PatternsGodfrey Nolan
 
Connect.js - Exploring React.Native
Connect.js - Exploring React.NativeConnect.js - Exploring React.Native
Connect.js - Exploring React.Nativejoshcjensen
 
Creating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfCreating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfShaiAlmog1
 
Building user interface with react
Building user interface with reactBuilding user interface with react
Building user interface with reactAmit Thakkar
 
I am looking for some assistance with SQLite database. I have tried se.pdf
I am looking for some assistance with SQLite database. I have tried se.pdfI am looking for some assistance with SQLite database. I have tried se.pdf
I am looking for some assistance with SQLite database. I have tried se.pdfConint29
 
Create methods to_insert
Create methods to_insertCreate methods to_insert
Create methods to_insertNayanBakhadyo
 
Stay with React.js in 2020
Stay with React.js in 2020Stay with React.js in 2020
Stay with React.js in 2020Jerry Liao
 
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdfSet up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdfxlynettalampleyxc
 
Reactive.architecture.with.Angular
Reactive.architecture.with.AngularReactive.architecture.with.Angular
Reactive.architecture.with.AngularEvan Schultz
 
Introduction_To_SWT_Rahul_Shukla
Introduction_To_SWT_Rahul_ShuklaIntroduction_To_SWT_Rahul_Shukla
Introduction_To_SWT_Rahul_ShuklaRahul Shukla
 
Postgresql function_basics.pdf
Postgresql function_basics.pdfPostgresql function_basics.pdf
Postgresql function_basics.pdfsvhtayrv
 

Similar to Jface (20)

Android Design Patterns
Android Design PatternsAndroid Design Patterns
Android Design Patterns
 
To-Do App With Flutter: Step By Step Guide
To-Do App With Flutter: Step By Step GuideTo-Do App With Flutter: Step By Step Guide
To-Do App With Flutter: Step By Step Guide
 
CSharp v1.0.2
CSharp v1.0.2CSharp v1.0.2
CSharp v1.0.2
 
React outbox
React outboxReact outbox
React outbox
 
Connect.js - Exploring React.Native
Connect.js - Exploring React.NativeConnect.js - Exploring React.Native
Connect.js - Exploring React.Native
 
Creating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfCreating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdf
 
Building user interface with react
Building user interface with reactBuilding user interface with react
Building user interface with react
 
I am looking for some assistance with SQLite database. I have tried se.pdf
I am looking for some assistance with SQLite database. I have tried se.pdfI am looking for some assistance with SQLite database. I have tried se.pdf
I am looking for some assistance with SQLite database. I have tried se.pdf
 
Create methods to_insert
Create methods to_insertCreate methods to_insert
Create methods to_insert
 
Stay with React.js in 2020
Stay with React.js in 2020Stay with React.js in 2020
Stay with React.js in 2020
 
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdfSet up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
 
Reactive.architecture.with.Angular
Reactive.architecture.with.AngularReactive.architecture.with.Angular
Reactive.architecture.with.Angular
 
React js
React jsReact js
React js
 
Scala on Your Phone
Scala on Your PhoneScala on Your Phone
Scala on Your Phone
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
Session 2- day 3
Session 2- day 3Session 2- day 3
Session 2- day 3
 
Android crashcourse
Android crashcourseAndroid crashcourse
Android crashcourse
 
Introduction_To_SWT_Rahul_Shukla
Introduction_To_SWT_Rahul_ShuklaIntroduction_To_SWT_Rahul_Shukla
Introduction_To_SWT_Rahul_Shukla
 
Gwt and Xtend
Gwt and XtendGwt and Xtend
Gwt and Xtend
 
Postgresql function_basics.pdf
Postgresql function_basics.pdfPostgresql function_basics.pdf
Postgresql function_basics.pdf
 

Jface

  • 1. A simple, productive path to effective cross-platform interfaces
  • 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 2
  • 3.  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
  • 4.  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
  • 5.  Hello, JFace Introduction to Jface| By Rahul Shukla 5
  • 6.  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
  • 7.  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
  • 8.  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
  • 9.  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
  • 10.  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
  • 11.  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
  • 12.  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
  • 13.  Suppose we really want to build a “To-do” list... Introduction to Jface| By Rahul Shukla 13
  • 14.  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
  • 15.  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
  • 16.  JFace TableViewer usage TableViewerTodoList TreeMapTodo “input” Introduction to Jface| By Rahul Shukla 16
  • 17.  JFace TableViewer usage TableViewerTodoList Todo “input” ContentProvider LabelProvider RowSorter CellEditor CellModifier Introduction to Jface| By Rahul Shukla 17
  • 18. ● 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
  • 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 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
  • 21.  Add TableViewer object to layout protected Control createContents(Composite parent) { return contents; } Introduction to Jface| By Rahul Shukla 21
  • 22.  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
  • 23.  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
  • 24.  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
  • 25.  JFace TableViewer usage TableViewerTodoList TreeMapTodo “input” ContentProvider LabelProvider RowSorter CellEditor CellModifier Introduction to Jface| By Rahul Shukla 25
  • 26.  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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 32.  JFace TableViewer usage TableViewerTodoList TreeMapTodo “input” ContentProvider LabelProvider RowSorter CellEditor CellModifier Introduction to Jface| By Rahul Shukla 33
  • 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
  • 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
  • 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
  • 36.  JFace TableViewer usage TableViewerTodoList TreeMapTodo “input” ContentProvider LabelProvider RowSorter CellEditor CellModifier Introduction to Jface| By Rahul Shukla 37
  • 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
  • 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
  • 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
  • 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
  • 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
  • 42.  JFace TableViewer usage TableViewerTodoList TreeMap Todo “input” ContentProvider LabelProvider RowSorter CellEditor CellModifier Introduction to Jface| By Rahul Shukla 43
  • 43.  JFace TableViewer usage TableViewerTodoList Todo “input” ContentProvider LabelProvider RowSorter CellEditor CellModifier The visible editor Introduction to Jface| By Rahul Shukla 44
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 51.  So here's what we just built... Introduction to Jface| By Rahul Shukla 52