Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 132
Registered: ‎10-25-2011
My Device: Tour 9630
My Carrier: MTN

Panemanager for os5.0

hello and good day ,

Please i am trying to implement a panemanager for os5.0 does anyone have any idea on how to do this for os5.0 , because it is available in os6.0 , any thoughts or insights will be apppreciated .

Developer
Developer
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook
My Carrier: Verizon

Re: Panemanager for os5.0

I did this by creating a top level manager that held other managers and allowed you to scroll through them horizontally. The top level manager can only scroll horizontally and the individual internal managers can scroll vertically as needed.
Developer
Posts: 132
Registered: ‎10-25-2011
My Device: Tour 9630
My Carrier: MTN

Re: Panemanager for os5.0

Please a sample code will be very much appreciated so I have a proper insight on how to handle this user interface , thanks .
Developer
Developer
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook
My Carrier: Verizon

Re: Panemanager for os5.0

[ Edited ]

I built my own custom library on top of the standard ui classes but here is the code for my pane manager:

public class CustomPaneField extends CustomLayoutManager {
   int used_panes = 0, scrollStartPos = 0, scrollTime = 500;
   int titleColor, titleGradient, titleFontColor;
   int titleVisibleHeight = MyField.MGR_MINUS_30;
   int currentPane = 0;
   boolean autoScroll       = true;
   boolean enableSwipe      = false;
   boolean enableTitle      = false;
   boolean autoHideTitle    = false;
   boolean layoutChanged    = true;
   boolean continuousScroll = false;
   boolean xLock            = false;
   boolean yLock            = false;
   CustomLayoutManager [] panes;
   CustomLabelField paneTitle;
   Scroller scroller;

   public CustomPaneField(int panes, int x, int y, int width, int height){
      super(height,width,x,y);
      this.panes = new CustomLayoutManager[panes];
      
      int tHght = 30;
      int fHght = getFont().getHeight();
      if( fHght > 24 ){ titleVisibleHeight = MyField.MGR_MINUS_40; tHght = 40; }
      if( fHght > 34 ){ titleVisibleHeight = MyField.MGR_MINUS_50; tHght = 50; }

      paneTitle = new CustomLabelField("",0,-tHght,MyField.MGR_FULLWIDTH,tHght);
      //paneTitle.setColor( backgroundColor );
      //paneTitle.setGradient( backgroundGradient );
      paneTitle.setBackgroundAlpha( 0 ); //bgAlpha
      paneTitle.useOutline(false);
      paneTitle.setTextJustify(MyField.JUSTIFY_CENTER);
      add(paneTitle);
   }

   /* ADD MANAGERS */
   public void createPane(int i){
      if( panes[i] == null ){
         panes[i] = new CustomLayoutManager(MyField.MGR_FULLHEIGHT,MyField.MGR_FULLWIDTH);
         panes[i].setBackgroundAlpha( bgAlpha );
         panes[i].setColor( backgroundColor );
         panes[i].setGradient( backgroundGradient );
         panes[i].setScrollWithFocus(true);
         add(panes[i]);
         layoutChanged = true;
         used_panes++;
      }
   }

   public void addPane(CustomLayoutManager clm){
      System.out.println("CustomPaneField - adding CLM");
      for( int i=0; i<panes.length; i++ ){
         if( panes[i] == null ){
            System.out.println("Position "+i+" is empty - adding");
            panes[i] = clm;
            add(clm);
            used_panes++;
            layoutChanged = true;
            return;
         }
      }
   }

   public void addPane(CustomLayoutManager clm, int i){
      System.out.println("CustomPaneField - adding CLM to position "+i);
      if( i >= panes.length ) return;
      if( panes[i] == null ){
         System.out.println("Position is empty - adding");
         panes[i] = clm;
         this.insert(clm, i);
         used_panes++;
         layoutChanged = true;            
      } else {
         System.out.println("Position is not empty and not all spots filled - shifting elements");
         if( used_panes < panes.length ){
            int index = 0;
            while( panes[index] != null ) index++;
            if( index < i ){
               for(int c=index; c<i; ++c) panes[c] = panes[c+1];
               panes[i] = clm;
            } else if( index > i ){
               for(int c=index; c>i; --c) panes[c] = panes[c-1];
               panes[i] = clm;
            }
            this.insert(clm, i);
            layoutChanged = true;
         }
      }
   }

   public void replacePane(CustomLayoutManager clm, int i){
      if( i >= panes.length ) return;
      if( panes[i] == null ){
         System.out.println("Position is empty - adding");
         panes[i] = clm;
         this.insert(clm, i);
         used_panes++;
         layoutChanged = true;
      } else {
         System.out.println("Position is not empty; replacing");
         panes[i] = null;
         panes[i] = clm;
         this.replace(getField(i), clm);
         layoutChanged = true;         
      }
   }

   public void removePane(int i){
      System.out.println("CustomPaneField - removing CLM from position "+i);
      if( panes[i] != null ){
         System.out.println("Position not empty - deleting");
         this.delete(panes[i]);
         panes[i] = null;
         used_panes--;         
         for(int n=i; n<panes.length-1; ++n) panes[n] = panes[n+1];
         panes[panes.length-1] = null;
         layoutChanged = true;
      }
   }

   public void namePane(String title,int pane){
      if( panes[pane] != null ){
         panes[pane].setText(title);
      }
   }

   /* ADD FIELDS */
   public void addField(MyField f, int pane){
      if( panes[pane] == null ){
         panes[pane] = new CustomLayoutManager( MyField.MGR_FULLHEIGHT,MyField.MGR_FULLWIDTH );
         panes[pane].setBackgroundAlpha( bgAlpha );
         panes[pane].setColor( backgroundColor );
         panes[pane].setGradient( backgroundGradient );
         panes[pane].setScrollWithFocus(true);
         add(panes[pane]);
         panes[pane].add((Field)f);
         layoutChanged = true;
         used_panes++;
      } else {
         panes[pane].add((Field)f);
      }
   }

   public void removeField(int pane, int field){
      if( panes[pane] != null ){
         panes[pane].deleteRange(field, 1);         
      }
   }
   
   public void replaceField(MyField f, int pane, int field){
      if( panes[pane] != null ){
         panes[pane].replace(panes[pane].getField(field), (Field)f);
      }
   }
   
   //*********** LAYOUT METHODS ****************//
   protected void layoutPanes(){
      if( layoutChanged ){
         int fieldWidth = 0;
         for( int i=0; i<panes.length; i++ ){
            if( panes[i] != null ){
               if( i == 0 ) panes[i].changeX(0);
               else panes[i].changeX(MyField.POS_FLOAT_RIGHT);
               if( enableTitle ){
                  if( paneTitle.getY() == 0 ){
                     panes[i].changeY(paneTitle.myHeight());
                     panes[i].resize(MyField.MGR_FULLWIDTH, titleVisibleHeight);
                  } else {
                     panes[i].changeY(0);
                     panes[i].resize(MyField.MGR_FULLWIDTH, MyField.MGR_FULLHEIGHT);
                  }
               } else {
                  panes[i].changeY(0);
                  panes[i].resize(MyField.MGR_FULLWIDTH, MyField.MGR_FULLHEIGHT);
               }
               //System.out.println("Layout Panes: changing title view ["+enableTitle+"]");
               fieldWidth += panes[i].myWidth();
            }
         }         
         setVirtualSize(fieldWidth,managerHeight);
         layoutChanged = false;
      }
   }
   
   protected void sublayout(int width, int height) {
      layoutPanes();
      super.sublayout(width, height);
   }

   //***********************************//
   public void snapToPane(boolean b){ autoScroll = b; }
   public void enableSwipe(boolean b){ enableSwipe = b; }
   public void autoHideTitle(boolean b){ autoHideTitle = b; }
   public void showPaneTitle(boolean b){
      this.enableTitle = b;
      if( b ){
         if( !autoHideTitle ){
            paneTitle.changeY(0);
         }
      } else {
         paneTitle.changeY(-paneTitle.myHeight());
      }
      layoutChanged = true;
   }
      
   public void setColor(int c){
      backgroundColor = c;
      for( int i=0; i<panes.length; i++ ){
         if( panes[i] != null ){
            panes[i].setColor(c);
         }
      }
      //paneTitle.setColor(c);
   }

   public void setGradient(int c){
      backgroundGradient = c;
      useGradient = true;
      for( int i=0; i<panes.length; i++ ){
         if( panes[i] != null ){
            panes[i].setGradient(c);
         }
      }
      //paneTitle.setGradient(c);
   }

   public void setFontColor(int c){
      fontColor = c;
      for( int i=0; i<panes.length; i++ ){
         if( panes[i] != null ){
            panes[i].setFontColor(c);
         }
      }
      //paneTitle.setFontColor(c);
   }

   /**
    * Sets the color pattern for title bar at top of panes
    *
    * @param dflt base color (color in center of title)
    * @param dfltGrad gradient color (color faded in from edges)
    * @param font font color
    * @return void
    */
   public void setTitleColors(int dflt, int dfltGrad, int font){
      paneTitle.setColor(dflt);        titleColor = dflt;
      paneTitle.setGradient(dfltGrad); titleGradient = dfltGrad;
      paneTitle.setFontColor(font);    titleFontColor = font;
   }

   public void setBackgroundAlpha(int alpha){
      bgAlpha = alpha;
      for( int i=0; i<panes.length; i++ ){
         if( panes[i] != null ){
            panes[i].setBackgroundAlpha(alpha);
         }
      }
      //paneTitle.setBackgroundAlpha(alpha);
   }
   
   public void paint(Graphics g){
      super.paint(g);

      if( paneTitle.getY() == 0 ){
         int width = myWidth();
         int tHght = paneTitle.myHeight()-1;

         g.setGlobalAlpha(240);
         int xpts [] = { 0,width/4,width/4,0 };
         int ypts [] = { 0,0,tHght,tHght };
         int cols [] = { backgroundColor,titleGradient,titleGradient,backgroundColor };
         g.drawShadedFilledPath(xpts, ypts, null, cols, null);

         g.setGlobalAlpha(255);
         xpts[0] = xpts[3] = width/4;
         xpts[1] = xpts[2] = width/2;
         cols[0] = cols[3] = titleGradient;
         cols[1] = cols[2] = titleColor;
         g.drawShadedFilledPath(xpts, ypts, null, cols, null);

         xpts[0] = xpts[3] = width/2;
         xpts[1] = xpts[2] = (width/4)*3;
         cols[0] = cols[3] = titleColor;
         cols[1] = cols[2] = titleGradient;
         g.drawShadedFilledPath(xpts, ypts, null, cols, null);

         g.setGlobalAlpha(240);
         xpts[0] = xpts[3] = (width/4)*3;
         xpts[1] = xpts[2] = width;
         cols[0] = cols[3] = titleGradient;
         cols[1] = cols[2] = backgroundColor;
         g.drawShadedFilledPath(xpts, ypts, null, cols, null);

         g.setColor(fontColor);
         g.drawLine(0, 0, width, 0);
         g.drawLine(0, tHght, width, tHght);

         //draw titles of next pane(s) centered on edges
         //int pane = (-drawX) / myWidth();
         int pane = MathUtilities.round((-(float)drawX) / width);
         String txt = panes[pane].getText();
         int fntWdth = getFont().getAdvance(txt);
         int bxWdth = width-20;
         //int scrAvl = (bxWdth-fntWdth)/2;
         //float rt = scrAvl/width;
         //int amt = -drawX % width;
         g.drawText(txt, 10, tHght>>>1, Graphics.VCENTER|Graphics.HCENTER, bxWdth);

         //System.out.println("DrawX: "+drawX+" | Pane (abs): "+(-((float)drawX)/width)+" | Pane: "+pane);

         if( pane > 0 ){
            txt = panes[pane-1].getText();
            fntWdth = getFont().getAdvance(txt);
            g.drawText(txt, -(fntWdth/2), tHght>>>1, Graphics.VCENTER|Graphics.HCENTER);
         } else {
            if( continuousScroll && (used_panes > 2) ){

            }
         }

         if( pane < panes.length-1 ){
            txt = panes[pane+1].getText();
            fntWdth = getFont().getAdvance(txt);
            g.drawText(txt, width-(fntWdth/2), tHght>>>1, Graphics.VCENTER|Graphics.HCENTER);
         } else {
            if( continuousScroll && (used_panes > 2) ){

            }
         }
      }
   }
   
   public boolean touchEvent(TouchEvent t){
      super.xTolerance = 20;
      boolean result = false;
      switch(t.getEvent()){
         case TouchEvent.MOVE:
              int x = t.getX(1)-(getAbsoluteX());
              int y = t.getY(1)-(getAbsoluteY());
              int xmov = prevX-t.getX(1); if( yLock ) xmov = 0;
              int ymov = prevY-t.getY(1); if( xLock ) ymov = 0;
              boolean xscr = (Math.abs(xmov) > xTolerance);
              boolean yscr = (Math.abs(ymov) > yTolerance);

              //Need to make sure contained managers do not scroll and not the pane manager as it will
              //never be able to scroll in y direction
              
              //if( xscr ){
              //   System.out.println("[HScroll] Amnt:"+xmov+" Toler:"+xTolerance+" X:"+t.getX(1)+" Px:"+prevX);
              //}

              if( !xLock && !yLock )
                 if( xscr && (Math.abs(xmov) > Math.abs(ymov)) ){
                    ymov = 0; xLock = true;
                 } else { xmov = 0; yLock = true; }

              if( focused != null ){
                 if( !xLock ) result = focused.myTouchEvent(t);
                 if( !result && (xscr || yscr) ){
                    if( focused instanceof CustomLayoutManager ){
                       result = checkForScroll(x,y,xmov,ymov,false);
                    } else {
                       result = checkForScroll(x,y,xmov,ymov,((Field)focused).isFocusable());
                    }
                    prevX = t.getX(1);
                    prevY = t.getY(1);
                 }
              } else {
                 if( (xscr || yscr) ){
                    result = checkForScroll(x,y,xmov,ymov,false);
                    prevX = t.getX(1);
                    prevY = t.getY(1);
                 }
              }
              break;
         case TouchEvent.UP:
         case TouchEvent.UNCLICK:
              xLock = false; yLock = false;
              result = super.touchEvent(t);
              break;
         default:
              result = super.touchEvent(t);
      }

      if( enableTitle && autoHideTitle && !yLock ){
         if( t.getEvent() == TouchEvent.MOVE ){
            if( Math.abs(prevX-t.getX(1)) > 5 ){
               paneTitle.changeY(0);
               layoutChanged = true;
               layoutPanes();
            }
         } else if( (t.getEvent() == TouchEvent.UP) ||
                    (t.getEvent() == TouchEvent.UNCLICK) ){
            paneTitle.changeY(-paneTitle.myHeight());
            layoutChanged = true;
            layoutPanes();
         }         
      }

      if( autoScroll ){
         if( t.getEvent() == TouchEvent.DOWN ){
            if( scroller != null ){ scroller = null; scroller.cancel(); }
            scrollStartPos = t.getX(1)-(getAbsoluteX());
         }
         if( t.getEvent() == TouchEvent.UP ){
            int pane = (-drawX) / myWidth();
            int offset = ((drawX+myVirtualWidth()) % myWidth());
            if( offset != 0 )
               if( offset < (myWidth()>>1) ){
                  //move to left boundary
                  if( drawX < 0 ) pane+=1;
                  if( pane >= panes.length ) pane = panes.length-1;
                  if( drawX > -myVirtualWidth() )
                     scroller = new Scroller(Scroller.SCROLL_LEFT,-(pane*myWidth()));
               } else {
                  //move to right boundary
                  if( pane >= panes.length ) pane = panes.length-1;
                  if( drawX > -myVirtualWidth() )
                     scroller = new Scroller(Scroller.SCROLL_RIGHT,-(pane*myWidth()));
               }
         }
      } else
      if( enableSwipe ){
         if( t.getEvent() == TouchEvent.GESTURE ){
            TouchGesture tg = t.getGesture();
            if( tg.getEvent() == TouchGesture.SWIPE ){
               int pane = 0;
               switch( tg.getSwipeDirection() ){
                  case TouchGesture.SWIPE_EAST:
                     pane = (-drawX) / myWidth();
                     if( ((-drawX)-50) >= (pane*myWidth()) ){
                        drawX = -(myWidth()*(pane));
                        if( drawX > 0 ) drawX = 0;
                     } else if (pane > 0) {
                        drawX = -(myWidth()*(pane-1));
                        if( drawX > 0 ) drawX = 0;
                     }
                     result = true;
                  case TouchGesture.SWIPE_WEST:
                     pane = (-drawX) / myWidth();
                     if( pane < (used_panes-1) ){
                        drawX = -((pane+1)*myWidth());
                        if( drawX < -(myVirtualWidth()-myWidth()) ) drawX = -(myVirtualWidth()-myWidth());
                     }
                     result = true;
               }
            }
         }
      }      
      return result;
   }

   protected boolean navigationMovement(int dx, int dy, int status, int time) {
      int pane = (-drawX) / myWidth();
      System.out.print("DX: "+dx+" -- Pane: "+pane);      
            
      boolean result = false;      
      
      if( Math.abs(dx) > 1 ){
         int dir = Scroller.SCROLL_RIGHT;
         if( dx > 1 ){ pane++; if(pane < panes.length-1) pane = panes.length-1; }
         if( dx < -1 ){ pane--; if(pane < 0) pane = 0; dir = Scroller.SCROLL_LEFT; }
         if( autoScroll ){
            if( scroller != null ){ scroller.cancel(); scroller = null; }
            scroller = new Scroller(dir,-(pane*myWidth()));
         } else {
            drawX = -pane * myWidth();
         }
         System.out.print(" -- Pane(new): "+pane);
      } else {
         if( panes[pane] != null ) result = panes[pane].myNavigationMovement(dx, dy, status, time);
      }
      System.out.println();
      //return super.navigationMovement(dx, dy, status, time);
      return result;
   }

   //==========================================================================================//
   private class Scroller extends TimerTask {
      static final int SCROLL_LEFT  = 0x1;
      static final int SCROLL_RIGHT = 0x2;
      int direction = 0; //Left = 1, Right = 2, Up = 3, Down = 4
      int scrollTo = 0;
      double moveAmnt = 0;
      long scrollStartTime = 0;
      Scroller(int dir, int to){
         String d = "left";
         if( dir == 2 ) d = "right";
         System.out.println("Pane Scroller created scrolling "+d+" to "+to+"!!!!!");
         System.out.println("Current drawing location: "+drawX);
         direction = dir;
         scrollTo = to;
         int iterations = scrollTime / 50;
         if( (dir==1) || (dir==2) ) moveAmnt = (Math.abs(to-drawX)/iterations);
         scrollStartTime = System.currentTimeMillis();
         if( timer == null ) timer = new Timer();
         timer.schedule(this, 0, 50);         
      }
      public void run(){
         if( direction == SCROLL_LEFT ){
            drawX = (int)(drawX-moveAmnt);
            if( drawX < scrollTo ) drawX = scrollTo;
         }
         if( direction == SCROLL_RIGHT ){
            drawX = (int)(drawX+moveAmnt);
            if( drawX > scrollTo ) drawX = scrollTo;
         }
         if( System.currentTimeMillis() > (scrollStartTime+scrollTime) ){
            drawX = scrollTo;
            System.out.println("Pane Scroller finished scrolling in ["+
                               (System.currentTimeMillis()-scrollStartTime)+"]!!!!!");
            this.cancel();
            scroller = null;
         }
         UiApplication.getUiApplication().getActiveScreen().invalidate();
      }
   }
}

 

Developer
Posts: 132
Registered: ‎10-25-2011
My Device: Tour 9630
My Carrier: MTN

Re: Panemanager for os5.0

Thanks a million will check it out and get back to u , stay blessed .
Developer
Posts: 132
Registered: ‎10-25-2011
My Device: Tour 9630
My Carrier: MTN

Re: Panemanager for os5.0

Please i am trying the code and i cant find the class CustomLayoutManager and MyField please what do i do?