views:

484

answers:

1

Hi , Is it possible to add checkboxes to a treefield in Blackberry?

If yes, how do I do it?

Thanks in advance.

Please help

A: 

Same trick as with ListBox CheckBoxes:
alt text

class CBTreeField extends VerticalFieldManager implements TreeFieldCallback,
        DrawStyle {
    boolean[] mBooleanValues = new boolean[] {};
    String[] mStringValues = new String[] {};

    public boolean getNodeBooleanValue(int node) {
        return mBooleanValues[node];
    }

    public void setNodeBooleanValue(int node, boolean value) {
        mBooleanValues[node] = value;
    }

    TreeField mRootField = null;

    public CBTreeField(String rootString, boolean rootBoolean) {
        mRootField = new TreeField(this, TreeField.FOCUSABLE);
        add(mRootField);
        mStringValues = insertAt(mStringValues, 0, rootString);
        mBooleanValues = insertAt(mBooleanValues, 0, rootBoolean);
    }

    public int addSiblingNode(int previousSibling, String stringValue,
            boolean booleanValue, Object cookie) {
        int index = mRootField.addSiblingNode(previousSibling, cookie);
        mBooleanValues = insertAt(mBooleanValues, index, booleanValue);
        mStringValues = insertAt(mStringValues, index, stringValue);
        return index;
    }

    public int addChildNode(int parent, String stringValue,
            boolean booleanValue, Object cookie) {
        int index = mRootField.addChildNode(parent, cookie);
        mBooleanValues = insertAt(mBooleanValues, index, booleanValue);
        mStringValues = insertAt(mStringValues, index, stringValue);
        return index;
    }

    static boolean[] insertAt(boolean[] inArray, int index, boolean value) {
        int newLen = inArray.length + 1;
        boolean[] outArray = new boolean[newLen];
        for (int i = 0, j = 0; i < newLen; i++, j++) {
            outArray[i] = (i != index) ? inArray[j] : value;
            if (i == index)
                j++;
        }
        return outArray;
    }

    static String[] insertAt(String[] inArray, int index, String value) {
        int newLen = inArray.length + 1;
        String[] outArray = new String[newLen];
        for (int i = 0, j = 0; i < newLen; i++, j++) {
            outArray[i] = (i != index) ? inArray[j] : value;
            if (i == index)
                j++;
        }
        return outArray;
    }

    public void drawTreeItem(TreeField treeField, Graphics g, int node, int y,
            int width, int indent) {
        String check = String
                .valueOf(mBooleanValues[node] ? 
                Characters.BALLOT_BOX_WITH_CHECK : Characters.BALLOT_BOX);
        g.drawText(check, indent, y, DrawStyle.LEFT);
        g.drawText(mStringValues[node], indent + 20, y, DrawStyle.RIGHT
                | ELLIPSIS);
    }

    protected void makeMenu(Menu menu, int instance) {
        super.makeMenu(menu, instance);
        menu.add(new MenuItem("Change value", 0, 0) {
            public void run() {
                int node = mRootField.getCurrentNode();
                mBooleanValues[node] = !mBooleanValues[node];
                invalidate();
            }
        });
    }
}

sample of use:

class Scr extends MainScreen {
    public Scr() {
        CBTreeField tree = new CBTreeField("root", false);
        add(tree);

        int ch01 = tree.addChildNode(0, "child 0-1", true, null);
        int ch02 = tree.addChildNode(0, "child 0-2", false, null);
        int ch03 = tree.addChildNode(0, "child 0-3", false, null);
        int ch011 = tree.addChildNode(ch01, "child 0-1-1", false, null);
        int ch012 = tree.addChildNode(ch01, "child 0-1-2", true, null);
        int ch031 = tree.addChildNode(ch03, "child 0-3-1", true, null);
    }
}
Max Gontar