⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 containerview.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
					// okay, the table is fine just how it is
					this.columnsWidths = maxColumnWidths;
				} else {
					// okay, some columns need to be adjusted:
					// re-initialise the table:
					int leftAvailableColumnWidth = (availableRowWidth - usedUpWidth) / leftColumns;
					int[] newMaxColumnWidths = new int[ this.numberOfColumns ];
					myContentHeight = 0;
					columnIndex = 0;
					rowIndex = 0;
					maxRowHeight = 0;
					maxWidth = 0;
					//System.out.println("starting init of " + myItems.length + " container items.");
					for (int i = 0; i < myItems.length; i++) {
						Item item = myItems[i];
						int width = item.itemWidth;
						int height = item.itemHeight;
						if (maxColumnWidths[ columnIndex ] <= availableColumnWidth) {
							newMaxColumnWidths[ columnIndex ] = maxColumnWidths[ columnIndex ];
						} else {
							// re-initialise this item,
							// if it is wider than the left-available-column-width
							if ( width > leftAvailableColumnWidth ) {
								item.isInitialized = false;
								width = item.getItemWidth( leftAvailableColumnWidth, leftAvailableColumnWidth );
								height = item.getItemHeight( leftAvailableColumnWidth, leftAvailableColumnWidth );
							}
							if (width > newMaxColumnWidths[ columnIndex ]) {
								newMaxColumnWidths[ columnIndex ] = width;
							}
						}
						if (height > maxRowHeight) {
							maxRowHeight = height;
						}
						//#if polish.css.colspan
							//# columnIndex += item.colSpan;
						//#else
							columnIndex++;
						//#endif
						item.relativeY = myContentHeight;
						//System.out.println( i + ": yTopPos=" + item.yTopPos );
						if (columnIndex == this.numberOfColumns) {
							//System.out.println("starting new row: rowIndex=" + rowIndex + "  numberOfRows: " + numberOfRows);
							columnIndex = 0;
							this.rowsHeights[rowIndex] = maxRowHeight;
							myContentHeight += maxRowHeight + this.paddingVertical;
							maxRowHeight = 0;
							rowIndex++;
						}
					} // for each item		
					this.columnsWidths = newMaxColumnWidths;
				}
				if (columnIndex != 0) {
					// last row is not completely filled.
					myContentHeight += maxRowHeight;
				}
			} else if (this.columnsSetting == EQUAL_WIDTH_COLUMNS) {
				// Use the maximum used column-width for each column,
				// unless this table should be expanded, in which
				// case the above set widths  will be used instead.
				if (!isLayoutExpand()) {
					for (int i = 0; i < this.columnsWidths.length; i++) {
						this.columnsWidths[i] = maxWidth;
					}
				}
			} // otherwise the column widths are defined statically.
			// set content height & width:
			int myContentWidth = 0;
			for (int i = 0; i < this.columnsWidths.length; i++) {
				myContentWidth += this.columnsWidths[i] + this.paddingHorizontal;
			}
			this.contentWidth = myContentWidth;
			this.contentHeight = myContentHeight;
		//#endif
	}
		
	/**
	 * Determines whether this view should be expanded horizontally
	 * 
	 * @return true when this view should be expanded horizontally
	 * @see #layout
	 * @see Item#LAYOUT_EXPAND
	 */
	protected boolean isLayoutExpand() {
		return ((this.layout & Item.LAYOUT_EXPAND) == Item.LAYOUT_EXPAND);
	}

	/**
	 * Paints the content of this container view.
	 * This method adjusts the x and y offsets and forwards the call to paintContent(Container, Item[], int, int, int, int, int, int, int, int, Graphics)
	 * 
	 * @param x the left start position
	 * @param y the upper start position
	 * @param leftBorder the left border, nothing must be painted left of this position
	 * @param rightBorder the right border, nothing must be painted right of this position
	 * @param g the Graphics on which this item should be painted.
	 * @see #paintContent(Container, Item[], int, int, int, int, int, int, int, int, Graphics)
	 */
	protected void paintContent( Item parent, int x, int y, int leftBorder, int rightBorder, Graphics g ) {
		//System.out.println("ContainerView: painting content for " + this + " with vertical-padding " + this.paddingVertical  + ", screen=" + this.parentContainer.getScreen());
		
		//#if polish.css.view-type-top-y-offset
			//# y += this.topYOffset;
		//#endif
		//#if polish.css.view-type-left-x-offset
			//# x += this.leftXOffset;
			//# leftBorder += this.leftXOffset;
		//#endif
		//#if polish.css.view-type-right-x-offset
			//# rightBorder -= this.rightXOffset;
		//#endif
		//System.out.println("ContainerView.paint(): width=" + (rightBorder - leftBorder ) + ", firstLineWidth=" + (rightBorder - x) + ", contentWidth=" + this.contentWidth + ", parentContentWidth=" + this.parentContainer.contentWidth );
		
		Item[] myItems = this.parentContainer.getItems();
		
		paintContent( this.parentContainer, myItems, x, y, leftBorder, rightBorder, g.getClipX(), g.getClipY(), g.getClipWidth(), g.getClipHeight(), g);
		
	}
	
	/**
	 * Paints the content of this container view.
	 * This method calls 
	 * 
	 * @param container the parent container
	 * @param myItems the items that should be painted
	 * @param x the left start position
	 * @param y the upper start position
	 * @param leftBorder the left border, nothing must be painted left of this position
	 * @param rightBorder the right border, nothing must be painted right of this position
	 * @param clipX absolute horizontal clipping start
	 * @param clipY absolute verical clipping start
	 * @param clipWidth clipping width
	 * @param clipHeight clipping height
	 * @param g the Graphics on which this item should be painted.
	 */
	protected void paintContent(Container container, Item[] myItems, int x, int y, int leftBorder, int rightBorder, int clipX, int clipY, int clipWidth, int clipHeight, Graphics g) {
		int focusedX = x;
		int focusedY = 0;
		int focusedRightBorder = rightBorder;
		//#ifdef tmp.useTable
			if (this.columnsSetting == NO_COLUMNS || myItems.length == 1) {
		//#endif
				if (!(this.isLayoutCenter || this.isLayoutRight)) {
					// adjust the right border:
					//rightBorder = leftBorder + this.contentWidth;
				}
				for (int i = 0; i < myItems.length; i++) {
					Item item = myItems[i];
					// currently the NEWLINE_AFTER and NEWLINE_BEFORE layouts will be ignored,
					// since after every item a line break will be done.
					if (i == this.focusedIndex) {
						focusedY = y;
						item.getItemHeight( rightBorder - x, rightBorder - leftBorder );
					} else {
						// the currently focused item is painted last
						paintItem(item, i, x, y, leftBorder, rightBorder, clipX, clipY, clipWidth, clipHeight, g);
						//item.paint(x, y, leftBorder, rightBorder, g);
					}
					y += item.itemHeight + this.paddingVertical;
				}
		//#ifdef tmp.useTable
			} else {
				x = leftBorder;
				int columnIndex = 0;
				int rowIndex = 0;
				for (int i = 0; i < myItems.length; i++) {
					Item item = myItems[i];
					int columnWidth = this.columnsWidths[ columnIndex ];
					//#if polish.css.colspan
						//# if (item.colSpan > 1) {						
							//# for (int j = columnIndex + 1; j < columnIndex + item.colSpan; j++) {
								//# columnWidth += this.columnsWidths[ j ] + this.paddingHorizontal;
							//# }
							//# //System.out.println("Painting item " + i + "/" + item + " with width=" + item.itemWidth + " and with increased colwidth of " + columnWidth + " (prev=" + this.columnsWidths[ columnIndex ] + ")" );
//# 							
						//# }
					//#endif
					//TODO this is a quick hack so that pointer events work - normally the item.relativeX position should be set in initContent! 
					item.relativeX = x - leftBorder;
					if (i == this.focusedIndex) {
						focusedY = y;
						focusedX = x;
						focusedRightBorder = x + columnWidth;
						//System.out.println("focusedItem in table: x=" + focusedX + ", rocusedRightBorder=" + focusedRightBorder + ", rightBorder=" + rightBorder +  ", isInitialized=" + this.focusedItem.isInitialised +  ", itemWidth=" + item.getItemWidth( columnWidth, columnWidth ));
						// item.getItemHeight( columnWidth, columnWidth );
					} else {
						paintItem( item, i, x, y, x, x + columnWidth, clipX, clipY, clipWidth, clipHeight,  g );
						// item.paint(x, y, x, x + columnWidth, g);
					}
					x += columnWidth + this.paddingHorizontal;
					//#if polish.css.colspan
						//# columnIndex += item.colSpan;
					//#else
						columnIndex++;
					//#endif
			
					if (columnIndex == this.numberOfColumns) {
						columnIndex = 0;
						y += this.rowsHeights[ rowIndex ] + this.paddingVertical;
						x = leftBorder;
						rowIndex++;
					}
				}
			}
		//#endif
		
		// paint the currently focused item:
		if (this.focusedItem != null) {
			//System.out.println("Painting focusedItem " + this.focusedItem + " with width=" + this.focusedItem.itemWidth + " and with increased colwidth of " + (focusedRightBorder - focusedX)  );
			paintItem( this.focusedItem, this.focusedIndex, focusedX, focusedY, focusedX, focusedRightBorder, clipX, clipY, clipWidth, clipHeight, g);
			//this.focusedItem.paint(focusedX, focusedY, focusedX, focusedRightBorder, g);
		}
	}

	/**
	 * Paints this item at the specified position.
	 * Subclasses can override this method for taking advantage of the table support of the basic ContainerView class. 
	 * When the item is outside of the given clipping area, it will not be painted.
	 *  
	 * @param item the item that needs to be painted
	 * @param index the index of the item
	 * @param x the horizontal position of the item
	 * @param y the vertical position of the item
	 * @param leftBorder the left border
	 * @param rightBorder the right border
	 * @param clipX absolute horizontal clipping start
	 * @param clipY absolute verical clipping start
	 * @param clipWidth clipping width
	 * @param clipHeight clipping height
	 * @param g the graphics context
	 */
	protected void paintItem( Item item, int index,  int x, int y, int leftBorder, int rightBorder, int clipX, int clipY, int clipWidth, int clipHeight, Graphics g ) {
		//#debug
		//# System.out.println("ContainerView: painting item at (" +  x + ", " + y + ") " + item );
		if (y < clipY + clipHeight && y + item.itemHeight > clipY && x < clipX + clipWidth && x + item.itemWidth > clipX) {
			item.paint(x, y, leftBorder, rightBorder, g);
		}
	}

	/**
	 * Interpretes the given user-input and retrieves the nexte item which should be focused.
	 * Please not that the focusItem()-method is not called as well. The
	 * view is responsible for updating its internal configuration here as well.
	 * 
	 * @param keyCode the code of the keyPressed-events
	 * @param gameAction the associated game-action to the given keyCode
	 * @return the next item which will be focused, null when there is
	 * 			no such element.
	 */
	protected Item getNextItem( int keyCode, int gameAction ) {

		Item[] myItems = this.parentContainer.getItems();
		if ( (gameAction == Canvas.RIGHT  && keyCode != Canvas.KEY_NUM6) 
				|| (gameAction == Canvas.DOWN  && keyCode != Canvas.KEY_NUM8)) {
			if (gameAction == Canvas.DOWN && this.columnsSetting != NO_COLUMNS) {
				return shiftFocus( true, this.numberOfColumns - 1, myItems );
			}
			return shiftFocus( true, 0, myItems );
			
		} else if ( (gameAction == Canvas.LEFT  && keyCode != Canvas.KEY_NUM4) 
				|| (gameAction == Canvas.UP && keyCode != Canvas.KEY_NUM2) ) {
			if (gameAction == Canvas.UP && this.columnsSetting != NO_COLUMNS) {
				return shiftFocus( false,  -(this.numberOfColumns -1 ), myItems);
			}
			return shiftFocus( false, 0, myItems );
		}
		
		
		return null;
		
	}
	
	/**
	 * Shifts the focus to the next or the previous item.
	 * 
	 * @param forwardFocus true when the next item should be focused, false when
	 * 		  the previous item should be focused.
	 * @param steps how many steps forward or backward the search for the next focusable item should be started,
	 *        0 for the current item, negative values go backwards.
	 * @param items the items of this view
	 * @return the item that has been focused or null, when no item has been focused.
	 */
	protected Item shiftFocus(boolean forwardFocus, int steps, Item[] items) {
		//#debug
		//# System.out.println("ContainerView.shiftFocus( forward=" + forwardFocus + ", steps=" + steps + ", focusedIndex=" + this.focusedIndex );
		//#if polish.css.colspan
			//# int i = this.focusedIndex;
			//# if ( i != -1 && steps != 0) {
				//# //System.out.println("ShiftFocus: steps=" + steps + ", forward=" + forwardFocus);
				//# int doneSteps = 0;
				//# steps = Math.abs( steps ) + 1;
				//# Item item = this.parentContainer.items[i];
				//# while( doneSteps <= steps ) {
					//# doneSteps += item.colSpan;
					//# if (doneSteps >= steps) {
						//# //System.out.println("bailing out at too many steps: focusedIndex=" + this.focusedIndex + ", startIndex=" + i + ", steps=" + steps + ", doneSteps=" + doneSteps);
						//# break;
					//# }
					//# if (forwardFocus) {
						//# i++;
						//# if (i == items.length - 1 ) {
							//#if polish.Container.allowCycling == false
								//# return null;
							//#else
								//# if (!this.allowCycling) {
									//# return null;
								//# }
								//# //System.out.println("reached items.length -1, breaking at -2");
								//# i = items.length - 2;
								//# break;
							//#endif
						//# } else if (i >= items.length) {
							//#if polish.Container.allowCycling == false
								//# return null;
							//#else
								//# if (!this.allowCycling) {
									//# return null;
								//# }
								//# //System.out.println("reached items.length, breaking at -1");
								//# i = items.length - 1;
								//# break;
							//#endif
						//# }
					//# } else {
						//# i--; 
						//# if (i < 0) {
							//# i = 1;
							//# break;
						//# }
					//# }
					//# item = items[i];
//# //					System.out.println("focusedIndex=" + this.focusedIndex + ", startIndex=" + i + ", steps=" + steps + ", doneSteps=" + doneSteps);
				//# }
				//# if (doneSteps >= steps && item.colSpan != 1 && i != this.focusedIndex)  {
					//# if (forwardFocus) {
						//# i--;
						//# if (i < 0) {
							//# i = items.length + i;
						//# }
//# //						System.out.println("forward: Adjusting startIndex to " + i );
					//# } else {
						//# i = (i + 1) % items.length;
//# //						System.out.println("backward: Adjusting startIndex to " + i );
					//# }
				//# }
			//# }
		//#else			
			int i = this.focusedIndex + steps;
			if (steps != 0) {
				if (!forwardFocus) {
					if (i < 0) {
						i = items.length + i;
					}
	//				System.out.println("forward: Adjusting startIndex to " + i );
				} else {
					i = i % items.length;
	//				System.out.println("backward: Adjusting startIndex to " + i );
				}
			}
		//#endif
		//TODO check how to integrate scrolling-cycling-coordination in containerviews
	//	//#if polish.Container.allowCycling != false
	//		boolean allowCycle = this.enableScrolling && this.allowCycling;
	//		if (allowCycle) {
	//			//#if polish.css.scroll-mode
	//				if (!this.scrollSmooth) {
	//					if (forwardFocus) {
	//						// when you scroll to the bottom and
	//						// there is still space, do
	//						// scroll first before cycling to the
	//						// first item:
	//						allowCycle = (this.yOffset + this.itemHeight <= this.yBottom);
	//					} else {
	//						// when you scroll to the top and
	//						// there is still space, do
	//						// scroll first before cycling to the
	//						// last item:
	//						allowCycle = (this.yOffset == 0);
	//					}						
	//				} else {
	//			//#endif
	//				if (forwardFocus) {
	//					// when you scroll to the bottom and
	//					// there is still space, do
	//					// scroll first before cycling to the
	//					// first item:
	//					allowCycle = (this.targetYOffset + this.itemHeight <= this.yBottom);
	//				} else {
	//					// when you scroll to the top and
	//					// there is still space, do
	//					// scroll first before cycling to the
	//					// last item:
	//					allowCycle = (this.targetYOffset == 0);
	//				}
	//			//#if polish.css.scroll-mode
	//				}
	//			//#endif
	//			//#debug
	//			System.out.println("shiftFocus: allowCycl=" + allowCycle + ", isFoward=" + forwardFocus + ", targetYOffset=" + this.targetYOffset + ", yOffset=" + this.yOffset );	
	//		}
	//	//#endif
		//#if polish.Container.allowCycling != false
			boolean allowCycle = this.allowCycling;
		//#else
			//# boolean allowCycle = false;
		//#endif
		Item nextItem = null;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -