我试图在日食轮廓视图中显示我的GEF编辑器中包含的图形的微缩视图,但在大纲中它显示“轮廓不可用”。这是我创建的ContentOutlinePage:
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.Viewport;
import org.eclipse.draw2d.parts.ScrollableThumbnail;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.editparts.ScalableRootEditPart;
import org.eclipse.gef.ui.parts.ContentOutlinePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
public class OverviewContentPage extends ContentOutlinePage {
private ScrollableThumbnail thumbnail;
private DisposeListener disposeListener;
private GraphicalViewer viewer;
public OverviewContentPage(GraphicalViewer viewer) {
super(viewer);
this.viewer = viewer;
}
@Override
public void createControl(Composite parent) {
Canvas canvas = new Canvas(new SashForm(parent, SWT.VERTICAL), SWT.BORDER);
LightweightSystem lws = new LightweightSystem(canvas);
thumbnail = new ScrollableThumbnail(
(Viewport) ((ScalableRootEditPart) viewer.getRootEditPart()).getFigure());
thumbnail.setSource(((ScalableRootEditPart) viewer.getRootEditPart()).getLayer(LayerConstants.PRINTABLE_LAYERS));
lws.setContents(thumbnail);
disposeListener = new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
if (thumbnail != null) {
thumbnail.deactivate();
thumbnail = null;
}
}
};
viewer.getControl().addDisposeListener(disposeListener);
}
@Override
public void dispose() {
if(viewer != null) {
if (viewer.getControl() != null && !viewer.getControl().isDisposed())
viewer.getControl().removeDisposeListener(disposeListener);
super.dispose();
}
}
}
在我的GEF编辑器中,我添加了以下几行:
@Override
public Object getAdapter(Class type) {
...
if(type == IContentOutlinePage.class) {
return new OverviewContentPage(getGraphicalViewer());
}
return super.getAdapter(type);
}
我错过了什么吗?
答案 0 :(得分:1)
重写方法getControl()解决了问题:
private SashForm sash;
...
@Override
public Control getControl() {
return sash;
}
其中sash是在createControl()中传递给LightweightSystem的SashForm。
答案 1 :(得分:0)
基于user2035039的解决方案,我创建了GraphicalOutlineEditPartViewer
,以便可以省略自己的OverviewContentPage
实现。
public class GraphicalOutlineEditPartViewer extends AbstractEditPartViewer {
public GraphicalOutlineEditPartViewer(GraphicalViewer graphicalViewer) {
setRootEditPart(graphicalViewer.getRootEditPart());
}
@Override
public Control createControl(Composite parent) {
SashForm sashForm = new SashForm(parent, SWT.VERTICAL);
Canvas canvas = new Canvas(sashForm, SWT.BORDER);
RootEditPart rootEditPart = getRootEditPart();
if (rootEditPart instanceof GraphicalEditPart) {
GraphicalEditPart graphicalEditPart = (GraphicalEditPart) rootEditPart;
IFigure figure = graphicalEditPart.getFigure();
if (figure instanceof Viewport) {
Viewport viewport = (Viewport) figure;
ScrollableThumbnail thumbnail = new ScrollableThumbnail(viewport);
DisposeListener disposeListener = new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
if (thumbnail != null) {
thumbnail.deactivate();
}
sashForm.removeDisposeListener(this);
}
};
sashForm.addDisposeListener(disposeListener);
LightweightSystem lws = new LightweightSystem(canvas);
lws.setContents(thumbnail);
if (graphicalEditPart instanceof LayerManager) {
LayerManager layerManager = (LayerManager) graphicalEditPart;
IFigure printableLayer = layerManager.getLayer(LayerConstants.PRINTABLE_LAYERS);
thumbnail.setSource(printableLayer);
}
}
}
return sashForm;
}
@Override
public EditPart findObjectAtExcluding(Point location, Collection exclusionSet, Conditional conditional) {
return null;
}
}
我仅使用由org.eclipse.gef.ui.parts.ContentOutlinePage
提供的大纲页面
@Override
public Object getAdapter(Class type) {
if (IContentOutlinePage.class.isAssignableFrom(type)) {
GraphicalViewer graphicalViewer = getGraphicalViewer();
GraphicalOutlineEditPartViewer viewer = new GraphicalOutlineEditPartViewer(graphicalViewer);
return new ContentOutlinePage(viewer);
}
return super.getAdapter(type);
}