Eclipse Multipage Page编辑器同步实践——PropertyPage

在MultipageEditor应用中属性页(PropertyPage)的同步和OutlinePage的同步比较相似,也是在MultipageEditor的getAdaptor方法返回一个外壳PropertySheetPage,在pageChange的时候切换不同的PropertyPage,源代码:

public class KULMultiPagePropertySheetPage extends PropertySheetPage implements
		IPropertySheetPage {
	// Current active property page
	private IPropertySheetPage activePage;
	// instance of KULEditor PropertyPage;
	private IPropertySheetPage textPropertyPage;
	// instance of KULDiagramEditor PropertyPage
	private IPropertySheetPage diagramPropertyPage;
	// The Multipage property page control
	private Composite control;
	// The control contains KULEditor property page
	private Composite textControl;
	// The control contains KULDiagramEditor property page
	private Composite diagramControl;
	// The active editor flag, diagram editor 0, text editor 1
	private int activeEditor = -1;
	/**
	 * Set the active editor.
	 * This property page shows the property which is provided by given editor.
	 * 
	 * @param editor the active editor
	 */
	public void setActiveEditor(IEditorPart editor){
		
		createPropertyPageForEditor(editor);
	
		if(control!=null){
			if(activePage!=null){
				initActivePage();
				if(this.activeEditor == 0) {
					this.createDiagramControl(control);
					if(this.diagramControl != null)
						((StackLayout)control.getLayout()).topControl = this.diagramControl;
				}
				else if(this.activeEditor == 1) {
					this.createTextControl(control);
					if(this.textControl != null)
						((StackLayout)control.getLayout()).topControl = this.textControl;
				}
				activePage.setActionBars(getSite().getActionBars());
				getSite().getActionBars().updateActionBars();
				control.layout();
			}
		}
	}
	/**
	 * Create the property page and init related attribute
	 * @param editor
	 */
	private void createPropertyPageForEditor(IEditorPart editor) {
		if(editor instanceof KULDiagramEditor) {
			createDiagramPropertyPage(editor);
			activePage = diagramPropertyPage;
			this.activeEditor = 0;
		} 
		else if(editor instanceof KULEditor) {
			createTextPropertyPage(editor);
			activePage = textPropertyPage;
			this.activeEditor = 1;
		}
	}

	private void createTextPropertyPage(IEditorPart editor) {
		if(textPropertyPage == null) {
			textPropertyPage = (IPropertySheetPage)editor.getAdapter(IPropertySheetPage.class);
		}
	}

	private void createDiagramPropertyPage(IEditorPart editor) {
		if(diagramPropertyPage == null) {
			diagramPropertyPage = (IPropertySheetPage)editor.getAdapter(IPropertySheetPage.class);
		}
	}
	
	public void createControl(Composite parent) {
		control = new Composite(parent, SWT.NULL);
		control.setLayout(new StackLayout());
		// if the active page is not null, init the property control	
		if(activePage == null){
			IEditorPart editor = null;
			try {
				editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
			} catch(Exception ex) {
				System.err.println("Error when fetch the active ecitor");
			}
			if(editor instanceof KULMultiPageEditor) {
				// create the property page
				createPropertyPageForEditor(((KULMultiPageEditor)editor).getKulActiveEditor());
			}
			
		} 
		if(activePage != null) {
			initActivePage();
			if(this.activeEditor == 0) {
				createDiagramControl(control);
				if(this.diagramControl != null)
					((StackLayout)control.getLayout()).topControl = this.diagramControl;
			} else if(this.activeEditor == 1) {
				createTextControl(control);
				if(this.textControl != null)
					((StackLayout)control.getLayout()).topControl = this.textControl;
			}
			control.layout();
		}
		
	}
	private void createDiagramControl(Composite parent) {
		if(this.activeEditor == 0 && this.diagramPropertyPage != null && (this.diagramControl==null || this.diagramControl.isDisposed())) {
			diagramControl = new Composite(parent, SWT.NULL);
			diagramControl.setLayout(new FillLayout());
			diagramPropertyPage.createControl(diagramControl);
		}
	}
	private void createTextControl(Composite parent) {
		if(this.activeEditor == 1 && this.textPropertyPage != null && (this.textControl==null || this.textControl.isDisposed())) {
			textControl = new Composite(parent, SWT.NULL);
			textControl.setLayout(new FillLayout());
			textPropertyPage.createControl(textControl);
		}
	}
	/**
	 * Initializes the active property page.
	 */
	private void initActivePage(){
		getSite().getActionBars().getToolBarManager().removeAll();
		getSite().getActionBars().getMenuManager().removeAll();
		
		if(activePage instanceof IPageBookViewPage){
			IPageBookViewPage pageBook = (IPageBookViewPage)activePage;
			if(pageBook.getSite()==null){
				try {
					pageBook.init(getSite());
				} catch(PartInitException ex){
				}
			}
		}
	}
	
	@Override
	public Control getControl() {
		if(control != null)
			return control;
		return super.getControl();
	}

	@Override
	public void setFocus() {
		if(activePage!=null /*&& !((Page) activePage).getControl().isDisposed()*/){
			activePage.setFocus();
		}
	}

	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		if(activePage!=null /*&& !((Page) activePage).getControl().isDisposed()*/){
			activePage.selectionChanged(part, selection);
		}

	}

	@Override
	public void dispose() {
		if(this.diagramPropertyPage != null) {
			this.diagramPropertyPage.dispose();
			if(this.diagramControl != null && !this.diagramControl.isDisposed()) {
				this.diagramControl.dispose();
			}
		}
		this.diagramPropertyPage = null;
		this.diagramControl = null;
		
		if(this.textPropertyPage != null) {
			this.textPropertyPage.dispose();
			if(this.textControl != null && !this.textControl.isDisposed()) {
				this.textControl.dispose();
			}
		}
		this.textControl = null;
		this.textPropertyPage = null;
		
		if(this.control != null && !this.control.isDisposed()) {
			this.control.dispose();
		}
		this.control = null;
		this.activePage = null;
		
		super.dispose();
		
	}

	@Override
	public void makeContributions(IMenuManager menuManager,
			IToolBarManager toolBarManager, IStatusLineManager statusLineManager) {
		if(activePage != null /*&& !((Page) activePage).getControl().isDisposed()*/) {
			((Page) activePage).makeContributions(menuManager, toolBarManager, statusLineManager);
		}
	}

	/*
	 * Override the method to make sure that the calling of makeContributions method of active page
	 * @see org.eclipse.ui.views.properties.PropertySheetPage#setActionBars(org.eclipse.ui.IActionBars)
	 */
	public void setActionBars(IActionBars actionBars) {
		if(activePage != null /*&& !((Page) activePage).getControl().isDisposed()*/) {
			((Page) activePage).setActionBars(actionBars);
		}
	}

	@Override
	public IPageSite getSite() {
		return super.getSite();
	}
	
}
 

不多说了,原理就是不同的Tab对应的PropertyPage编辑器可以用Stacklayout组织起来,当切换不同的tab时把对应的PropertyPage放到StackLayout的最上边就好了

((StackLayout)control.getLayout()).topControl=activeEditor
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值