symbolinputmode.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 728 行 · 第 1/2 页
JAVA
728 行
{ // choose the table sizes rows = cols = (int) Math.sqrt(symbolTableChars.length); if (rows*cols < symbolTableChars.length) { rows++; } if (rows*cols < symbolTableChars.length) { cols++; } } int w = getWidth() / cols; int h = getHeight() / rows; cellSize = (w > h) ? h : w; int cw = 0, ch = 0; int temp_cols = getWidth() / cellSize; if ( temp_cols > cols) { cols = temp_cols; rows = (int)Math.ceil((double)symbolTableChars.length / cols); } int[] fs = { Font.SIZE_LARGE, Font.SIZE_MEDIUM, Font.SIZE_SMALL }; for (int i = 0; i < fs.length; i++) { font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, fs[i]); cw = font.charWidth('M'); ch = font.getHeight(); if (cw <= cellSize && ch <= cellSize) { break; } } ww = cols * cellSize; wh = rows * cellSize; hmargin = (cellSize - ch) / 2; wmargin = cellSize / 2; wx = (getWidth() - ww) / 2; wy = (getHeight() - wh) / 2; } /** * Notify this symbol table its being shown on the screen. * Overrides Canvas.showNotify. */ protected void showNotify() { pos = newpos = defaultSymbolCursorPos; } /** * Called when the drawable area of the <code>Canvas</code> has * been changed. This * method has augmented semantics compared to {@link * Displayable#sizeChanged(int,int) Displayable.sizeChanged}. * * @param w the new width in pixels of the drawable area of the * <code>Canvas</code> * @param h the new height in pixels of the drawable area of * the <code>Canvas</code> */ protected void sizeChanged(int w, int h) { calculateProportions(); } /** * Paint this symbol table. * Overrides Canvas.paint. * * @param g The Graphics object to paint to */ protected void paint(Graphics g) { paintPanel(g); } /** * Paint the symbol table panel * * @param g The Graphics object to paint to */ void paintPanel(Graphics g) { int clipX = g.getClipX(); int clipY = g.getClipY(); int clipW = g.getClipWidth(); int clipH = g.getClipHeight(); Font old_font = g.getFont(); g.setFont(font); g.setGrayScale(255); g.fillRect(clipX, clipY, clipW, clipH); int sr = (clipY - wy) / cellSize; if (sr < 0) sr = 0; int sc = (clipX - wx) / cellSize; if (sc < 0) sc = 0; int er = (clipH + clipY + cellSize - wy) / cellSize; if (er > rows - 1) er = rows - 1; int ec = (clipW + clipX + cellSize - wx) / cellSize; if (ec > cols - 1) ec = cols - 1; g.setGrayScale(0); g.drawRect(wx, wy, ww, wh); for (int r = sr; r <= er; r++) { for (int c = sc; c <= ec; c++) { int i = r * cols + c; if (i >= symbolTableChars.length) { break; } if (i == newpos) { g.setGrayScale(0); setCursorCoord(newpos); g.fillRect(x_cursor_coord, y_cursor_coord, cellSize - margin, cellSize - margin); drawChar(g, symbolTableChars[i], r, c, true); g.setGrayScale(255); pos = newpos; } else { drawChar(g, symbolTableChars[i], r, c, false); } } } g.setFont(old_font); } /** * Draw a character * * @param g The Graphics object to paint to * @param c The character to draw * @param row The row the character is in * @param col The column the character is in * @param reverse A flag to draw the character in inverse */ void drawChar(Graphics g, char c, int row, int col, boolean reverse) { int y = wy + row * cellSize + hmargin; int x = wx + col * cellSize + wmargin; Font old_font = g.getFont(); g.setFont(font); g.setGrayScale(reverse ? 255 : 0); g.drawChar(c, x, y, Graphics.HCENTER | Graphics.TOP); g.setFont(old_font); } /** * Move focus to the symbol * @param x x coordinate of the pointer * @param y y coordinate of the pointer */ protected void pointerPressed(int x, int y) { int pressedId; pressedId = getIdAtPointerPosition(x, y); // move focus to the clicked position if (pressedId < symbolTableChars.length && pressedId >= 0) { newpos = pressedId; repaint(); } } /** * Select the symbol by one click * @param x x coordinate of the pointer * @param y y coordinate of the pointer */ protected void pointerReleased(int x, int y) { int pressedId; // select character pressedId = getIdAtPointerPosition(x, y); if (pos == pressedId) { new Thread(accept).start(); } } /** * Helper function to determine the cell index at the x,y position * * @param x, y pointer coordinates in this popup * layer's space (0,0 means left-top corner) * * @return cell id at the pointer position */ private int getIdAtPointerPosition(int x, int y) { int ret = -1; int col = (x - wx) / cellSize; int row = (y - wy) / cellSize; if (col >= 0 && col < cols && row >= 0 && row < rows) { ret = cols * row + col; } return ret; } /** * Handle a key press event on this symbol table. * Overrides Canvas.keyPressed. * * @param keyCode The key that was pressed */ protected void keyPressed(int keyCode) { validateState(true); if (mediator != null && mediator.isClearKey(keyCode)) { new Thread(reject).start(); } else { switch (getGameAction(keyCode)) { case Canvas.RIGHT: if ((pos + 1) < symbolTableChars.length) { newpos = pos + 1; setCursorCoord(pos); repaint(x_cursor_coord,y_cursor_coord,cellSize - margin,cellSize - margin); setCursorCoord(newpos); repaint(x_cursor_coord,y_cursor_coord,cellSize - margin,cellSize - margin); } break; case Canvas.LEFT: if (pos > 0) { newpos = pos - 1; setCursorCoord(pos); repaint(x_cursor_coord,y_cursor_coord,cellSize - margin,cellSize - margin); setCursorCoord(newpos); repaint(x_cursor_coord,y_cursor_coord,cellSize - margin,cellSize - margin); } break; case Canvas.UP: { int p = pos - cols; if (p >= 0) { newpos = p; setCursorCoord(pos); repaint(x_cursor_coord,y_cursor_coord,cellSize - margin,cellSize - margin); setCursorCoord(newpos); repaint(x_cursor_coord,y_cursor_coord,cellSize - margin,cellSize - margin); } break; } case Canvas.DOWN: { int p = pos + cols; if (p < symbolTableChars.length) { newpos = p; setCursorCoord(pos); repaint(x_cursor_coord,y_cursor_coord,cellSize - margin,cellSize - margin); setCursorCoord(newpos); repaint(x_cursor_coord,y_cursor_coord,cellSize - margin,cellSize - margin); } break; } case Canvas.FIRE: new Thread(accept).start(); break; } } } /** * Set bounds of repaint area * @param curr_pos - position of cursor */ private void setCursorCoord(int curr_pos) { int row = curr_pos / cols; int col = curr_pos % cols; y_cursor_coord = wy + row * cellSize; x_cursor_coord = wx + col * cellSize; } /** * This method is used to immediately commit the given * string and then call the TextInputMediator's inputModeCompleted() * method * @param c command * @param d displayable */ public void commandAction(Command c, Displayable d) { validateState(true); if (c == backCmd) { new Thread(reject).start(); } else if (c == okCmd) { new Thread(accept).start(); } } } /** * Complete current input mode * @param commit true if the symbol has to be committed otherwise false */ protected void completeInputMode(boolean commit) { if (commit) { commitPendingChar(); } mediator.inputModeCompleted(); } /** * Commit pending char * @return true if the char has been committed otherwise false */ protected boolean commitPendingChar() { boolean committed = false; if (lastKey != KEYCODE_NONE) { committed = true; mediator.commit(String.valueOf((char)lastKey)); } lastKey = KEYCODE_NONE; return committed; } /** * Check if the char is the symbol from the symbol table * @param c char * @return true if this char exists in the symbol table otherwise false. */ public static boolean isSymbol(char c) { for (int i = 0; i < symbolTableChars.length; i++) { if (symbolTableChars[i] == c) { return true; } } return false; } /** this mode is not set as default. So the map is initialized by false */ private static final boolean[][] isMap = new boolean[TextInputSession.INPUT_SUBSETS.length + 1] [TextInputSession.MAX_CONSTRAINTS]; /** * Returns the map specifying this input mode is proper one for the * particular pair of input subset and constraint. The form of the map is * * |ANY|EMAILADDR|NUMERIC|PHONENUMBER|URL|DECIMAL| * --------------------------------------------------------------------- * IS_FULLWIDTH_DIGITS |t|f| t|f | t|f | t|f |t|f| t|f | * IS_FULLWIDTH_LATIN |t|f| t|f | t|f | t|f |t|f| t|f | * IS_HALFWIDTH_KATAKANA |t|f| t|f | t|f | t|f |t|f| t|f | * IS_HANJA |t|f| t|f | t|f | t|f |t|f| t|f | * IS_KANJI |t|f| t|f | t|f | t|f |t|f| t|f | * IS_LATIN |t|f| t|f | t|f | t|f |t|f| t|f | * IS_LATIN_DIGITS |t|f| t|f | t|f | t|f |t|f| t|f | * IS_SIMPLIFIED_HANZI |t|f| t|f | t|f | t|f |t|f| t|f | * IS_TRADITIONAL_HANZI |t|f| t|f | t|f | t|f |t|f| t|f | * MIDP_UPPERCASE_LATIN |t|f| t|f | t|f | t|f |t|f| t|f | * MIDP_LOWERCASE_LATIN |t|f| t|f | t|f | t|f |t|f| t|f | * NULL |t|f| t|f | t|f | t|f |t|f| t|f | * * @return input subset x constraint map */ public boolean[][] getIsConstraintsMap() { return isMap; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?