javauis/lcdui_qt/src/javax/microedition/lcdui/Row.java
changeset 21 2a9601315dfc
child 23 98ccebc37403
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 package javax.microedition.lcdui;
       
    18 
       
    19 import java.util.Vector;
       
    20 
       
    21 /**
       
    22  * Class which represents one row in the Form.
       
    23  */
       
    24 class Row {
       
    25 
       
    26     private Vector layoutObjects = new Vector();
       
    27 
       
    28     private int xShift;
       
    29     private int yShift;
       
    30     private int rowHeight;
       
    31     private int rowWidth;
       
    32     private int occupiedSpace;
       
    33 
       
    34     private int rowHLayout;
       
    35 
       
    36     /**
       
    37      * Constructor.
       
    38      *
       
    39      * @param rowWidth - total row width.
       
    40      * @param hLayout horizontal layout
       
    41      */
       
    42     Row(int rowWidth, int hLayout) {
       
    43         this.rowWidth = rowWidth;
       
    44         setRowHLayout(hLayout);
       
    45     }
       
    46 
       
    47     /**
       
    48      * Add new LayoutObject to the Row.
       
    49      *
       
    50      * @param layoutObj structure which represents Item or part Of Item.
       
    51      */
       
    52     void addLayoutObject(final LayoutObject layoutObj) {
       
    53         ESWTUIThreadRunner.syncExec(new Runnable() {
       
    54             public void run() {
       
    55                 eswtAddLayoutObject(layoutObj);
       
    56             }
       
    57         });
       
    58     }
       
    59 
       
    60     /**
       
    61      * eSWT specific calls to implement addLayoutObject.
       
    62      *
       
    63      * @param lo structure which represents Item or part Of Item.
       
    64      */
       
    65     void eswtAddLayoutObject(LayoutObject lo) {
       
    66         layoutObjects.addElement(lo);
       
    67         // update actual occupiedSpace
       
    68         occupiedSpace += lo.getWidth();
       
    69         // update rowHeight
       
    70         rowHeight = Math.max(rowHeight, lo.getHeight());
       
    71     }
       
    72 
       
    73     /**
       
    74      * Get the number of items with the given layout.
       
    75      */
       
    76     int getNumLayoutObjects(int layoutMask) {
       
    77         int ret = 0;
       
    78         int num = layoutObjects.size();
       
    79         LayoutObject lo = null;
       
    80         for (int i = 0; i < num; i++) {
       
    81             lo = (LayoutObject) layoutObjects.elementAt(i);
       
    82             if (lo.getOwningItem().hasLayout(layoutMask)) {
       
    83                 ret++;
       
    84             }
       
    85         }
       
    86         return ret;
       
    87     }
       
    88 
       
    89     /**
       
    90      * Get available space in the Row for layout calculation.
       
    91      *
       
    92      * @return space available for Items on that Row in pixels.
       
    93      */
       
    94     int getFreeSpace() {
       
    95         return getFreeSpace(Item.LAYOUT_SHRINK | Item.LAYOUT_EXPAND);
       
    96     }
       
    97 
       
    98     /**
       
    99      * Get free space based on LAYOUT mask. Item matching the layout mask will
       
   100      * be counted with their layouting size (not actual size). Used for LAYOUT_2
       
   101      * row resizing.
       
   102      *
       
   103      * @param layoutMask layout mask
       
   104      */
       
   105     int getFreeSpace(int layoutMask) {
       
   106         int ret = 0;
       
   107         int num = layoutObjects.size();
       
   108         LayoutObject lo = null;
       
   109         for (int i = 0; i < num; i++) {
       
   110             lo = (LayoutObject) layoutObjects.elementAt(i);
       
   111             ret += getObjectWidth(lo, layoutMask);
       
   112         }
       
   113         return rowWidth - ret;
       
   114     }
       
   115 
       
   116     /**
       
   117      * Get row height based on LAYOUT mask. Item matching the layout mask will
       
   118      * be counted with their layouting size (not actual size). Used for LAYOUT_2
       
   119      * row resizing.
       
   120      *
       
   121      * @param layoutMask layout mask
       
   122      */
       
   123     int getRowHeight(int layoutMask) {
       
   124         int ret = 0;
       
   125         int num = layoutObjects.size();
       
   126         LayoutObject lo = null;
       
   127         for (int i = 0; i < num; i++) {
       
   128             lo = (LayoutObject) layoutObjects.elementAt(i);
       
   129             ret = Math.max(ret, getObjectHeight(lo, layoutMask));
       
   130         }
       
   131         return ret;
       
   132     }
       
   133 
       
   134     private int getObjectWidth(LayoutObject lo, int layoutMask) {
       
   135         if (lo.getOwningItem().hasLayout(layoutMask)) {
       
   136             // this returns the min/pref width
       
   137             return lo.getOwningItem().getLayoutWidth();
       
   138         }
       
   139         else {
       
   140             return lo.getWidth();
       
   141         }
       
   142     }
       
   143 
       
   144     private int getObjectHeight(LayoutObject lo, int layoutMask) {
       
   145         if (lo.getOwningItem().hasLayout(layoutMask)) {
       
   146             // this returns the min/pref width
       
   147             return lo.getOwningItem().getLayoutHeight();
       
   148         }
       
   149         else {
       
   150             return lo.getHeight();
       
   151         }
       
   152     }
       
   153 
       
   154     /**
       
   155      * Clean row, free resources.
       
   156      *
       
   157      * @param keepItem Last Item to leave in a row.
       
   158      */
       
   159     boolean cleanRow(Item keepItem) {
       
   160         LayoutObject lo = null;
       
   161         for (int i = (layoutObjects.size() - 1); i >= 0; i--) {
       
   162             lo = (LayoutObject) layoutObjects.elementAt(i);
       
   163             if (keepItem != null && keepItem == lo.getOwningItem()) {
       
   164                 updateRowInternals();
       
   165                 return true;
       
   166             }
       
   167             else {
       
   168                 layoutObjects.removeElement(lo);
       
   169                 lo.dispose();
       
   170             }
       
   171         }
       
   172         updateRowInternals();
       
   173         return false;
       
   174     }
       
   175 
       
   176     /**
       
   177      * Update item positions in a row.<br>
       
   178      * This method called by Layouters or FormLayoutPolicy when row is full and
       
   179      * ready for layout.
       
   180      */
       
   181     void updateRowLayout(boolean isLeftToRight) {
       
   182         // update xShift
       
   183         xShift = ItemLayouter.getXLocation(rowWidth, occupiedSpace, rowHLayout);
       
   184         int xDelta = 0;
       
   185         LayoutObject lo = null;
       
   186         if (isLeftToRight) {
       
   187             for (int i = 0; i < layoutObjects.size(); i++) {
       
   188                 lo = (LayoutObject) layoutObjects.elementAt(i);
       
   189                 int yDelta = ItemLayouter.getYLocation(rowHeight,
       
   190                         lo.getHeight(), lo.getVerticalLayout());
       
   191                 lo.eswtSetLocation(xShift + xDelta, yShift + yDelta);
       
   192                 xDelta += lo.getWidth();
       
   193             }
       
   194         }
       
   195         else {
       
   196             for (int i = layoutObjects.size() - 1; i > 0; i--) {
       
   197                 lo = (LayoutObject) layoutObjects.elementAt(i);
       
   198                 int yDelta = ItemLayouter.getYLocation(rowHeight,
       
   199                         lo.getHeight(), lo.getVerticalLayout());
       
   200                 lo.eswtSetLocation(xShift + xDelta, yShift + yDelta);
       
   201                 xDelta += lo.getWidth();
       
   202             }
       
   203         }
       
   204     }
       
   205 
       
   206     /**
       
   207      * Update width and height of a row if some layout objects were removed or
       
   208      * changed.
       
   209      */
       
   210     void updateRowInternals() {
       
   211         int newRowHeight = 0;
       
   212         int newOccupiedSpace = 0;
       
   213         int num = layoutObjects.size();
       
   214         LayoutObject lo = null;
       
   215         for (int i = 0; i < num; i++) {
       
   216             lo = (LayoutObject) layoutObjects.elementAt(i);
       
   217             newRowHeight = Math.max(newRowHeight, lo.getHeight());
       
   218             newOccupiedSpace += lo.getWidth();
       
   219         }
       
   220         rowHeight = newRowHeight;
       
   221         occupiedSpace = newOccupiedSpace;
       
   222     }
       
   223 
       
   224     /**
       
   225      * Set the yShift to corresponding in ScrolledComposite which represents
       
   226      * Form.
       
   227      *
       
   228      * @param aYShift yPosition of the row inside ScrolledComposite
       
   229      */
       
   230     void setYShift(int aYShift) {
       
   231         yShift = aYShift;
       
   232     }
       
   233 
       
   234     /**
       
   235      * Get the yPosition of a Row inside of ScrolledComposite.
       
   236      *
       
   237      * @return yShift in pixels
       
   238      */
       
   239     int getYShift() {
       
   240         return yShift;
       
   241     }
       
   242 
       
   243     /**
       
   244      * Gets the width of the row in pixels.
       
   245      *
       
   246      * @return - row's total width.
       
   247      */
       
   248     int getRowWidth() {
       
   249         return rowWidth;
       
   250     }
       
   251 
       
   252     /**
       
   253      * Get the height of a row in pixels (height of tallest item).
       
   254      *
       
   255      * @return height of a row;
       
   256      */
       
   257     int getRowHeight() {
       
   258         return rowHeight;
       
   259     }
       
   260 
       
   261     /**
       
   262      * Get the bottom (yPosition + rowHeight) of a Row .
       
   263      */
       
   264     int getBottomPosition() {
       
   265         return yShift + rowHeight;
       
   266     }
       
   267 
       
   268     /**
       
   269      * Get the yPosition of a Row's height inside of ScrolledComposite.
       
   270      *
       
   271      * @param yPosition the y position
       
   272      * @return yShift + rowHeight in pixels
       
   273      */
       
   274     boolean isInsideRow(int yPosition) {
       
   275         return (yShift <= yPosition && yPosition < yShift + rowHeight);
       
   276     }
       
   277 
       
   278     /**
       
   279      * set new horizontal Layout of a row.
       
   280      *
       
   281      * @param aRowHLayout - set new horizontal of the row.
       
   282      */
       
   283     void setRowHLayout(int aRowHLayout) {
       
   284         rowHLayout = aRowHLayout;
       
   285     }
       
   286 
       
   287     /**
       
   288      * Get horizontal Layout of row.
       
   289      *
       
   290      * @return horizontal Layout of a row;
       
   291      */
       
   292     int getRowHLayout() {
       
   293         return rowHLayout;
       
   294     }
       
   295 
       
   296     boolean isEmpty() {
       
   297         return ((occupiedSpace == 0) && (layoutObjects.size() == 0));
       
   298     }
       
   299 
       
   300     LayoutObject getNextLayoutObject(LayoutObject lo, Item item) {
       
   301         int startIdx = layoutObjects.indexOf(lo);
       
   302         startIdx = (startIdx < 0 ? 0 : startIdx + 1);
       
   303         int num = layoutObjects.size();
       
   304         LayoutObject temp = null;
       
   305         for (int i = startIdx; i < num; i++) {
       
   306             temp = getLayoutObject(i);
       
   307             if (item == null || item == temp.getOwningItem()) {
       
   308                 return temp;
       
   309             }
       
   310         }
       
   311         return null;
       
   312     }
       
   313 
       
   314     LayoutObject getPrevLayoutObject(LayoutObject lo, Item item) {
       
   315         int startIdx = layoutObjects.indexOf(lo);
       
   316         startIdx = (startIdx < 0 ? layoutObjects.size() - 1 : startIdx - 1);
       
   317         LayoutObject temp = null;
       
   318         for (int i = startIdx; i >= 0; i--) {
       
   319             temp = getLayoutObject(i);
       
   320             if (item == null || item == temp.getOwningItem()) {
       
   321                 return temp;
       
   322             }
       
   323         }
       
   324         return null;
       
   325     }
       
   326 
       
   327     LayoutObject getNextLayoutObject(LayoutObject lo, int layoutMask) {
       
   328         int startIdx = layoutObjects.indexOf(lo);
       
   329         startIdx = (startIdx < 0 ? 0 : startIdx + 1);
       
   330         int num = layoutObjects.size();
       
   331         LayoutObject temp = null;
       
   332         for (int i = startIdx; i < num; i++) {
       
   333             temp = getLayoutObject(i);
       
   334             if (temp.getOwningItem().hasLayout(layoutMask)) {
       
   335                 return temp;
       
   336             }
       
   337         }
       
   338         return null;
       
   339     }
       
   340 
       
   341     LayoutObject getLayoutObject(int index) {
       
   342         return (LayoutObject) layoutObjects.elementAt(index);
       
   343     }
       
   344 
       
   345     int size() {
       
   346         return layoutObjects.size();
       
   347     }
       
   348 
       
   349 }