X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.common%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fcommon%2Fextension%2Finternal%2FContributorExtensionManager.java;h=0d74c6000b126f86a047ef5d041fa2a15914cea8;hp=897bd67c2999afd23a8d0dc00359f3add7f68079;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/extension/internal/ContributorExtensionManager.java b/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/extension/internal/ContributorExtensionManager.java index 897bd67c2..0d74c6000 100644 --- a/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/extension/internal/ContributorExtensionManager.java +++ b/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/extension/internal/ContributorExtensionManager.java @@ -1,116 +1,116 @@ -/******************************************************************************* - * 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.browsing.ui.common.extension.internal; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -import org.eclipse.core.runtime.CoreException; -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.browsing.ui.common.Activator; -import org.simantics.browsing.ui.common.ErrorLogger; -import org.simantics.browsing.ui.content.Contributor; -import org.simantics.utils.strings.StringUtils; - -/** - * @author Tuukka Lehtonen - */ -public abstract class ContributorExtensionManager implements IExtensionChangeHandler { - - private final static String NAMESPACE = Activator.PLUGIN_ID; - - private ExtensionTracker tracker; - - @SuppressWarnings("unchecked") - ContributorExtension[] extensions = new ContributorExtension[0]; - - abstract public String getExtensionPointName(); - - ContributorExtensionManager() { - tracker = new ExtensionTracker(); - - // Cache defined actions - IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, getExtensionPointName()); - loadExtensions(expt.getConfigurationElements()); - - // Start tracking for new and removed extensions - IFilter filter = ExtensionTracker.createExtensionPointFilter(expt); - tracker.registerHandler(this, filter); - } - - @SuppressWarnings("unchecked") - void close() { - tracker.close(); - tracker = null; - extensions = new ContributorExtension[0]; - } - - public ContributorExtension[] getExtensions() { - return extensions; - } - - @SuppressWarnings("unchecked") - private void loadExtensions(IConfigurationElement[] elements) { - - Set> newExtensions = new HashSet>(Arrays.asList(extensions)); - - for (IConfigurationElement el : elements) { - try { - - String id = StringUtils.safeString(el.getAttribute("id")); - Contributor factory = (Contributor)el.createExecutableExtension("class"); - ContributorExtension ext = new ContributorExtensionImpl(id, factory); - - // Start tracking the new extension object, its removal will be notified of - // with removeExtension(extension, Object[]). - tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG); - - newExtensions.add(ext); - } catch (CoreException e) { - ErrorLogger.defaultLogError("Failed to initialize " + getExtensionPointName() + " extension \"" + el.getName() - + "\" with name \"" + el.getAttribute("name") + "\": " - + e.getMessage(), e); - } - } - - // Atomic assignment - this.extensions = newExtensions.toArray(new ContributorExtension[newExtensions.size()]); - } - - @Override - public void addExtension(IExtensionTracker tracker, IExtension extension) { - loadExtensions(extension.getConfigurationElements()); - } - - @SuppressWarnings("unchecked") - @Override - public void removeExtension(IExtension extension, Object[] objects) { - Set> newExtensions = new HashSet>(Arrays.asList(extensions)); - - for (Object o : objects) { - tracker.unregisterObject(extension, o); - newExtensions.remove(o); - } - - // Atomic assignment - this.extensions = newExtensions.toArray(new ContributorExtension[newExtensions.size()]); - } - -} +/******************************************************************************* + * 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.browsing.ui.common.extension.internal; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import org.eclipse.core.runtime.CoreException; +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.browsing.ui.common.Activator; +import org.simantics.browsing.ui.common.ErrorLogger; +import org.simantics.browsing.ui.content.Contributor; +import org.simantics.utils.strings.StringUtils; + +/** + * @author Tuukka Lehtonen + */ +public abstract class ContributorExtensionManager implements IExtensionChangeHandler { + + private final static String NAMESPACE = Activator.PLUGIN_ID; + + private ExtensionTracker tracker; + + @SuppressWarnings("unchecked") + ContributorExtension[] extensions = new ContributorExtension[0]; + + abstract public String getExtensionPointName(); + + ContributorExtensionManager() { + tracker = new ExtensionTracker(); + + // Cache defined actions + IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, getExtensionPointName()); + loadExtensions(expt.getConfigurationElements()); + + // Start tracking for new and removed extensions + IFilter filter = ExtensionTracker.createExtensionPointFilter(expt); + tracker.registerHandler(this, filter); + } + + @SuppressWarnings("unchecked") + void close() { + tracker.close(); + tracker = null; + extensions = new ContributorExtension[0]; + } + + public ContributorExtension[] getExtensions() { + return extensions; + } + + @SuppressWarnings("unchecked") + private void loadExtensions(IConfigurationElement[] elements) { + + Set> newExtensions = new HashSet>(Arrays.asList(extensions)); + + for (IConfigurationElement el : elements) { + try { + + String id = StringUtils.safeString(el.getAttribute("id")); + Contributor factory = (Contributor)el.createExecutableExtension("class"); + ContributorExtension ext = new ContributorExtensionImpl(id, factory); + + // Start tracking the new extension object, its removal will be notified of + // with removeExtension(extension, Object[]). + tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG); + + newExtensions.add(ext); + } catch (CoreException e) { + ErrorLogger.defaultLogError("Failed to initialize " + getExtensionPointName() + " extension \"" + el.getName() + + "\" with name \"" + el.getAttribute("name") + "\": " + + e.getMessage(), e); + } + } + + // Atomic assignment + this.extensions = newExtensions.toArray(new ContributorExtension[newExtensions.size()]); + } + + @Override + public void addExtension(IExtensionTracker tracker, IExtension extension) { + loadExtensions(extension.getConfigurationElements()); + } + + @SuppressWarnings("unchecked") + @Override + public void removeExtension(IExtension extension, Object[] objects) { + Set> newExtensions = new HashSet>(Arrays.asList(extensions)); + + for (Object o : objects) { + tracker.unregisterObject(extension, o); + newExtensions.remove(o); + } + + // Atomic assignment + this.extensions = newExtensions.toArray(new ContributorExtension[newExtensions.size()]); + } + +}