ScrollableContainer.java
资源名称:src.zip [点击查看]
上传用户:luxiaowei
上传日期:2022-06-06
资源大小:58k
文件大小:19k
源码类别:
J2ME
开发平台:
Java
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.framework;
- import java.util.Vector;
- import javax.microedition.lcdui.Graphics;
- /**
- *
- * @author swaroop_kumar
- */
- public class ScrollableContainer extends UIControl implements ScrollBarInterface{
- int totalHeight , barWidth;
- int scrollBarBackgroundColor = ColorTheme.SCROLLBAR_BGCOLOR, scrollBarColor = ColorTheme.SCROLLBAR_COLOR;
- int scrollSliderHeight;
- protected int scrolledY = 0;
- boolean paintUserDataFirst = true;
- int start;
- Layout layout;
- public void setScrollType(int scrollType) {
- this.scrollType = scrollType;
- }
- Vector childrens = new Vector();
- static boolean selectChild = true;
- int selectedChild = 0;
- public static final int SMOOTH_SCROLL = 0;
- public static final int SELECT_COMPONENT_SCROLL = 1;
- int scrollType = SELECT_COMPONENT_SCROLL;
- public static final int SCROLL_BAR_WIDTH = 3;
- public void setEventManager(EventManager manager)
- {
- super.setEventManager(manager);
- for (int idx = 0; idx < childrens.size(); idx++) {
- UIControl control = (UIControl)childrens.elementAt(idx);
- control.setEventManager(manager);
- }
- }
- public void addChildren(UIControl control)
- {
- control.setParent(this);
- childrens.addElement(control);
- control.setEventManager(getEventListener());
- }
- public void setPosition(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- public void setWidth(int width)
- {
- this.width = width;
- }
- public int getTotalHeight() {
- return totalHeight;
- }
- public void setHeight(int height)
- {
- this.height = height;
- }
- public void setTotalHeight(int totalHeight)
- {
- this.totalHeight = totalHeight;
- }
- public int getWidth()
- {
- return this.width - barWidth;
- }
- public int getChildrenIndex(UIControl control){
- return childrens.indexOf(control);
- }
- public UIControl getChild(int index){
- if(childrens.size()>index)
- return (UIControl)childrens.elementAt(index);
- else
- return null;
- }
- public int getSize()
- {
- return childrens.size();
- }
- public void addChildrenAt(UIControl control,int index){
- control.setParent(this);
- childrens.insertElementAt(control, index);
- }
- public void removeAll()
- {
- selectedChild = 0;
- childrens.removeAllElements();
- totalHeight = 0;
- scrolledY = 0;
- }
- public void removeChildren(UIControl children) {
- for (int i = 0; i < childrens.size(); i++) {
- UIControl control = (UIControl) childrens.elementAt(i);
- if (control.equals(children)) {
- childrens.removeElementAt(i);
- }
- }
- }
- public int getSelectedChildIndex()
- {
- return selectedChild;
- }
- public UIControl getSelectedChild()
- {
- if(selectedChild >= 0 && selectedChild < childrens.size())
- {
- UIControl control = (UIControl)childrens.elementAt(selectedChild);
- return control;
- }
- else
- return null;
- }
- protected boolean pointerDragged(int x, int y) {
- for (int idx = 0; idx < childrens.size(); idx++) {
- UIControl control = (UIControl)childrens.elementAt(idx);
- if(control.isSelectable() && control.isVisible() && Util.isInRect(control, x, y - getY() + scrolledY))
- {
- if(control.pointerDragged(x, y - getY()))
- return true;
- }
- }
- if(totalHeight > height)
- {
- if(moveScrollBar(-DisplayManager.getInst().getDiffrenceY()))
- return true;
- }
- return false;
- }
- protected boolean pointerReleased(int x, int y) {
- for (int idx = 0; idx < childrens.size(); idx++) {
- UIControl control = (UIControl)childrens.elementAt(idx);
- if(control.isSelectable() && control.isVisible() && Util.isInRect(control, x, y - getY() + scrolledY ))
- {
- selectChild(idx);
- return control.pointerReleased(x, y - getY());
- }
- }
- return super.pointerReleased(x, y);
- }
- public void paint(Graphics g)
- {
- int scrollBar_y =0;
- if(totalHeight > height)
- {
- barWidth = SCROLL_BAR_WIDTH;
- int temp = ((((height - (barWidth ) - 2) * 100) << 14) / totalHeight);
- scrollSliderHeight = ((temp * (height - (barWidth) - 2)) / 100) >> 14;
- int percentage = ((((scrolledY + height) << 6) *100) / totalHeight) >> 6;
- scrollBar_y = (((((height - (barWidth >> 1) - 1) * percentage) << 14) / 100) >> 14 ) - scrollSliderHeight;
- if(scrolledY == 0)
- scrollBar_y = (barWidth >> 1)+1 ;
- }
- g.translate(0, -scrolledY);
- int transY = g.getTranslateY();
- for (int idx = 0; idx < childrens.size(); idx++) {
- UIControl control = (UIControl)childrens.elementAt(idx);
- if( (transY + control.getY() +control.getHeight() < 0 || transY + control.getY() > DisplayManager.getInst().getHeight()))
- {
- continue;
- }
- control.paintUI(g);
- }
- g.translate(0, scrolledY);
- if(totalHeight > height)
- {
- g.setColor(scrollBarBackgroundColor);
- g.fillRect(getWidth(), 0, barWidth, height);
- g.setColor(scrollBarColor);
- g.fillRect(getWidth(),scrollBar_y, barWidth, scrollSliderHeight);
- Util.drawFilledTriangle(g, getWidth() , (barWidth >> 1) + 1, true, barWidth, false);
- Util.drawFilledTriangle(g, getWidth() , height - (barWidth >> 1) - 1, false, barWidth, false);
- }
- }
- public void setPaintUserDataFirst(boolean paintUserDataFirst) {
- this.paintUserDataFirst = paintUserDataFirst;
- }
- // protected void paintForground(Graphics g) {
- // if(borderColor != -1)
- // {
- // g.setColor(borderColor);
- // g.drawRect(0, 0, width - SCROLL_BAR_WIDTH - 1, height - 1);
- // }
- // }
- public boolean keyRepeated(int key) {
- if(selectedChild >= 0 && selectedChild < childrens.size())
- {
- UIControl control = (UIControl)childrens.elementAt(selectedChild);
- if(control.keyRepeated(key))
- return true;
- }
- return super.keyRepeated(key);
- }
- private boolean moveScrollBar(int steps)
- {
- int oldPos = scrolledY;
- if(steps > 0)
- {
- if(scrolledY + height < totalHeight)
- {
- scrolledY+= steps;
- }
- else
- scrolledY = totalHeight - height;
- }else if(steps < 0)
- {
- if(scrolledY > 0)
- scrolledY -= Math.abs(steps);
- if(scrolledY < 0)
- scrolledY = 0;
- }
- if(scrolledY != oldPos)
- return true;
- else
- return false;
- }
- public boolean keyPressed(int keycode)
- {
- if(scrollType == SMOOTH_SCROLL && totalHeight > height)
- {
- int gameKey = DisplayManager.getInst().getGameAction(keycode);
- if(gameKey == DisplayManager.DOWN)
- {
- if(moveScrollBar(15))
- return true;
- }else if(gameKey == DisplayManager.UP)
- {
- if(moveScrollBar(-15))
- return true;
- }
- }
- if(selectedChild >= 0 && selectedChild < childrens.size())
- {
- UIControl control = (UIControl)childrens.elementAt(selectedChild);
- if(control.keyPressed(keycode))
- return true;
- }
- int gameKey = DisplayManager.getInst().getGameAction(keycode);
- int newIndex = getNextChildIndex(keycode,selectedChild);
- if(newIndex != -1 && newIndex != selectedChild)
- {
- selectChild(newIndex);
- return true;
- }else if(totalHeight > height){
- if(gameKey == DisplayManager.UP)
- {
- scrolledY = 0;
- }else if(gameKey == DisplayManager.DOWN)
- {
- scrolledY = totalHeight - height;
- }
- }
- return false;
- }
- int getNextChildIndex(int keycode, int index)
- {
- int oldIndex = -1;
- if(index >= 0 && index < childrens.size())
- {
- UIControl currentChild = (UIControl)childrens.elementAt(index);
- int gameKey = DisplayManager.getInst().getGameAction(keycode);
- int currentPosX = currentChild.getX();
- int currentPosY = currentChild.getY();
- switch(gameKey)
- {
- case DisplayManager.UP:
- currentPosX += (currentChild.getWidth() >> 1);
- break;
- case DisplayManager.DOWN:
- currentPosY += currentChild.getHeight();
- currentPosX += (currentChild.getWidth() >> 1);
- break;
- case DisplayManager.LEFT:
- currentPosY += (currentChild.getHeight() >> 1);
- break;
- case DisplayManager.RIGHT:
- currentPosY += (currentChild.getHeight() >> 1);
- currentPosX += (currentChild.getWidth());
- break;
- }
- int oldDist = Integer.MAX_VALUE, dist;
- for (int i = 0; i < childrens.size(); i++) {
- UIControl control = (UIControl)childrens.elementAt(i);
- if(control.isVisible() && control.isSelectable())
- {
- int controlX = control.getX() + (control.getWidth() >> 1);
- int controlY = control.getY() + (control.getHeight() >> 1);
- if( (gameKey == DisplayManager.UP && controlY < currentPosY ) || (gameKey == DisplayManager.DOWN && controlY > currentPosY) || (gameKey == DisplayManager.LEFT && controlX < currentPosX) || (gameKey == DisplayManager.RIGHT && controlX > currentPosX))
- {
- dist = Util.getApproxDistance(controlX,controlY , currentPosX , currentPosY);
- if(dist < oldDist)
- {
- oldDist = dist;
- oldIndex = i;
- }
- }
- }
- }
- }
- return oldIndex;
- }
- // int getNextChildIndex(int keycode, int index)
- // {
- // int tmpIndex = index;
- // int gameKey = DisplayManager.getInst().getGameAction(keycode);
- // if(gameKey == DisplayManager.UP || gameKey == DisplayManager.LEFT)
- // {
- // tmpIndex--;
- // }else if(gameKey == DisplayManager.DOWN || gameKey == DisplayManager.RIGHT)
- // {
- // tmpIndex++;
- // }
- // else
- // return -1;
- // if(tmpIndex >= 0 && tmpIndex < childrens.size())
- // {
- // UIControl control = (UIControl)childrens.elementAt(tmpIndex);
- // if(control.isSelectable())
- // return tmpIndex;
- // else
- // return getNextChildIndex(keycode , tmpIndex);
- // }
- // else
- // return -1;
- //
- // }
- public void setSelected(boolean selected) {
- super.setSelected(selected);
- if(selected == true)
- {
- if(selectedChild >= 0 && selectedChild < childrens.size())
- {
- UIControl control = (UIControl)childrens.elementAt(selectedChild);
- if(!control.isSelectable())
- {
- selectedChild = -1;
- selectedChild = getNextChildIndex(DisplayManager.DOWN,selectedChild);
- for (int i = 0; i < childrens.size(); i++) {
- UIControl object = (UIControl)childrens.elementAt(i);
- if(object.isSelectable())
- {
- selectedChild = i;
- break;
- }
- }
- }
- selectChild(selectedChild);
- }
- }
- else
- {
- if(selectedChild >= 0 && selectedChild < childrens.size())
- {
- UIControl control = (UIControl)childrens.elementAt(selectedChild);
- control.setSelected(false);
- }
- }
- }
- protected void selectChild(int index)
- {
- if(selectedChild >= 0 && selectedChild < childrens.size())
- {
- UIControl control = (UIControl)childrens.elementAt(selectedChild);
- control.setSelected(false);
- }
- if(index >= 0 && index < childrens.size())
- {
- UIControl control = (UIControl)childrens.elementAt(index);
- control.setSelected(true);
- if(selectedChild != index && getEventListener() != null)
- {
- getEventListener().event(EventManager.SELECTION_CHANGED, this,new Integer(index));
- }
- selectedChild = index;
- if(scrollType == SELECT_COMPONENT_SCROLL && totalHeight > height && ((UIControl) childrens.elementAt(selectedChild)) instanceof ScrollBarInterface)
- {
- UIControl container = ((UIControl) childrens.elementAt(selectedChild));
- int bckUp = scrolledY;
- if (selectedChild == upperMostElement) {
- scrolledY = getScrolledY(container, 0);
- } else {
- scrolledY = getScrolledY(container, scrolledY);
- }
- // if(container.getY() + container.getHeight() > scrolledY + height)
- // {
- // setScrollBarPosition(((UIControl) childrens.elementAt(this.selectedChild)).y,
- // ((UIControl) childrens.elementAt(selectedChild)).height);
- // }
- } else {
- setScrollBarPosition(((UIControl) childrens.elementAt(this.selectedChild)).y,
- ((UIControl) childrens.elementAt(selectedChild)).height);
- }
- }
- }
- private int getScrolledY(UIControl base, int virtualScrollHeight) {
- int tmpHeight = 0;
- if (base instanceof ScrollBarInterface) {
- int _yPos = ((ScrollBarInterface) base).getDrawingYPos();
- UIControl component = null;
- component = ((ScrollBarInterface) base).getSelectedChild();
- while (component instanceof ScrollBarInterface) {
- _yPos += component.getHeight();
- UIControl tmp = ((ScrollBarInterface) component).getSelectedChild();
- if (tmp == null) {
- break;
- }
- component = tmp;
- }
- if (component != null) {
- if (_yPos + component.getY() + component.getHeight() >= getHeight() + virtualScrollHeight) {
- tmpHeight = _yPos + component.getY() + component.getHeight() - height ;
- } else if (_yPos + component.getY() < virtualScrollHeight) {
- tmpHeight = _yPos + component.getY();
- }
- }
- } else {
- if (base.getY() + base.getHeight() >= getHeight() + virtualScrollHeight) {
- tmpHeight = base.getY() + base.getHeight() - height ;
- } else if (base.getY() < virtualScrollHeight) {
- tmpHeight = base.getY() ;
- }
- }
- if (tmpHeight == 0) {
- return virtualScrollHeight;
- } else {
- return tmpHeight;
- }
- }
- public void setLayout(Layout layout) {
- this.layout = layout;
- }
- public void resize()
- {
- if(layout != null)
- {
- layout.applyLayout(this);
- }
- if(height == 0)
- {
- int tmp = 0;
- for (int i = 0; i < childrens.size(); i++) {
- UIControl control = (UIControl)childrens.elementAt(i);
- if(control.getY() + control.getHeight() > tmp)
- {
- tmp = control.getY() + control.getHeight();
- }
- }
- setHeight(tmp);
- }
- if(width == 0)
- {
- setWidth(DisplayManager.getInst().getWidth());
- }
- }
- int upperMostElement = -1;
- public void showNotify() {
- resize();
- if(childrens.size()>0)
- start = getChild(0).getY();
- int actHeight = 0;
- upperMostElement = -1;
- for (int i = 0; i < childrens.size(); i++) {
- UIControl control = (UIControl)childrens.elementAt(i);
- if ((upperMostElement == -1 || ((UIControl) childrens.elementAt(i)).getY() < ((UIControl) childrens.elementAt(upperMostElement)).y) && ((UIControl) childrens.elementAt(i)).isSelectable()) {
- upperMostElement = i;
- }
- if(control.getY() + control.getHeight() > actHeight)
- {
- actHeight = control.getY() + control.getHeight();
- }
- if(selectChild)
- {
- selectChild = false;
- control.showNotify();
- selectChild(i);
- }else
- {
- control.showNotify();
- control.setSelected(false);
- }
- }
- if(actHeight > height)
- {
- totalHeight = actHeight;
- if(layout != null)
- layout.applyLayout(this);
- }
- }
- protected boolean isScrollBarPresent()
- {
- return (totalHeight > height);
- }
- public void hideNotify() {
- selectChild = true;
- super.hideNotify();
- for (int i = 0; i < childrens.size(); i++) {
- UIControl control = (UIControl)childrens.elementAt(i);
- control.hideNotify();
- }
- }
- public void scrollTillLast()
- {
- if(totalHeight > height)
- scrolledY = totalHeight - height;
- }
- public int getDrawingYPos() {
- return getY();
- }
- public void setScrollBarPosition(int yPos, int controlHeight) {
- if (totalHeight <= height) {
- if (getParent() != null && getParent() instanceof ScrollBarInterface) {
- ((ScrollBarInterface) getParent()).setScrollBarPosition(((ScrollBarInterface) this).getDrawingYPos() + yPos - totalHeight, controlHeight);
- }
- } else {
- if (selectedChild == upperMostElement) {
- scrolledY = getScrolledY((UIControl) childrens.elementAt(selectedChild), 0);
- } else if (yPos + controlHeight >= getHeight() + scrolledY) {
- scrolledY = yPos + controlHeight - height ;
- } else if (yPos < scrolledY) {
- scrolledY = yPos;
- }
- if (getParent() != null && getParent() instanceof ScrollBarInterface) {
- ((ScrollBarInterface) getParent()).setScrollBarPosition(((ScrollBarInterface) this).getDrawingYPos() + yPos - scrolledY, controlHeight);
- }
- }
- }
- }