📄 phonepolicy.java
字号:
/* * @(#)PhonePolicy.java 1.50 01/08/21 * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */package javax.microedition.lcdui;/** * A policy class to handle telephone numbers */class PhonePolicy extends Policy { /** The x, y, and ofset of the phone number */ int x, y, offset; /** The number of characters in this policy */ int numChars; /** * Construct a new PhonePolicy * * @param f The Font to use in this policy */ PhonePolicy(Font f) { super(f); } /** * Validate the content of this policy * * @param buffer The data of this policy * @param offset The start of the data in the buffer * @param length The length of the data in the buffer * @return boolean A flag indicating this policy's data is valid */ boolean validateContent(char[] buffer, int offset, int length) { for (int i = offset; i < (offset + length); i++) { char c = buffer[i]; if (((c < '0') || (c > '9')) && (!(c == '#' || c == '*' || c == '+'))) { return false; } } return true; } // puts an extra space at the end if necessary /** * Paint a block of this policy * * @param g The Graphics context to paint to * @param buf The data buffer of this policy * @param cursorPos The location of the cursor * @param cursorEnabled A boolean flag indicating if the cursor is enabled * @param numCharsInBlock The number of characters to paint in this block */ void paintBlock(Graphics g, char[] buf, int cursorPos, boolean cursorEnabled, int numCharsInBlock) { int neededSpace = 0; int xCurPos = 0; int numCharsToPaint = numChars - offset; if (numCharsToPaint > numCharsInBlock) { numCharsToPaint = numCharsInBlock; } int clipY; if (g == null) { clipY = 3*lineHeight; } else { clipY = g.getClipY() + g.getClipHeight(); } if (numCharsToPaint > 0) { neededSpace = f.charsWidth(buf, offset, numCharsToPaint); // System.out.println("neededSpace is "+neededSpace+" x is "+x+ // " width is "+width); if ((x + neededSpace) > width) { if (neededSpace <= width) { y += lineHeight; if (y + lineHeight > clipY) { return; } x = 0; } else { int numDigsOnLine = width / f.charWidth('9'); paintBlock(g, buf, cursorPos, cursorEnabled, numDigsOnLine); paintBlock(g, buf, cursorPos, cursorEnabled, numCharsToPaint - numDigsOnLine); return; } } if (g != null) { g.drawChars(buf, offset, numCharsToPaint, x, y, Graphics.LEFT | Graphics.TOP); if (cursorEnabled) { if (((cursorPos > offset) && (cursorPos <= offset + numCharsToPaint)) || (cursorPos == 0 && offset == 0)) { // int xCurPos = x + neededSpace; cursorX = x + f.charsWidth(buf, offset, cursorPos - offset); cursorY = y; g.drawLine(cursorX, cursorY, cursorX, cursorY + lineHeight); } } } x += neededSpace; if (numCharsToPaint == numCharsInBlock) { x += f.charWidth(' '); } offset += numCharsToPaint; } } /** * paint the phone number using phone format * For example 123 3456. * * @param g graphics context, could be null when it is called * during layout to calculate height. * @param buf buffer * @param numChars no of chars * @param cursorEnabled cursorEnabled * @param cursorPos cursor Position * @param noCharShow noCharShow */ void paint(Graphics g, char[] buf, int numChars, boolean cursorEnabled, int cursorPos, boolean noCharShow) { y = 0; x = 0; offset = 0; this.numChars = numChars; if (g != null) { // We assume that background was cleared with erase color g.setColor(Display.FG_COLOR); g.setFont(f); } if (numChars > 0) { if (buf[0] == '0') { // international paintBlock(g, buf, cursorPos, cursorEnabled, 3); paintBlock(g, buf, cursorPos, cursorEnabled, 3); paintBlock(g, buf, cursorPos, cursorEnabled, numChars - offset); } else if (buf[0] == '1') { // long distance with 1 as starter paintBlock(g, buf, cursorPos, cursorEnabled, 1); paintBlock(g, buf, cursorPos, cursorEnabled, 3); paintBlock(g, buf, cursorPos, cursorEnabled, 3); paintBlock(g, buf, cursorPos, cursorEnabled, numChars - offset); } else { // local & long distance paintBlock(g, buf, cursorPos, cursorEnabled, 3); if ((numChars - offset) > 4) { paintBlock(g, buf, cursorPos, cursorEnabled, 3); } paintBlock(g, buf, cursorPos, cursorEnabled, numChars - offset); } height = y + lineHeight; } else { if ((cursorEnabled) && (g != null)) { // paint cursor when no chars need to be painted g.drawLine(0, 0, 0, lineHeight); } height = lineHeight; } } /** * Signal this policy that its content has changed * * @param buf The policy's data buffer * @param numChars The number of characters in the buffer * @param fromPos The start position of the change * @param curPos The current position in the buffer * @return int */ int contentChanged(char[] buf, int numChars, int fromPos, int curPos) { if (width == -1) { return 0; } int oldHeight = height; // REMIND!!! Is there are a better way to do it??? // call paint with NULL graphics context to calculate the height paint(null, buf, numChars, false, 0, false); return (height - oldHeight); } /** * Move the cursor in this policy * * @param dir The direction the cursor is moving * @param curPos The position of the cursor * @param buf The data buffer of this policy * @param numChars The number of characters in this policy's buffer * @return int The new cursor position */ int moveCursor(int dir, int curPos, char[] buf, int numChars) { switch (dir) { case Canvas.LEFT: if (curPos > 0) { --curPos; } break; case Canvas.RIGHT: if (curPos < numChars) { ++curPos; } break; } return curPos; } /** * Set the constrained size of this policy * * @param size * @param buffer * @return int */ int constrainedSize(int size, char buffer[]) { // allowing a max of 3 lines for phone number int maxNumCharsAllowed = (Display.WIDTH-8)/f.charWidth('9'); int currentH = (Display.HEIGHT - 8)/lineHeight; if (currentH >= 3) { maxNumCharsAllowed *= 2; } else { maxNumCharsAllowed -= lineHeight; } if ((buffer.length == 0) || (buffer[0] == '1')) { maxNumCharsAllowed += 7; } else { maxNumCharsAllowed += 6; } if (size > maxNumCharsAllowed) { return maxNumCharsAllowed; } else { return size; } } /** * Get the maximum width of this policy * * @param allowedWidth * @param maxSize * @return int */ int getMaxWidth(int allowedWidth, int maxSize) { return allowedWidth; } /** * Get the minimum height of this policy * * @param totalHeight * @return int */ int getMinimumHeight(int totalHeight) { int currentH = (totalHeight - 8)/lineHeight; if (currentH >= 3) { return 3 * lineHeight; } else { return 2 * lineHeight; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -