问题
I created a simple view based off of the eclipse plugin view tutorial. 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. 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. 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. 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.
Thank you for any help.
回答1:
createPartControls
is that time of the life cycle of a view, where its contained widgets are created (when the view becomes visible initially). 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 (e.g. the user might click on another stack frame in the debug view). 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.
回答2:
I have a similar problem with a tableViewer. For one case, the simple viewer.refresh() worked, but for another I had to get a solution not so "beautiful"..
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. 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.
回答3:
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. I've added ISelectionListener that listens to when a selection event happens (this goes in the ViewPart class as a global variable):
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.
来源:https://stackoverflow.com/questions/11352718/how-to-refresh-eclipse-view-plugin