-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.ui;\r
-\r
-import java.util.HashMap;\r
-import java.util.HashSet;\r
-import java.util.Map;\r
-import java.util.Set;\r
-import java.util.concurrent.atomic.AtomicBoolean;\r
-\r
-import org.eclipse.ui.IWindowListener;\r
-import org.eclipse.ui.IWorkbench;\r
-import org.eclipse.ui.IWorkbenchWindow;\r
-import org.simantics.db.management.ISessionContext;\r
-import org.simantics.db.management.ISessionContextProvider;\r
-import org.simantics.db.management.ISessionContextProviderSource;\r
-import org.simantics.db.management.SessionContextProvider;\r
-import org.simantics.ui.internal.SessionUtils;\r
-\r
-/**\r
- *\r
- */\r
-public class WorkbenchWindowSessionContextProviderSource implements ISessionContextProviderSource, IWindowListener {\r
-\r
- private static final boolean DEBUG = false;\r
-\r
- ISessionContextProvider[] ZERO = new ISessionContextProvider[0];\r
-\r
- Object temporaryContext = new Object() {\r
- @Override\r
- public String toString() {\r
- return "Temporary Context Object";\r
- }\r
- };\r
-\r
- /**\r
- * The set of providers associated through\r
- * {@link #put(Object, ISessionContextProvider)} for knowing which\r
- * associations can be removed with {@link #remove(Object)}.\r
- */\r
- Set<Object> manualProviders = new HashSet<Object>();\r
-\r
- Map<Object, ISessionContextProvider> providers = new HashMap<Object, ISessionContextProvider>();\r
-\r
- ISessionContextProvider[] allProviders = ZERO;\r
-\r
- IWorkbenchWindow activeWindow = null;\r
-\r
- ISessionContextProvider activeProvider = null;\r
-\r
- AtomicBoolean isFirstWindow = new AtomicBoolean(true);\r
-\r
- public WorkbenchWindowSessionContextProviderSource(IWorkbench workbench) {\r
- workbench.addWindowListener(this);\r
-\r
- // Bind the initial context provider to a temporary object.\r
- // This binding will be replaced later on when a workbench\r
- // window is opened.\r
- activeProvider = new SessionContextProvider(temporaryContext);\r
- if (DEBUG)\r
- System.out.println("Initial active session context provider: " + activeProvider);\r
- addProvider(temporaryContext, activeProvider);\r
- }\r
-\r
- @Override\r
- public ISessionContextProvider get(Object context) {\r
- synchronized (providers) {\r
- return providers.get(context);\r
- }\r
- }\r
-\r
- @Override\r
- public ISessionContextProvider getActive() {\r
- // activeWindow may be null at this time.\r
- return activeProvider;\r
- }\r
-\r
- @Override\r
- public ISessionContextProvider[] getAll() {\r
- return allProviders;\r
- }\r
-\r
- @Override\r
- public void put(Object context, ISessionContextProvider provider) {\r
- synchronized (providers) {\r
- ISessionContextProvider prev = providers.get(context);\r
- if (prev != null)\r
- throw new IllegalArgumentException("invalid context (" + context\r
- + "), a session context provider is already associated with this context: " + prev\r
- + ". Attempted to associate the context with this provider: " + provider);\r
- providers.put(context, provider);\r
- manualProviders.add(context);\r
- }\r
- }\r
-\r
- @Override\r
- public ISessionContextProvider remove(Object context) {\r
- synchronized (providers) {\r
- if (!manualProviders.remove(context)) {\r
- throw new IllegalArgumentException("specified context not found: " + context);\r
- }\r
- return providers.remove(context);\r
- }\r
- }\r
-\r
- @Override\r
- public void windowActivated(IWorkbenchWindow window) {\r
- if (DEBUG)\r
- System.out.println("window activated: " + window);\r
-\r
- boolean firstActiveWindow = (activeWindow == null);\r
- activeWindow = window;\r
- if (firstActiveWindow) {\r
- // activeProvider should already be set.\r
- assert activeProvider != null;\r
- if (DEBUG)\r
- System.out.println(" first window activation!");\r
- } else {\r
- synchronized (providers) {\r
- activeProvider = providers.get(window);\r
- if (activeProvider == null) {\r
- // This may be an issue, the context provider\r
- // should already have been created through\r
- // createContextForWindow(IWorkbenchWindow).\r
- activeProvider = new SessionContextProvider(window);\r
- addProvider(window, activeProvider);\r
- }\r
- }\r
- }\r
- }\r
-\r
- @Override\r
- public void windowClosed(IWorkbenchWindow window) {\r
- if (DEBUG)\r
- System.out.println("window closed: " + window);\r
- removeProvider(window);\r
- }\r
-\r
- @Override\r
- public void windowDeactivated(IWorkbenchWindow window) {\r
- if (DEBUG)\r
- System.out.println("window deactivated: " + window);\r
- }\r
-\r
- @Override\r
- public void windowOpened(IWorkbenchWindow window) {\r
- if (DEBUG)\r
- System.out.println("window opened: " + window + ", active window = " + activeWindow);\r
- }\r
-\r
- private void addProvider(Object context, ISessionContextProvider p) {\r
- synchronized (providers) {\r
- if (providers.put(context, p) != null) {\r
- // This is a bug, should never happen.\r
- throw new Error("Bug encountered, contact application support with stack trace.");\r
- }\r
- allProviders = providers.values().toArray(ZERO);\r
- }\r
- }\r
-\r
- private void removeProvider(Object context) {\r
- synchronized (providers) {\r
- ISessionContextProvider provider = providers.remove(context);\r
- if (provider == null) {\r
- // This is a bug, should never happen.\r
- throw new Error("Bug encountered, contact application support with stack trace.");\r
- }\r
- allProviders = providers.values().toArray(ZERO);\r
-\r
- // Make sure that any session remaining in the removed context will\r
- // be disposed of eventually.\r
- ISessionContext ctx = provider.getSessionContext();\r
- if (ctx != null) {\r
- if (!SimanticsUI.isInUse(ctx)) {\r
- SessionUtils.releaseUnusedSessionAfterHoldTime(ctx);\r
- }\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * This is a purely internal mechanism to allow proper creation of context\r
- * providers for workbench windows as early as possible during the workbench\r
- * window construction process, i.e. in the\r
- * <code>WorkbenchWindowAdvisor.preWindowOpen()</code> instantiated\r
- * by <code>WorkbenchAdvisor</code>.\r
- * \r
- * @param context\r
- */\r
- public void createProviderForWindow(IWorkbenchWindow context) {\r
- synchronized (providers) {\r
- if (isFirstWindow.compareAndSet(true, false)) {\r
- assert activeProvider != null;\r
- if (DEBUG)\r
- System.out.println("[Create Context] first window: " + context);\r
-\r
- SessionContextProvider v = (SessionContextProvider) providers.remove(temporaryContext);\r
- if (v != null) {\r
- v.setHandle(context);\r
- providers.put(context, v);\r
- if (DEBUG)\r
- System.out.println(" rebound: '" + v + "' to '" + context + "'");\r
- }\r
- } else {\r
- if (DEBUG)\r
- System.out.println("[Create Context] new window: " + context);\r
- // Create new session context provider\r
- SessionContextProvider v = new SessionContextProvider(context);\r
- ISessionContextProvider active = getActive();\r
- if (active != null)\r
- v.setSessionContext(active.getSessionContext());\r
- addProvider(context, v);\r
- }\r
- }\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.ui;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.eclipse.ui.IWindowListener;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.simantics.db.management.ISessionContext;
+import org.simantics.db.management.ISessionContextProvider;
+import org.simantics.db.management.ISessionContextProviderSource;
+import org.simantics.db.management.SessionContextProvider;
+import org.simantics.ui.internal.SessionUtils;
+
+/**
+ *
+ */
+public class WorkbenchWindowSessionContextProviderSource implements ISessionContextProviderSource, IWindowListener {
+
+ private static final boolean DEBUG = false;
+
+ ISessionContextProvider[] ZERO = new ISessionContextProvider[0];
+
+ Object temporaryContext = new Object() {
+ @Override
+ public String toString() {
+ return "Temporary Context Object";
+ }
+ };
+
+ /**
+ * The set of providers associated through
+ * {@link #put(Object, ISessionContextProvider)} for knowing which
+ * associations can be removed with {@link #remove(Object)}.
+ */
+ Set<Object> manualProviders = new HashSet<Object>();
+
+ Map<Object, ISessionContextProvider> providers = new HashMap<Object, ISessionContextProvider>();
+
+ ISessionContextProvider[] allProviders = ZERO;
+
+ IWorkbenchWindow activeWindow = null;
+
+ ISessionContextProvider activeProvider = null;
+
+ AtomicBoolean isFirstWindow = new AtomicBoolean(true);
+
+ public WorkbenchWindowSessionContextProviderSource(IWorkbench workbench) {
+ workbench.addWindowListener(this);
+
+ // Bind the initial context provider to a temporary object.
+ // This binding will be replaced later on when a workbench
+ // window is opened.
+ activeProvider = new SessionContextProvider(temporaryContext);
+ if (DEBUG)
+ System.out.println("Initial active session context provider: " + activeProvider);
+ addProvider(temporaryContext, activeProvider);
+ }
+
+ @Override
+ public ISessionContextProvider get(Object context) {
+ synchronized (providers) {
+ return providers.get(context);
+ }
+ }
+
+ @Override
+ public ISessionContextProvider getActive() {
+ // activeWindow may be null at this time.
+ return activeProvider;
+ }
+
+ @Override
+ public ISessionContextProvider[] getAll() {
+ return allProviders;
+ }
+
+ @Override
+ public void put(Object context, ISessionContextProvider provider) {
+ synchronized (providers) {
+ ISessionContextProvider prev = providers.get(context);
+ if (prev != null)
+ throw new IllegalArgumentException("invalid context (" + context
+ + "), a session context provider is already associated with this context: " + prev
+ + ". Attempted to associate the context with this provider: " + provider);
+ providers.put(context, provider);
+ manualProviders.add(context);
+ }
+ }
+
+ @Override
+ public ISessionContextProvider remove(Object context) {
+ synchronized (providers) {
+ if (!manualProviders.remove(context)) {
+ throw new IllegalArgumentException("specified context not found: " + context);
+ }
+ return providers.remove(context);
+ }
+ }
+
+ @Override
+ public void windowActivated(IWorkbenchWindow window) {
+ if (DEBUG)
+ System.out.println("window activated: " + window);
+
+ boolean firstActiveWindow = (activeWindow == null);
+ activeWindow = window;
+ if (firstActiveWindow) {
+ // activeProvider should already be set.
+ assert activeProvider != null;
+ if (DEBUG)
+ System.out.println(" first window activation!");
+ } else {
+ synchronized (providers) {
+ activeProvider = providers.get(window);
+ if (activeProvider == null) {
+ // This may be an issue, the context provider
+ // should already have been created through
+ // createContextForWindow(IWorkbenchWindow).
+ activeProvider = new SessionContextProvider(window);
+ addProvider(window, activeProvider);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void windowClosed(IWorkbenchWindow window) {
+ if (DEBUG)
+ System.out.println("window closed: " + window);
+ removeProvider(window);
+ }
+
+ @Override
+ public void windowDeactivated(IWorkbenchWindow window) {
+ if (DEBUG)
+ System.out.println("window deactivated: " + window);
+ }
+
+ @Override
+ public void windowOpened(IWorkbenchWindow window) {
+ if (DEBUG)
+ System.out.println("window opened: " + window + ", active window = " + activeWindow);
+ }
+
+ private void addProvider(Object context, ISessionContextProvider p) {
+ synchronized (providers) {
+ if (providers.put(context, p) != null) {
+ // This is a bug, should never happen.
+ throw new Error("Bug encountered, contact application support with stack trace.");
+ }
+ allProviders = providers.values().toArray(ZERO);
+ }
+ }
+
+ private void removeProvider(Object context) {
+ synchronized (providers) {
+ ISessionContextProvider provider = providers.remove(context);
+ if (provider == null) {
+ // This is a bug, should never happen.
+ throw new Error("Bug encountered, contact application support with stack trace.");
+ }
+ allProviders = providers.values().toArray(ZERO);
+
+ // Make sure that any session remaining in the removed context will
+ // be disposed of eventually.
+ ISessionContext ctx = provider.getSessionContext();
+ if (ctx != null) {
+ if (!SimanticsUI.isInUse(ctx)) {
+ SessionUtils.releaseUnusedSessionAfterHoldTime(ctx);
+ }
+ }
+ }
+ }
+
+ /**
+ * This is a purely internal mechanism to allow proper creation of context
+ * providers for workbench windows as early as possible during the workbench
+ * window construction process, i.e. in the
+ * <code>WorkbenchWindowAdvisor.preWindowOpen()</code> instantiated
+ * by <code>WorkbenchAdvisor</code>.
+ *
+ * @param context
+ */
+ public void createProviderForWindow(IWorkbenchWindow context) {
+ synchronized (providers) {
+ if (isFirstWindow.compareAndSet(true, false)) {
+ assert activeProvider != null;
+ if (DEBUG)
+ System.out.println("[Create Context] first window: " + context);
+
+ SessionContextProvider v = (SessionContextProvider) providers.remove(temporaryContext);
+ if (v != null) {
+ v.setHandle(context);
+ providers.put(context, v);
+ if (DEBUG)
+ System.out.println(" rebound: '" + v + "' to '" + context + "'");
+ }
+ } else {
+ if (DEBUG)
+ System.out.println("[Create Context] new window: " + context);
+ // Create new session context provider
+ SessionContextProvider v = new SessionContextProvider(context);
+ ISessionContextProvider active = getActive();
+ if (active != null)
+ v.setSessionContext(active.getSessionContext());
+ addProvider(context, v);
+ }
+ }
+ }
+
+}