📄 svgbrowser.java
字号:
handleError("Failed to open " + newPath, prevDisplayable);
}
}
/**
* Opens an SVG file and shows on the screen. If an error happens while
* opening the file, 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 svgPath the path to the SVG file
* @param prevDisplayable the Displayable to be shown in the case of error
*/
private void open(String svgPath, Displayable prevDisplayable) {
try {
SVGImage newSVGImage;
FileConnection fc = (FileConnection)Connector.open(svgPath,
Connector.READ);
try {
InputStream is = fc.openInputStream();
try {
newSVGImage = (SVGImage)ScalableImage.createImage(is, null);
} finally {
try {
is.close();
} catch (IOException e) {
// ignore
}
}
} finally {
fc.close();
}
Document newSVGDocument = newSVGImage.getDocument();
SVGSVGElement newSVGRoot = (SVGSVGElement)newSVGDocument.getDocumentElement();
SVGAnimator newSVGAnimator = SVGAnimator.createAnimator(newSVGImage);
synchronized (this) {
if ((svgAnimator != null) && (animationState != STATE_STOPPED)) {
svgAnimator.stop();
}
svgImage = newSVGImage;
svgRoot = newSVGRoot;
svgAnimator = newSVGAnimator;
// Set to 10 fps (frames per second)
svgAnimator.setTimeIncrement(0.01f);
svgAnimator.setSVGEventListener(this);
svgCanvas = (Canvas)svgAnimator.getTargetComponent();
svgCanvas.setTitle(svgPath);
svgCanvas.addCommand(backCommand);
svgCanvas.setCommandListener(this);
animationState = STATE_STOPPED;
animationNextState = STATE_PLAYING;
display.setCurrent(svgCanvas);
}
} catch (IOException e) {
handleError("Failed to open " + svgPath, prevDisplayable);
}
}
/**
* Handles error conditions. It shows an Alert with the error message.
* After the Alert timeouts the behaviour depends on the value of
* prevDisplayable. If prevDisplayable is not null it is displayed,
* otherwise the MIDlet exits.
*
* @param message the error message
* @param prevDisplayable the Displayable to be shown after the timeout
*/
private void handleError(String message, Displayable prevDisplayable) {
if (prevDisplayable == null) {
errorExit(message);
} else {
warningMessage(message, prevDisplayable);
}
}
/**
* Displays an alert with the given error message. When the alert timeouts
* the current MIDlet exits.
*
* @param message the error message
*/
private void errorExit(String message) {
Alert errorAlert = new Alert("Fatal error", message, null,
AlertType.ERROR);
errorAlert.setCommandListener(this);
synchronized (this) {
display.setCurrent(errorAlert);
}
}
/**
* Displays an alert with the given warning message. When the alert timeouts
* the given Displayable is displayed.
*
* @param message the warning message
* @param nextDisplayable the next Displayable to display
*/
private void warningMessage(String message, Displayable nextDisplayable) {
Alert warningAlert = new Alert("Warning", message, null, AlertType.WARNING);
synchronized (this) {
display.setCurrent(warningAlert, nextDisplayable);
}
}
public void keyPressed(int keyCode) {
}
public void keyReleased(int keyCode) {
}
public void pointerPressed(int x, int y) {
}
public void pointerReleased(int x, int y) {
}
public synchronized void hideNotify() {
if (animationState == STATE_PLAYING) {
svgAnimator.pause();
animationState = STATE_PAUSED;
animationNextState = STATE_PLAYING;
}
}
public synchronized void showNotify() {
svgImage.setViewportWidth(svgCanvas.getWidth());
svgImage.setViewportHeight(svgCanvas.getHeight());
if (animationState != animationNextState) {
switch (animationNextState) {
case STATE_PLAYING:
svgAnimator.play();
break;
case STATE_PAUSED:
svgAnimator.pause();
break;
case STATE_STOPPED:
svgAnimator.stop();
break;
}
animationState = animationNextState;
updateAnimatorCommands();
}
}
/**
* Updates the SVG image viewport size when the canvas size changes.
*/
public synchronized void sizeChanged(int width, int height) {
svgImage.setViewportWidth(width);
svgImage.setViewportHeight(height);
}
/**
* Updates the available commands in the menu according to the current
* animation state.
*/
private void updateAnimatorCommands() {
svgCanvas.removeCommand(playCommand);
svgCanvas.removeCommand(pauseCommand);
svgCanvas.removeCommand(stopCommand);
switch (animationState) {
case STATE_PLAYING:
svgCanvas.addCommand(pauseCommand);
svgCanvas.addCommand(stopCommand);
break;
case STATE_PAUSED:
svgCanvas.addCommand(playCommand);
svgCanvas.addCommand(stopCommand);
break;
case STATE_STOPPED:
svgCanvas.addCommand(playCommand);
break;
}
}
/**
* This methods starts the current animation.
*/
private void play() {
if ((animationState == animationNextState) && (animationState != STATE_PLAYING)) {
svgAnimator.play();
animationState = STATE_PLAYING;
updateAnimatorCommands();
}
animationNextState = STATE_PLAYING;
}
/**
* This methods pauses the current animation.
*/
private void pause() {
if ((animationState == animationNextState) && (animationState != STATE_PAUSED)) {
svgAnimator.pause();
animationState = STATE_PAUSED;
updateAnimatorCommands();
}
animationNextState = STATE_PAUSED;
}
/**
* This methods stops the current animation.
*/
private void stop() {
if ((animationState == animationNextState) && (animationState != STATE_STOPPED)) {
svgAnimator.stop();
svgRoot.setCurrentTime(0);
animationState = STATE_STOPPED;
updateAnimatorCommands();
}
animationNextState = STATE_STOPPED;
}
private final class BrowseAction extends Thread {
private final String directory;
private final int depth;
private final Displayable prevDisplayable;
public BrowseAction(String directory, int depth,
Displayable prevDisplayable) {
this.directory = directory;
this.depth = depth;
this.prevDisplayable = prevDisplayable;
}
public void run() {
browse(directory, depth, prevDisplayable);
}
}
private final class OpenAction extends Thread {
private final String svgFile;
private final Displayable prevDisplayable;
public OpenAction(String svgFile, Displayable prevDisplayable) {
this.svgFile = svgFile;
this.prevDisplayable = prevDisplayable;
}
public void run() {
open(svgFile, prevDisplayable);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -