0

I am new to Eclipse RCP and I am working on an application (Eclipse 4), I have multiple parts within I display data from different sources. I would like to add a menu that displays a Dialog that offers the possibility to select dynamically the data sources that the user wants. When the options are selected I would like to re-instantiate the Part's class using the options as parameters and refresh the view. Is that possible ?

My Part's createComposite method :

    @PostConstruct
    public void createComposite(Composite parent) {
        Composite composite = new Composite(parent, SWT.EMBEDDED);
        parent_C = parent;
        Frame frame_1 = SWT_AWT.new_Frame(composite);
        JPanel mainPanel = new JPanel();
        BorderLayout layout = new BorderLayout();
        mainPanel.setLayout(layout);

          /* Layout Definition */
     }

I would like to add another parameter to the createComposite Method that indicates the options :

@PostConstruct
    public void createComposite(Composite parent, String[] options) {
    /*Code Here*/
}

The Value of the String array changes when the user validate the options from the Menu. When the users validate his options the part's class should be called with the new options.

Is there any way to do this ? Thank You

EL Kamel
  • 824
  • 2
  • 12
  • 27

2 Answers2

1

To do this you need to get the values in the IEclipseContext of the part being created. One way to do this is to subscribe to the UIEvents.Context.TOPIC_CONTEXT event and modify the new part's context in that event.

@Inject
IEventBroker eventBroker;


eventBroker.subscribe(UIEvents.Context.TOPIC_CONTEXT, this::handleContextEvent);


private void handleContextEvent(Event event)
{
  Object origin = event.getProperty(UIEvents.EventTags.ELEMENT);
  if (!(origin instanceof MPart))
    return;

  MPart part = (MPart)origin;

  // TODO check this is the MPart you want

  Object context = event.getProperty(UIEvents.EventTags.NEW_VALUE);
  if (!(context instanceof IEclipseContext))
    return;

  IEclipseContext newContext = (IEclipseContext)context;

  newContext.set("nameForOptions", .... options ....);
}

I have used a name for the options here so you would use @Named:

@PostConstruct
public void createComposite(Composite parent, @Named("nameForOptions") String[] options)
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Thank You for your response, I should add a Context handler event to each part and the question is how to call the part's class from an Menu Item handler ? how to instantiate it knowing that my part's class doesn't extend from any other class , the part were created from the wizard in the Application.e4xmi file and linked with a Java class as a Class URI – EL Kamel Mar 26 '16 at 19:48
  • No, you only use one context handler for the whole RCP. You set this up in whatever you are using to create your parts. – greg-449 Mar 26 '16 at 20:04
  • I created the parts from the Application.e4xmi file. it is not clear how to change the context and refresh the part's content when a menu item is clicked – EL Kamel Mar 26 '16 at 20:07
  • Well how are you (re)creating the part? Are you using EPartService? You have to close and reopen a part to get the @PostConstruct method called again. Alternatively you could just call some other method of your part. – greg-449 Mar 26 '16 at 20:16
  • I didn't use EPartService, but I will , I just want to know how it works because I 'am not Familiar with RCP and I am required to develop a feature that filters data basing on the user's choice. Thank You greg-449 for your help – EL Kamel Mar 26 '16 at 20:34
0

Instead of recreating the entire part again, it will be easier to refresh of re-create the content inside the part itself. That should be possible by either disposing the content of the part and recreate the content again under that container, or by refresh mechanism of any table/table viewer.

Loganathan
  • 903
  • 2
  • 10
  • 23