teststringitemsizing.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 511 行 · 第 1/2 页
JAVA
511 行
maxIs = numOfIperLine[i]; } } return params[ITEM_LEFT_PAD] + params[CONTENT_LEFT_PAD] + maxIs*params[TEXT_I_WIDTH]+ params[CONTENT_RIGHT_PAD] + params[ITEM_RIGHT_PAD]; } int calculateHeightEmptyLabel(int params[], int numOfLines) { return params[ITEM_TOP_PAD] + params[CONTENT_LEFT_PAD] + params[TEXT_FONT_HEIGHT]*numOfLines + params[CONTENT_RIGHT_PAD] + params[ITEM_BOTTOM_PAD]; } int calculateWidthEmptyText(int params[], int numOfIperLine[]) { int maxIs = 0; for (int i=0; i<numOfIperLine.length; i++) { if (numOfIperLine[i] > maxIs) { maxIs = numOfIperLine[i]; } } return params[ITEM_LEFT_PAD] + maxIs*params[LABEL_I_WIDTH]+ params[ITEM_RIGHT_PAD]; } int calculateHeightEmptyText(int params[], int numOfLines) { return params[ITEM_TOP_PAD] + params[LABEL_FONT_HEIGHT]*numOfLines + params[ITEM_BOTTOM_PAD]; } int calculateWidthSameLine(int params[], int labelNumOfI, int textNumOfI) { return params[ITEM_LEFT_PAD] + labelNumOfI*params[LABEL_I_WIDTH] + params[LABEL_BODY_HORIZ_PAD] + params[CONTENT_LEFT_PAD] + textNumOfI*params[TEXT_I_WIDTH]+ params[CONTENT_RIGHT_PAD] + params[ITEM_RIGHT_PAD]; } int calculateHeightSameLine(int params[]) { int contentHeight = params[CONTENT_LEFT_PAD] + params[TEXT_FONT_HEIGHT] + params[CONTENT_RIGHT_PAD]; return params[ITEM_TOP_PAD] + params[ITEM_BOTTOM_PAD] + (params[LABEL_FONT_HEIGHT] > contentHeight ? params[LABEL_FONT_HEIGHT] : contentHeight); } int calculateWidthMultLines(int params[], int labelNumOfIperLine[], int textNumOfIperLine[]) { int labelMaxI = 0; for(int i=0; i<labelNumOfIperLine.length; i++) { if (labelNumOfIperLine[i] > labelMaxI) { labelMaxI = labelNumOfIperLine[i]; } } labelMaxI *= params[LABEL_I_WIDTH]; int textMaxI = 0; for(int i=0; i<textNumOfIperLine.length; i++) { if (textNumOfIperLine[i] > textMaxI) { textMaxI = textNumOfIperLine[i]; } } textMaxI *= params[TEXT_I_WIDTH]; textMaxI += params[CONTENT_LEFT_PAD] + params[CONTENT_RIGHT_PAD]; return params[ITEM_LEFT_PAD] + params[ITEM_RIGHT_PAD] + (labelMaxI > textMaxI ? labelMaxI : textMaxI); } int calculateHeightMultLines(int params[], int labelNumLines, int textNumLines) { return params[ITEM_TOP_PAD] + labelNumLines*params[LABEL_FONT_HEIGHT] + params[LABEL_BODY_VERTICAL_PAD] + params[CONTENT_TOP_PAD] + labelNumLines*params[TEXT_FONT_HEIGHT] + params[CONTENT_BOTTOM_PAD] + params[ITEM_BOTTOM_PAD]; } // it is assumed in the following function that all 'i's following each // other can fit on one line. '\n' is the only line break // All characters other than '\n' should be 'i' int []getNumOfIsPerLine(String str) { if (str == null || str.length() == 0) { return new int[]{0}; } int lines = 1; for(int i=0; i < str.length(); i++) { if (str.charAt(i) == '\n') { lines++; } } int return_array[] = new int[lines]; int j = 0, numOfIs = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == '\n') { return_array[j] = numOfIs; numOfIs = 0; j++; } else { numOfIs++; } } return_array[j] = numOfIs; return return_array; } int[] getParameters(StringItem strItem) { // Hyperlink and Button appearance settings are ignored // if there are no commands added // If a command is added to a StringItem with PLAIN appearance mode // it is changed to be HYPERLINK switch(strItem.getAppearanceMode()) { case Item.PLAIN: return (strItem.numCommands == 0 ? PLAIN_PRMS : HYPERLINK_PRMS); case Item.HYPERLINK: return (strItem.numCommands == 0 ? PLAIN_PRMS : HYPERLINK_PRMS); case Item.BUTTON: return (strItem.numCommands == 0 ? PLAIN_PRMS : BUTTON_PRMS); } return null; } int calculateWidth(StringItem strItem, int type) { int params[] = getParameters(strItem); switch (type) { case EMPTY: return 0; case EMPTY_LABEL: return calculateWidthEmptyLabel(params, getNumOfIsPerLine(strItem.str)); case EMPTY_TEXT: return calculateWidthEmptyText(params, getNumOfIsPerLine(strItem.label)); case SAME_LINE: // we rely on the fact that both label and text are present // and not empty return calculateWidthSameLine(params, getNumOfIsPerLine(strItem.label)[0], getNumOfIsPerLine(strItem.str)[0]); case MULT_LINE: return calculateWidthMultLines(params, getNumOfIsPerLine(strItem.label), getNumOfIsPerLine(strItem.str)); default: System.err.println("Incorrect type passing"); return 0; } } int calculateHeight(StringItem strItem, int type) { int params[] = getParameters(strItem); switch (type) { case EMPTY: return 0; case EMPTY_LABEL: return calculateHeightEmptyLabel(params, getNumOfIsPerLine(strItem.str).length); case EMPTY_TEXT: return calculateHeightEmptyText(params, getNumOfIsPerLine(strItem.label).length); case SAME_LINE: return calculateHeightSameLine(params); case MULT_LINE: return calculateHeightMultLines(params, getNumOfIsPerLine(strItem.label).length, getNumOfIsPerLine(strItem.str).length); default: System.err.println("Incorrect type passing"); return 0; } } // the tests public void testDefaultPreferredWidth(StringItemInfo strItemInfo) { assertEquals(calculateWidth(strItemInfo.strItem, strItemInfo.type), strItemInfo.strItem.stringItemLF.lGetPreferredWidth(-1)); } public void testDefaultPreferredHeight(StringItemInfo strItemInfo) { assertEquals(calculateHeight(strItemInfo.strItem, strItemInfo.type), strItemInfo.strItem.stringItemLF.lGetPreferredHeight(-1)); } public void runTests() { StringItemInfo newItemInfos[] = createTestArray(); for (int i = 0 ; i < newItemInfos.length; i++) { declare("testPreferredWidth StringItem(" + newItemInfos[i].description); testDefaultPreferredWidth(newItemInfos[i]); declare("testPreferredHeight StringItem(" + newItemInfos[i].description); testDefaultPreferredHeight(newItemInfos[i]); } } class StringItemInfo { StringItemInfo(StringItem strItem, int type, String description) { this.strItem = strItem; this.type = type; this.description = description; } StringItem strItem; int type; String description; };}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?