/*
* Copyright � 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
*/
package com.sun.lwuit.uidemo;
import com.sun.lwuit.Button;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.Painter;
import com.sun.lwuit.RGBImage;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.animations.Animation;
import com.sun.lwuit.animations.Motion;
import com.sun.lwuit.animations.Transition;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.FocusListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.geom.Rectangle;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.GridLayout;
import com.sun.lwuit.plaf.Border;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.InputConnection;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
/**
* Bootstraps the UI toolkit demos
*
* @author Shai Almog
*/
public class UIDemoMIDlet extends javax.microedition.midlet.MIDlet implements ActionListener {
private static final int EXIT_COMMAND = 1;
private static final int RUN_COMMAND = 2;
private static final int BACK_COMMAND = 3;
private static final int ABOUT_COMMAND = 4;
private static final Command runCommand = new Command("Run", RUN_COMMAND);
private static final Command exitCommand = new Command("Exit", EXIT_COMMAND);
private static final Command backCommand = new Command("Back", BACK_COMMAND);
private static final Command aboutCommand = new Command("About", ABOUT_COMMAND);
private static final Demo[] DEMOS = new Demo[]{
new ThemeDemo(), new RenderingDemo(), new AnimationDemo(), new ButtonsDemo(),
new TransitionDemo(), new FontDemo(), new TabbedPaneDemo(), new DialogDemo(), new LayoutDemo(), new ScrollDemo()
};
private Demo currentDemo;
private static Transition componentTransitions;
private Hashtable demosHash = new Hashtable();
private static Form mainMenu;
private int cols;
private int elementWidth;
private static boolean localResources;
private static final String[] RESOURCE_NAMES = {"resources", "duke", "javaTheme", "businessTheme", "starTheme", "oceanfishTheme", "images"};
protected void startApp() {
try {
Display.init(this);
InputStream stream = getClass().getResourceAsStream("/resources.res");
Resources r2;
if (stream == null) {
localResources = false;
try {
RecordStore.openRecordStore(RESOURCE_NAMES[0], false).closeRecordStore();
Resources r1 = getResource("javaTheme");
UIManager.getInstance().setThemeProps(r1.getTheme(r1.getThemeResourceNames()[0]));
setMainForm(UIDemoMIDlet.getResource("resources"));
return;
} catch (Exception ignor) {
// this exception is expected the first time the application is executed
}
downloadResources();
return;
} else {
localResources = true;
r2 = Resources.open(stream);
stream.close();
}
//resources are built during the build process
//open the build.xml file to figure out how to construct the
//resource files
Resources r1 = Resources.open("/javaTheme.res");
UIManager.getInstance().setThemeProps(r1.getTheme(r1.getThemeResourceNames()[0]));
setMainForm(r2);
} catch (Throwable ex) {
ex.printStackTrace();
Dialog.show("Exception", ex.getMessage(), "OK", null);
}
}
/**
* Downloads resources for the very first activation of the demo if it is running
* with light deployment where the resources are not packaged in the JAR itself
*/
private void downloadResources() {
// download resources from the internet and install them in the RMS
// while showing a progress indicator
Form pleaseWait = new Form("Download");
pleaseWait.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
pleaseWait.addComponent(new Label("Downloading Resources"));
final Label progressInfo = new Label("Starting");
progressInfo.setAlignment(Component.CENTER);
final Progress prog = new Progress();
pleaseWait.addComponent(progressInfo);
pleaseWait.addComponent(prog);
pleaseWait.show();
new Thread() {
public void run() {
try {
byte[] buffer = new byte[4096];
for (int iter = 0; iter < RESOURCE_NAMES.length; iter++) {
progressInfo.setText("Downloading: " + RESOURCE_NAMES[iter]);
byte percent = (byte) (100.0f * ((iter + 0.25f) / ((float) RESOURCE_NAMES.length)));
prog.setProgress(percent);
String url = getAppProperty(RESOURCE_NAMES[iter] + "-url");
InputConnection inputCon = (InputConnection) Connector.open(url, Connector.READ);
InputStream stream = inputCon.openInputStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
int size = stream.read(buffer);
while (size > -1) {
output.write(buffer, 0, size);
size = stream.read(buffer);
}
stream.close();
inputCon.close();
progressInfo.setText("Storing: " + RESOURCE_NAMES[iter]);
RecordStore store = RecordStore.openRecordStore(RESOURCE_NAMES[iter], true);
byte[] array = output.toByteArray();
store.addRecord(array, 0, array.length);
store.closeRecordStore();
}
progressInfo.setText("Done!");
prog.setProgress((byte) 100);
Resources r1 = getResource("javaTheme");
UIManager.getInstance().setThemeProps(r1.getTheme(r1.getThemeResourceNames()[0]));
setMainForm(UIDemoMIDlet.getResource("resources"));
} catch (Exception ex) {
ex.printStackTrace();
Dialog.show("Exception", ex.getMessage(), "OK", null);
}
}
}.start();
}
/**
* Used instead of using the Resources API to allow us to fetch locally downloaded
* resources
*
* @param name the name of the resource
* @return a resources object
*/
public static Resources getResource(String name) throws IOException {
if (localResources) {
return Resources.open("/" + name + ".res");
}
byte[] resourceData = null;
try {
resourceData = RecordStore.openRecordStore(name, false).enumerateRecords(null, null, false).nextRecord();
} catch (RecordStoreException ex) {
ex.printStackTrace();
throw new IOException(ex.getMessage());
}
return Resources.op