📄 alert.java
字号:
} else { this.image = img; this.mutableImage = null; // Make sure to clear mutableImage } } /** * Paint the contents of this Alert given the graphics context. * * @param g The Graphics object to paint this Alert to * @param target the target Object of this repaint */ void callPaint(Graphics g, Object target) { super.callPaint(g, target); // Vertically center alert content if the alert height // is less than the view port height. int yOffset = (viewport[HEIGHT] - height) / 2; if (yOffset < 0) { yOffset = 0; } int xOffset; // Translate into screen coordinates yOffset += viewport[Y] - view[Y]; xOffset = viewport[X] - view[X]; // center and paint the image if (image != null) { int tX = (viewport[WIDTH] - image.getWidth()) / 2; if (tX < 0) { tX = 0; } tX += xOffset; g.translate(tX, yOffset); g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT); g.translate(-tX, -yOffset); // amount of space between Image and next component yOffset += image.getHeight() + CELL_SPACING; } // Paint the activity indicator if (indicator != null) { // We do not need to center the activity indicator. // That has already been done in the layout() method. int tX = indicator.bounds[X] + xOffset; g.translate(tX, yOffset); indicator.callPaint(g, viewport[WIDTH], viewport[HEIGHT]); g.translate(-tX, -yOffset); // amount of space between indicator and next component yOffset += indicator.bounds[HEIGHT] + CELL_SPACING; } // paint the text if (text != null) { g.translate(xOffset, yOffset); Text.paint(text, Screen.CONTENT_FONT, g, viewport[WIDTH] - xOffset, (viewport[HEIGHT] + viewport[Y]) - yOffset, 0, Text.NORMAL, null); g.translate(-xOffset, -yOffset); } setVerticalScroll(); } /** * Layout the content of this Alert given the width and * height parameters */ void layout() { super.layout(); // height of image, if any height = (image != null) ? image.getHeight() + CELL_SPACING : 0; // height of activity indicator, if any if (indicator != null) { if (indicator.bounds == null) { indicator.bounds = new int[4]; } int pW = indicator.callPreferredWidth(-1); int pH = indicator.callPreferredHeight(-1); // Center indicator horizontally int xOffset = (viewport[WIDTH] - pW) / 2; if (xOffset < 0) { xOffset = 0; } // We assign the item a bounds which is its pixel location, // width, and height in coordinates which represent offsets // of the viewport origin (that is, are in the viewport // coordinate space) indicator.bounds[X] = xOffset; indicator.bounds[Y] = height; indicator.bounds[WIDTH] = pW; indicator.bounds[HEIGHT] = pH; height += pH + CELL_SPACING; } // height of alert text, if any height += Text.getHeightForWidth(text, Screen.CONTENT_FONT, viewport[WIDTH], 0); // Adjust indicator if the Alert is shorter than the screen if ((indicator != null) && (height < viewport[HEIGHT])) { // Shift bounds to vertically center indicator on the screen indicator.bounds[Y] += (viewport[HEIGHT] - height) / 2; } view[HEIGHT] = height; } /** * Set the screen that the display should return to * when this Alert is dismissed. * * @param d The Displayable to display when this Alert is completed */ void setReturnScreen(Displayable d) { if (d != null) { this.returnScreen = d; } else { this.returnScreen = new Form(""); } } /** * Get the screen the display should return to * when this Alert is dismissed. * * @return The Displayable to display when this Alert is completed */ Displayable getReturnScreen() { return returnScreen; } /** * Verify the activity indicator is conformant with the spec * requirements for addition to an Alert. * * @param ind The indicator to be verified * @return boolean True if the gauge is conformant; false otherwise */ boolean isConformantIndicator(Gauge ind) { return ((ind.isInteractive() == false) && (ind.getOwner() == null) && (ind.getCommandCount() == 0) && (ind.getItemCommandListener() == null) && (ind.getLabel() == null) && (ind.getLayout() == Item.LAYOUT_DEFAULT) && (ind.lockedWidth == -1) && (ind.lockedHeight == -1)); } /** * Handle a key press * * @param keyCode the key which was pressed */ void callKeyPressed(int keyCode) { int gameAction = Display.getGameAction(keyCode); switch (gameAction) { case Canvas.UP: case Canvas.DOWN: if (scrollViewport(gameAction)) { repaintContents(); } break; default: break; } } /** * Handle a key repeat * * @param keyCode the key which was repeated */ void callKeyRepeated(int keyCode) { callKeyPressed(keyCode); } /** * Scroll the viewport in the specified direction. * * @param dir direction to scroll the viewport. * @return boolean True if the viewport was scrolled */ boolean scrollViewport(int dir) { if ((dir == Canvas.DOWN) && (view[Y] + viewport[HEIGHT] < height)) { view[Y] += SCROLL_AMOUNT; return true; } else if ((dir == Canvas.UP) && (view[Y] > 0)) { view[Y] -= SCROLL_AMOUNT; return true; } return false; }// *****************************************************// Package private members// *****************************************************// *****************************************************// Private members// ***************************************************** /** * Static default Command for "OK" */ private static final Command OK = new Command(Resource.getString("Done"), Command.OK, 0); /** * Default spacing between elements of this Alert */ private static final int CELL_SPACING = 6; /** * Number of pixels to scroll when using up/down keys */ private static final int SCROLL_AMOUNT = 40; /** * The default timeout of all alerts */ private static final int DEFAULT_TIMEOUT = 2000; /** * The type of this alert */ private AlertType type; /** * The layout object for the alert text string */ private String text; /** * The image of this alert */ private Image image; /** * A reference to the original, mutable Image passed to setImage(). This * is only so getImage() can return the correct Image Object. */ private Image mutableImage; /** * The activity indicator for this alert */ private Gauge indicator; /** * The timeout value of this alert */ private int time; /** * The overall height of this alert */ private int height; /** * A Timer which serves all Alert objects to schedule * their timout tasks */ private static Timer timeoutTimer; /** * A TimerTask which will be set to expire this Alert after * its timeout period has elapsed. */ private TimerTask timerTask; /** * The screen which the display will return to when this Alert * is completed */ private Displayable returnScreen; /** * The application's command listener */ private CommandListener userCommandListener;// *****************************************************// Internal Class// ***************************************************** /** * A TimerTask subclass which will notify the Display to * make the 'returnScreen' of this Alert the new current screen. */ class timeoutTask extends TimerTask { /** The Alert for this timeout task */ Displayable alert; /** * Create a new timeout task * * @param d the Alert */ public timeoutTask(Displayable d) { super(); alert = d; } /** * Simply set the Display's current screen to be this * Alert's return screen */ public void run() { synchronized (Display.LCDUILock) { Command cmds[] = getCommands(); Command c; if (cmds == null) { c = Alert.DISMISS_COMMAND; } else { c = cmds[0]; } // Timer task functions as if the default // Command had been activated try { synchronized (Display.calloutLock) { implicitListener.commandAction(c, alert); } } catch (Throwable thr) { Display.handleThrowable(thr); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -