]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.editors.win32/src/org/simantics/editors/win32/EditorDefinitionManager.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.editors.win32 / src / org / simantics / editors / win32 / EditorDefinitionManager.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.editors.win32;
13
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collections;
17 import java.util.List;
18
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.editors.win32.ole.EditorDefinition;
28 import org.simantics.utils.ui.ErrorLogger;
29
30 public class EditorDefinitionManager implements IExtensionChangeHandler {
31
32     private final static String                           NAMESPACE  = Activator.PLUGIN_ID;
33
34     private final static String                           EP_NAME    = "editorDefinition";
35
36     private ExtensionTracker                              tracker;
37
38     private List<IEditorDefinitionExtension> extensions = new ArrayList<IEditorDefinitionExtension>();
39
40     private static EditorDefinitionManager       INSTANCE;
41
42     public static synchronized EditorDefinitionManager getInstance() {
43         if (INSTANCE == null)
44             INSTANCE = new EditorDefinitionManager();
45         return INSTANCE;
46     }
47
48     public static synchronized void dispose() {
49         if (INSTANCE != null) {
50             INSTANCE.close();
51             INSTANCE = null;
52         }
53     }
54
55     private EditorDefinitionManager() {
56         tracker = new ExtensionTracker();
57
58         // Cache defined actions
59         IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);
60         loadExtensions(expt.getConfigurationElements());
61
62         // Start tracking for new and removed extensions
63         IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);
64         tracker.registerHandler(this, filter);
65     }
66
67     private void close() {
68         tracker.close();
69         tracker = null;
70         extensions = new ArrayList<IEditorDefinitionExtension>();
71     }
72
73     public synchronized List<IEditorDefinitionExtension> getExtensions(String perspectiveId) {
74         if (extensions == null)
75             return Arrays.asList();
76         return Collections.unmodifiableList(extensions);
77     }
78
79     private synchronized void loadExtensions(IConfigurationElement[] elements) {
80         for (IConfigurationElement el : elements) {
81                 try {
82                         EditorDefinition fac = (EditorDefinition)el.createExecutableExtension("factory");
83             
84                         IEditorDefinitionExtension ext = new IEditorDefinitionExtension.Stub(fac);
85
86                 // Start tracking the new extension object, its removal will be notified of
87                 // with removeExtension(extension, Object[]).
88                 tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);
89
90                 extensions.add(ext);
91                 } catch (Exception e) {
92                         ErrorLogger.defaultLogError(e);
93                 }
94         }
95     }
96
97
98     @Override
99     public void addExtension(IExtensionTracker tracker, IExtension extension) {
100         loadExtensions(extension.getConfigurationElements());
101     }
102
103     @Override
104     public synchronized void removeExtension(IExtension extension, Object[] objects) {
105         List<IEditorDefinitionExtension> newExtensions = new ArrayList<IEditorDefinitionExtension>();
106
107         for (Object o : objects) {
108             IEditorDefinitionExtension ext = (IEditorDefinitionExtension) o;
109             tracker.unregisterObject(extension, ext);
110             extensions.remove(ext);
111         }
112
113         // Atomic assignment
114         this.extensions = newExtensions;
115     }
116     
117     public EditorDefinition getFactoryForSuffix(String suffix) {
118         for (IEditorDefinitionExtension e : extensions) {
119                 if (e.getFactory().supportSuffix(suffix)) {
120                         return e.getFactory();
121                 }
122                 
123         }
124         return null;            
125     }
126
127 }