Gef中MultiPageEditorPart中属性页和Outline的使用
程序员文章站
2022-06-01 18:57:46
...
当使用Eclipse的多页签Editor时,如果按照单个Editor的Property页和Outline的设计,会出现两个Editor使用同一个CommandStack和多页签的Outline不能切换的问题,经过试验以两个页签为例使用下面的解决方案:建立在于MultPageEditorPart级别的IPropertySheetPage和IContentOutlinePage然后通过获取MultiPageEditorPart的pageChange(int index)方法来动态的设置相应的Property和Outline。具体是先步骤:
1.建立MultiPageEditorPart的子类FormEditor,主要实现CreatePages方法,在这个方法里边具体是的实现相应两个子Editor的初始化工作,并且使用阿addPage(Editor,EditorInput)方法把它们加入到MultiPageEditorPart中
2.实现相应的getAdapter(Class)方法,当传入的Class是IPropertySheetPage和IContentOutlinePage的时候,初始化相应的MultiPageEditorPart级别的PropertySheetPage和ContentOutlinePage,这两个类的实现代码如下:
OutlinePage:
在MultiPageEditorPart的 protected void pageChange(int newPageIndex);方法中进行更新当前的激活Editor的PropertySheetPage和OutlinePage,方法如下:
更新PropertySheet:
更新OutlinePage:
具体的子Editor中的获得PropertySheet和OutlinePage的方法可以参照Gef例子中的编写,在子Editor中初始化Outline的时候由于同一使用MultiPageEditorPart中的OutlinePage的Action,所以在子Editor的OutlinePage中不能去设定Action按钮,具体的参照FormContentOutline的Action的初始化,这点是与gef例子中给的不同的!
1.建立MultiPageEditorPart的子类FormEditor,主要实现CreatePages方法,在这个方法里边具体是的实现相应两个子Editor的初始化工作,并且使用阿addPage(Editor,EditorInput)方法把它们加入到MultiPageEditorPart中
2.实现相应的getAdapter(Class)方法,当传入的Class是IPropertySheetPage和IContentOutlinePage的时候,初始化相应的MultiPageEditorPart级别的PropertySheetPage和ContentOutlinePage,这两个类的实现代码如下:
import java.util.HashMap; import java.util.Map; import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.ui.IActionBars; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.SubActionBars; import org.eclipse.ui.part.PageBook; import org.eclipse.ui.views.properties.IPropertySheetPage; import org.eclipse.ui.views.properties.PropertySheetPage; /** * @author kang.ao * * */ public class FormPropertySheetPage extends PropertySheetPage implements IPropertySheetPage { private PageBook pagebook; private Map recMap = new HashMap(); private PropertySheetPage defaultPage; private IActionBars actionBars; private IPropertySheetPage currentPage; private boolean disposed = false; class PageRec { IPropertySheetPage page; SubActionBars bars; void setBarsActive(boolean active) { if (active) bars.activate(); else bars.deactivate(); } } public FormPropertySheetPage() { defaultPage = new PropertySheetPage(); } public void createControl(Composite parent) { pagebook = new PageBook(parent, SWT.NULL); defaultPage.createControl(pagebook); if (currentPage != null) setPageActive(currentPage); } private PageRec createPageRec(IPropertySheetPage page) { if (actionBars == null) return null; PageRec rec = new PageRec(); rec.page = page; rec.bars = new SubActionBars(actionBars); getPageControl(page); page.setActionBars(rec.bars); recMap.put(page, rec); return rec; } public void dispose() { updateActionBars(); if (pagebook != null && !pagebook.isDisposed()) pagebook.dispose(); pagebook = null; disposed = true; super.dispose(); } public boolean isDisposed() { return disposed; } public Control getControl() { return pagebook; } private Control getPageControl(IPropertySheetPage page) { Control control = page.getControl(); if (control == null || control.isDisposed()) { // first time page.createControl(pagebook); control = page.getControl(); } return control; } public void selectionChanged(IWorkbenchPart part, ISelection sel) { if (currentPage != null) currentPage.selectionChanged(part, sel); } public void setActionBars(IActionBars bars) { this.actionBars = bars; createPageRec(defaultPage); if (currentPage != null) { PageRec rec = createPageRec(currentPage); setPageActive(rec); updateActionBars(); } } public void setDefaultPageActive() { setPageActive(defaultPage); } public void setFocus() { if (currentPage != null) currentPage.setFocus(); } private void setPageActive(PageRec pageRec) { IPropertySheetPage page = pageRec.page; Control control = getPageControl(page); pagebook.showPage(control); pageRec.setBarsActive(true); } public void setPageActive(IPropertySheetPage page) { IPropertySheetPage oldPage = currentPage; this.currentPage = page; if (pagebook == null) { return; } if (oldPage != null) { PageRec oldRec = (PageRec) recMap.get(oldPage); if (oldRec != null) { oldRec.setBarsActive(false); } } PageRec rec = (PageRec) recMap.get(page); if (rec == null) { rec = createPageRec(page); } if (rec != null) { setPageActive(rec); updateActionBars(); } } private void updateActionBars() { actionBars.updateActionBars(); } }
OutlinePage:
/* * 创建日期 Oct 31, 2005 * */ package com.kenoah.kde.formdesignerX; import org.eclipse.gef.EditPartViewer; import org.eclipse.gef.ui.parts.ContentOutlinePage; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IAction; import org.eclipse.jface.action.IToolBarManager; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.ui.IActionBars; import org.eclipse.ui.part.IPageSite; import org.eclipse.ui.part.PageBook; import org.eclipse.ui.views.contentoutline.IContentOutlinePage; import com.kenoah.kde.formdesignerX.utils.FormdesignerImages; /** * @author kang.ao * * */ public class FormContentOutline extends ContentOutlinePage implements IContentOutlinePage { private PageBook pagebook; private IContentOutlinePage currentPage; private boolean disposed; private IAction showOutlineAction, showOverviewAction; public static final int ID_OUTLINE = 0; public static final int ID_OVERVIEW = 1; public FormContentOutline(EditPartViewer viewer) { super(viewer); } /** * @see org.eclipse.ui.part.IPage#createControl(Composite) */ public void createControl(Composite parent) { pagebook = new PageBook(parent, SWT.NONE); configureOutlineViewer(); if (currentPage != null) setPageActive(currentPage); showPage(ID_OUTLINE); } private void configureOutlineViewer() { IToolBarManager tbm = getSite().getActionBars().getToolBarManager(); showOutlineAction = new Action() { public void run() { showPage(ID_OUTLINE); } }; showOutlineAction.setImageDescriptor(FormdesignerImages.getImageDescriptor(FormdesignerImages.OUTLINE)); tbm.add(showOutlineAction); showOverviewAction = new Action() { public void run() { showPage(ID_OVERVIEW); } }; showOverviewAction.setImageDescriptor(FormdesignerImages .getImageDescriptor(FormdesignerImages.OVERVIEW)); tbm.add(showOverviewAction); } protected void showPage(int id_outline2) { if(currentPage instanceof IOutlinePageShowPage){ ((IOutlinePageShowPage)currentPage).showPage(id_outline2); if(id_outline2==ID_OVERVIEW){ showOutlineAction.setChecked(false); showOverviewAction.setChecked(true); }else{ showOutlineAction.setChecked(true); showOverviewAction.setChecked(false); } } } private int getCurrentPageId(){ if(showOverviewAction!=null && showOutlineAction!=null){ if(showOverviewAction.isChecked()) return ID_OVERVIEW; if(showOutlineAction.isChecked()){ return ID_OUTLINE; } } return ID_OUTLINE; } /** * @see org.eclipse.ui.part.IPage#dispose() */ public void dispose() { if (pagebook != null && !pagebook.isDisposed()) pagebook.dispose(); pagebook = null; disposed = true; } public boolean isDisposed() { return disposed; } /** * @see org.eclipse.ui.part.IPage#getControl() */ public Control getControl() { return pagebook; } /** * @see org.eclipse.ui.part.IPage#setActionBars(IActionBars) */ public void setActionBars(IActionBars actionBars) { } /** * @see org.eclipse.ui.part.IPage#setFocus() */ public void setFocus() { if (currentPage != null) currentPage.setFocus(); } /** * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(ISelectionChangedListener) */ public void addSelectionChangedListener(ISelectionChangedListener listener) { if (currentPage != null) currentPage.addSelectionChangedListener(listener); } /** * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection() */ public ISelection getSelection() { return (currentPage != null) ? currentPage.getSelection() : null; } /** * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(ISelectionChangedListener) */ public void removeSelectionChangedListener(ISelectionChangedListener listener) { if (currentPage != null) currentPage.removeSelectionChangedListener(listener); } /** * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(ISelection) */ public void setSelection(ISelection selection) { if (currentPage != null) currentPage.setSelection(selection); } public void setPageActive(IContentOutlinePage page) { this.currentPage = page; if (pagebook == null) { return; } if(getSite()!=null){ if(page instanceof ContentOutlinePage){ ((ContentOutlinePage)page).init(getSite()); } Control control = page.getControl(); if (control == null || control.isDisposed()) { page.createControl(pagebook); control = page.getControl(); } pagebook.showPage(control); showPage(getCurrentPageId()); } } public void init(IPageSite pageSite) { super.init(pageSite); } }
在MultiPageEditorPart的 protected void pageChange(int newPageIndex);方法中进行更新当前的激活Editor的PropertySheetPage和OutlinePage,方法如下:
更新PropertySheet:
void updatePropertySheet(IEditorPageBase part) { if (part != null) { IPropertySheetPage propertySheetPage = part.getPropertySheetPage(); if (propertySheetPage != null) { propertySheet.setPageActive(propertySheetPage); } else { propertySheet.setDefaultPageActive(); } } }
更新OutlinePage:
void updateOutline(IEditorPageBase part) { IContentOutlinePage page = null; if (part != null) { page = ((IEditorPageBase) part).getOutlinePage(); if (page != null && contentOutline!=null) contentOutline.setPageActive(page); } }
具体的子Editor中的获得PropertySheet和OutlinePage的方法可以参照Gef例子中的编写,在子Editor中初始化Outline的时候由于同一使用MultiPageEditorPart中的OutlinePage的Action,所以在子Editor的OutlinePage中不能去设定Action按钮,具体的参照FormContentOutline的Action的初始化,这点是与gef例子中给的不同的!