]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/DoubleClickExtensionManager.java
Combination of Simantics-platform related changes and fixes for district
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / DoubleClickExtensionManager.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.ui;
13
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collections;
17 import java.util.Comparator;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Set;
21
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.core.runtime.IExtension;
24 import org.eclipse.core.runtime.IExtensionPoint;
25 import org.eclipse.core.runtime.Platform;
26 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
27 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
28 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
29 import org.eclipse.core.runtime.dynamichelpers.IFilter;
30 import org.simantics.ui.internal.Activator;
31 import org.simantics.utils.strings.StringUtils;
32
33 /**
34  * @author Tuukka Lehtonen
35  */
36 public class DoubleClickExtensionManager implements IExtensionChangeHandler {
37
38     private final static String                NAMESPACE    = Activator.PLUGIN_ID;
39
40     private final static String                EP_NAME      = "doubleClick";
41
42     private ExtensionTracker                   tracker;
43
44     private IDoubleClickExtension[]            extensions   = new IDoubleClickExtension[0];
45
46     private static DoubleClickExtensionManager INSTANCE;
47
48
49     public static synchronized DoubleClickExtensionManager getInstance() {
50         if (INSTANCE == null)
51             INSTANCE = new DoubleClickExtensionManager();
52         return INSTANCE;
53     }
54
55     public static synchronized void dispose() {
56         if (INSTANCE != null) {
57             INSTANCE.close();
58             INSTANCE = null;
59         }
60     }
61
62
63     private DoubleClickExtensionManager() {
64         tracker = new ExtensionTracker();
65
66         // Cache defined actions
67         IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);
68         loadExtensions(expt.getConfigurationElements());
69
70         // Start tracking for new and removed extensions
71         IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);
72         tracker.registerHandler(this, filter);
73     }
74
75     private void close() {
76         tracker.close();
77         tracker = null;
78         extensions = new IDoubleClickExtension[0];
79     }
80
81     public IDoubleClickExtension[] getExtensions() {
82         return extensions;
83     }
84
85     private void loadExtensions(IConfigurationElement[] elements) {
86         Set<IDoubleClickExtension> newExtensions = new HashSet<IDoubleClickExtension>(Arrays.asList(extensions));
87
88         for (IConfigurationElement el : elements) {
89             String name = StringUtils.safeString(el.getAttribute("name"));
90             double priority = 0;
91             try {
92                 String p = el.getAttribute("priority");
93                 if (p != null) {
94                     priority = Double.parseDouble(p);
95                 }
96             } catch (NumberFormatException e) {
97             }
98             IDoubleClickExtension ext = new DoubleClickExtension(el, name, priority);
99
100             // Start tracking the new extension object, its removal will be notified of
101             // with removeExtension(extension, Object[]).
102             tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);
103
104             newExtensions.add(ext);
105         }
106
107         // Sort in descending priority order
108         List<IDoubleClickExtension> unsorted = new ArrayList<IDoubleClickExtension>(newExtensions);
109         Collections.sort(unsorted, new Comparator<IDoubleClickExtension>() {
110             @Override
111             public int compare(IDoubleClickExtension o1, IDoubleClickExtension o2) {
112                 return Double.compare(o2.getPriority(), o1.getPriority());
113             }
114         });
115
116         // Atomic assignment
117         this.extensions = unsorted.toArray(new IDoubleClickExtension[unsorted.size()]);
118     }
119
120     @Override
121     public void addExtension(IExtensionTracker tracker, IExtension extension) {
122         loadExtensions(extension.getConfigurationElements());
123     }
124
125     @Override
126     public void removeExtension(IExtension extension, Object[] objects) {
127         Set<IDoubleClickExtension> newExtensions = new HashSet<IDoubleClickExtension>(Arrays.asList(extensions));
128
129         for (Object o : objects) {
130             tracker.unregisterObject(extension, o);
131             newExtensions.remove(o);
132         }
133
134         // Atomic assignment
135         this.extensions = newExtensions.toArray(new IDoubleClickExtension[newExtensions.size()]);
136     }
137
138 }