]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/extension/internal/ContributorExtensionManager.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / extension / internal / ContributorExtensionManager.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.common.extension.internal;
13
14 import java.util.Arrays;
15 import java.util.HashSet;
16 import java.util.Set;
17
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.browsing.ui.content.Contributor;
30 import org.simantics.utils.strings.StringUtils;
31
32 /**
33  * @author Tuukka Lehtonen
34  */
35 public abstract class ContributorExtensionManager<Factory> implements IExtensionChangeHandler {
36
37     private final static String                NAMESPACE    = Activator.PLUGIN_ID;
38
39     private ExtensionTracker                   tracker;
40
41     @SuppressWarnings("unchecked")
42     ContributorExtension<Factory>[]            extensions   = new ContributorExtension[0];
43
44     abstract public String getExtensionPointName();
45
46     ContributorExtensionManager() {
47         tracker = new ExtensionTracker();
48
49         // Cache defined actions
50         IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, getExtensionPointName());
51         loadExtensions(expt.getConfigurationElements());
52
53         // Start tracking for new and removed extensions
54         IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);
55         tracker.registerHandler(this, filter);
56     }
57
58     @SuppressWarnings("unchecked")
59     void close() {
60         tracker.close();
61         tracker = null;
62         extensions = new ContributorExtension[0];
63     }
64
65     public ContributorExtension<Factory>[] getExtensions() {
66         return extensions;
67     }
68
69     @SuppressWarnings("unchecked")
70     private void loadExtensions(IConfigurationElement[] elements) {
71
72         Set<ContributorExtension<Factory>> newExtensions = new HashSet<ContributorExtension<Factory>>(Arrays.asList(extensions));
73
74         for (IConfigurationElement el : elements) {
75             try {
76
77                 String id = StringUtils.safeString(el.getAttribute("id"));
78                 Contributor<Factory> factory = (Contributor<Factory>)el.createExecutableExtension("class");
79                 ContributorExtension<Factory> ext = new ContributorExtensionImpl<Factory>(id, factory);
80
81                 // Start tracking the new extension object, its removal will be notified of
82                 // with removeExtension(extension, Object[]).
83                 tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);
84
85                 newExtensions.add(ext);
86             } catch (CoreException e) {
87                 ErrorLogger.defaultLogError("Failed to initialize " + getExtensionPointName() + " extension \"" + el.getName()
88                         + "\" with name \"" + el.getAttribute("name") + "\": "
89                         + e.getMessage(), e);
90             }
91         }
92
93         // Atomic assignment
94         this.extensions = newExtensions.toArray(new ContributorExtension[newExtensions.size()]);
95     }
96
97     @Override
98     public void addExtension(IExtensionTracker tracker, IExtension extension) {
99         loadExtensions(extension.getConfigurationElements());
100     }
101
102     @SuppressWarnings("unchecked")
103     @Override
104     public void removeExtension(IExtension extension, Object[] objects) {
105         Set<ContributorExtension<Factory>> newExtensions = new HashSet<ContributorExtension<Factory>>(Arrays.asList(extensions));
106
107         for (Object o : objects) {
108             tracker.unregisterObject(extension, o);
109             newExtensions.remove(o);
110         }
111
112         // Atomic assignment
113         this.extensions = newExtensions.toArray(new ContributorExtension[newExtensions.size()]);
114     }
115
116 }