Wednesday, July 7, 2010

Swipe That Image

The touch device Swipe gesture has been well supported on LWUIT from day one however strictly for the purpose of scrolling. Its pretty easy and common to implement the swipe for other uses such as paging through pictures but there users often want immediate feedback which is a bit harder.

Novices to LWUIT might think it makes sense to try and combine the swipe and transition, but that is probably not so practical since the transitions assume speed and structure (partially because they are generic and can slide between anything)...



This demo shows off Yoga posture pictures taken from beyoga.co.il (copyrighted!) in such a slide demo on the HD2. The code works well on pretty much any device with or without touch and is actually really simple to implement. I will commit it to my incubator directory soon:



public class BrowserMIDlet extends MIDlet implements Runnable {

private static final String[] IMAGE_NAMES = {

"http://beyoga.co.il/beyoga/gallery/asana/mainColumnParagraphs/0/subPar/03/image/19.JPG",

"http://beyoga.co.il/beyoga/gallery/asana/mainColumnParagraphs/0/subPar/08/image/setu_bandhasana.jpg",

"http://beyoga.co.il/beyoga/gallery/asana/mainColumnParagraphs/0/subPar/02/image/Kukkutasana.jpg",

"http://beyoga.co.il/beyoga/gallery/asana/mainColumnParagraphs/0/subPar/016/image/parighasana.jpg",

"http://beyoga.co.il/beyoga/gallery/asana/mainColumnParagraphs/0/subPar/06/image/Kapotasana.jpg",

"http://beyoga.co.il/beyoga/gallery/asana/mainColumnParagraphs/0/subPar/014/image/24.jpg",

"http://beyoga.co.il/beyoga/gallery/asana/mainColumnParagraphs/0/subPar/07/image/18.jpg"

};



private Image[] slides;

private Form images;





public void startApp() {

try {

Display.init(this);

Resources r = Resources.open("/LWUITtheme.res");

UIManager.getInstance().setThemeProps(r.getTheme(r.getThemeResourceNames()[0]));

images = new Form("Be Yoga");

images.setLayout(new BorderLayout());

Image progress = Image.createImage("/progress0.png");

AnimationObject[] animations = new AnimationObject[] { AnimationObject.createAnimationImage(progress, 0, 0) };

animations[0].defineOrientation(AnimationObject.MOTION_TYPE_LINEAR, 0, 1500, 0, 359);

Timeline t = Timeline.createTimeline(1500, animations, new Dimension(progress.getWidth(), progress.getHeight()));

images.addComponent(BorderLayout.NORTH, new Label("Loading Images Please Wait..."));

images.addComponent(BorderLayout.CENTER, new Label(t));

images.show();

new Thread() {

public void run() {

// download the images from the web site

slides = new Image[IMAGE_NAMES.length];

for(int iter = 0 ; iter < IMAGE_NAMES.length ; iter++) {

InputStream i = null;

try {

i = Connector.openInputStream(IMAGE_NAMES[iter]);

slides[iter] = EncodedImage.createImage(i);

i.close();

} catch (IOException ex) {

ex.printStackTrace();

Dialog.show("Error", ex.getMessage(), "Exit", null);

BrowserMIDlet.this.notifyDestroyed();

} finally {

try {

i.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

Display.getInstance().callSerially(BrowserMIDlet.this);

}

}.start();

} catch (IOException ex) {

ex.printStackTrace();

}

}



public void run() {

images.removeAll();

images.addComponent(BorderLayout.CENTER, new Label() {

private int pos = 0;

private int dest = 1;

private int initialX = 0;

private int currentX = 0;

private Image currentImage = null;

private Image destImage = null;

private int currentImageOffset = 0;

private int destImageOffset = -1;

private Motion slideToDestMotion;



{

setFocusable(true);

requestFocus();

}



public boolean animate() {

boolean b = super.animate();

if(slideToDestMotion != null) {

currentX = slideToDestMotion.getValue();

if(slideToDestMotion.isFinished()) {

slideToDestMotion = null;

int dist = currentX - initialX;

if(dist != 0) {

if(Math.abs(dist) > getWidth() / 2) {

if(dist > 0) {

pos = pos - 1;

if(pos < 0) {

pos = slides.length - 1;

}

} else {

pos = pos + 1;

if(pos == slides.length) {

pos = 0;

}

}

dest = pos;

}

}

currentX = 0;

initialX = 0;

getComponentForm().deregisterAnimated(this);

}

return true;

}

return b;

}



private Image getCurrentImage() {

if(pos == currentImageOffset && currentImage != null && currentImage.getWidth() == getWidth()) {

return currentImage;

}

currentImage = slides[pos].scaledSmallerRatio(getWidth(), getHeight());

currentImageOffset = pos;

return currentImage;

}



private Image getDestImage() {

if(dest == destImageOffset && destImage != null && destImage.getWidth() == getWidth()) {

return destImage;

}

destImage = slides[dest].scaledSmallerRatio(getWidth(), getHeight());

destImageOffset = dest;

return destImage;

}



public void keyReleased(int c) {

int g = Display.getInstance().getGameAction(c);

if(slideToDestMotion == null) {

switch(g) {

case Display.GAME_LEFT:

dest = pos - 1;

if(dest < 0) {

dest = slides.length - 1;

}

slideToDestMotion = Motion.createSplineMotion(0, getWidth() + initialX, 200);

slideToDestMotion.start();

getComponentForm().registerAnimated(this);

break;

case Display.GAME_RIGHT:

dest = pos + 1;

if(dest == slides.length) {

dest = 0;

}

slideToDestMotion = Motion.createSplineMotion(0, -getWidth(), 200);

slideToDestMotion.start();

getComponentForm().registerAnimated(this);

break;

}

}

super.keyReleased(c);

}



public void pointerPressed(int x, int y) {

initialX = x;

currentX = x;

}



public void pointerDragged(int x, int y) {

currentX = x;

int dist = currentX - initialX;

if(dist != 0) {

if(dist > 0) {

dest = pos - 1;

if(dest < 0) {

dest = slides.length - 1;

}

} else {

dest = pos + 1;

if(dest == slides.length) {

dest = 0;

}

}

}

repaint();

}



public void pointerReleased(int x, int y) {

int dist = currentX - initialX;

if(dist != 0) {

if(Math.abs(dist) > getWidth() / 2) {

if(dist > 0) {

slideToDestMotion = Motion.createSplineMotion(currentX, getWidth() + initialX, 250);

} else {

slideToDestMotion = Motion.createSplineMotion(currentX, initialX - getWidth(), 250);

}

} else {

slideToDestMotion = Motion.createSplineMotion(currentX, initialX, 250);

}

slideToDestMotion.start();

getComponentForm().registerAnimated(this);

}

repaint();

}



public void paint(Graphics g) {

Image currentImg = getCurrentImage();

int currentImgX = getX() + (getWidth() - currentImg.getWidth()) / 2;

int currentImgY = getY() + (getHeight() - currentImg.getHeight()) / 2;

if(currentX == initialX) {

g.drawImage(currentImg, currentImgX, currentImgY);

} else {

Image destImg = getDestImage();

int dist = currentX - initialX;

g.drawImage(currentImg, currentImgX + dist, currentImgY);

int destImgX = getX() + (getWidth() - destImg.getWidth()) / 2;

int destImgY = getY() + (getHeight() - destImg.getHeight()) / 2;

if(dist > 0) {

g.drawImage(destImg, destImgX - getWidth() + dist, destImgY);

} else {

g.drawImage(destImg, destImgX + getWidth() + dist, destImgY);

}

}

}

});

images.revalidate();

}



public void pauseApp() {

}



public void destroyApp(boolean unconditional) {

}

}



Wednesday, June 30, 2010

Ginga-J Approval Process

As you may know the Ginga-J specification for DTV (Digital Television) is becoming a national standard in Brazil by ABNT. Just in case you don't remember Ginga-J uses LWUIT for its component UI layer.
The specification is now in the public comments phase, looking for feedback from the community/public.
If you would like to see Ginga-J become a finalized standard for ABNT you can vote for that matter by:
1. Going to http://www.abntonline.com.br/consultanacional/default.aspx
2. A list of Commissions will be presented. Choose "ABNT - CEE85 - Televisao >>> Digital"
3. The project of Standard "85:000.00-006/6" will be presented (JavaDTV 1.3)
4. An email / password data request will be required. With this done, you >>> will be able to vote.
5. Vote

Wednesday, June 23, 2010

Headon That Table



A couple of important news before I begin, Terrence Barr has written a great new LWUIT introductory article for java.net and its at the top of java.sun.com... Great for introducing people to LWUIT!



Ofir has just passed a significant milestone of 1000 commits into our java.net SVN repository, take into consideration that we had an internal repository where we are passed the 56k version number... Its not strictly LWUIT commits though ;-)



The main subject of this post though is fixed table headers. The default LWUIT table implementation scrolls the title together with the table body, we made that choice since we assume small tables as the main use case. Fixing a column or a row (such as the titles of the table) into place isn't trivial in an efficient way. However, its possible to do something like that by using two tables and keeping their scrolling in sync.



This works great and pretty seamlessly for touch/keyboard usages however the approach has one major drawback that the size of the title might differ from the size of the cells thus causing the table titles to be misaligned from the table columns.



The solution I chose was to hardcode the cell sizes based on the size of the titles, you can obviously adapt that approach to something more appropriate for your need.



public class FixedTableDemo extends MIDlet {

private static final String[] TITLES = {"Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6", "Title 7", "Title 8", "Title 9"};

private static final int ROWS = 100;

private static Dimension[] TITLE_SIZES;



static class MirroredTable extends Table {

private MirroredTable mirrorTo;

public MirroredTable(TableModel m) {

super(m);

}



public MirroredTable(TableModel m, boolean b) {

super(m, b);

}



public void setScrollX(int x) {

super.setScrollX(x);

if(isDragActivated()) {

mirrorTo.setScrollX(x);

}

}



/**

* @param mirrorTo the mirrorTo to set

*/


public void setMirrorTo(MirroredTable mirrorTo) {

this.mirrorTo = mirrorTo;

}



public Component createCell(Object value, int row, int column, boolean editable) {

Component c = super.createCell(value, row, column, editable);

if(TITLE_SIZES != null && TITLE_SIZES[column] != null) {

c.setPreferredSize(TITLE_SIZES[column]);

}

return c;

}

}



public void startApp() {

Display.init(this);

Resources r;

try {

r = Resources.open("/LWUITtheme.res");

UIManager.getInstance().setThemeProps(r.getTheme(r.getThemeResourceNames()[0]));

} catch (IOException ex) {

ex.printStackTrace();

}

Form f = new Form("Table Title");

MirroredTable titlesTable = new MirroredTable(new DefaultTableModel(TITLES, new Object[0][0]));

TITLE_SIZES = new Dimension[TITLES.length];

for(int iter = 0 ; iter < TITLES.length ; iter++) {

TITLE_SIZES[iter] = titlesTable.createCell(TITLES[iter], -1, iter, false).getPreferredSize();

}



Object[][] body = new Object[ROWS][TITLES.length];

for(int rows = 0 ; rows < body.length ; rows++) {

for(int cols = 0 ; cols < body[rows].length ; cols++) {

body[rows][cols] = "" + rows + ", " + cols;

}

}

MirroredTable bodyTable = new MirroredTable(new DefaultTableModel(TITLES, body), false);

bodyTable.setMirrorTo(titlesTable);

titlesTable.setMirrorTo(bodyTable);

bodyTable.setScrollable(true);

titlesTable.setScrollableX(true);

f.setScrollable(false);

f.setLayout(new BorderLayout());

titlesTable.setTensileDragEnabled(false);

bodyTable.setTensileDragEnabled(false);

titlesTable.setIsScrollVisible(false);

f.addComponent(BorderLayout.NORTH, titlesTable);

f.addComponent(BorderLayout.CENTER, bodyTable);

f.show();

}



public void pauseApp() {

}



public void destroyApp(boolean unconditional) {

}

}





Sunday, June 6, 2010

Pimp the VirtualKeyboard - by Chen Fishbein

LWUIT 1.4 is just around the corner, before the release I wanted to share
Some of the VirtualKeyboard enhancements/improvements that will be released as part of 1.4.

Since the VirtualKeyboard is a pure LWUIT component it can be customized in various ways:

1. Changing the Virtual Keyboard look – All Virtual Keyboard items can be customized from the resource editor, the associated ui id's are:

VKB – this id is used to style the Virtual Keyboard body.
VKBtooltip – this id is used to style the popup tooltip.
VKBButton – this id is used to style a regular button on the virtual keyboard (usually a char or a string).
VKBSpecialButton – this id is used to style the special buttons such as: 'Space', 'SH', ...
VKBTextInput – this id is used to style the textfield on the virtual keyboard.






















2. Adding a language -

The example below demonstrates how to add an input mode that supports hebrew:
Create an array of String arrays, each array represents a buttons column.
private static final String[][] DEFAULT_HEBREW = new String[][]{       
{"\u05e7", "\u05e8", "\u05d0", "\u05d8", "\u05d5", "\u05df", "\u05dd", "\u05e4", "$Delete$"}, 
{"\u05e9", "\u05d3", "\u05d2", "\u05db", "\u05e2", "\u05d9", "\u05d7", "\u05dc", "\u05da"}, 
{"\u05d6", "\u05e1", "\u05d1", "\u05d4", "\u05e0", "\u05de", "\u05e6", "\u05ea", "\u05e5"}, 
{"$Mode$", "$Space$", "\u05E3", "$OK$"}   };

Now extends the VirtualKeyboard and make sure when the VirtualKeyboard is initialized the new language mode is added.

public static class HebrewK extends VirtualKeyboard {  
public HebrewK() {    
   addInputMode("\u05d0\u05d1\u05d2", DEFAULT_HEBREW);    
   setInputModeOrder(new String[]{"\u05d0\u05d1\u05d2", QWERTY_MODE,    
   NUMBERS_SYMBOLS_MODE, NUMBERS_MODE, SYMBOLS_MODE   
  }
 );  
} 
}
Now you need to make sure the new HebrewK will be used as the default virtual keyboard.
Call this:
VKBImplementationFactory.init(HebrewK.class);
instead of the regular
VKBImplementationFactory.init();



3. Binding a VirtualKeyboard to a TextField – Now we have a use case where a TextField should accept only numbers, therefore launching the regular VirtualKeyboard will be a mistake.
What we need to do is to create a 'numbers only' VirtualKeyboard and launch it on a specific TextField.

TextField txt = new TextField(); 
txt.setConstraint(TextField.NUMERIC); 
txt.setInputModeOrder(new String[]{"123"}); 
txt.setInputMode("123");  
VirtualKeyboard vkb = new VirtualKeyboard(); 
vkb.setInputModeOrder(new String[]{VirtualKeyboard.NUMBERS_MODE});  
VirtualKeyboard.bindVirtualKeyboard(txt, vkb);

4. Adding your own button to a TextField – There are several use cases where you would want to place your own buttons on a specific Virtual Keyboard, for example if you are asking the user to insert input for a search field you might want a “search” command instead of the regular “ok” command that will automatically when pressed will invoke a submit action to the network.
To accomplish this you need to create a new virtual keyboard, declare your own input buttons and to add your own special button to be part of the virtual keyboard.



Declare a new input with a new special button “Search” (By default Virtual Keyboard is able to understand only the following special keys: "Shift", "Delete", "T9", “Mode”, “Space”, “OK”):

String[][] SEARCH_QWERTY = new String[][]{ 
{"q", "w", "e", "r", "t", "y", "u", "i", "o", "p"}, 
{"a", "s", "d", "f", "g", "h", "j", "k", "l"}, 
{"$Shift$", "z", "x", "c", "v", "b", "n", "m", "$Delete$"}, 
{"$Mode$", "$Space$", "$Search$"} };  

VirtualKeyboard vkb = new VirtualKeyboard(); 
//add the new input mode 
vkb.addInputMode("ABC_S", SEARCH_QWERTY); 
vkb.setInputModeOrder(new String[]{"ABC_S"}); 
//add the new special button to the vkb 
vkb.addSpecialButton("Search", new Command("Search") {  
public void actionPerformed(ActionEvent evt) { 
  //search logic
... 
} 
});  
//bind the vkb to the textfield 
VirtualKeyboard.bindVirtualKeyboard(txt, vkb); 
f.addComponent(txt);