layoutmanager.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,377 行 · 第 1/4 页
JAVA
1,377 行
/* * * * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */package javax.microedition.lcdui;import com.sun.midp.log.Logging;import com.sun.midp.log.LogChannels;import com.sun.midp.configurator.Constants;/** * Layout management class for <code>Form</code>. * See DisplayableLF.java for naming convention. */class LayoutManager { // Required test: multiple/simultaneous forms with different layout /** * Singleton design pattern. Obtain access using instance() method. */ LayoutManager() { sizingBox = new int[3]; // x,y,width } /** * Do layout. * SYNC NOTE: caller must hold LCDUILock around a call to this method * * @param layoutMode one of <code>FULL_LAYOUT</code> or * <code>UPDATE_LAYOUT</code> * @param numOfLFs number of elements in the calling form * @param itemLFs reference to the items array of the calling form * @param inp_viewportWidth width of the screen area available for the form * @param inp_viewportHeight height of the screen area available * for the form * @param viewable area needed for the content of the form */ void lLayout(int layoutMode, ItemLFImpl[] itemLFs, int numOfLFs, int inp_viewportWidth, int inp_viewportHeight, int[] viewable) { viewportWidth = inp_viewportWidth; viewportHeight = inp_viewportHeight; if (Logging.REPORT_LEVEL <= Logging.INFORMATION) { Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI_FORM_LAYOUT, "\n<<<<<<<<<< Doing " + (layoutMode == FULL_LAYOUT ? "FULL_LAYOUT" : "UPDATE_LAYOUT") + "... >>>>>>>>>>"); } if (layoutMode == FULL_LAYOUT) { // first Layout updateBlock(0, 0, true, itemLFs, numOfLFs, viewable); } else { // UPDATE_LAYOUT // most of the time only one or two items are updated at a time. // here we find the minimum items that needs a re-layout, and // calling layout for them if (Logging.REPORT_LEVEL <= Logging.INFORMATION) { Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI_FORM_LAYOUT, "UPDATE_LAYOUT - START"); } // loop on all the items, and find which item needs an update // If more than one Item needs update, they both will update, // in their layout order. // * we keep a "moving anchor" to use when we identify // an invalid Item. This anchor is always at the beginning // of the row above the current Item checked, or at the // beginning of the row of the current Item, in case there // was an explicit line break. int anchorIndex = 0; // this index is needed to identify new lines to be set as // anchors later. int newLineIndex = 0; // find where to start the layout. It should be at the beginning // of the line above the invalid item, or of the same line // in case the line break is explicit. // We loop on all the Items to find the first invalid, while // keeping the anchorIndex up do date. When calling "updateBlock", // the index will jump directly to the next block, because we laid // out all the Items in that block. for (int index = 0; index < numOfLFs; index++) { if (Logging.REPORT_LEVEL <= Logging.INFORMATION) { Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI_FORM_LAYOUT, "\n["+itemLFs[index]+"]" + "BEFORE: index: " +index + "\t[" + itemLFs[index].bounds[X] + "," + itemLFs[index].bounds[Y] + "," + itemLFs[index].bounds[WIDTH] + "," + itemLFs[index].bounds[HEIGHT] + "]\t newLine?" + itemLFs[index].isNewLine + " lineHeight=" + itemLFs[index].rowHeight + "\t actualBoundsInvalid[" + itemLFs[index].actualBoundsInvalid[X] + "," + itemLFs[index].actualBoundsInvalid[Y] + "," + itemLFs[index].actualBoundsInvalid[WIDTH] + "," + itemLFs[index].actualBoundsInvalid[HEIGHT] + "]\t ** viewable: " + index + "\t[" + viewportWidth + "," + viewable[HEIGHT] +"]"); } if (itemLFs[index].actualBoundsInvalid[WIDTH] || itemLFs[index].actualBoundsInvalid[X]) { if (Logging.REPORT_LEVEL <= Logging.INFORMATION) { Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI_FORM_LAYOUT, "> WIDTH or X is invalid!"); } // if width is changed, than we have to do a layout two // a block of Items. So it covers the height as well call // layout block. The index will jump to the first item on // the next block. // return the last item on the block: index = updateBlock(anchorIndex, index, false, itemLFs, numOfLFs, viewable); // set the anchor on the next item, if there is one. // if (i+1) is larger than the length of itemLFs, the for // loop will end anyway so we don't have to check it here. anchorIndex = index + 1; } else if (itemLFs[index].actualBoundsInvalid[HEIGHT]) { // item current height int h = itemLFs[index].bounds[HEIGHT]; // item preferred height int ph = itemLFs[index].lGetAdornedPreferredHeight( itemLFs[index].bounds[WIDTH]); if (Logging.REPORT_LEVEL <= Logging.INFORMATION) { Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI_FORM_LAYOUT, "> HEIGHT is invalid from:" + h + " to:" + ph); } if (h != ph) { itemLFs[index].lSetSize(itemLFs[index].bounds[WIDTH], ph); itemLFs[index].rowHeight += (ph-h); // NOTE: We should check whole row height, // instead of just this item. itemLFs[index].actualBoundsInvalid[HEIGHT] = false; if (numOfLFs > index+1) { itemLFs[index+1].actualBoundsInvalid[Y] = true; // NOTE: We should calculate new LineHeight, // instead of just Item Height updateVertically(index+1, itemLFs, numOfLFs, viewable); } else { // only need to update the viewable viewable[HEIGHT] += (ph - h); } } } else if (itemLFs[index].actualBoundsInvalid[Y]) { if (Logging.REPORT_LEVEL <= Logging.INFORMATION) { Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI_FORM_LAYOUT, "> *only* Y is invalid for #" + index); } updateVertically(index, itemLFs, numOfLFs, viewable); if (Logging.REPORT_LEVEL <= Logging.INFORMATION) { Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI_FORM_LAYOUT, "> Y - done"); } } else { // current item is valid. // check if i can be a new anchor point // (has an explicit line break before it, // or after the previous Item). if (itemLFs[index].isNewLine) { if (itemLFs[index].equateNLB() || ((index > 0) && (itemLFs[index-1].equateNLA()))) { // explicit newline anchorIndex = index; newLineIndex = index; } else { // implicit newline // we can move the anchor to the next line: // set the anchorIndex to be the old newLineIndex // (which is the first item on the row above the // current item). anchorIndex = newLineIndex; // set i as the first in its line newLineIndex = index; } } } if (Logging.REPORT_LEVEL <= Logging.INFORMATION) { Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI_FORM_LAYOUT, "AFTER: index: " + index + "\t[" + itemLFs[index].bounds[X] + "," + itemLFs[index].bounds[Y] + "," + itemLFs[index].bounds[WIDTH] + "," + itemLFs[index].bounds[HEIGHT] + "]\t newLine?" + itemLFs[index].isNewLine + " lineHeight=" + itemLFs[index].rowHeight + "\t actualBoundsInvalid[" + itemLFs[index].actualBoundsInvalid[X] + "," + itemLFs[index].actualBoundsInvalid[Y] + "," + itemLFs[index].actualBoundsInvalid[WIDTH] + "," + itemLFs[index].actualBoundsInvalid[HEIGHT] + "]\t ** viewable: " +index + "\t[" + viewportWidth + "," + viewable[HEIGHT] + "]"); } } // for loop if (Logging.REPORT_LEVEL <= Logging.INFORMATION) { Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI_FORM_LAYOUT, "UPDATE_LAYOUT - DONE"); } } // correct viewable area if required // if there are no items in the form just reset viewable[HEIGHT] if (numOfLFs == 0) { viewable[HEIGHT] = 0; } } /** * Used both to do a full layout or just update a layout. * * assumptions: startIndex<=invalidIndex * * @param startIndex The index to start the layout. Should start a row. * @param invalidIndex The index causing the re-layout, should * be equal or greater than startIndex * @param fullLayout if <code>true</code>, does a full layout and ignores * the rest of the parameters sent to this method. * @param itemLFs reference to the items array of the calling form * @param numOfLFs number of elements in the calling form * @param viewable area needed for the content of the form * * @return the index of the last <code>Item</code> laid out */ private int updateBlock(int startIndex, int invalidIndex, boolean fullLayout, ItemLFImpl[] itemLFs, int numOfLFs, int[] viewable) { if (Logging.REPORT_LEVEL <= Logging.INFORMATION) { Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI_FORM_LAYOUT, "\n - updateBlock(START="+startIndex +", INVALID="+invalidIndex +", Full Layout="+fullLayout+") {"); } // SYNC NOTE: layout() is always called from within a hold // on LCDUILock int oldWidth = viewable[WIDTH]; int oldHeight = viewable[HEIGHT]; // If we don't have any Items, just return if (numOfLFs == 0) { if (Logging.REPORT_LEVEL <= Logging.INFORMATION) { Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI_FORM_LAYOUT, " we don't have any Items, just return }"); } return 0; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?