📄 svgbrowser.java
字号:
/*
*
* Copyright (c) 2007, Sun Microsystems, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Sun Microsystems nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
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 animation. */
private static final int STATE_STOPPED = 0;
/** State representing paused animation. */
private static final int STATE_PAUSED = 1;
/** State representing running animation. */
private static final int STATE_PLAYING = 2;
/** Command for exiting 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -