Thread: About TreeField
View Single Post
Old 03-20-2007, 12:04 AM   #9
renuka_anil
Thumbs Must Hurt
 
Join Date: Mar 2007
Location: India
Model: 8100
Carrier: Airtel
Posts: 64
Default About Tree field

Hi

here is my whole code. I create a class in which tree is dynamically read from XML file generated.


public class CustomChart extends VerticalFieldManager{
// Declaration
private TreeCallback myCallback;
private TreeField myTree;
private boolean hasFocus; //false
private int[] nodeArray;
private int arrLength;
private Util objUtil ;

protected void sublayout(int maxWidth, int maxHeight){
maxWidth = 200 ;
maxHeight = 350;
super.sublayout(maxWidth, maxHeight);
}

//vecParent is vector for all parent node
//vecChild stores childs corresponding to that parent
// sizes is total number of nodes
// strTitle title for tree.

public CustomChart(String strTitle, Vector vecParent, Vector vecChild, int sizes ) {
super(Manager.HORIZONTAL_SCROLL | Manager.VERTICAL_SCROLL);
try{
objUtil = new Util();
//_screen = new MainScreen();
sizes = sizes + 1;
nodeArray = new int[sizes];
myCallback = new TreeCallback();
myTree = new TreeField(myCallback, Field.FOCUSABLE){
//Handles moving the focus within this field.
/* public int moveFocus(int amount, int status, int time)
{
invalidateNode(getCurrentNode());
return super.moveFocus(amount,status,time);
//return true;
}*/

//Invoked when this field receives the focus.
/* public void onFocus(int direction)
{
hasFocus = true;
//super.onFocus(direction);
}

//Invoked when a field loses the focus.
public void onUnfocus()
{
hasFocus = false;
//super.onUnfocus();
invalidate();
}
*/
//Over ride paint to produce the alternating colours.
/* public void paint(Graphics graphics)
{
//Get the current clipping region as it will be the only part that requires repainting
XYRect redrawRect = graphics.getClippingRect();
if(redrawRect.y < 0)
{
throw new IllegalStateException("Clipping rectangle is wrong.");
}

//Determine the start location of the clipping region and end.
int rowHeight = getRowHeight();

int curSelected;

//If the ListeField has focus determine the selected row.
if (hasFocus)
{
curSelected = getCurrentNode();
}
else
{
curSelected = -1;
}

int startLine = redrawRect.y / rowHeight;
int endLine = (redrawRect.y + redrawRect.height - 1) / rowHeight;
endLine = Math.min(endLine, getNodeCount() - 1);
int y = startLine * rowHeight;

//Setup the data used for drawing.
int[] yInds = new int[]{y, y, y + rowHeight, y + rowHeight};
int[] xInds = new int[]{0, getPreferredWidth(), getPreferredWidth(), 0};

//Get the ListFieldCallback.
//This sample assumes that the object returned by the get
//method of the callback is a String or has a toString method.
//If this is not the case you will need to add the required logic
//for your implementation.



//Draw each row
for(; startLine <= endLine; ++startLine)
{
if (startLine % 2 == 0 && startLine != curSelected)
{
//Draw the even and non selected rows.
graphics.setColor(LIGHT_TEXT);
graphics.drawShadedFilledPath(xInds, yInds, null, cols, null);
graphics.drawText((String)getCookie(startLine), 0, yInds[0]);
graphics.setColor(DARK_TEXT);
}
else
{
//Draw the odd or selected rows.
graphics.drawText((String)getCookie(startLine), 0, yInds[0]);
}

//Assign new values to the y axis moving one row down.
y += rowHeight;
yInds[0] = y;
yInds[1] = yInds[0];
yInds[2] = y + rowHeight;
yInds[3] = yInds[2];
}
}*/
};
int c = 0;
int j = 0;

String[] strSplit = null;
boolean flgParent = false;

String strTemp = null;

for(int i = 0; i < vecParent.size(); ){
int tmp = i;
tmp = tmp + 1;
boolean flag = true;
String strParent = (String) vecParent.elementAt(i);
String strChild = (String)vecChild.elementAt(i);
strSplit = objUtil.splitMethod(strChild, ',');
for(int cnt = 0; cnt < strSplit.length; cnt++){
if(strSplit[0].equals("0") && flag == true){
if(j == 0){
//j = j + 1;
nodeArray[j] = myTree.addChildNode(0,strParent);
c = j;
flag = false;
} else {
//j = j + 1;
nodeArray[j] = myTree.addChildNode(0,strParent);
c = j;
flag = false;
}
}else{
if(tmp < vecParent.size()){
strTemp = (String)vecParent.elementAt(tmp);
//System.out.println(" strTemp "+strTemp);
}

if(strSplit[cnt].equalsIgnoreCase(strTemp)){
strParent = strTemp;
nodeArray[j] = myTree.addChildNode(nodeArray[c],strParent);
c = j;
flgParent = true;
i = i + 1;
}else{
nodeArray[j] = myTree.addChildNode(nodeArray[c],strSplit[cnt]);
flgParent = false;
}

}
j = j + 1;
}
if( flgParent == false)
i = i + 1;
flag = true;
}


add(new LabelField (
strTitle , LabelField.NON_FOCUSABLE | LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH
));

add(myTree);

}catch(Exception e){
System.out.println("Exception in buiding chart : "+e.toString());
}// end of try-catch
}// end of constructor.


private class TreeCallback implements TreeFieldCallback {
Util objUtils;

private TreeCallback(){
objUtils = new Util();
}
public void drawTreeItem(TreeField _tree, Graphics g, int node, int y, int width, int indent) {
String text = (String)_tree.getCookie(node);
indent = indent + 5;
int row = _tree.getCurrentNode();
objUtils.setCurrentRow(row);
//System.out.println(" strParent "+text);
g.drawText(text, indent, y);
}
}
}// end of class



Please check this out. And tell me what's wrong with this. Also, can u tell me how to change textcolor and background color for current node having focus on it. Also how to add bitmaps for parent and child. Parent has different bitmaps than child


Thanks
__________________
Crazy for BlackBerry
Offline