1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.ui.workbench;
\r
14 import java.util.Arrays;
\r
15 import java.util.Collections;
\r
16 import java.util.List;
\r
18 import org.eclipse.core.runtime.IConfigurationElement;
\r
19 import org.eclipse.core.runtime.IExtension;
\r
20 import org.eclipse.core.runtime.IExtensionPoint;
\r
21 import org.eclipse.core.runtime.Platform;
\r
22 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
\r
23 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
\r
24 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
\r
25 import org.eclipse.core.runtime.dynamichelpers.IFilter;
\r
26 import org.simantics.ui.internal.Activator;
\r
27 import org.simantics.utils.datastructures.MapList;
\r
29 public class PerspectiveBarsManager implements IExtensionChangeHandler {
\r
31 private final static String NAMESPACE = Activator.PLUGIN_ID;
\r
33 private final static String EP_NAME = "perspectiveBars";
\r
35 private ExtensionTracker tracker;
\r
37 private MapList<String, IPerspectiveBarsExtension> extensions = new MapList<String, IPerspectiveBarsExtension>();
\r
39 private static PerspectiveBarsManager INSTANCE;
\r
41 public static synchronized PerspectiveBarsManager getInstance() {
\r
42 if (INSTANCE == null)
\r
43 INSTANCE = new PerspectiveBarsManager();
\r
47 public static synchronized void dispose() {
\r
48 if (INSTANCE != null) {
\r
54 private PerspectiveBarsManager() {
\r
55 tracker = new ExtensionTracker();
\r
57 // Cache defined actions
\r
58 IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);
\r
59 loadExtensions(expt.getConfigurationElements());
\r
61 // Start tracking for new and removed extensions
\r
62 IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);
\r
63 tracker.registerHandler(this, filter);
\r
66 private void close() {
\r
69 extensions = new MapList<String, IPerspectiveBarsExtension>();
\r
72 public synchronized List<IPerspectiveBarsExtension> getExtensions(String perspectiveId) {
\r
73 List<IPerspectiveBarsExtension> exts = extensions.getValues(perspectiveId);
\r
75 return Arrays.asList();
\r
76 return Collections.unmodifiableList(exts);
\r
79 private synchronized void loadExtensions(IConfigurationElement[] elements) {
\r
80 for (IConfigurationElement el : elements) {
\r
81 String perspectiveId = el.getAttribute("perspectiveId");
\r
82 Boolean menuBar = toBoolean(el.getAttribute("menuBar"));
\r
83 Boolean coolBar = toBoolean(el.getAttribute("coolBar"));
\r
84 Boolean statusLine = toBoolean(el.getAttribute("statusLine"));
\r
85 Boolean perspectiveBar = toBoolean(el.getAttribute("perspectiveBar"));
\r
86 Boolean fastViewBar = toBoolean(el.getAttribute("fastViewBar"));
\r
87 Boolean progressIndicator = toBoolean(el.getAttribute("progressIndicator"));
\r
89 IPerspectiveBarsExtension ext = new IPerspectiveBarsExtension.Stub(perspectiveId,menuBar,coolBar,statusLine,perspectiveBar,fastViewBar,progressIndicator);
\r
91 // Start tracking the new extension object, its removal will be notified of
\r
92 // with removeExtension(extension, Object[]).
\r
93 tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);
\r
95 extensions.add(perspectiveId, ext);
\r
99 private Boolean toBoolean(String s) {
\r
102 return Boolean.parseBoolean(s);
\r
106 public void addExtension(IExtensionTracker tracker, IExtension extension) {
\r
107 loadExtensions(extension.getConfigurationElements());
\r
111 public synchronized void removeExtension(IExtension extension, Object[] objects) {
\r
113 for (Object o : objects) {
\r
114 IPerspectiveBarsExtension ext = (IPerspectiveBarsExtension) o;
\r
115 tracker.unregisterObject(extension, ext);
\r
116 extensions.remove(ext.getPerspectiveId(), ext);
\r