www.pudn.com > j2mewireless_examples.zip > PhotoAlbum.java
/*
* @(#)PhotoAlbum.java 1.6 01/04/04
*
* Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*/
package examples.photoalbum;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Vector;
/**
* The PhotoAlbum MIDlet class provides the commands and screens
* that implement a simple Photo and Animation Album.
* The images and animations to be displayed are configured
* in the descriptor file with attributes.
*
* It provides simple options to vary the speed of display
* and the picture frames used.
*
*/
public class PhotoAlbum extends MIDlet
implements CommandListener, ItemStateListener
{
private Display display; // The display for this MIDlet
private PhotoFrame frame; // The Frame and Canvas for images
private ChoiceGroup borderChoice; // List of border choices
private ChoiceGroup speedChoice; // List of speed choices
private Form optionsForm; // The form holding the options
private Alert alert; // The Alert used for errors
private Vector imageNames; // Strings with the image names
private List imageList; // List of Image titles
private Command exitCommand; // The exit command
private Command okCommand; // The ok command
private Command optionsCommand; // The command to edit options
private Command backCommand; // The command to go back
/**
* Construct a new PhotoAlbum MIDlet and initialize the base
* options and main PhotoFrame to be used when the MIDlet is
* started.
*/
public PhotoAlbum() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.EXIT, 1);
optionsCommand = new Command("Options", Command.SCREEN, 1);
okCommand = new Command("Ok", Command.OK, 3);
backCommand = new Command("Back", Command.SCREEN, 3);
frame = new PhotoFrame();
frame.setStyle(2);
frame.setSpeed(2);
frame.addCommand(optionsCommand);
frame.addCommand(backCommand);
frame.setCommandListener(this);
alert = new Alert("Warning");
setupImages();
}
/**
* Start up the MIDlet by setting the display
* to show the image name list.
*/
protected void startApp() {
display.setCurrent(imageList);
}
/**
* Pausing is easy since there are no background activities
* or record stores that need to be closed.
*/
protected void pauseApp() {
frame.reset(); // Discard images cached in the frame
}
/**
* Destroy must cleanup everything not handled by the garbage
* collector.
*/
protected void destroyApp(boolean unconditional) {
frame.reset(); // Discard images cached in the frame
}
/**
* Respond to commands, including exit.
* On the exit command, cleanup and notify that
* the MIDlet has been destroyed.
*/
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
} else if (c == optionsCommand) {
display.setCurrent(genOptions());
} else if (c == okCommand && s == optionsForm) {
display.setCurrent(frame);
} else if (c == List.SELECT_COMMAND) {
int i = imageList.getSelectedIndex();
String image = (String)imageNames.elementAt(i);
try {
frame.setImage(image);
display.setCurrent(frame);
} catch (java.io.IOException e) {
alert.setString("Unable to locate " + image);
display.setCurrent(alert, imageList);
}
} else if (c == backCommand) {
display.setCurrent(imageList);
}
}
/**
* Listener for changes to options.
*/
public void itemStateChanged(Item item) {
if (item == borderChoice) {
frame.setStyle(borderChoice.getSelectedIndex());
} else if (item == speedChoice) {
frame.setSpeed(speedChoice.getSelectedIndex());
}
}
/**
* Generate the options form with speed and style choices.
* Speed choices are stop, slow, medium, and fast.
* Style choices for borders are none, plain, fancy.
*/
private Screen genOptions() {
if (optionsForm == null) {
optionsForm = new Form("Options");
optionsForm.addCommand(okCommand);
optionsForm.setCommandListener(this);
optionsForm.setItemStateListener(this);
speedChoice = new ChoiceGroup("Speed",
Choice.EXCLUSIVE);
speedChoice.append("Stop", null);
speedChoice.append("Slow", null);
speedChoice.append("Medium", null);
speedChoice.append("Fast", null);
speedChoice.setSelectedIndex(2, true);
optionsForm.append(speedChoice);
borderChoice = new ChoiceGroup("Borders",
Choice.EXCLUSIVE);
borderChoice.append("None", null);
borderChoice.append("Plain", null);
borderChoice.append("Fancy", null);
borderChoice.setSelectedIndex(2, true);
optionsForm.append(borderChoice);
}
return optionsForm;
}
/**
* Check the attributes in the Descriptor that identify
* images and titles and initialize the lists imageNames
* and imageList.
*/
private void setupImages() {
imageNames = new Vector();
imageList = new List("Images", List.IMPLICIT);
imageList.addCommand(exitCommand);
imageList.setCommandListener(this);
for (int n = 1; n < 100; n++) {
String nthImage = "PhotoImage-"+ n;
String image = getAppProperty(nthImage);
if (image == null || image.length() == 0)
break;
String nthTitle = "PhotoTitle-" + n;
String title = getAppProperty(nthTitle);
if (title == null || title.length() == 0)
title = image;
imageNames.addElement(image);
imageList.append(title, null);
}
}
}