1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.selectionview;
14 import java.util.Arrays;
15 import java.util.HashSet;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.IExtension;
21 import org.eclipse.core.runtime.IExtensionPoint;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
24 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
25 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
26 import org.eclipse.core.runtime.dynamichelpers.IFilter;
27 import org.simantics.browsing.ui.common.Activator;
28 import org.simantics.browsing.ui.common.ErrorLogger;
29 import org.simantics.utils.strings.StringUtils;
32 * @author Tuukka Lehtonen
34 public class SelectionProcessorExtensionManager implements IExtensionChangeHandler {
36 private final static String NAMESPACE = Activator.PLUGIN_ID;
38 private final static String EP_NAME = "selectionProcessor";
40 private ExtensionTracker tracker;
42 SelectionProcessorExtension[] extensions = new SelectionProcessorExtension[0];
44 SelectionProcessorExtensionManager() {
45 tracker = new ExtensionTracker();
47 // Cache defined actions
48 IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);
49 loadExtensions(expt.getConfigurationElements());
51 // Start tracking for new and removed extensions
52 IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);
53 tracker.registerHandler(this, filter);
59 extensions = new SelectionProcessorExtension[0];
62 public SelectionProcessorExtension[] getExtensions() {
66 private void loadExtensions(IConfigurationElement[] elements) {
68 Set<SelectionProcessorExtension> newExtensions = new HashSet<SelectionProcessorExtension>(Arrays.asList(extensions));
70 for (IConfigurationElement el : elements) {
73 String id = StringUtils.safeString(el.getAttribute("id"));
74 SelectionProcessor<?, ?> factory = (SelectionProcessor<?, ?>) el.createExecutableExtension("class");
75 SelectionProcessorExtension ext = new SelectionProcessorExtensionImpl(id, factory);
77 // Start tracking the new extension object, its removal will be notified of
78 // with removeExtension(extension, Object[]).
79 tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);
81 newExtensions.add(ext);
82 } catch (CoreException e) {
83 ErrorLogger.defaultLogError("Failed to initialize " + EP_NAME + " extension \"" + el.getName()
84 + "\" with name \"" + el.getAttribute("name") + "\": "
90 this.extensions = newExtensions.toArray(new SelectionProcessorExtension[newExtensions.size()]);
94 public void addExtension(IExtensionTracker tracker, IExtension extension) {
95 loadExtensions(extension.getConfigurationElements());
99 public void removeExtension(IExtension extension, Object[] objects) {
100 Set<SelectionProcessorExtension> newExtensions = new HashSet<SelectionProcessorExtension>(Arrays.asList(extensions));
102 for (Object o : objects) {
103 tracker.unregisterObject(extension, o);
104 newExtensions.remove(o);
108 this.extensions = newExtensions.toArray(new SelectionProcessorExtension[newExtensions.size()]);
111 private static SelectionProcessorExtensionManager INSTANCE;
113 public static synchronized SelectionProcessorExtensionManager getInstance() {
114 if (INSTANCE == null)
115 INSTANCE = new SelectionProcessorExtensionManager();
120 public static synchronized void dispose() {
121 if (INSTANCE != null) {
127 public static SelectionProcessor<?, ?> getProcessor(String id) {
128 for (SelectionProcessorExtension ext : getInstance().extensions) {
129 if (ext.getProcessor().getClass().getCanonicalName().equals(id))
130 return ext.getProcessor();