如何显示最新的Eclipse错误日志条目

时间:2014-07-02 10:36:59

标签: eclipse

在Eclipse错误日志中创建一个新条目后,我希望它可以在用户的​​视图中,即使它们之前已在日志视图中向下滚动。

org.osgi.framework.Bundle bundle = ...;
ILog log = myPlatform.getLog(bundle);
log.log(status);

然后我想让UI视图滚动到该新项目,以便它可供用户查看。我将如何继续这样做?

1 个答案:

答案 0 :(得分:0)

所以似乎没有任何适当的API来做到这一点。我必须在日志记录中注册一个监听器并使用反射来实际使顶部项目滚动到视图中。

/* (non-Javadoc)
 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
 */
@Override
public void start(BundleContext context) throws Exception {
    super.start(context);
    // other stuff here
    Platform.addLogListener(new ScrollToLatestLogListener());
}

public class ScrollToLatestLogListener implements ILogListener {

    @Override
    public void logging(IStatus status, String plugin) {
        Display.getDefault().asyncExec(new Runnable() {
            @Override
            public void run() {
                IViewPart part = getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("org.eclipse.pde.runtime.LogView");
                if (part != null) {
                    ErrorLogAction action = new ErrorLogAction();
                    action.setActivePart(null, part);
                    action.run(null);
                }
            }
        });
    }
}

幸运的是,一位同事已经实施了这项工作,以便做真正的工作:

public class ErrorLogAction implements IObjectActionDelegate, IViewActionDelegate {

    IWorkbenchPart part;

    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
        part = targetPart;
    }

    public void run(IAction action) {
        try {
            Field treeField = part.getClass().getDeclaredField("fTree");
            treeField.setAccessible(true);
            Tree treeWidget = (Tree)(treeField.get(part));
            if(!treeWidget.isDisposed()) {
                TreeItem[] treeItems = treeWidget.getItems();
                if((treeItems != null) && (treeItems.length > 0)) {
                    treeWidget.setTopItem(treeItems[0]);
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        part.getSite().getSelectionProvider().setSelection(null);
    }

    public void selectionChanged(IAction action, ISelection selection) {
    }

    public void init(IViewPart view) {
        part = view;
    }

}