For information about the design and functionality of the MIDlet, see section Design.
Note: Since all the element modules are implemented in the same
way, this implementation example only covers the ListDemoModule
.
To implement the ListDemoModule
class:
Create the ListDemoModule.java
class file.
Import the required classes.
import org.eclipse.ercp.swt.mobile.Command; import org.eclipse.ercp.swt.mobile.QueryDialog; import org.eclipse.ercp.swt.mobile.TimedMessageBox; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.Shell;
Set ListDemoModule
to implement DemoModule
and SelectionListener
. MIDlets use the SelectionListener
interface to receive events when a selection
occurs in a Control
.
public class ListDemoModule implements DemoModule, SelectionListener {
Create the required
variables and the ListDemoModule
class constructor.
private Shell demoShell; private Shell parent; private Composite demoComposite; private List demoList; private Command backCommand; private Command addCommand; private Command removeCommand; private static final String[] originalContent = new String[] { "Apples", "Oranges", "Grapefruit", "Milk" }; public ListDemoModule() { }
The DemoModule
interface requires the class to implement three
methods:
getDescription
returns a simple description
of the module.
public String getDescription() { return "Demonstrates the List component"; }
getTitle
returns the title of the module.
public String getTitle() { return "Shopping List"; }
runDemo
runs the demonstration of the module.
For the ListDemoModule
, the method sets up the demonstration
and the selection listeners.
public void runDemo(Shell parent) { this.parent = parent; demoShell = new Shell(SWT.MAX); demoShell.setLayout(new FillLayout(SWT.VERTICAL)); demoShell.setText("Shopping List"); demoComposite = new Composite(demoShell, SWT.NONE); demoComposite.setLayout(new FillLayout(SWT.VERTICAL)); demoShell.layout(); backCommand = new Command(demoShell, Command.EXIT, 0); backCommand.setText("Back"); backCommand.addSelectionListener(this); addCommand = new Command(demoShell, Command.SELECT, 1); addCommand.setText("Add"); addCommand.addSelectionListener(this); removeCommand = new Command(demoShell, Command.SELECT, 1); removeCommand.setText("Remove"); removeCommand.addSelectionListener(this); //first we create a multiple selection list demoList = new List(demoComposite, SWT.MULTI); demoList.addSelectionListener(this); //then we set the original content demoList.setItems(originalContent); demoComposite.layout(); demoShell.open(); demoShell.setActive(); }
The element
module classes can use some helper methods. The ListDemoModule
class uses the following helper methods:
widgetSelected
and widgetDefaultSelected
handle selections in the Control
. The user can
add and remove list items, and exit the list.
public void widgetSelected(SelectionEvent e) { if(e.widget.equals(backCommand)) { back(); } else if(e.widget.equals(addCommand)) { add(); } else if(e.widget.equals(removeCommand)) { remove(); } } public void widgetDefaultSelected(SelectionEvent e) { }
back
closes the list view and returns to the
previous view in the MIDlet.
private void back() { demoShell.dispose(); demoShell = null; parent.getShell().setActive(); }
add
is used to add a new item to the list.
private void add() { //here we first create a dialog to ask the user for a new item QueryDialog dialog = new QueryDialog(demoShell, QueryDialog.STANDARD); dialog.setPromptText("I should also remember:", ""); String itemToAdd = dialog.open(); //check whether the user pressed the cancel button or is trying to add //a duplicate item if(itemToAdd == null) { return; } else if(demoList.indexOf(itemToAdd) != -1) { showMessage("One entry of " + itemToAdd + " should be enough for you to remember..."); return; } //add the item to the list demoList.add(itemToAdd); }
remove
is used to remove an existing item
from the list.
private void remove() { //if the user hasn't selected anything if(demoList.getSelectionCount() == 0) { showMessage("Please mark the items you want to remove first!"); return; } //remove the selections from the list demoList.remove(demoList.getSelectionIndices()); }
showMessage
is used to display messages from
the add
and remove
helper methods.
private void showMessage(String message) { TimedMessageBox box = new TimedMessageBox(demoShell, SWT.ICON_ERROR); box.setMessage(message); box.open(); } }