X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.selectionview%2Fsrc%2Forg%2Fsimantics%2Fselectionview%2FAbstractPropertyPage.java;fp=bundles%2Forg.simantics.selectionview%2Fsrc%2Forg%2Fsimantics%2Fselectionview%2FAbstractPropertyPage.java;h=7d7c015986c4a683417444fb4ff3a76712b7c8ad;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.selectionview/src/org/simantics/selectionview/AbstractPropertyPage.java b/bundles/org.simantics.selectionview/src/org/simantics/selectionview/AbstractPropertyPage.java new file mode 100644 index 000000000..7d7c01598 --- /dev/null +++ b/bundles/org.simantics.selectionview/src/org/simantics/selectionview/AbstractPropertyPage.java @@ -0,0 +1,143 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.selectionview; + +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.ui.IPartListener; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.ui.IWorkbenchPartSite; +import org.eclipse.ui.part.Page; +import org.simantics.ui.workbench.IPropertyPage; + +/** + *

+ * Subclasses must implement the following methods: + *

+ *

+ *

+ * Subclasses may extend or reimplement the following methods as required: + *

+ *

+ * + * @author Tuukka Lehtonen + */ +public abstract class AbstractPropertyPage extends Page implements IPropertyPage { + + /** + * Part listener which cleans up this page when the source part is closed. + * This is hooked only when there is a source part. + */ + private class PartListener implements IPartListener { + public void partActivated(IWorkbenchPart part) { + } + + public void partBroughtToTop(IWorkbenchPart part) { + } + + public void partClosed(IWorkbenchPart part) { + if (sourcePart == part) { + sourcePart = null; + sourcePartClosed(part); + } + } + + public void partDeactivated(IWorkbenchPart part) { + } + + public void partOpened(IWorkbenchPart part) { + } + } + + private final PartListener partListener = new PartListener(); + + protected IWorkbenchPartSite sourceSite; + + protected IWorkbenchPart sourcePart; + + /** + * @param sourceSite the workbench part site that produces this page or + * null if there is no site, i.e. the page is within a + * dialog or a plain shell. + */ + public AbstractPropertyPage(IWorkbenchPartSite sourceSite) { + super(); + this.sourceSite = sourceSite; + } + + @Override + public void dispose() { + super.dispose(); + + if (sourcePart != null) { + sourcePart.getSite().getPage().removePartListener(partListener); + sourcePart = null; + } + + sourceSite = null; + + } + + @Override + public void selectionChanged(IWorkbenchPart part, ISelection selection) { + if (getControl() == null) { + return; + } + + if (sourcePart != null) { + sourcePart.getSite().getPage().removePartListener(partListener); + sourcePart = null; + } + + // change the viewer input since the workbench selection has changed. + sourcePart = part; + sourceSelectionChanged(selection); + + if (sourcePart != null) { + IWorkbenchPartSite site = sourcePart.getSite(); + if(site == null) { + new Exception("null site").printStackTrace(); + return; + } + IWorkbenchPage page = site.getPage(); + if(page == null) { + new Exception("null page").printStackTrace(); + return; + } + page.addPartListener(partListener); + } + } + + /** + * @param selection + */ + protected abstract void sourceSelectionChanged(ISelection selection); + + /** + * @param part + */ + protected abstract void sourcePartClosed(IWorkbenchPart part); + +}