简体   繁体   English

在Eclipse RCP中显示视图时传递参数

[英]Passing parameters while showing a view in Eclipse RCP

I am creating eclipse RCP application and I am stuck with passing parameter while showing a view. 我正在创建eclipse RCP应用程序,我在显示视图时遇到了传递参数。 As the first approach, I tried setting a static variable in View2 from View1 and opened that view (as below). 作为第一种方法,我尝试在View1中从View1设置静态变量并打开该视图(如下所示)。 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() ie, I want to populate the parameters in View 2 based on View 1's selection. 但我遇到的问题是,要打开的视图应该在调用showView()时具有所选行的值,即,我想根据View 1的选择填充View 2中的参数。 Is there any way to achieve this? 有没有办法实现这个目标? Is it good to use PULL instead of PUSH approach? 使用PULL而不是PUSH方法是否合适? Any suggestion is appreciated. 任何建议表示赞赏。 Thanks!!! 谢谢!!!

UPDATE 1: Approach mentioned in Passing parameters to the view Interface: 更新1:将参数传递给视图接口中提到的方法:

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

Calling accept(): 调用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: 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. 它将Student Id打印为null。

Am I missing something obvious? 我错过了一些明显的东西吗

Thanks! 谢谢!

Platform calls createPartControl when the view is opened by activePage.showView(viewIdToOpen) . activePage.showView(viewIdToOpen)打开视图时,Platform调用createPartControl So don't populate your fields on createPartControl . 所以不要在createPartControl上填充你的字段。 Do it when setStudentId is called. 调用setStudentId时执行此setStudentId

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: Setter界面:

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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