Biswabir
Biswabir

Reputation: 19

want to create drop down list (Combo box viewer) in TreeColumn in SWT

I am using Tree and in this tree I have a five treecolumn. Also create two treeItem one is parent and other child, put their values in treecolumn by programatically. Now I need a dropdown List(Combobox) in each tree column(except first one) to view the list data. Currently getting only single value. Please see the below code to get tree item values editable in treecolumn.

enter image description here

private void editTreeTable(final Tree table){
     final TreeEditor editor = new TreeEditor(table);
        editor.horizontalAlignment = SWT.LEFT;
        editor.grabHorizontal = true;

        table.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseUp(final MouseEvent e) {
                final Control oldEditor = editor.getEditor();
                if (oldEditor != null) {
                    oldEditor.dispose();
                }

                final Point p = new Point(e.x, e.y);
                final TreeItem item = table.getItem(p);
                if (item == null) {
                    return;
                }
                for (int i = 1; i < table.getColumnCount(); ++i) {
                    if (item.getBounds(i).contains(p)) {
                        final int columnIndex = i;
                        // The control that will be the editor must be a
                        final Text newEditor = new Text(table, SWT.NONE);

                        newEditor.setText(item.getText(columnIndex ));

                        newEditor.addModifyListener(new ModifyListener() {
                            public void modifyText(final ModifyEvent e) {
                                final Text text = (Text) editor.getEditor();
                                editor.getItem().setText(columnIndex , text.getText());
                            }
                        });
                        newEditor.selectAll();
                        newEditor.setFocus();
                        editor.setEditor(newEditor, item, columnIndex );
                    }
                }
            }

        });

}

Now find the below code to get the tree item value from API

private void createTestSuiteTable( final Tree table) 
{      
       //Dispose all elements
    TreeItem items[] = table.getItems();
    for(int i=0;i<items.length;i++)
    {
        items[i].dispose();
    }
    TSGson tsGsons[] = TestSuiteAPIHandler.getInstance().getAllTestSuites();
    boolean checked=false;
    for (TSGson tsGson : tsGsons) 
    {
        parentTestSuite = new TreeItem(table, SWT.NONE|SWT.MULTI);
        parentTestSuite.setText(new String[] { "" +tsGson.tsName, "", "","","","" });
        parentTestSuite.setData("EltType","TESTSUITE");

        if(tsGson.tsTCLink==null)
                continue;

        for(TSTCGson tsTCGson : tsGson.tsTCLink) 
        {
            TreeItem trtmTestcases = new TreeItem(parentTestSuite, SWT.NONE|SWT.MULTI);
            trtmTestcases.setText(new String[] {tsTCGson.tcName, 
                    tsTCGson.tcParams.get(0)!=null ?tsTCGson.tcParams.get(0).tcparamValue:"",
                    tsTCGson.tcParams.get(1)!=null ?tsTCGson.tcParams.get(1).tcparamValue:"",
                    tsTCGson.tcParams.get(2)!=null ?tsTCGson.tcParams.get(2).tcparamValue:"",
                    "local",
                    tsTCGson.tcParams.get(4)!=null ?tsTCGson.tcParams.get(4).tcparamValue:"" });
            trtmTestcases.setData("EltType","TESTCASE");

            table.setSelection(parentTestSuite);
            if(checked)
            {
                  trtmTestcases.setChecked(checked);
            }
        }

    }
}

Find the below code for tree column creation in SWT

localHostTable = new Tree(composite_2,SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION | SWT.VIRTUAL);
localHostTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
localHostTable.setLinesVisible(true);
localHostTable.setHeaderVisible(true);

TreeColumn trclmnNewColumn_1 = new TreeColumn(localHostTable, SWT.NONE);
trclmnNewColumn_1.setWidth(113);
trclmnNewColumn_1.setText("TestSuite/TestCase");

TreeColumn trclmnColumn_5 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_5.setWidth(73);
trclmnColumn_5.setText("Exe_Platform");


TreeColumn trclmnColumn_6 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_6.setWidth(77);
trclmnColumn_6.setText("Exe_Type");

TreeColumn trclmnColumn_7 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_7.setWidth(85);
trclmnColumn_7.setText("Run_On");

TreeColumn trclmnColumn_8 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_8.setWidth(81);
trclmnColumn_8.setText("Thread-Count");

final TreeColumn trclmnColumn_9 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_9.setWidth(97);
trclmnColumn_9.setText("Column5");

please suggest

Upvotes: 0

Views: 1496

Answers (1)

avojak
avojak

Reputation: 2360

Since there's nothing in your question about Combo or CCombo controls, I can't help you troubleshoot an issue. I also am not going to write your code for you, but I can try to point you in the right direction with a short example.

Yes, i want the combo to always be visible.

You can still use a TreeEditor to accomplish this, and it will actually be simpler than the code snippet you posted with the MouseListener.

Create the CCombo (or Combo) as you would in any other situation, and use TreeEditor.setEditor(...) methods to specify that the CCombo control should be displayed in that cell:

// ...
final CCombo combo = new CCombo(tree, SWT.NONE);
final TreeEditor editor = new TreeEditor(tree);
editor.setEditor(combo, item, 1);
// ...

enter image description here

Full MCVE:

public class TreeComboBoxTest {

    private final Display display;
    private final Shell shell;

    public TreeComboBoxTest() {
        display = new Display();
        shell = new Shell(display);
        shell.setLayout(new FillLayout());

        final Tree tree = new Tree(shell, SWT.BORDER | SWT.VIRTUAL | SWT.FULL_SELECTION);
        tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        tree.setLinesVisible(true);
        tree.setHeaderVisible(true);

        final TreeColumn column1 = new TreeColumn(tree, SWT.NONE);
        column1.setWidth(75);
        column1.setText("Column 1");

        final TreeColumn column2 = new TreeColumn(tree, SWT.NONE);
        column2.setWidth(75);
        column2.setText("Column 2");

        final TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText(0, "Hello");

        final CCombo combo = new CCombo(tree, SWT.NONE);
        combo.setItems(new String[] { "Item 1", "Item 2", "Item 3" });

        final TreeEditor editor = new TreeEditor(tree);
        editor.setEditor(combo, item, 1);
        editor.horizontalAlignment = SWT.LEFT;
        editor.grabHorizontal = true;

        // Optional, but allows you to get the current value by calling
        // item.getText() instead of going through the TreeEditor and
        // calling ((CCombo) editor.getEditor()).getText()
        combo.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(final SelectionEvent e) {
                item.setText(1, combo.getText());
            }
        });
    }

    public void run() {
        shell.setSize(200, 200);
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    public static void main(final String... args) {
        new TreeComboBoxTest().run();
    }

}

Note the SelectionListener added to the CCombo. Even though you've used the TreeEditor, if you call item.getText(index), it will return an empty String because setText(...) has not been called. By calling setText(...) in the listener, you won't have to go through the TreeEditor to get the value.

So you can call item.getText(index) instead of ((CCombo) editor.getEditor()).getText().

Upvotes: 2

Related Questions