]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.selectionview/src/org/simantics/selectionview/SelectionProcessorExtensionManager.java
Revert "Default property editing restores assertions"
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / SelectionProcessorExtensionManager.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.selectionview;
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.utils.strings.StringUtils;
30
31 /**
32  * @author Tuukka Lehtonen
33  */
34 public class SelectionProcessorExtensionManager implements IExtensionChangeHandler {
35
36     private final static String   NAMESPACE  = Activator.PLUGIN_ID;
37
38     private final static String   EP_NAME    = "selectionProcessor";
39
40     private ExtensionTracker      tracker;
41
42     SelectionProcessorExtension[] extensions = new SelectionProcessorExtension[0];
43
44     SelectionProcessorExtensionManager() {
45         tracker = new ExtensionTracker();
46
47         // Cache defined actions
48         IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);
49         loadExtensions(expt.getConfigurationElements());
50
51         // Start tracking for new and removed extensions
52         IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);
53         tracker.registerHandler(this, filter);
54     }
55
56     void close() {
57         tracker.close();
58         tracker = null;
59         extensions = new SelectionProcessorExtension[0];
60     }
61
62     public SelectionProcessorExtension[] getExtensions() {
63         return extensions;
64     }
65
66     private void loadExtensions(IConfigurationElement[] elements) {
67
68         Set<SelectionProcessorExtension> newExtensions = new HashSet<SelectionProcessorExtension>(Arrays.asList(extensions));
69
70         for (IConfigurationElement el : elements) {
71             try {
72
73                 String id = StringUtils.safeString(el.getAttribute("id"));
74                 SelectionProcessor<?, ?> factory = (SelectionProcessor<?, ?>) el.createExecutableExtension("class");
75                 SelectionProcessorExtension ext = new SelectionProcessorExtensionImpl(id, factory);
76
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);
80
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") + "\": "
85                         + e.getMessage(), e);
86             }
87         }
88
89         // Atomic assignment
90         this.extensions = newExtensions.toArray(new SelectionProcessorExtension[newExtensions.size()]);
91     }
92
93     @Override
94     public void addExtension(IExtensionTracker tracker, IExtension extension) {
95         loadExtensions(extension.getConfigurationElements());
96     }
97
98     @Override
99     public void removeExtension(IExtension extension, Object[] objects) {
100         Set<SelectionProcessorExtension> newExtensions = new HashSet<SelectionProcessorExtension>(Arrays.asList(extensions));
101
102         for (Object o : objects) {
103             tracker.unregisterObject(extension, o);
104             newExtensions.remove(o);
105         }
106
107         // Atomic assignment
108         this.extensions = newExtensions.toArray(new SelectionProcessorExtension[newExtensions.size()]);
109     }
110
111     private static SelectionProcessorExtensionManager INSTANCE;
112
113     public static synchronized SelectionProcessorExtensionManager getInstance() {
114         if (INSTANCE == null)
115             INSTANCE = new SelectionProcessorExtensionManager();
116         return INSTANCE;
117     }
118
119
120     public static synchronized void dispose() {
121         if (INSTANCE != null) {
122             INSTANCE.close();
123             INSTANCE = null;
124         }
125     }
126
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();
131         }
132         return null;
133     }
134
135 }