在GEF3.0中,支持了GraphicalEditorWithFlyoutPalette面板,它可以折叠,而且有右键菜单,通过重载,我们可以在它的右键菜单上增加我们自己的功能。
首先,需要重载Editor的createPaletteViewerProvider方法,返回一个我们自己的
PaletteViewerProvider对象,如下:
@Override
protected PaletteViewerProvider createPaletteViewerProvider() {
return new MenuPaletteProvider( getEditDomain());
}
然后,新建一个MenuPaletteProvider,继承自PaletteViewerProvider,这样我们可以在
configurePaletteViewer方法中添加我们自己的菜单就可以了,完整代码如下:
import org.eclipse.gef.EditDomain;
import org.eclipse.gef.ui.palette.PaletteViewer;
import org.eclipse.gef.ui.palette.PaletteViewerProvider;
import org.eclipse.gef.ui.palette.PaletteContextMenuProvider;
import org.eclipse.jface.action.IMenuManager;
import umldesigner.actions.AddNewPaletteObjectAction;
public class MenuPaletteProvider extends PaletteViewerProvider{
private BasicGraphicalEditor edit;
public MenuPaletteProvider(BasicGraphicalEditor editor, EditDomain graphicalViewerDomain)
{
super(graphicalViewerDomain);
this.edit = editor;
}
@Override
protected void configurePaletteViewer(PaletteViewer viewer) {
// TODO Auto-generated method stub
viewer.setContextMenu(new MenuPaletteContextMenuProvider(edit, viewer));
}
}
class MenuPaletteContextMenuProvider extends PaletteContextMenuProvider
{
private AddNewPaletteObjectAction anpoa;
private BasicGraphicalEditor edit;
public MenuPaletteContextMenuProvider(BasicGraphicalEditor editor, PaletteViewer palette) {
super(palette);
this.edit = editor;
// TODO Auto-generated constructor stub
}
@Override
public void buildContextMenu(IMenuManager menu) {
// TODO Auto-generated method stub
super.buildContextMenu(menu);
anpoa = new AddNewPaletteObjectAction(edit);
//menu.add(anpoa);
}
}