Monday, April 27, 2009

Why Do You Need A Variable Sized List?

Last week the forum member juliofabio sent this gorgeous looking video to the forum showing a variable size list component. This looks really stunning and shows off how much you can do with LWUIT when you put your mind to it.

What I'm not exactly sure about is why did he choose to use a List?

Why not container and components like I used in my recent Tree demo?

Why List? Even if we think of our data as a list it might not always be appropriate for a list component.

Specifically in this case the set of elements within the "list" seem to be predefined and small hence there is no advantage to using a model. In fact I would assume that writing that code using a model and renderer paradigm would be very unintuitive. This set will never reach a very large size so the advantages of a huge million element list won't be realized either. It seems from the demo that even list animations were disabled, this is something that can be acheived using containers though.

Using Container.replace() can allow for most of the special effects within this list and can provide even more impressive effects (such as 3d). Building something like that can be trivial without getting into all of the complexities related to the list component, so why would someone want such a list?

Thursday, April 23, 2009

JavaVerified: LWUIT's Killer Feature?

I'm generally very excited about Nokia's upcoming Ovi store, I think it has the potential to empower smaller developers to deliver content to end users.

One of the decisions made by Nokia is to go with Java Verified for the QA and signing of application for Ovi. Java Verified has an issue with requiring a cost for every tested device & every failure. This cost can become remarkably high the more devices need certification/signing, here LWUIT really shines. Its approach of "One JAR for all devices" makes a world of difference in the Java Verified cost for signing, it can make the difference between turning a profit and losing money when publishing smaller applications.

I've always viewed LWUIT as a tool to assist the smaller shops and individual hackers to reach mass market device deployment and this is a concrete example of this perspective.

Tuesday, April 14, 2009

LWUIT Model Tree

Since the forum is down (as usual) my responses to some of the comments in the forum just never made it past the mailing list, one of the questions in the forum that comes up every now and again is "how to create a tree in LWUIT?"

We get this question every now and again and generally our response is that we don't but would like to get such a contribution. However, looking at our samples it seems that most of our component building explanations revolve around "small" components rather than composites which would fit a tree much better.

A composite component is really a Container which we would assemble into a specialized component by tailoring together smaller components. Naturally we could also perform custom painting for such a composite but that often defeats the purpose...

These components aren't suitable for everything but for things such as a tree we gain several huge advantages by using Container nesting to build a tree:
  • We represent the nodes using custom components allowing subclasses to manipulate the look of the tree to a great degree.
  • We hardly need to write any code and styles, touch support, keyboard navigation etc. are all built in
  • We can leverage transitions and component nesting for really stunning effects
The tree uses a simple model component with some dummy data, mapping it to a filesystem or any other such element is trivial. All you need to do is implement TreeModel appropriately and it only contains 2 methods (I spent allot of time with the Swing tree model...). Its not as powerful as the Swing model since it doesn't support editing or mutation (from the model) but these are relatively easy to add. It took me roughly 2 hours to write everything including this post, it was so long because I discovered a minor LWUIT bug in scrolling/animation that is triggered when a tree starts off small and ends up being rather large.

Lets go strait to the code, first we need the model:
/**
* Arranges tree node objects, a node can essentially be anything
*
* @author Shai Almog
*/

interface TreeModel {
/**
* Returns the child objects representing the given parent, null should return
* the root objects
*/

public Vector getChildren(Object parent);

/**
* Is the node a leaf or a folder
*/

public boolean isLeaf(Object node);
}

I think thats pretty self explanitory...
Then we would like the implementation:
class Node {
Object[] children;
String value;

public Node(String value, Object[] children) {
this.children = children;
this.value = value;
}

public String toString() {
return value;
}
}

TreeModel model = new TreeModel() {
Node[] sillyTree = {
new Node("Root 1", new Node[] {
new Node("Child 1", new Node[] {
new Node("Gand Child 1", new Node[] {
}),
new Node("Gand Child 2", new Node[] {
}),
new Node("Gand Child 3", new Node[] {
}),
new Node("Gand Child 4", new Node[] {
}),
}),
new Node("Child 2", new Node[] {
new Node("Something Else", new Node[] {
}),
new Node("More of the same", new Node[] {
}),
}),
new Node("Child 3", new Node[] {
}),
new Node("Child 4", new Node[] {
}),
}),
new Node("Root 2", new Node[] {
new Node("Something Else", new Node[] {
}),
new Node("More of the same", new Node[] {
}),
}),
new Node("Root 3", new Node[] {
new Node("Something Else", new Node[] {
}),
new Node("More of the same", new Node[] {
}),
}),
new Node("Root 4", new Node[] {
}),
};
Thats a bit long and tedious but its mostly unrelated to LWUIT, the main code is the tree component code which is really rather simple:
/**
* LWUIT Tree component sample
*
* @author Shai Almog
*/

public class TreeComponent extends Container {
private static final String KEY_OBJECT = "TREE_OBJECT";
private static final String KEY_PARENT = "TREE_PARENT";
private static final String KEY_EXPANDED = "TREE_NODE_EXPANDED";
private static final String KEY_DEPTH = "TREE_DEPTH";

private ActionListener expansionListener = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Component c = (Component)evt.getSource();
Object e = c.getClientProperty(KEY_EXPANDED);
if(e != null && e.equals("true")) {
collapseNode(c);
} else {
expandNode(c);
}
}
};
private TreeModel model;
private Image folder;
private Image nodeImage;
private int depthIndent = 15;

public TreeComponent(TreeModel model) {
try {
this.model = model;
folder = Image.createImage("/folder.png");
nodeImage = Image.createImage("/page_white.png");
setLayout(new BoxLayout(BoxLayout.Y_AXIS));
buildBranch(null, 0, this);
setScrollableY(true);
} catch (IOException ex) {
ex.printStackTrace();
}
}

private void expandNode(Component c) {
c.putClientProperty(KEY_EXPANDED, "true");
int depth = ((Integer)c.getClientProperty(KEY_DEPTH)).intValue();
Container parent = c.getParent();
Object o = c.getClientProperty(KEY_OBJECT);
Container dest = new Container(new BoxLayout(BoxLayout.Y_AXIS));
Label dummy = new Label();
parent.addComponent(BorderLayout.CENTER, dummy);
buildBranch(o, depth, dest);
parent.replace(dummy, dest, CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, true, 300));
}

private void collapseNode(Component c) {
c.putClientProperty(KEY_EXPANDED, null);
Container p = c.getParent();
for(int iter = 0 ; iter < p.getComponentCount() ; iter++) {
if(p.getComponentAt(iter) != c) {
Label dummy = new Label();
p.replaceAndWait(p.getComponentAt(iter), dummy, CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, 300));
p.removeComponent(dummy);
}
}
}

/**
* Adds the child components of a tree branch to the given container.nt
*/

protected void buildBranch(Object parent, int depth, Container destination) {
Vector children = model.getChildren(parent);
int size = children.size();
Integer depthVal = new Integer(depth + 1);
for(int iter = 0 ; iter < size ; iter++) {
Object current = children.elementAt(iter);
Button nodeComponent = createNodeComponent(current, depth);
if(model.isLeaf(current)) {
destination.addComponent(nodeComponent);
} else {
Container componentArea = new Container(new BorderLayout());
componentArea.addComponent(BorderLayout.NORTH, nodeComponent);
destination.addComponent(componentArea);
nodeComponent.addActionListener(expansionListener);
}
nodeComponent.putClientProperty(KEY_OBJECT, current);
nodeComponent.putClientProperty(KEY_PARENT, parent);
nodeComponent.putClientProperty(KEY_DEPTH, depthVal);
}
}

protected Button createNodeComponent(Object node, int depth) {
Button cmp = new Button(node.toString());
if(model.isLeaf(node)) {
cmp.setIcon(nodeImage);
} else {
cmp.setIcon(folder);
}
updateNodeComponentStyle(cmp.getSelectedStyle(), depth);
updateNodeComponentStyle(cmp.getUnselectedStyle(), depth);
return cmp;
}

protected void updateNodeComponentStyle(Style s, int depth) {
s.setBorder(null);
s.setPadding(LEFT, depth * depthIndent);
}
}


Then all we need is to use it in order to produce the video on the right:
Form treeForm = new Form("Tree Test");
treeForm.setLayout(new BorderLayout());
TreeComponent tree = new TreeComponent(model);
treeForm.addComponent(BorderLayout.CENTER, tree);
treeForm.setScrollable(false);
treeForm.show();

Tuesday, March 24, 2009

LWUIT At JavaOne 2009

LWUIT will be featured in 5 different JavaOne 2009 sessions/bof's which is pretty cool considering it will be one year old in the market by then. You can find out all of them by searching for LWUIT in the JavaOne content catalog.
A session & BoF will be presented by myself and Chen, the session will aim to create a more "wow' sort of effect presenting what can be done to create compelling portable UI's in LWUIT. I hope I still have enough tricks up my sleave to impress a jaded audience that already saw the stuff in the blog...
The BoF will probably aim to be a more open discussion although we will try to provide a guiding theme. Other than that there are quite a few other places to see LWUIT in JavaOne including Tamir's session for interactive TV development. Tamir is our "go to guy" for LWUIT on TV/CDC and I'm sure his session will be pretty cool especially if you are thinking of taking your skills into the realm of TV.

Wednesday, March 18, 2009

Breaking To Make Things Better

The side image to the post has nothing to do with the post, just a screenshot of the new MajiPic application from majimob.

Our stance on API backwards compatibility has always been to try as hard as possible but no more than that. When we find design mistakes, even painful ones we choose to cut them down and fix them even with a compatibility issue especially when the potential benefits are noticeable.

We are now in the process of fixing a mistake in Styles in regards to the fgSelection/bgSelection properties. We intend to make two potential styles for every component: Selected and unselected.

Let start with the practical terms... We would have three methods to get/set style in LWUIT's components: getStyle(), getSelectedStyle() & getUnselectedStyle().

The getStyle() method will return the selected/unselected style based on the focus state of the component so you will no longer have to write:

if(hasFocus()) {
color = getStyle().getFgSelectionColor();
} else {
color = getStyle().getFgColor();
}

Instead this will work pretty much as you would expect:
color = getStyle().getFgColor();

This would also mean that get/set*SelectionColor would now be deprecated. But this goes deeper still, we have some various hacks such as focus/pressed borders which will now be deprecated as well.

This is pretty revolutionary and we intend to offer a migration path to ease the pain, the Style class would have a compatibility mode flag which will implicitly synchronize the selected/unselected styles. Furthermore, calling the selected color methods will try to "do the right thing" for most use cases.

However, we are doing this for a reason and this compatibility will break if you try to leverage the newer features enabled by this change such as the newer LWUIT Designer (resource editor). This would allow designers to define far more elaborate themes with the LWUIT Designer including complex backgrounds depending on component state (we intend to offer "meta-styles" as well but this isn't finalized). If you will use the newer version of the LWUIT Designer you will be forced to disable compatibility mode which will naturally break these features. We feel that this new approach is far more intuitive and powerful!

Following is a short mini faq for this change with questions Chen and myself raised about this feature:

Q: Won't it increase the overhead to add a selected style for every component?

A: Selected styles are created lazily and so won't exist for most components, they will only be created when the component actually gets the focus. Even then, this would be more efficient since it would allow to generalize more code and reduce application size.


Q: How comitted are you to the current compatibility layer?

A: We assume some use cases will break even with the compatibility turned on (which would be the default in the immediate future), when possible we will try to get the compatibility working properly but we won't bend over backwards for that.


Q: Will you drop compatibility and if so when?

A: We defined the compatibility methods as deprecated, however there is no defined timeline. Once the code is available we will poll the community about the usage of compatibility and remove it when we feel migration is successful.


Q: When will this land?

A: It won't land next week. Probably.
I hope to commit by the end of the month but can't guarantee. We will also need the new LWUIT Designer to really take advantage of this feature. The delay is mostly because this change is a part of a major restructuring related to the LWUIT Designer as well and this takes quite some time...

Sunday, March 8, 2009

Validating And The Art Of GlassPane

The GlassPane in LWUIT is inspired by the Swing GlassPane & layered pane with quite a few twists (note: the picture on the right is of the Swing glass pane, I didn't have the energy to make one of my own)... We tried to imagine how Swing developers would have implemented the glass pane knowing what they do now about painters and Swings learning curve. But I'm getting ahead of myself, what is the glass pane?

A typical LWUIT application is essentially composed of 3 layers (this is a gross simplification though), the bg painters are responsible for drawing the background of all components including the main form. The component draws its own content which might overrule the painter and the glass pane paints last...

Essentially the glass pane is a painter that allows us to draw an overlay on top of the LWUIT application. Initially we didn't think we need a glass pane, we used to suggest that people should override the form's paint() method to reach the same result. Feel free to try and guess why this failed before reading the explanation in the next paragraph.


Overriding the paint method of a form worked initially, when you enter a form this behaves just as you would expect. However, when modifying an element within the form only that element gets repainted not the entire form! So if I had a form with a Button and text drawn on top using the Form's paint method it would get erased whenever the button got focus.

Thats good for the forms paint method, calling the forms paint method would be REALLY expensive for every little thing that occurs in LWUIT. However, we do want overlays for some things and we don't need to repaint every component in the screen to get them. The glass pane is called whenever a component gets painted, it only paints within the clipping region of the component hence it won't break the rest of the glass pane.

The painter chain is a tool that allows us to chain several painters together to perform different logistical tasks such as a validation painter coupled with a fade out painter. The sample bellow shows a crude validation panel that allows us to draw error icons next to components while exceeding their physical bounds as is common in many user interfaces

public class ValidationPane implements Painter {
private Vector components = new Vector();
private static Image error;
public ValidationPane(Form parentForm) {
try {
if(error == null) {
error = Image.createImage("/error.png");
}
} catch (IOException ex) {
ex.printStackTrace();
}
PainterChain.installGlassPane(parentForm, this);
}

public void paint(Graphics g, Rectangle rect) {
for(int iter = 0 ; iter < components.size() ; iter++) {
Component c = (Component) components.elementAt(iter);
if(c == null) {
components.removeElementAt(iter);
continue;
}
Object p = c.getClientProperty(VALIDATION_PROP);
int x = c.getAbsoluteX();
int y = c.getAbsoluteY();
x -= error.getWidth() / 2;
y += c.getHeight() - error.getHeight() / 2;
g.drawImage(error, x, y);
}
}

public void addInvalid(Component c) {
components.addElement(c);
}

public void removeInvalid(Component c) {
components.removeElement(c);
}
}

Monday, March 2, 2009

Chen Just Updated The Featured Apps section

Chen just updated the featured apps section of the LWUIT web site to include more recent applications. We hear of many applications developed in LWUIT but often we aren't given explicit permission to use screenshots and resources related to the application.
If you have developed a good looking LWUIT based application which isn't highlighted in the featured applications section please let us know by writing to lwuit at sun.com with screenshots, a short descriptive blurb about the application and optionally a link for further details.

If you wrote to us a while back but you are not on the page please write to us again, as engineers we tend to suck in this marketing related efforts so please bare with us...