Top Banner
SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces
108

SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

SWT, The Standard Widget Toolkit

A simple, productive path to effective cross-platform interfaces

Page 2: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

Objectives

● Cut through the hype and explain what SWT really is

● Describe SWT's advantages and disadvantages

● Explain SWT's technical relationship to Swing

● Provide a taste of coding with SWT● List where to find additional SWT resources

Page 3: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

Assumptions

● You feel comfortable with Java● You have prior experience programming

using Swing, Visual Basic, Delphi, C++ Builder, or a similar visual programming environment

Page 4: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

What is SWT?

● Strict technical● Common usage

Page 5: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

What is SWT?

● Technical description– org.eclipse.swt.*– A portable toolkit providing access to the native

platform's graphical resources

Page 6: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

What is SWT?

● Implementation strategy– A thin layer of Java objects over the native

operating system's “objects”– Minimum abstraction required to be portable

Page 7: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

What is SWT?

● Common Usage:– The GUI toolkit used to create:

● Eclipse plug-ins● Standalone applications based on Eclipse technology● New SWT controls

– This definition includes Eclipse technologies in addition to SWT

Page 8: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

What is SWT (common usage)?

● Related technologies– JFace – Swing-like MVC layer built on top of

SWT plus other helpful user interface classes

Page 9: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

What is SWT (common usage)?

● Related technologies– Java2d – An object-based drawing toolkit built on

top of SWT– GEF – A toolkit built on top of Java2d for

creating GUI builders, HTML editors, UML diagrammers, object-based drawing editors, etc.

Page 10: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

What is SWT (common usage)?

● Related technologies– Sweet – A component layer on top of SWT

similar to Active-X or VCL for the Win32 API● JavaBeans for SWT● http://sweet-swt.sourceforge.net

Page 11: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

What is SWT (common usage)?

● Related technologies– All of the Eclipse framework, when used as an

application framework for custom applications and not as an IDE

Page 12: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

SWT Advantages

● Native Look & Feel – almost for free● Lightweight and simple● “Familiarly” Fast● Open source

Page 13: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

SWT Advantages – Win2k

Used by permission from: http://gallery.livemedia.com.au/

Page 14: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

SWT Advantages – Win2k

Page 15: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

SWT Advantages – WinXP + theme

Used by permission from: http://gallery.livemedia.com.au/

Page 16: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

SWT Advantages – GTK + theme

Used by permission from: http://gallery.livemedia.com.au/

Page 17: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

SWT Advantages – GTK + theme

Used by permission from: http://gallery.livemedia.com.au/

Page 18: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

SWT Advantages – GTK + theme

Used by permission from: http://gallery.livemedia.com.au/

Page 19: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

SWT Advantages – MacOS X

Used by permission from: http://gallery.livemedia.com.au/

Page 20: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

SWT Disadvantages

● Lightweight● Immature compared to Swing● A few fewer platforms supported

Page 21: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

How does SWT relate to Swing?

● Swing's main strengths– Consistent look & feel across all platforms;

attractive themes are available.– Sun-supported

– (What else would you add?)

Page 22: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

How does SWT relate to Swing?

● SWT's main strengths– Native look and feel, because it is native.– Open source support

Page 23: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

How does SWT relate to Swing?

● Swing's main strengths– Consistent look & feel across all platforms;

attractive themes are available.– Sun-supported.

● SWT's main strengths– Native look and feel, because it is native.– Open source support.

These are complementary, not competitive strengths

Page 24: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

Why SWT?

● Web Services shift the emphasis on the client away from the web browser, back toward traditional GUI applications

● SWT strengthens Java's position in this market

● SWT provides features that compliment, rather than replace, Swing

Page 25: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT

● Hello, SWTpublic class HelloSWT {

public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch())

display.sleep(); } display.dispose();

}}

Page 26: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT

● Hello, SWT

Page 27: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT

● A simple SWT controlpublic class HelloSWTControl extends Canvas implements PaintListener { public HelloSWTControl(Composite parent, int style) { super(parent, style); addPaintListener(this); }

public void paintControl(PaintEvent e) { e.gc.drawText("Hello, world", 5, 5); }}

Page 28: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT

● A closer look at the constructorpublic class HelloSWTControl extends Canvas implements PaintListener { public HelloSWTControl(Composite parent, int style) { super(parent, style); addPaintListener(this); }

public void paintControl(PaintEvent e) { e.gc.drawText("Hello, world", 5, 5); }}

Page 29: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT

● SWT constructors have 2 arguments:– The parent control

● SWT controls are added to their parents during construction, not by using an add( ) method

– Style bits● Some things you're used to seeing as properties of an

object show up as style bits on the constructor● This is the way the underlying O/S toolkits actually

work– (SWT just wraps the underlying O/S's objects)

Page 30: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT

● Displaying the SWT controlpublic class HelloSWT {

public static void main(String[] args) { Display display = new Display();

Shell shell = new Shell(display); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch())

display.sleep(); } display.dispose();

}}

Page 31: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT

● Displaying the SWT controlpublic class HelloSWT {

public static void main(String[] args) { Display display = new Display();

Shell shell = new Shell(display); shell.setLayout(new FillLayout()); new HelloSWTControl(shell, SWT.NULL); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch())

display.sleep(); } display.dispose();

}}

Page 32: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT

● Displaying the SWT control

Page 33: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT

● Managing graphical resources: a more complex example

Page 34: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT

● Managing graphical resourcespublic class Blotter extends Canvas implements PaintListener { public Blotter(Composite parent, int style) { super(parent, style); addPaintListener(this); }

public void paintControl(PaintEvent e) { // Put our drawing code here }}

Page 35: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT

● Managing graphical resourcespublic Blotter(Composite parent, int style) { super(parent, style); addPaintListener(this);}

Page 36: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT

● Managing graphical resourcespublic Blotter(Composite parent, int style) { super(parent, style); addPaintListener(this);

// Set a default background color display = Display.getCurrent();

Color background = display.getSystemColor( SWT.COLOR_DARK_GREEN);

setBackground(background);}

private Display display;

Page 37: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT

● Managing graphical resourcespublic class Blotter extends Canvas implements PaintListener { public Blotter(Composite parent, int style) { super(parent, style); addPaintListener(this); }

public void paintControl(PaintEvent e) { // Put our drawing code here }}

Page 38: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT

● Managing graphical resourcespublic void paintControl(PaintEvent e) { // Put our drawing code here}

Page 39: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT

● Managing graphical resourcespublic void paintControl(PaintEvent e) { GC gc = e.gc; Color black = display.getSystemColor(SWT.COLOR_BLACK); Color gray = display.getSystemColor(SWT.COLOR_GRAY);

Rectangle bounds = this.getBounds();

gc.setForeground(black); gc.drawLine(0, 0, 0, bounds.height); gc.setForeground(gray); gc.drawLine(1, 0, 1, bounds.height); // ...and so on...}

Page 40: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT

● Managing graphical resources– This is all there is to creating this control!– Conclusion:

● If you don't construct a graphical resource, you do not have to dispose it

● You can get away with this an amazing amount of the time

Page 41: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

What is 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

Page 42: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

What is 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

Page 43: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT + JFace

● Hello, JFacepublic class HelloJFace extends ApplicationWindow { public HelloJFace(Shell parentShell) { super(parentShell); setBlockOnOpen(true); }

public static void main(String[] args) { (new HelloJFace(null)).open(); }}

Page 44: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT + JFace

● Hello, JFace

Page 45: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT + JFace

● Ways to add features to your JFace application:

– Add code to constructor– Override protected methods– Add new methods or classes

Page 46: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT + JFace

● Add menu bar, tool bar, status linepublic class HelloJFace extends ApplicationWindow { public HelloJFace(Shell parentShell) { super(parentShell); setBlockOnOpen(true); }

public static void main(String[] args) { (new HelloJFace(null)).open(); }}

Page 47: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT + JFace

● Add menu bar, tool bar, status linepublic 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(); }}

Page 48: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT + JFace

● Add application title and iconprotected void configureShell(Shell shell) { super.configureShell(shell);

shell.setText("Hello, JFace");

shell.setImage( ImageDescriptor.createFromFile( HelloJFace.class, "icons/app.png").createImage());}

Page 49: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT + JFace

● Add menu to menu barprotected 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;}

Page 50: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT + JFace

● Add button to tool barprotected ToolBarManager createToolBarManager( int style) { ToolBarManager toolBar = new ToolBarManager(style);

toolBar.add(new NewAction()); return toolBar;}

Page 51: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT + JFace

● Add private class NewAction for buttonprivate 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 }}

Page 52: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT + JFace

● Add a blotter to the client areaprotected Control createContents(Composite parent) { Composite contents = new Blotter(parent, SWT.NULL);

return contents;}

Page 53: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of SWT + JFace

● Here's how it looks now...

Page 54: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

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

Page 55: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

What is 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

Page 56: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● 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

Page 57: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● JFace TableViewer usage

TableViewerTodoList

TreeMap

Todo

“input”

Page 58: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● JFace TableViewer usage

TableViewerTodoList

TreeMap

Todo

“input”

ContentProvider

LabelProvider

RowSorter

CellEditor

CellModifier

Page 59: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

● 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

A Taste of JFace Viewers

Page 60: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● Create TodoList “model” objectpublic class TodoList implements Serializable {

// The Model's entry-point is here... public static TodoList theList = null;

public TodoList() { theList = this; }

// <continued/>...

Page 61: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● Create TodoList “model” object (continued) public Object[] toArray() { Object[] results = new Object[todoList.size()]; int i = 0; Iterator iter = todoList.keySet().iterator(); while (iter.hasNext()) { results[i] = todoList.get(iter.next()); ++i; } return results; }

}

Page 62: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● 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

Page 63: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● Add TableViewer object to layoutprotected Control createContents(Composite parent) { Composite contents = new Blotter(parent, SWT.NULL);

return contents;}

Page 64: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● Add TableViewer object to layoutprotected 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);

table = new TableViewer(contents, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION); table.setLayoutData( new GridData(GridData.FILL_BOTH)); return contents;}

Page 65: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● 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

Page 66: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● 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);

table = new TableViewer(contents, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION); table.setLayoutData( new GridData(GridData.FILL_BOTH)); return contents;}

Page 67: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● 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);

table = new TableViewer(contents, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION); table.setLayoutData( new GridData(GridData.FILL_BOTH)); initTable(); return contents;}

Page 68: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● Initialize TableViewer... private void initTable() { addColumns(); addProvidersAndEditors(); }

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;

Page 69: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● JFace TableViewer usage

TableViewerTodoList

TreeMap

Todo

“input”

ContentProvider

LabelProvider

RowSorter

CellEditor

CellModifier

Page 70: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● 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);}

Page 71: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● 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

Page 72: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● Set TodoList as the TableViewer's input private void initTable() { addColumns(); addProvidersAndEditors(); }

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;

Page 73: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● 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;

Page 74: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● 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

Page 75: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● 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);}

Page 76: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● JFace TableViewer usage

TableViewerTodoList

TreeMap

Todo

“input”

ContentProvider

LabelProvider

RowSorter

CellEditor

CellModifier

Page 77: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● A closer look at JFace “event handler objects”

Event Handler When TriggeredContentProvider On setInput(), refresh(), update()LabelProvider A cell needs redrawingCellEditor Edit cell valueCellModifier Fetch editable value / store valueRowSorter On table redraw

Page 78: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● A closer look at JFace “event handler objects”

Event Handler When TriggeredContentProvider On setInput(), refresh(), update()LabelProvider A cell needs redrawingCellEditor Edit cell valueCellModifier Fetch editable value / store valueRowSorter On table redraw

Page 79: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● Implementing ContentProviderprivate 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) {}}

Page 80: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● JFace TableViewer usage

TableViewerTodoList

TreeMap

Todo

“input”

ContentProvider

LabelProvider

RowSorter

CellEditor

CellModifier

Page 81: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● A closer look at JFace “event handler objects”

Event Handler When TriggeredContentProvider On setInput(), refresh(), update()LabelProvider A cell needs redrawingCellEditor Edit cell valueCellModifier Fetch editable value / store valueRowSorter On table redraw

Page 82: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● Implementing LabelProviderprivate 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/>...

Page 83: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● Implementing LabelProvider public Image getColumnImage(Object element, int columnIndex) { if (columnIndex == COL_DONE) { Todo todo = (Todo) element; if (todo.isDone()) { return done; } else { return notdone; } } return null; }

// <continued/>...

Page 84: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● Implementing LabelProvider public String getColumnText(Object element, int columnIndex) {

Todo todo = (Todo) element; switch (columnIndex) { case COL_PRIORITY: return Integer.toString( todo.getPriority());

case COL_DESC: return todo.getDesc(); default: return ""; } } // <continued/>...

Page 85: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● Implementing LabelProvider public boolean isLabelProperty( Object element, String property) { return true; }

public void dispose() { done.dispose(); notdone.dispose(); }

public void addListener( ILabelProviderListener listener) {}

public void removeListener( ILabelProviderListener listener) {}}

Page 86: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● A closer look at JFace “event handler objects”

Event Handler When TriggeredContentProvider On setInput(), refresh(), update()LabelProvider A cell needs redrawingCellEditor Edit cell valueCellModifier Fetch editable value / store valueRowSorter On table redraw

Page 87: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● JFace TableViewer usage

TableViewerTodoList

TreeMap

Todo

“input”

ContentProvider

LabelProvider

RowSorter

CellEditor

CellModifier

Page 88: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● JFace TableViewer usage

TableViewerTodoList

TreeMap

Todo

“input”

ContentProvider

LabelProvider

RowSorter

CellEditor

CellModifier

The visible editor

Page 89: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● JFace TableViewer usage

TableViewerTodoList

TreeMap

Todo

“input”

ContentProvider

LabelProvider

RowSorter

CellEditor

CellModifier

Translates betweenvisible editor's

data type and themodel's storage

data type

Page 90: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● Implementing CellModifierprivate class CellModifier implements ICellModifier {

public boolean canModify(Object element, String property) { return true; }

// <continued/>...

Page 91: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● Implementing CellModifierpublic Object getValue(Object element, String property) { Todo todo = (Todo) element; if (property.equals(colNames[COL_DONE])) { return new Boolean(todo.isDone()); } else if (property.equals(colNames[COL_PRIORITY])) { return Integer.toString(todo.getPriority()); } else if (property.equals(colNames[COL_DESC])) { return todo.getDesc(); } return null;}// <continued/>...

Page 92: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● Implementing CellModifier public void modify(Object element, String property, Object value) { Item item = (Item) element; Todo todo = (Todo) item.getData();

if (property.equals(colNames[COL_DONE])) { boolean val = ((Boolean)value).booleanValue(); todo.setDone(val); } else if (property.equals(colNames[COL_PRIORITY])) { // ...handle the other columns similarly } table.refresh(); }}

Page 93: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● A closer look at JFace “event handler objects”

Event Handler When TriggeredContentProvider On setInput(), refresh(), update()LabelProvider A cell needs redrawingCellEditor Edit cell valueCellModifier Fetch editable value / store valueRowSorter On table redraw

Page 94: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● Implementing RowSorterprivate 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(); }}

Page 95: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● A closer look at JFace “event handler objects”

Event Handler When RequiredContentProvider AlwaysLabelProvider AlwaysCellEditor If grid is not read-onlyCellModifier If grid is not read-onlyRowSorter If grid is sorted

Page 96: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

A Taste of JFace Viewers

● So here's what we just built...

Page 97: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

SWT GUI Builders

● Shortens the learning curve by handling layout tasks for you

● Easier to quickly prototype an interface● Several commercial SWT GUI builders are

under development

Page 98: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

SWT GUI Builders (under development)

● V4All– Ramen Assisi

● Scott Stanchfield's unnamed GUI builder● SWTworkbench

– Advanced Systems Concepts

Page 99: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

Summary

● SWT provides a fast, native user interface for rich client Java applications.

● SWT has taken two meanings depending on if a formal or informal context is being used:

– Formal: SWT = Java-wrapped O/S widgets.– Informal: SWT+JFace all the way up to the entire

Eclipse application framework.● SWT and Swing have complementary (not

competitive) strengths and weaknesses.

Page 100: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

Source Code Availability

● Complete source code is available at– http://www.swtworkbench.com/wsee_2003.zip

Page 101: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

SWT, the Standard Widget Toolkit

Any questions?

Page 102: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

SWT Development Resources

● SWT Home Page– http://dev.eclipse.org/viewcvs/index.cgi/

%7Echeckout%7E/platform-swt-home/main.html● SWT articles at Eclipse.org

– http://www.eclipse.org/articles/index.html● SWT/JFace page on the Eclipse Wiki

– http://eclipsewiki.swiki.net/2● See especially the “Hello, world” examples provided

here.

Page 103: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

SWT Development Resources (cont.)

● SWT Development Resources / Examples– http://dev.eclipse.org/viewcvs/index.cgi/

%7Echeckout%7E/platform-swt-home/dev.html● Especially note the code snippets starting about

halfway down the page.

● SWT FAQ– http://dev.eclipse.org/viewcvs/index.cgi/

%7Echeckout%7E/platform-swt-home/faq.html

Page 104: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

SWT GUI Builder Resources

● GUI builder page on the Eclipse Wiki– http://eclipsewiki.swiki.net/145

● Sweet Project Home Page– http://sweet-swt.sourceforge.net

● Sweet is an open source, cross-vendor effort to provide SWT metadata to all SWT GUI builders. Basically, it's BeanInfo for SWT.

Page 105: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

Relevant Eclipse Resources

● Eclipse.org newsgroups, archives, IRC– http://www.eclipse.org/newsgroups/index.html– http://www.eclipse.org/search/search.cgi– irc://irc.freenode.net/#eclipse

● Eclipse Wiki– http://eclipsewiki.swiki.net/1

● Eclipse plug-in registry– http://eclipse-plugins.2y.net/

Page 106: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

Page 107: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com

Page 108: SWTworkbench.com SWT, The Standard Widget Toolkit A simple, productive path to effective cross-platform interfaces.

SWTworkbench.com