如何将调色板中的数字拖放到GEF编辑器中?

时间:2013-02-04 12:33:02

标签: eclipse-rcp eclipse-emf eclipse-gef

我正在研究GEF项目。在那个项目中,我想将调色板中的数字拖放到GEF编辑器中。我尝试添加一些听众,但我仍然无法拖放。

这是我的代码:

public class OPMGraphicalEditor extends GraphicalEditorWithFlyoutPalette {

private Resource opdResource;
private ObjectProcessDiagram opd;

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


@Override
protected PaletteViewerProvider createPaletteViewerProvider() {
    return new PaletteViewerProvider(getEditDomain()) {
        @Override
        protected void configurePaletteViewer(PaletteViewer viewer) {
            super.configurePaletteViewer(viewer);
            viewer.addDragSourceListener(new TemplateTransferDragSourceListener(
                    viewer));
        }
    };
}


@SuppressWarnings("deprecation")
private TransferDropTargetListener createTransferDropTargetListener() {
    return new TemplateTransferDropTargetListener(getGraphicalViewer()) {
        @SuppressWarnings("unchecked")
        @Override
        protected CreationFactory getFactory(Object template) {
            return new NodeCreationFactory((Class<OPMObject>) template, getModel());
        }

        private Object getModel() {

            return null;
        }
    };
}


@Override protected void initializeGraphicalViewer() {
    GraphicalViewer viewer = getGraphicalViewer();
    super.initializeGraphicalViewer();
    getGraphicalViewer().setContents(opd);
    getPaletteViewerProvider().getEditDomain().getPaletteViewer().addDragSourceListener(
            new TemplateTransferDragSourceListener(getPaletteViewerProvider().getEditDomain().getPaletteViewer()));

    // listen for dropped parts
    viewer.addDropTargetListener(createTransferDropTargetListener());
}

@Override
protected void setGraphicalViewer(GraphicalViewer viewer) {
    super.setGraphicalViewer(viewer);
    this.getGraphicalViewer().addDropTargetListener(new TemplateTransferDropTargetListener(getGraphicalViewer()));

}

@Override protected void configureGraphicalViewer() {
    super.configureGraphicalViewer();
    getGraphicalViewer().setEditPartFactory(new OPMEditPartFactory());
}


@Override
protected PaletteRoot getPaletteRoot() {
    PaletteRoot palette = new PaletteRoot();
    Activator activator = Activator.getDefault();
    IWorkbench workbench = activator.getWorkbench();
    IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
    ISelectionService selectionService = workbenchWindow
            .getSelectionService();
    ISelection selection = selectionService.getSelection();
    return new OPMGraphicalEditorPalette();

}

@Override public void doSave(IProgressMonitor monitor) {
    if(opdResource == null) {
        return;
    }

    try {
        opdResource.save(null);
    } catch(IOException e) {

        e.printStackTrace();
        opdResource = null;
    }
}

@Override public void init(IEditorSite site, IEditorInput input) throws PartInitException {
    super.init(site, input);

    loadInput(input);
}

private void loadInput(IEditorInput input) {
    OpmPackage.eINSTANCE.eClass(); // This initializes the OPMPackage singleton implementation.
    ResourceSet resourceSet = new ResourceSetImpl();
    if(input instanceof IFileEditorInput) {
        IFileEditorInput fileInput = (IFileEditorInput) input;
        IFile file = fileInput.getFile();
        opdResource = resourceSet.createResource(URI.createURI(file.getLocationURI().toString()));
        try {
            opdResource.load(null);
            ObjectProcessDiagram opd = (ObjectProcessDiagram) opdResource.getContents().get(0);
        }
        catch(IOException e) {
            e.printStackTrace();
            opdResource = null;
        }
    }
}

}

1 个答案:

答案 0 :(得分:2)

你必须做两件事:

  1. 向图形查看器添加目标放置目标侦听器:getGraphicalViewer().addDropTargetListener(new TemplateTransferDropTargetListener(getGraphicalViewer()));
  2. 向调色板添加拖动源侦听器侦听器: getEditDomain().getPaletteViewer().addDragSourceListener( new TemplateTransferDragSourceListener(getEditDomain().getPaletteViewer()));
  3. 将您的创作工具更改为CombinedTemplateCreationEntry
  4. 可以在blog post I created for the occasion

    中找到更全面的解释