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
Contributor
Posts: 33
Registered: ‎08-19-2010
My Device: Not Specified

Implementing a Custom EyeLidField

hi evrybody,

 

i want to create my custom eyeLidField, because  i have some trouble with the one given in the framework.

 

so i have i method which hide and show a custom Manager and for that i call

myEyeLidFieldManager.sublayout(width, height) with a incremented attribute when i set the position of the eyeLidField. 

myEyeLidFieldManager.invalidate();

 

it works fine but i have during the process the same Exception which appears during the showing and the hiding, it's a outOfTheBounds exception...

 

here is the code:

 

i have a diapoManager which allow me to put my custom semi transparent eyeLidField on a PictureScroller.

and my custom eyeLidField which i want to go up and down...

 

Thanks for your help

 

 

package redshift.blackberry.minutes;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import redshift.blackberry.utils.Diaporama;
import redshift.blackberry.utils.Image;
import redshift.blackberry.utils.RefreshDiapoPopup;
import redshift.blackberry.utils.RefreshListener;
import redshift.blackberry.utils.RefreshPopupScreen;
import redshift.j2me.webtools.Webtools;
import redshift.j2me.xmltools.DiaporamaParser;

import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.browser.field2.BrowserFieldConfig;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiEngine;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.FullScreen;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.extension.component.PictureScrollField;
import net.rim.device.api.ui.extension.component.PictureScrollField.ScrollEntry;
import net.rim.device.api.ui.extension.container.EyelidFieldManager;

public class DiapoScreen extends FullScreen implements FieldChangeListener{

	private MinuteController _controller;
	private DiapoScreenManager _diapoManager;
	private CustomPictureScrollField _pictureScrollField;
	private Diaporama _diapo;
	private EyeLidField _eyeLidField;
	private BrowserField _bf;
	private Vector _images;
	private int _currentPosition;
	private int _size;
	private ScrollEntry _entry[];
	private int _y;
	
	public DiapoScreen(MinuteController controller, Diaporama diapo ,long style){
		super(style);
	
		System.out.println("DiapoScreen => constructor");
		
		this._diapo = diapo;
		this._images = diapo.getImages();
		this._controller = controller;
		this._currentPosition = 0;
		this._y = 260;
		
		//header
		HorizontalFieldManager header = new HorizontalFieldManager(Manager.NO_HORIZONTAL_SCROLL);
		header.add(new BitmapField(Bitmap.getBitmapResource("bkg_header.png")));
		this.add(header);
		
		this._diapoManager = new DiapoScreenManager(NO_VERTICAL_SCROLL);
		this.add(this._diapoManager);
		
		//pictureScroll
		if(this._images != null){			
			this._pictureScrollField = new CustomPictureScrollField(480, 340);
			this._pictureScrollField.setChangeListener(this);
			_entry = new ScrollEntry[this._images.size()];
			for(int i=0; i<this._images.size(); i++){
				Image image = (Image)this._images.elementAt(i);
				if(image.getImage() == null){
					_entry[i] = new ScrollEntry(Bitmap.getBitmapResource("loading.png"), null, null);					
				}
				else{
					_entry[i] = new ScrollEntry(image.getImage(), null,null);
				}
			}
			this._pictureScrollField.setData(_entry, 0);
			this._pictureScrollField.setLabelsVisible(false);
			this._pictureScrollField.setHighlightStyle(PictureScrollField.HighlightStyle.NO_HIGHLIGHT);
			this._pictureScrollField.setPaddingImageTop(0);
			this._diapoManager.add(this._pictureScrollField);
			
			//eyeField
			this._eyeLidField = new EyeLidField(NO_VERTICAL_SCROLL);
			this._eyeLidField.setLabelText(((Image)this._images.elementAt(0)).getLegende());
			this._diapoManager.add(this._eyeLidField);
			
			this.loadBitmap();
		}
		else{
			this.loadDiapo();
		}
		
		
	}
	
	private void loadDiapo(){
		System.out.println("DiapoScreen => loadDiapo");
		RefreshDiapoThread refresh = new RefreshDiapoThread();
		refresh.start();
	}
	
	public void constructView(){
		if(this._images != null){			
			this._pictureScrollField = new CustomPictureScrollField(480, 340);
			this._pictureScrollField.setChangeListener(this);
			_entry = new ScrollEntry[this._images.size()];
			Image image = null;
			for(int i=0; i<this._images.size(); i++){
				image = (Image) this._images.elementAt(i);
				_entry[i] = new ScrollEntry(Bitmap.getBitmapResource("loading.png"),null,null);
			}
			this._pictureScrollField.setData(_entry, 0);
			this._pictureScrollField.setLabelsVisible(false);
			this._pictureScrollField.setHighlightStyle(PictureScrollField.HighlightStyle.NO_HIGHLIGHT);
			this._pictureScrollField.setPaddingImageTop(0);
			this._diapoManager.add(this._pictureScrollField);
			
			//eyeField
			this._eyeLidField = new EyeLidField(NO_VERTICAL_SCROLL);
			this._eyeLidField.setLabelText(((Image)this._images.elementAt(0)).getLegende());
			this._diapoManager.add(this._eyeLidField);
			
			this.loadBitmap();
			
			//this.hideEyeLidField();
		}
	}
	
	public void hideEyeLidField(){
		Timer t = new Timer();
		TimerTask hideTask = new TimerTask() {
			public void run() {
				if(_y == 360){
					showEyeLidField();
					this.cancel();
				}
				else{
					_y++;
					_diapoManager.sublayout(_diapoManager.getContentWidth(), _diapoManager.getContentHeight());
					_diapoManager.invalidate();
				}
			}
		};
		t.schedule(hideTask, 0, 10);
	}
	
	
	public void showEyeLidField(){
		Timer t = new Timer();
		TimerTask showTask = new TimerTask() {
			
			public void run() {
				if(_y == 260){
					hideEyeLidField();
					this.cancel();
				}
				else{
					_y--;
					_diapoManager.sublayout(_diapoManager.getWidth(), _diapoManager.getHeight());
					_diapoManager.invalidate();
				}
				
			}
		};
		t.schedule(showTask, 0, 10);
	}
	

	public void loadBitmap(){
		BitmapThread t = new BitmapThread();
		t.start();
	}
	
	private class BitmapThread implements Runnable {
		
		public BitmapThread(){
			
		}
		
		public void run(){
			Image image = null;
			for(int i=0; i<_images.size(); i++){
				image = (Image) _images.elementAt(i);
				if(image.getImage() == null){
					Bitmap bitmap = null;
					try {
						bitmap = Webtools.getRemoteBitmap(image.getImageUrl(), 480, 340, true);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
					synchronized (Application.getEventLock()) {
						image.setImage(bitmap);
						_entry[i].setPicture(bitmap);
						_pictureScrollField.test();
					}
				}
			}
			hideEyeLidField();
		}
		
		public void start(){
			Thread t = new Thread(this);
			t.start();
		}
	}
	
	private class CustomPictureScrollField extends PictureScrollField {

		public CustomPictureScrollField(int imageWidth,int imageHeight){super(imageWidth, imageHeight);
		// TODO Auto-generated constructor stub
		}
		
		public void test(){
			this.invalidate();
		}
		
	}
	
	private class EyeLidField extends Manager {
		
		private int _alpha;
		private LabelField _label;
		
		public EyeLidField(long style){
			super(style);
			this.setBackground(BackgroundFactory.createSolidTransparentBackground(Color.BLACK, 150));
			this._label = new LabelField(){
				public void paint(Graphics g){
					g.setColor(Color.WHITE);
					super.paint(g);
				}
			};
			this.add(this._label);
		}
		
		public void setLabelText(String t){
			this._label.setText(t);
		}
		

		protected void sublayout(int width, int height) {
			Field field = null;
			int nb = this.getFieldCount();
			for(int i=0; i<nb; i++){
				field = this.getField(i);
				int position[] = getFieldPosition(i);
				this.setPositionChild(field, position[0], position[1]);
				this.layoutChild(field, width, 100);
			}
			setExtent(width, 100);
		}
		
		/*
		protected void subpaint(Graphics g){
			super.subpaint(g);
		}
		*/
		
		private int[] getFieldPosition(int index){
			int[] position = new int[2];
			switch(index){
			case 0: //label
				position[0] = 5;
				position[1] = 5;
				break;
			default:
				position[0] = 0;
				position[1] = 0;
				break;
			}
			return position;
		}
		
	}
	
	private class DiapoScreenManager extends Manager{
		
		public DiapoScreenManager(long style){
			super(style);
		}

		protected void sublayout(int width, int height) {
			Field field = null;
			int nb = this.getFieldCount();
			for(int i=0; i<nb; i++){
				field = this.getField(i);
				int position[] = getFieldPosition(i);
				this.setPositionChild(field, position[0], position[1]);
				this.layoutChild(field, width, height);
			}
			setExtent(width, height);
		}
		
		private int[] getFieldPosition(int index){
			int[] position = new int[2];
			switch(index){
			case 0: //picture
				position[0] = 0;
				position[1] = 0; 
				break;
			case 1: //eye
				position[0] = 0;
				position[1] = _y;
				break;
			default:
				position[0] = 0;
				position[1] = 0;
				break;
			}
			return position;
		}
		
		public void layoutEyeLidField(){
			this.setPositionChild(_eyeLidField, 0, _y);
			this.layoutChild(_eyeLidField, this.getWidth(), this.getHeight());
		}
	}
	
	private class RefreshDiapoThread implements Runnable {

		
		private RefreshDiapoPopup _popup;
		public void run() {
			
			//On affiche la popup de mise en attente
			synchronized(Application.getEventLock()){    
				UiEngine ui = Ui.getUiEngine();
				this._popup = new RefreshDiapoPopup(new VerticalFieldManager(), Screen.DEFAULT_CLOSE, "Chargement du diaporama...");
				ui.pushGlobalScreen(this._popup, 1, UiEngine.GLOBAL_QUEUE);
			}
			
			DiaporamaParser parser = new DiaporamaParser(_diapo.getId());
			Vector images = parser.getList();
			_diapo.setVector(images);
			_images = _diapo.getImages();
			
			synchronized (Application.getEventLock()) {
				this._popup.close();
				constructView();
			}
		}
		
		public void start(){
			Thread t = new Thread(this);
			t.start();
		}
	}

	public void fieldChanged(Field field, int context) {
		if(context == PictureScrollField.CONTEXT_CHANGE_SELECTION){
			Image image = (Image) this._images.elementAt(this._pictureScrollField.getCurrentImageIndex());
			this._eyeLidField.setLabelText(image.getLegende());
		}
	}

	
}

 

 

 

 

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Implementing a Custom EyeLidField

You should never explicitly call your sublayout() method (except in cases when you call super.sublayout() from your own sublayout() - not applicable when extending Manager) - it is called by the BlackBerry UI framework. You override it to provide your custom behavior instead.

 

If you want to force sublayout, call updateLayout().

 

As for index out of bounds exception - set breakpoint "When exception ares thrown..." and choose the exception you are getting. When the debugger stops at the breakpoint, examine the current statement, index value etc. and think how it could get out of bounds...

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!