0
votes

I have a view in my eclipse plugin with a treeviewer and I am trying to select a row in TreeViewer based on a selection in my editor using ISelectionService. I am able to locate the element which has to be selected and I have tried following in code snippet below:

v.setExpandedState(file, true);

v.refresh();

v.setSelection(new StructuredSelection(iDialogSettings), true);

I have also checked Contentprovider for the View and it is returning the correct parent element.

What I am doing wrong here ? what else I need to check and try ?

I have checked following post :

https://alexandraniculai.wordpress.com/2010/08/14/when-jface-treeviewer-setselection-doesnt-work/

Edit :

View-

 public class Objects extends ViewPart {

        public static final String ID = "suitacore.views.Objects"; //$NON-NLS-1$

        private TreeViewer v = null;

        private Tree tree;

        private List <IFile> outer; 

        private Action groupByDate;

        public Objects() {
        }


        private void resizeTable(Tree table_) {
        for (TreeColumn tc : table_.getColumns())
            tc.pack();
    }

        /**
         * Create contents of the view part.
         * @param parent
         */
        @Override
        public void createPartControl(Composite parent) {

        v = new TreeViewer(parent, SWT.FULL_SELECTION | SWT.V_SCROLL
            | SWT.H_SCROLL | SWT.BORDER);

        tree = v.getTree();
        v.getTree().setLinesVisible(true);

        v.getTree().setHeaderVisible(true);

        v.getTree().setFont((new Font(null, "Calibri", 11, SWT.CENTER)));



        String[] columLabels = { 
            "                   ObjectMaps                 ",
            "ObjectReference", 
            "ObjectTagName",
            "ObjectIDAttribute",
            "ObjectNameAttribute", 
            "ObjectXPathAttribute",
            "ObjectClassAttributes",
            "     ObjectText    " , 
            "MoreAttributes"};


        for (int i = 0; i < columLabels.length; i++) {
            TreeViewerColumn column = new TreeViewerColumn(v, SWT.NONE);
            column.getColumn().setWidth(100);
            column.getColumn().setMoveable(true);
            column.getColumn().setText(columLabels[i]);

            if (i == 1) {
                column.setEditingSupport(new ObjectsViewTextCellEditingSupport(v));
            }

        }



        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

        IProject projects[] = root.getProjects();

        outer = new ArrayList<IFile>();

        for (IProject iProject : projects) {
            IFolder objectMap = iProject.getFolder(ProjectConstants.objectMap);

            if (null != objectMap) {
            try {
                for (IResource iResource : objectMap.members()) {
                if (iResource instanceof IFile) {
                    IFile file = (IFile) iResource;
                    if (file.getFileExtension().equals("xml")) {

                    outer.add(file);

                    }

                }
                }
            } catch (CoreException e) {
                e.printStackTrace();
            }

        }

        }

        v.setContentProvider(new ObjectViewContentProvider(outer));
        v.setLabelProvider(new ObjectViewLabelProvider());
        v.setInput(outer);




        createActions();
        initializeToolBar();
        initializeMenu();
        resizeTable(tree);

        getSite().setSelectionProvider(v);

        ISelectionService ss = getSite().getWorkbenchWindow().getSelectionService();
        ss.addPostSelectionListener(listener);


        }

        private Object searchObjectfromID(String objectID) {

            Object iSection = null;
            for (IFile file : outer) {

                if (file.getFileExtension().equalsIgnoreCase("xml")) {

                    DialogSettings settings = new DialogSettings("root");

                    try {
                        settings.load(file.getLocation().toString());

                        IDialogSettings sections[] = settings.getSections();

                        for (IDialogSettings iDialogSettings : sections) {

                            if (iDialogSettings.getName().equals(objectID)) {

                                System.out.println("Dialog setting for the element>" + iDialogSettings.getName());

                                v.setExpandedState(file, true);

                                //v.setSelection(new StructuredSelection(file), true);

                                v.setSelection(new StructuredSelection(iDialogSettings), true);

                                v.refresh(true);

                                break;
                            }

                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return iSection;
        }

        /* adding listeners*/
        private ISelectionListener listener = new ISelectionListener() {

            public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection) {

                if (sourcepart != Objects.this)  {

                    System.out.println("printing selection for object view ..." + selection);
                    if (selection instanceof IStructuredSelection) {
                        IStructuredSelection ss = (IStructuredSelection) selection;
                        Object element = ss.getFirstElement();
                        System.out.println("printing selected element for object view ..." + element);
                        if (element instanceof CSVRow) {
                            CSVRow row = (CSVRow) element;
                            System.out.println("here is our obejct id in the obejct view" + row.getElementAt(2));
                            final String objectID= row.getElementAt(2);

                            PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
                                public void run() {
                                searchObjectfromID(objectID);
                                }
                            });

                        }
                    }
                }
            }
        };

        /**
         * Create the actions.
         */
        private void createActions() {
        // Create the actions
        }

        /**
         * Initialize the toolbar.
         */
        private void initializeToolBar() {
        IToolBarManager toolbarManager = getViewSite().getActionBars()
            .getToolBarManager();
        }

        /**
         * Initialize the menu.
         */
        private void initializeMenu() {
        IMenuManager menuManager = getViewSite().getActionBars()
            .getMenuManager();
        }

        @Override
        public void setFocus() {
        // Set the focus
        }

    }

ContentProvider:

public class ObjectViewContentProvider implements ITreeContentProvider {

    private List <IFile> outer; 

    public ObjectViewContentProvider(List<IFile> outer) {
        super();
        this.outer = outer;
    }

    @Override
    public void dispose() {

    }

    @Override
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

    }

    @Override
    public Object[] getElements(Object inputElement) {
        return ((List) inputElement).toArray();
    }

    @Override
    public Object[] getChildren(Object parentElement) {

        Object obj[] = null;

        if (parentElement instanceof IFile) {

            System.out.println("get children called ObejctVewContentProvider......." + parentElement);

            IFile file = (IFile) parentElement;

            if (file.getFileExtension().equalsIgnoreCase("xml")) {

                DialogSettings rootSection = new DialogSettings("root");

                try {
                    rootSection.load(file.getLocation().toString());

                    obj = rootSection.getSections();

                    System.out.println("no of childeren for file parent>>" + obj.length );
                } 
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        else if (parentElement instanceof IDialogSettings) {
            /*System.out.println("get children called ObejctVewContentProvider......." + parentElement);

            IDialogSettings objects = (IDialogSettings) parentElement;

            obj = objects.getSections();

            System.out.println("no of childeren for IDialogsettings parent>>" + obj.length );*/
        }

        return obj;
    }

    @Override
    public Object getParent(Object element) {

        IFile ifile = null;

        System.out.println("get parent called ObejctVewContentProvider........................" + element);

        if (element instanceof IDialogSettings) {

            System.out.println("ObejctVewContentProvider......>>>" + element);

            IDialogSettings section = (IDialogSettings) element;

            for (IFile file : outer) {

                if (file.getFileExtension().equalsIgnoreCase("xml")) {

                    DialogSettings settings = new DialogSettings("root");

                    try {
                        settings.load(file.getLocation().toString());

                        IDialogSettings sections[] = settings.getSections();

                        for (IDialogSettings iDialogSettings : sections) {

                            if (iDialogSettings.getName().equals(section.getName())) {
                                ifile = file;
                                System.out.println("Returning parent for section >>" + ifile);
                                break;
                            }

                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        }

        else if (element instanceof DialogSettings) {
            System.out.println("ObejctVewContentProvider......<<<<<<<<<<<<<<<<<<<<<<<<<<<" + element);

        }

        return ifile;
    }

    @Override
    public boolean hasChildren(Object element) {
        Object[] obj = getChildren(element);
        return obj == null ? false : obj.length > 0;
    }



}
1
It is impossible to tell from this. What is iDialogSettings? Is it something returned by the content provider? - greg-449
I have added code for View and ContentProvider - Amrit

1 Answers

0
votes

You appear to be loading the DialogSettings again in the code that is doing the selection and using the new dialog settings to set the selection. This will not work because the newly loaded IDialogSettings object is not the same object as the IDialogSettings object returned by your content provider.

You can only use different objects if the equals and hashCode methods of the objects are implemented so that the two object are equal - this is not the case for IDialogSettings.

You should use the same IDialogSettings object that the content provider returned.

Alternatively you can provide the viewer with an IElementComparer which has the methods:

public boolean equals(Object a, Object b);

public int hashCode(Object element);

You would have to implement this to make the two IDialogSetting object equal and have the same hash code.