Passing parameters while showing a view in Eclipse RCP

依然范特西╮ 提交于 2019-12-11 10:29:29

问题


I am creating eclipse RCP application and I am stuck with passing parameter while showing a view. As the first approach, I tried setting a static variable in View2 from View1 and opened that view (as below). It works.

IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();               
String viewIdToClose = studentListView.ID;
IViewPart viewToClose = activePage.findView(viewIdToClose);
TableItem item = studentTable.getItem(studentTable.getSelectionIndex());
String text = item.getText(ListViewConstants.TABLE_COLUMN_ONE);
activePage.hideView(viewToClose);
try {
    String viewIdToOpen = StudentReview.ID;
    StudentReview.userId = text;
     activePage.showView(viewIdToOpen);                 
} catch (PartInitException e) {...}

As this doesn't seem to be a good approach, I tried as per the suggestion mentioned in the below link(accepted answer). Passing parameters to the view . In this approach, we can pass parameters only after showing the view.

But the issue I have is, the view to be opened should have the value from selected row while calling showView() i.e, I want to populate the parameters in View 2 based on View 1's selection. Is there any way to achieve this? Is it good to use PULL instead of PUSH approach? Any suggestion is appreciated. Thanks!!!

UPDATE 1: Approach mentioned in Passing parameters to the view Interface:

public interface ICommunicationView extends IViewPart{
   public void accept(Object parameter);
}

Calling accept():

IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
String viewIdToClose = StudentListView.ID;
IViewPart viewToClose = activePage.findView(viewIdToClose);
TableItem item = studentTable.getItem(studentTable.getSelectionIndex());
String text = item.getText(ListViewConstants.TABLE_COLUMN_ONE);
activePage.hideView(viewToClose);
try {
   String viewIdToOpen = StudentReview.ID;
   ICommunicationView viewToOpen = (ICommunicationView) activePage.showView(viewIdToOpen);
   viewToOpen.accept(text);//Sending userId to Review view
} catch (PartInitException e) { ... }

StudentReviewView:

private String studentId;
//getters and setters
@Override
public void accept(Object parameter) {
  setStudentId(parameter.toString());
}

public void createPartControl(final Composite parent) {
   ...
  System.out.println("Student Id" + this.getStudentId());    
}

It prints Student Id as null.

Am I missing something obvious?

Thanks!


回答1:


Platform calls createPartControl when the view is opened by activePage.showView(viewIdToOpen). So don't populate your fields on createPartControl. Do it when setStudentId is called.




回答2:


Here is the good links try this..

http://tomsondev.bestsolution.at/2011/02/07/enhanced-rcp-how-views-can-communicate-the-e4-way/




回答3:


I also encountered this problem. Found a way around it by repacking the specific view component that needs to be updated. Here is a proof of concept that sets a string:

Setter interface:

public interface ViewSetter {    
    void setMessage(String message); 
}

View that gets initialized:

//view that implements ViewSetter  

@Override
public void createPartControl(Composite parent) {       
    label = new Label(parent, SWT.NONE); //label is a field
}

@Override
public void setMessage(final String message) {
    Display.getDefault().syncExec(new Runnable() {  
        @Override
        public void run() {
            label.setText(message);
            label.pack(true);
        }
    });
}

Code that creates and initializes the view:

IViewPart view = page.showView(viewID);
ViewSetter viewSetter = (ViewSetter)view;
viewSetter.setMessage("Hello World");


来源:https://stackoverflow.com/questions/17206509/passing-parameters-while-showing-a-view-in-eclipse-rcp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!