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