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