X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.ui%2Fsrc%2Forg%2Fsimantics%2Fui%2Fworkbench%2FPerspectiveContextBindingManager.java;fp=bundles%2Forg.simantics.ui%2Fsrc%2Forg%2Fsimantics%2Fui%2Fworkbench%2FPerspectiveContextBindingManager.java;h=4da7b3688a202d230f5aab0da2bca33b05f65d7a;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/PerspectiveContextBindingManager.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/PerspectiveContextBindingManager.java new file mode 100644 index 000000000..4da7b3688 --- /dev/null +++ b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/PerspectiveContextBindingManager.java @@ -0,0 +1,111 @@ +/******************************************************************************* + * 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.ui.workbench; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IExtension; +import org.eclipse.core.runtime.IExtensionPoint; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker; +import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; +import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; +import org.eclipse.core.runtime.dynamichelpers.IFilter; +import org.simantics.ui.internal.Activator; +import org.simantics.utils.datastructures.MapList; + +public class PerspectiveContextBindingManager implements IExtensionChangeHandler { + + private final static String NAMESPACE = Activator.PLUGIN_ID; + + private final static String EP_NAME = "perspectiveContextBinding"; + + private ExtensionTracker tracker; + + private MapList extensions = new MapList(); + + private static PerspectiveContextBindingManager INSTANCE; + + public static synchronized PerspectiveContextBindingManager getInstance() { + if (INSTANCE == null) + INSTANCE = new PerspectiveContextBindingManager(); + return INSTANCE; + } + + public static synchronized void dispose() { + if (INSTANCE != null) { + INSTANCE.close(); + INSTANCE = null; + } + } + + private PerspectiveContextBindingManager() { + tracker = new ExtensionTracker(); + + // Cache defined actions + IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME); + loadExtensions(expt.getConfigurationElements()); + + // Start tracking for new and removed extensions + IFilter filter = ExtensionTracker.createExtensionPointFilter(expt); + tracker.registerHandler(this, filter); + } + + private void close() { + tracker.close(); + tracker = null; + extensions = new MapList(); + } + + public synchronized List getExtensions(String perspectiveId) { + List exts = extensions.getValues(perspectiveId); + if (exts == null) + return Arrays.asList(); + return Collections.unmodifiableList(exts); + } + + private synchronized void loadExtensions(IConfigurationElement[] elements) { + for (IConfigurationElement el : elements) { + String perspectiveId = el.getAttribute("perspectiveId"); + String contextId = el.getAttribute("contextIds"); + String[] contextIds = contextId.split(","); + + IPerspectiveContextExtension ext = new IPerspectiveContextExtension.Stub(perspectiveId, contextIds); + + // Start tracking the new extension object, its removal will be notified of + // with removeExtension(extension, Object[]). + tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG); + + extensions.add(perspectiveId, ext); + } + } + + @Override + public void addExtension(IExtensionTracker tracker, IExtension extension) { + loadExtensions(extension.getConfigurationElements()); + } + + @Override + public synchronized void removeExtension(IExtension extension, Object[] objects) { + + for (Object o : objects) { + IPerspectiveContextExtension ext = (IPerspectiveContextExtension) o; + tracker.unregisterObject(extension, ext); + extensions.remove(ext.getPerspectiveId(), ext); + } + + } + +}