0

I have several views in my RCP project.

In one view I have a TableViewer.

In another view I have a JFrame with a Button.

I want to update the data in the TableViewer using the setInput() method when I press the button in another view.

How do I do this?

EDIT:

Initial input to the table viewer:

tableViewer.setContentProvider(ArrayContentProvider.getInstance());
tableViewer.setLabelProvider(new TableLabelProvider() );
tableViewer.setInput(TraceData.getTraceData());     // get realtime data

I add the listener to the tableViewer to listen to changes in the GUI

listener = new ISelectionListener() {
    public void selectionChanged(IWorkbenchPart part, ISelection sel) {
       if (!(sel instanceof IStructuredSelection))
          return;
       IStructuredSelection ss = (IStructuredSelection) sel;
       Object o = ss.getFirstElement();
       if (o instanceof Book) // something else in place of Book
          tableViewer.setInput(TraceData.getSavedTraceData());
    }
 };

getSite().getPage().addSelectionListener(listener);

And the problem is how to make it react to a button event in another view? That is how to brodcast the JButton press event and then listen to that event in this TreeViewer.

flavio.donze
  • 6,077
  • 9
  • 41
  • 69
2c00L
  • 487
  • 9
  • 26

1 Answers1

1

In my RCP application, I have a View class which is extended by all others. In this View I have :

abstract void refresh();

Now, you have to use the refresh method of the view with the TableViewer.

@Override
public void refresh() {
    tableViewer.setInput(...);
    tableViewer.refresh();
}

And you have to call the refresh method from the button

How to access a view from anotherone.

final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
final IWorkbenchPage page = window.getActivePage();
try {
    if (page.getActivePart() != null) {
    viewTitle = page.getActivePart().getTitle();
    IViewPart view = page.showView(MainView.ID)  //id de la view in plugin.xml
    page.hideView(page.findView(SitesView.ID));
    }
Xavier Bouclet
  • 861
  • 1
  • 9
  • 22
  • Not exactly what I meant. I edited my post to clarify things. – 2c00L Oct 27 '14 at 18:50
  • I would use View view = (View) page.findView(View.ID); to access the view and then the refresh method. Worked for me. But may it's not the best way to do so. – Xavier Bouclet Oct 27 '14 at 19:05
  • Thanks for the reply. I could not find the page.findView() method. And I also think listeners are the best way. – 2c00L Oct 29 '14 at 14:05
  • Ok just in case you need to access a view from its id. See my post I added some code. – Xavier Bouclet Oct 30 '14 at 20:45
  • Turns out that the [IEventBroker](http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fe4%2Fcore%2Fservices%2Fevents%2FIEventBroker.html) is the best way to communicate with views, then I used setInput and refresh as you had mentioned, so +1 :) – 2c00L Nov 12 '14 at 15:19
  • Thx I am gonna look for the IEventBroker. – Xavier Bouclet Nov 14 '14 at 16:02
  • You can check this [Link](http://codeandme.blogspot.de/2013/08/using-event-broker-without-di.html) – 2c00L Nov 14 '14 at 16:26