2

我正在研究 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;
        }
    }
}

}

4

1 回答 1

2

你必须做两件事:

  1. 将目标放置目标侦听器添加到图形查看器:getGraphicalViewer().addDropTargetListener(new TemplateTransferDropTargetListener(getGraphicalViewer()));
  2. 向调色板添加一个拖动源侦听器侦听器: getEditDomain().getPaletteViewer().addDragSourceListener( new TemplateTransferDragSourceListener(getEditDomain().getPaletteViewer()));
  3. 将您的创作工具更改为CombinedTemplateCreationEntry

更详尽的解释可以在我为这个场合创建的博客文章中找到

于 2013-02-04T19:10:11.213 回答