在Eclipse插件/ RCP中更新/刷新视图?

时间:2018-02-24 11:32:05

标签: java eclipse eclipse-plugin eclipse-rcp

我正在尝试构建一个eclipse插件。该插件有以下2个组件。 1)文件浏览器。 2)图表

目标和工作已经完成:浏览并使用文件浏览器选择文件后,将根据所选文件制作图表。文件浏览器和图表都在同一父级下的同一ViewPart下。所以我在文件浏览器中设置了一个动作监听器。因此,无论何时发生选择操作,我都会在此视图类中处理事件。我使用命令参数获取文件路径数据。现在我想更新图表视图。我想我差不多了,但是由于缺乏理解,我被困住了。我已经尝试了以下链接How to update/refresh a view in an eclipse plug-in?,但它对我不起作用。你能帮忙吗?以下是ViewPart中的代码,以方便您使用。

public ChartComposite frame;
public FileChooser fileChooser;
public XYSeriesCollection dataset;
public FetchDataChart chart1 = new FetchDataChart();
public JFreeChart chart;
public Composite parent;

public void createPartControl(Composite _parent){
    parent = _parent;
    createBrowser();
}

public void createBrowser() {
    Composite top = new Composite(parent, SWT.NONE);// embedded Composite

    // setup the layout of top to be GridLayout.
    GridLayout layout1 = new GridLayout();
    layout1.marginHeight = 0;
    layout1.marginWidth = 0;
    top.setLayout(layout1);

    Composite banner = new Composite(top, SWT.NONE);// banner is added to
                                                    // "top"
    banner.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL,
            GridData.VERTICAL_ALIGN_BEGINNING, true, false));
    layout1 = new GridLayout();
    layout1.marginHeight = 5;
    layout1.marginWidth = 10;

    layout1.numColumns = 1;
    banner.setLayout(layout1);

    Font boldFont = JFaceResources.getFontRegistry().getBold(
            JFaceResources.DEFAULT_FONT);

    GridData gridData = new GridData();
    gridData.horizontalAlignment = SWT.FILL;
    gridData.minimumWidth = 400;
    gridData.minimumHeight = 50;
    gridData.grabExcessHorizontalSpace = true;

    Label l = new Label(banner, SWT.WRAP);
    l.setText("Source File:");
    l.setFont(boldFont);

    fileChooser = new FileChooser(banner);
    gridData.heightHint = 25;
    fileChooser.setLayoutData(gridData);

    createChart(""); // here i call the create chart method

}

public void createChart(String filePath) {
    chart1 = new FetchDataChart();
    dataset = chart1.createDataset(filePath);
    try {
        chart = chart1.createChart(dataset);
        frame = new ChartComposite(parent, SWT.NONE, chart, true);
        frame.pack();
    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
@Override
public void setFocus() {
}

@Override
public void addHandlerListener(IHandlerListener handlerListener) {

}

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    String filePath = event
            .getParameter("press.command.parameter");
    Display.getDefault().asyncExec(new Runnable() {
          public void run() {
            frame.dispose();
            createChart(filePath);
            parent.pack();
            parent.layout(true);
          }
        });

    return null;
}

@Override
public boolean isEnabled() {
    return true;
}

@Override
public boolean isHandled() {
    return true;
}

@Override
public void removeHandlerListener(IHandlerListener handlerListener) {

}

当事件发生时,我在 frame.dispose(); 中收到NullPointerException。请通知我检查处理程序,它正在完美地工作。但无法仅更新图表。感谢您的时间和帮助。

0 个答案:

没有答案