Eclipse JFace Treeviewer视图未选择调用setSelection

时间:2015-09-23 11:28:52

标签: eclipse-plugin eclipse-rcp jface

我在带有treeviewer的eclipse插件中有一个视图,我试图根据编辑器中使用ISelectionService的选择在TreeViewer中选择一行。我能够找到必须选择的元素,我在下面的代码片段中尝试了以下内容:

v.setExpandedState(file, true);

v.refresh();

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

我还检查了Contentprovider的View,它返回了正确的父元素。

我在这里做错了什么?还有什么我需要检查和尝试?

我查了以下帖子:

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

编辑:

查看 -

 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 个答案:

答案 0 :(得分:0)

您似乎在正在执行选择的代码中再次加载DialogSettings,并使用新的对话框设置来设置选择。这不起作用,因为新加载的IDialogSettings对象与内容提供者返回的IDialogSettings对象不是同一对象。

如果实现了对象的equalshashCode方法以使两个对象相等,则只能使用不同的对象 - IDialogSettings不是这种情况。

您应该使用内容提供商返回的相同IDialogSettings对象。

或者,您可以为查看者提供IElementComparer,其中包含以下方法:

public boolean equals(Object a, Object b);

public int hashCode(Object element);

您必须实现此操作才能使两个IDialogSetting对象相等并具有相同的哈希码。