当我在postStartup()方法中调用它时,getActiveWorkbenchWindow()返回null

时间:2013-07-06 03:54:55

标签: eclipse-gef

我是GEF的新手。 所以我试图在我的项目文件中跟踪示例。

但是当我试图开始我的例子时,我得到了一个N​​ullPointerException。

at kr.co.hoony.test.ApplicationWorkbenchAdvisor.postStartup(ApplicationWorkbenchAdvisor.java:37)

我的代码就像吼叫一样。

@Override
public void postStartup() {

            try {

                IWorkbenchPage wbPage = PlatformUI.getWorkbench()
                        .getActiveWorkbenchWindow()
                        .getActivePage();

                wbPage.openEditor(
                        new MyEditorInput("KSH"), 
                        MyGraphicalEditor.ID, 
                        false);

            } catch (Exception e) {
                e.printStackTrace();
            }


}

所以我尝试搜索,我找到了一个解决方案,我再次尝试如下。

@Override
public void postStartup() {

    Display.getDefault().asyncExec(new Runnable() {

        @Override
        public void run() {

            try {

                IWorkbenchPage wbPage = PlatformUI.getWorkbench()
                        .getActiveWorkbenchWindow()
                        .getActivePage();

                wbPage.openEditor(
                        new MyEditorInput("KSH"), 
                        MyGraphicalEditor.ID, 
                        false);

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });

}

然后,没有错误消息,但还有另一个问题。

我想在活动页面上显示一些标签,但它不起作用。

所以我想上传我写的所有代码

Block.java

package kr.co.hoony.test.example;

public class Block {

    private String name;
    private String width;

    public Block() {

        this.name = "Block";
        this.width = "20";

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getWidth() {
        return width;
    }

    public void setWidth(String width) {
        this.width = width;
    }

}

BlockEditPartFactory.java

package kr.co.hoony.test.example;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;

public class BlockEditPartFactory implements EditPartFactory {

    @Override
    public EditPart createEditPart(EditPart context, Object model) {

        AbstractGraphicalEditPart part = null;

        if(model instanceof Block){
            part = new BlockGrahpicalEditPart();
        }

        part.setModel(model);
        return part;
    }

}

BlockFigure.java

package kr.co.hoony.test.example;





import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Rectangle;

public class BlockFigure extends Figure {

    private Label labelName = new Label();
    private Label labelWidth = new Label();
    private XYLayout layout;

    public BlockFigure() {

        layout = new XYLayout();
        setLayoutManager(layout);

        labelName.setForegroundColor(ColorConstants.blue);
        add(labelName);
        setConstraint(labelName, new Rectangle(5, 5, -1, -1));

        labelWidth.setForegroundColor(ColorConstants.green);
        add(labelWidth);
        setConstraint(labelWidth, new Rectangle(5, 5, -1, -1)); 

        setForegroundColor(ColorConstants.black);
        setBorder(new LineBorder(5));
    }

    public void setLabelName(Label labelName) {
        this.labelName = labelName;
    }

    public void setLabelWidth(Label labelWidth) {
        this.labelWidth = labelWidth;
    }

    public void setLayout(XYLayout layout) {
        this.layout = layout;
    }

}

BlockGraphicalEditPart.java

package kr.co.hoony.test.example;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;

public class BlockGrahpicalEditPart extends AbstractGraphicalEditPart {

    @Override
    protected IFigure createFigure() {
        IFigure figure = new BlockFigure();
        return figure;
    }

    @Override
    protected void createEditPolicies() {
        // TODO Auto-generated method stub

    }

}

MyEditorInput.java

package kr.co.hoony.test.example;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPersistableElement;

public class MyEditorInput implements IEditorInput {

    public String name = null;

    public MyEditorInput(String name){
        this.name = name;
    }

    @Override
    public Object getAdapter(Class adapter) {
        return null;
    }

    @Override
    public boolean exists() {
        return (this.name != null);
    }

    @Override
    public boolean equals(Object o) {
        if(!(o instanceof MyEditorInput))
            return false;
        return ((MyEditorInput)o).getName().equals(this.getName());
    }

    @Override
    public ImageDescriptor getImageDescriptor() {
        return ImageDescriptor.getMissingImageDescriptor();
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public IPersistableElement getPersistable() {
        return null;
    }

    @Override
    public String getToolTipText() {
        return this.name;
    }

}

MyGraphicalEditor.java

package kr.co.hoony.test.example;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.ui.parts.GraphicalEditor;

public class MyGraphicalEditor extends GraphicalEditor {

    public static final String ID = "kr.co.hoony.test.example.MyGraphicalEditor";

    public MyGraphicalEditor() {
        setEditDomain(new DefaultEditDomain(this));
    }

    @Override
    protected void initializeGraphicalViewer() {
        // TODO Auto-generated method stub

    }

    @Override
    public void doSave(IProgressMonitor monitor) {
        // TODO Auto-generated method stub

    }

}

我不知道我制作的标签没有效果的原因。 有没有人告诉我我还没有抓到的一点?

1 个答案:

答案 0 :(得分:0)

您必须将EditPartFactory链接到您的编辑器类。

public void initializeGraphicalViewer() {
    GraphicalViewer viewer = getGraphicalViewer();
    //here you set the content of the viewer with your model
           viewer.setContents(getModel());
}

public void configureGraphicalViewer() {
    super.configureGraphicalViewer();
    GraphicalViewer viewer = getGraphicalViewer();
    //link the EditPartFactory to the editor
           viewer.setEditPartFactory(new BlockEditPartFactory());
}

您没有义务实施适当的IEditorInput和 我强烈要求你先阅读一些关于这个主题的tutorials

one对我来说是一个好的开始