简体   繁体   English

如何刷新Eclipse View插件

[英]How to Refresh Eclipse View Plugin

I created a simple view based off of the eclipse plugin view tutorial. 我基于eclipse插件视图教程创建了一个简单的视图。 I added functionality that allows my plugin to listen to changes on the debugger. 我添加了允许我的插件侦听调试器上的更改的功能。 My problem is, every time something on the debugger happens, I want my view to be refreshed and be updated with new information. 我的问题是,每次调试器发生问题时,我都希望刷新视图并使用新信息进行更新。 Here is what I have/what I'm trying: 这是我所尝试的/正在尝试的:

    public void createPartControl(Composite parent) {

        listener = new DebugContextListener(this);
        DebugUITools.getDebugContextManager().addDebugContextListener(listener);

        // Check if there is an already started debug context
        IAdaptable dc = DebugUITools.getDebugContext();
        if (dc != null) {
                dataCollection.add(new ProxyScope("hi")); // manually injecting data 
                Object o = dc.getAdapter(IStackFrame.class);
                if (o instanceof IStackFrame)
                        setStackFrame((IStackFrame) o);

                viewer.refresh(); // this doesn't work
        }       


        viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);

        // Set column lines visible, and create two columns
        Tree tree = viewer.getTree();
        tree.setHeaderVisible(true);
        tree.setLinesVisible(true);

        TreeColumn column1 = new TreeColumn(tree, SWT.LEFT);
        column1.setText("Name");
        column1.setWidth(400);
        TreeColumn column2 = new TreeColumn(tree, SWT.LEFT);
        column2.setText("Value");
        column2.setWidth(200);

        drillDownAdapter = new DrillDownAdapter(viewer);
        viewer.setContentProvider(new VariableViewContentProvider());
        viewer.setLabelProvider(new VariableViewLabelProvider());
        viewer.setSorter(new ViewerSorter());
        viewer.setInput(getViewSite());

....

}

So basically, I have all this debug listening logic at the top, which happens inside that if statement. 因此,基本上,所有这些调试侦听逻辑都位于顶部,它发生在if语句内部。 I know for a fact that my program gets in there. 我知道我的程序进入那里。 Everytime somethign changes I want to refresh my view, and I've tried doing viewer.refresh, which doesnt work. 每次发生重大变化时,我都希望刷新视图,并且我尝试执行viewer.refresh,但该方法不起作用。 The information my view displays is based off of the dataCollection object, so that line with the dataCollection.add... is me just adding data manually as if the debugger did something. 我的视图显示的信息基于dataCollection对象,因此与dataCollection.add ...对应的行就是我只是手动添加数据,就像调试器做了什么一样。 If I put that line outside the if statement, then my view works (I'm guessing this is just the original construction of the view when I first start the plugin), but when I put it inside the if statement it doesn't work, meaning my view never refreshes. 如果我把那行放在if语句之外,那么我的视图就起作用了(我猜这只是我第一次启动该插件时视图的原始结构),但是当我把它放在if语句中时,它是行不通的,这表示我的观点永不刷新。

Thank you for any help. 感谢您的任何帮助。

createPartControls is that time of the life cycle of a view, where its contained widgets are created (when the view becomes visible initially). createPartControls是视图生命周期的时间,在该时间中创建其包含的小部件(当视图最初可见时)。 This code is only executed once during the view life cycle, therefore you cannot add anything here directly to refresh your view. 该代码在视图生命周期中仅执行一次,因此您不能在此处直接添加任何内容来刷新视图。

Eclipse parts typically update their content as a reaction to a changed selection inside of the workbench (eg the user might click on another stack frame in the debug view). Eclipse部件通常会更新其内容,以响应工作台内部更改的选择(例如,用户可能在调试视图中单击另一个堆栈框架)。 I'm not sure if that already completely fulfills your needs, but it's a good start for sure and described well in the Eclipse FAQ . 我不确定这是否已经完全满足您的需求,但这是一个很好的开始,并且在Eclipse FAQ中进行了很好的描述。

I have a similar problem with a tableViewer. 我有一个与tableViewer类似的问题。 For one case, the simple viewer.refresh() worked, but for another I had to get a solution not so "beautiful".. 在一种情况下,简单的viewer.refresh()起作用了,但在另一种情况下,我不得不获得一种并非如此“美丽”的解决方案。

The solution that I have for now is not set the input to null with viewer.setInput(null), and then set the input again the updated data. 我现在拥有的解决方案不是使用viewer.setInput(null)将输入设置为null,然后再次将输入设置为更新的数据。 This temporarily results for me, since I don't have a large amount of data. 这对我来说是暂时的结果,因为我没有大量数据。 Otherwise, the overhead of the creation on every change may not be feasible. 否则,每次更改的创建开销可能都不可行。

If I find a cleaner solution I will post it. 如果我找到一个更清洁的解决方案,我将其发布。

Thanks to Bananeweizen's answer, I've been able to hack up some code that does similar to what I need it to do, even though there is probably a better way. 感谢Bananeweizen的回答,即使有更好的方法,我也能够破解一些与我需要执行的代码类似的代码。 I've added ISelectionListener that listens to when a selection event happens (this goes in the ViewPart class as a global variable): 我添加了ISelectionListener,它在选择事件发生时进行侦听(这在ViewPart类中作为全局变量出现):

 ISelectionListener selectionListener = new ISelectionListener() { public void selectionChanged(IWorkbenchPart part, ISelection sel) { if (!(sel instanceof IStructuredSelection)) return; IStructuredSelection ss = (IStructuredSelection) sel; if(sel.toString().contains("org.eclipse.jdt.internal.debug.core.model")){ viewer.setInput(getSite()); } } }; 

So if I click on any of the debug buttons, it will refresh my view. 因此,如果单击任何调试按钮,它将刷新我的视图。 setInput basically calls getElements() in the ContentProvider which provides specific implementation of how to form the table inside my view. setInput基本上在ContentProvider中调用getElements(),它提供了有关如何在视图中形成表格的特定实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM