简体   繁体   中英

Update/Refresh a view in an eclipse plug-in/RCP?

i am trying to build an eclipse plugin. There are 2 following components of the plugin. 1) File Browser. 2) A Chart

The goal and job already done: After browsing and selecting a file with the file browser, a chart will be made based on the selected file. Both File Browser and Chart are under same ViewPart under same parent. So i have set a action listener in File Browser. So whenever a selection action takes place i handle the event in this view class. I get the file-path data using command parameter. Now i want to update the chart view. I think i am almost there but due to some lack of understanding i am stuck. I have already tried the following link How to update/refresh a view in an eclipse plug-in? but it does not work for me. Can you please help. Below is the code from ViewPart for your convenience.

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) {

}

I am getting NullPointerException in frame.dispose(); when the event is taking place. Please be informed i check the handler and it is working poerfectly. But not able to update the chart only. Thanks for your time and help.

It looks like you are trying to put a handler in the same class as a view part - this won't work. Separate instances of the class will be created for the handler and the view. In the view createPartControl will be run and frame will be non-null. In the handler createPartControl is not run so frame will be null.

Use a separate class for the handler (extending AbstractHandler ). In the handler find the view and call a method on the view to do the update. Something like:

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
  String filePath = event.getParameter("press.command.parameter");

  IWorkbenchPage page = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();

  IViewPart view = page.findView("your.view.id");

  if (view instanceof MyViewClass) {
     MyViewClass myView = (MyViewClass)view;

     myView.updateView(filePath); 
  }

where MyViewClass is your ViewPart class and updateView is a method you define to update the view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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