📄 svgbrowser.java
字号:
/*
*
* Copyright © 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
package com.sun.perseus.demo.svgbrowser;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.m2g.SVGAnimator;
import javax.microedition.m2g.SVGEventListener;
import javax.microedition.m2g.SVGImage;
import javax.microedition.m2g.ScalableImage;
import javax.microedition.midlet.MIDlet;
import org.w3c.dom.Document;
import org.w3c.dom.svg.SVGSVGElement;
/**
* This demo implements a simple file browser which allows opening SVG images
* and animations.
*/
public final class SVGBrowser extends MIDlet implements CommandListener, SVGEventListener {
/*
* Animation state constants.
*/
/** State representing stopped adnimation. */
private static final int STATE_STOPPED = 0;
/** State representing paused adnimation. */
private static final int STATE_PAUSED = 1;
/** State representing running adnimation. */
private static final int STATE_PLAYING = 2;
/** Command for exitting the MIDlet. */
private final Command exitCommand;
/** Command for returning from a displayed SVG image. */
private final Command backCommand;
/** Command for entering directory or opening SVG file. */
private final Command openCommand;
/** Command for starting a SVG animation. */
private final Command playCommand;
/** Command for pausing a SVG animation. */
private final Command pauseCommand;
/** Command for stopping a SVG animation. */
private final Command stopCommand;
/** Screen with directory / SVG file listing. */
private final List browserList;
/** The loaded SVG image. */
private SVGImage svgImage;
/** The root <svg> element of the image. */
private SVGSVGElement svgRoot;
/** The animator created for the image. */
private SVGAnimator svgAnimator;
/** The canvas from the animator. */
private Canvas svgCanvas;
/** The current animation state. */
private int animationState = STATE_STOPPED;
/** The next animation state. */
private int animationNextState = STATE_STOPPED;
/** The display assigned for the MIDlet. */
private final Display display;
/** The parent path of the current path. */
private String parentPath;
/** The browsing depth. Value 0 means a virtual root of all filesystems. */
private int browsingDepth;
/** The current path. */
private String currentPath;
/** Creates a new instance of SVGBrowser. */
public SVGBrowser() {
browserList = new List(null, Choice.IMPLICIT);
exitCommand = new Command("Exit", Command.EXIT, 1);
backCommand = new Command("Back", Command.BACK, 1);
openCommand = new Command("Open", Command.OK, 1);
playCommand = new Command("Play", Command.SCREEN, 1);
pauseCommand = new Command("Pause", Command.SCREEN, 1);
stopCommand = new Command("Stop", Command.SCREEN, 2);
browserList.addCommand(exitCommand);
browserList.addCommand(openCommand);
browserList.setSelectCommand(openCommand);
browserList.setCommandListener(this);
display = Display.getDisplay(this);
new BrowseAction("file:///", 0, null).start();
}
protected void startApp() {
}
protected void pauseApp() {
}
protected synchronized void destroyApp(boolean unconditional) {
if ((svgAnimator != null) && (animationState != STATE_STOPPED)) {
svgAnimator.stop();
animationState = STATE_STOPPED;
animationNextState = STATE_STOPPED;
}
}
public synchronized void commandAction(Command c, Displayable d) {
if ((c == exitCommand) || (c == Alert.DISMISS_COMMAND)) {
destroyApp(false);
notifyDestroyed();
} else if (c == openCommand) {
int selectedIdx = browserList.getSelectedIndex();
if ((selectedIdx == 0) && (browsingDepth > 0)) {
new BrowseAction(parentPath,
browsingDepth - 1,
browserList).start();
} else {
String selectedString = browserList.getString(selectedIdx);
if (selectedString.endsWith("/")) {
// open the selected directory
new BrowseAction(currentPath + selectedString,
browsingDepth + 1,
browserList).start();
} else {
// open the selected svg file
new OpenAction(currentPath + selectedString,
browserList).start();
}
}
} else if (c == backCommand) {
display.setCurrent(browserList);
} else if (c == playCommand) {
play();
} else if (c == pauseCommand) {
pause();
} else if (c == stopCommand) {
stop();
}
}
/**
* Opens browser for the given path. If an error happens while reading
* the directory content, an alert with the error message is displayed.
* After timeout this alert is dismissed automatically and the
* prevDisplayable is shown. If prevDisplayable is null and an error happens
* the MIDlet is ended after the alert timeouts.
*
* @param newPath the path to browse
* @param newDepth the browsing depth, 0 means "root"
* @param prevDisplayable the Displayable to be shown in the case of error
*/
private void browse(String newPath, int newDepth,
Displayable prevDisplayable) {
try {
String newParentPath;
Vector directories = new Vector();
Vector svgFiles = new Vector();
if (newDepth == 0) {
newParentPath = null;
// get filesystems listing
Enumeration roots = FileSystemRegistry.listRoots();
if (!roots.hasMoreElements()) {
// this is fatal, no filesystems enumerated
handleError("No filesystems found", prevDisplayable);
return;
}
do {
directories.addElement(roots.nextElement());
} while (roots.hasMoreElements());
} else {
int slashIndex = newPath.lastIndexOf('/', newPath.length() - 2);
// slashIndex != -1
newParentPath = newPath.substring(0, slashIndex + 1);
// get directory listing
FileConnection fc =
(FileConnection)Connector.open(newPath, Connector.READ);
try {
Enumeration files = fc.list();
while (files.hasMoreElements()) {
String fileName = (String)files.nextElement();
if (fileName.endsWith("/")) {
directories.addElement(fileName);
continue;
}
int dotIndex = fileName.lastIndexOf('.');
if (dotIndex != -1) {
String extension = fileName.substring(dotIndex + 1);
if ("svg".equalsIgnoreCase(extension)) {
svgFiles.addElement(fileName);
}
}
}
} finally {
fc.close();
}
}
synchronized (this) {
browserList.setTitle(newPath);
browserList.deleteAll();
if (newDepth > 0) {
browserList.append("..", null);
}
Enumeration e;
// add directories
e = directories.elements();
while (e.hasMoreElements()) {
browserList.append((String)e.nextElement(), null);
}
// add svg files
e = svgFiles.elements();
while (e.hasMoreElements()) {
browserList.append((String)e.nextElement(), null);
}
parentPath = newParentPath;
browsingDepth = newDepth;
currentPath = newPath;
display.setCurrent(browserList);
}
} catch (IOException e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -