Displaying result in a custom view
I am quite new to eclipse plug-in development and so please bear with me if the question is very trivial. I am in the process of developing an eclipse plugin.I have almost completed with it, except I am stuck in the last step. I have already created the custom view in plugin.xml file and I am also implementing an IAction function with this to perform the corresponding action for a selection. So, the logic behind using this is that after the user has performed the particular action, the result of the action should be displayed in the custom view if it is opened otherwise the view should be opened through the program and then the result should be displayed in it. The code which I have written is as below -
public class example extends ViewPart
{
private Text text;
@Override
public void createPartControl(Composite parent)
{
Text text = new Text(parent,SWT.NULL);
this.text = text;
}
@Override
public void setFocus()
{
}
@Override
@Override
public void printOnView(String str)
{
text.setText(str);
}
}
The problem is every time the program fails with NullPointerExeption at the first setText() statement in public void run() function. I have tried my best and I am not able to find the solution. If I give setText() statement in the same createPartControl() function it is displaying the text in the view when I open the view for the first but if I try it from outside it is not working. I am at my wits end here and let me just warn that my entire approach might be wrong since I am very new to plugin development. I would be happy if anyone can provide me with some help or guidance with this problem. Thanks in Advance!
Eclipse is creating two instances of your object - one for the View and one for the Action. For the View the createPartControl
will be called, and for the Action only the run
method will be called and since createPartControl has not run the Text field is null.
You should use two separate classes, one for the View and one for the Action.
In your Action class you need to find your View, with something like:
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IViewPart viewPart = page.findView("your view id");
MyViewClass myViewClass = (MyViewClass)viewPart;
// TODO call a method in MyViewClass to set the text
This is assuming the view is already open. If the view is not open you will have to open it.
链接地址: http://www.djcxy.com/p/18688.html下一篇: 在自定义视图中显示结果