-/*******************************************************************************\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 org.eclipse.jface.resource.ImageDescriptor;\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.swt.widgets.Display;\r
-import org.eclipse.swt.widgets.Widget;\r
-import org.eclipse.ui.PlatformUI;\r
-import org.simantics.DatabaseJob;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.common.primitiverequest.Adapter;\r
-import org.simantics.db.common.utils.Logger;\r
-import org.simantics.db.common.utils.RequestUtil;\r
-import org.simantics.db.exception.DatabaseException;\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.project.IProject;\r
-import org.simantics.project.ProjectKeys;\r
-import org.simantics.utils.datastructures.Arrays;\r
-import org.simantics.utils.ui.BundleUtils;\r
-import org.simantics.utils.ui.ISelectionUtils;\r
-import org.simantics.utils.ui.SWTUtils;\r
-\r
-/**\r
- */\r
-public class SimanticsUI {\r
-\r
- public static final String PLUGIN_ID = "org.simantics.ui";\r
-\r
- /**\r
- * The maximum amount of time in milliseconds to wait for the execution of a\r
- * database request to start when the request is executed synchronously in\r
- * the UI thread. The timeout counting starts from the moment the request is\r
- * first scheduled into the database {@link Session}. The purpose is to\r
- * prevent synchronous UI thread database requests from locking the whole UI\r
- * thread up.\r
- *\r
- * <p>\r
- * The default value is 20. The default value can be customized at class\r
- * load time by setting the system property\r
- * <code>simantics.ui.request.start.timeout</code> to the desired value at\r
- * JVM startup.\r
- * \r
- * @see RequestUtil\r
- */\r
- public static final long UI_THREAD_REQUEST_START_TIMEOUT;\r
- /**\r
- * The maximum amount of time in milliseconds to wait for the execution of a\r
- * database request to complete when the request is executed synchronously\r
- * in the UI thread. The timeout counting starts from the moment the request\r
- * execution is scheduled. The purpose is to prevent synchronous UI thread\r
- * database requests from locking the whole UI thread up.\r
- *\r
- * <p>\r
- * The default value is 50. The default value can be customized at class\r
- * load time by setting the system property\r
- * <code>simantics.ui.request.execution.timeout</code> to the desired value\r
- * at JVM startup.\r
- * \r
- * @see RequestUtil\r
- */\r
- public static final long UI_THREAD_REQUEST_EXECUTION_TIMEOUT;\r
- /**\r
- *\r
- * <p>\r
- * The default value is 100. The default value can be customized at class\r
- * load time by setting the system property\r
- * <code>simantics.ui.request.execution.timeout.long</code> to the desired\r
- * value at JVM startup.\r
- * \r
- * @see RequestUtil\r
- */\r
- public static final long UI_THREAD_REQUEST_EXECUTION_TIMEOUT_LONG;\r
-\r
- static {\r
- UI_THREAD_REQUEST_START_TIMEOUT = parseLongProperty("simantics.ui.request.start.timeout", 500L);\r
- UI_THREAD_REQUEST_EXECUTION_TIMEOUT = parseLongProperty("simantics.ui.request.exec.timeout", 50L);\r
- UI_THREAD_REQUEST_EXECUTION_TIMEOUT_LONG = parseLongProperty("simantics.ui.request.exec.timeout.long", 100L);\r
- }\r
-\r
- /**\r
- * Information of the currently open database session for the Simantics UI.\r
- * Contains just the vital information to connect to the database. Is\r
- * <code>null</code> when there is no open database session.\r
- */\r
- private static ISessionContextProviderSource providerSource = null;\r
-\r
-// /**\r
-// * TODO: support different contexts\r
-// * @deprecated no replacement\r
-// */\r
-// @Deprecated\r
-// public static void undo() {\r
-// try {\r
-// PlatformUI.getWorkbench().getOperationSupport().getOperationHistory().undo(\r
-// IOperationHistory.GLOBAL_UNDO_CONTEXT, null, null);\r
-// } catch (ExecutionException e) {\r
-// // TODO Auto-generated catch block\r
-// e.printStackTrace();\r
-// }\r
-// }\r
-//\r
-// /**\r
-// * TODO: support different contexts\r
-// * @deprecated no replacement\r
-// */\r
-// @Deprecated\r
-// public static void redo() {\r
-// try {\r
-// PlatformUI.getWorkbench().getOperationSupport().getOperationHistory().redo(\r
-// IOperationHistory.GLOBAL_UNDO_CONTEXT, null, null);\r
-// } catch (ExecutionException e) {\r
-// // TODO Auto-generated catch block\r
-// e.printStackTrace();\r
-// }\r
-// }\r
-\r
- /**\r
- * Only for use in application startup code such as the workbench window\r
- * advisor. Must be invoked before calling any other methods in this class.\r
- * \r
- * @param manager the ISessionManager to be used by the application\r
- * @throw IllegalArgumentException if manager is <code>null</code>\r
- */\r
- public static void setSessionContextProviderSource(ISessionContextProviderSource source) {\r
- if (source == null)\r
- throw new IllegalArgumentException("null provider source");\r
- providerSource = source;\r
- }\r
-\r
- /**\r
- * Asserts that the current context provider source has been initialized\r
- * before allowing access to it.\r
- * \r
- * @return current context provider source\r
- */\r
- public static ISessionContextProviderSource getProviderSource() {\r
- if (providerSource == null)\r
- throw new IllegalStateException(\r
- "providerSource must be initialized by the application before using SimanticsUI");\r
- return providerSource;\r
- }\r
-\r
- /**\r
- * Close and remove the current session contexts of the UI. Afterwards\r
- * getSessionContext will return <code>null</code>.\r
- * \r
- * Not for client use, only for internal purposes.\r
- */\r
- public static synchronized void closeSessions() {\r
- ISessionContextProviderSource source = providerSource;\r
- if (source == null)\r
- return;\r
- for (ISessionContextProvider p : source.getAll()) {\r
- ISessionContext ctx = p.setSessionContext(null);\r
- if (ctx != null) {\r
- ctx.dispose();\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * @return <code>true</code> if the session manager contains the specified\r
- * session context\r
- */\r
- public static synchronized boolean isInUse(ISessionContext ctx) {\r
- for (ISessionContextProvider p : getProviderSource().getAll()) {\r
- if (p.getSessionContext() == ctx)\r
- return true;\r
- }\r
- return false;\r
- }\r
-\r
- /**\r
- * @param project the project to check\r
- * @param excluding\r
- * @return <code>true</code> if the session manager contains an\r
- * ISessionContext that contains a reference to the specified\r
- * project, disregarding the excluded ISessionContexts listed\r
- */\r
- public static synchronized boolean isInUse(IProject project, ISessionContext... excluding) {\r
- for (ISessionContextProvider p : getProviderSource().getAll()) {\r
- ISessionContext ctx = p.getSessionContext();\r
- if (ctx != null) {\r
- if (Arrays.indexOf(excluding, ctx) == -1) {\r
- if (ctx.getHint(ProjectKeys.KEY_PROJECT) == project)\r
- return true;\r
- }\r
- }\r
- }\r
- return false;\r
- }\r
-\r
-// /**\r
-// * Looks if there is an ISessionContextProvider within the Simantics workbench\r
-// * that is currently using a ProCore database server at the specified\r
-// * address.\r
-// * \r
-// * @param address the address to look for connections to\r
-// * @return <code>null</code> if there is currently no session in use to the\r
-// * specified address.\r
-// */\r
-// public static synchronized ISessionContext findSessionTo(ServerAddress address) {\r
-// if (address == null)\r
-// throw new IllegalArgumentException("null address");\r
-// for (ISessionContextProvider provider : getProviderSource().getAll()) {\r
-// ISessionContext ctx = provider.getSessionContext();\r
-// if (ctx != null) {\r
-// ServerAddress addr = ctx.getAddress();\r
-// if (address.equals(addr))\r
-// return ctx;\r
-// }\r
-// }\r
-// return null;\r
-// }\r
-\r
- /**\r
- * Returns the session context provider of the curretly active workbench\r
- * window. This method will always return a valid session context provider.\r
- * \r
- * @return a valid ISessionContextProvider\r
- */\r
- public static ISessionContextProvider getSessionContextProvider() {\r
- return getProviderSource().getActive();\r
- }\r
-\r
- /**\r
- * Returns the session context provider for the specified handle if one\r
- * exists. Workbench windows (IWorkbenchWindow) are currently used as\r
- * handles.\r
- * \r
- * @param handle the handle associated with the requested session context\r
- * provider\r
- * @return <code>null</code> if there is no session associated to the\r
- * specified handle\r
- */\r
- public static ISessionContextProvider getSessionContextProvider(Object handle) {\r
- return getProviderSource().get(handle);\r
- }\r
-\r
- /**\r
- * Returns the database session context associated with the currently active\r
- * workbench window. This method should be used to retrieve session contexts\r
- * only when the client is sure that the correct workbench window has focus.\r
- * \r
- * <p>\r
- * If the client knows the workbench window it is working with, but it isn't\r
- * sure that the correct workbench window has focus, use\r
- * {@link #getSessionContext(Object)} instead.\r
- * </p>\r
- * \r
- * @return the session context associated with the currently active\r
- * workbench window or <code>null</code> if the active window has no\r
- * session context\r
- */\r
- public static ISessionContext getSessionContext() {\r
- ISessionContextProvider provider = getSessionContextProvider();\r
- return provider != null ? provider.getSessionContext() : null;\r
- }\r
-\r
- /**\r
- * Returns the database session context associated with the specified\r
- * handle. Workbench windows (IWorkbenchWindow) are currently used as\r
- * handles. This method should be used to retrieve session contexts in cases\r
- * where the workbench window is known, but the thread of execution is such\r
- * that the client cannot be certain that the same workbench window has\r
- * focus.\r
- * \r
- * @return the session context associated with the specified handle\r
- * (IWorkbenchWindow)\r
- */\r
- public static ISessionContext getSessionContext(Object handle) {\r
- return getSessionContextProvider(handle).getSessionContext();\r
- }\r
-\r
- /**\r
- * Associates the specified ISessionContext with the currently active\r
- * workbench window. To remove an ISessionContext association from the\r
- * active workbench window, specify <code>null</code> as ctx.\r
- * \r
- * <p>\r
- * After invoking this method you should be able to retrieve the same\r
- * ISessionContext through {@link #getSessionContext()}, provided that the\r
- * same workbench window has focus at that time.\r
- * </p>\r
- * \r
- * @param ctx the new UI database session context or <code>null</code> to\r
- * replace the current UI session with no session.\r
- * @return The previous session context if one existed, otherwise\r
- * <code>null</code>. If the specified <code>ctx</code> matched the\r
- * current session context (<code>null</code> or\r
- * <code>non-null</code>), null is also returned and nothing is\r
- * done.\r
- */\r
- public static synchronized ISessionContext setSessionContext(ISessionContext ctx) {\r
- return getSessionContextProvider().setSessionContext(ctx);\r
- }\r
-\r
- /**\r
- * Associates the specified ISessionContext with the specified handle\r
- * object.\r
- * \r
- * <p>\r
- * Currently IWorkbenchWindow's are used as handles. This implies\r
- * that each workbench window can only have one active ISessionContext bound\r
- * to it. After invoking this method with a valid workbench window handle\r
- * you should be able to retrieve the same ISessionContext through\r
- * {@link #getSessionContext(Object)} with the same workbench window\r
- * specified as the handle.\r
- * </p>\r
- * \r
- * @param handle the handle to associate the specified ISessionContext with.\r
- * @param ctx the new UI database session context or <code>null</code> to\r
- * replace the current UI session with no session.\r
- * @return The previous session context if one existed, otherwise\r
- * <code>null</code>. If the specified <code>ctx</code> matched the\r
- * current session context (<code>null</code> or\r
- * <code>non-null</code>), null is also returned and nothing is\r
- * done.\r
- */\r
- public static synchronized ISessionContext setSessionContext(Object handle, ISessionContext ctx) {\r
- ISessionContextProvider provider = getProviderSource().get(handle);\r
- if (provider != null)\r
- return provider.setSessionContext(ctx);\r
- return null;\r
- }\r
-\r
- /**\r
- * Returns the database Session bound to the currently active workbench\r
- * window.\r
- * \r
- * <p>\r
- * This method should only be invoked in cases where it is certain that the\r
- * correct workbench window has focus or it is the latest of all workbench\r
- * windows to have had focus. Basically any invocation from the SWT UI\r
- * thread is safe, since because in those cases the currently active\r
- * workbench window is generally known. Instead invocations from any other\r
- * thread should be carefully considered. The rule of thumb is that if you\r
- * cannot be sure that the correct workbench window has focus, you should\r
- * always get a hold of the Session to be used in some other manner.\r
- * </p>\r
- * \r
- * <p>\r
- * The method always returns a non-null Session or produces an\r
- * IllegalStateException if a Session was not attainable.\r
- * </p>\r
- * \r
- * @return the Session bound to the currently active workbench window\r
- * @throws IllegalStateException if no Session was available\r
- */\r
- public static Session getSession() {\r
- ISessionContext ctx = getSessionContext();\r
- if (ctx == null)\r
- throw new IllegalStateException("Session unavailable, no database session open");\r
- return ctx.getSession();\r
- }\r
- \r
- /**\r
- * Returns the database Session bound to the currently active workbench\r
- * window. Differently from {@link #getSession()}, this method returns\r
- * <code>null</code> if there is no current Session available.\r
- * \r
- * <p>\r
- * This method should only be invoked from the SWT UI thread. Check the\r
- * explanations given in {@link #getSession()}. The same applies to this\r
- * method also.\r
- * </p>\r
- * \r
- * @return the Session bound to the currently active workbench window or\r
- * <code>null</code>\r
- */\r
- public static Session peekSession() {\r
- ISessionContext ctx = getSessionContext();\r
- return ctx == null ? null : ctx.peekSession();\r
- }\r
-\r
- /**\r
- * @return the currently open and active project as an IProject or\r
- * <code>null</code> if there is no active session or project\r
- */\r
- public static IProject peekProject() {\r
- ISessionContext ctx = getSessionContext();\r
- return ctx == null ? null : (org.simantics.project.IProject) ctx.getHint(ProjectKeys.KEY_PROJECT);\r
- }\r
-\r
- /**\r
- * @return the currently open and active project for the specified database\r
- * session or <code>null</code> if there is no current project\r
- */\r
- public static IProject peekProject(ISessionContext ctx) {\r
- if (ctx == null)\r
- return null;\r
- return ctx.getHint(ProjectKeys.KEY_PROJECT);\r
- }\r
-\r
- /**\r
- * @return the currently open and active project as an IProject\r
- * @throws IllegalStateException if there is no currently active database\r
- * session, which also means there is no active project at the\r
- * moment\r
- */\r
- public static IProject getProject() {\r
- ISessionContext ctx = getSessionContext();\r
- if (ctx == null)\r
- throw new IllegalStateException("No current database session");\r
- return ctx.getHint(ProjectKeys.KEY_PROJECT);\r
- }\r
-\r
- /**\r
- * TODO: refactor this out of here\r
- * \r
- * @param imageFilePath\r
- * @return\r
- */\r
- public static ImageDescriptor getImageDescriptor(String imageFilePath) {\r
- return BundleUtils.getImageDescriptorFromPlugin(PLUGIN_ID, imageFilePath);\r
- }\r
-\r
- /**\r
- * TODO: [Tuukka] I'm really unsure this belongs here.\r
- * \r
- * @param <T>\r
- * @param sel\r
- * @param assignableFrom\r
- * @return\r
- */\r
- public static <T> T filterSingleSelection(ISelection sel, Class<T> assignableFrom) {\r
-\r
- T result = ISelectionUtils.filterSingleSelection(sel, assignableFrom);\r
- if (result != null)\r
- return result;\r
-\r
- Resource resource = ISelectionUtils.filterSingleSelection(sel, Resource.class);\r
- if(resource == null) return null;\r
- \r
- try {\r
- return getSession().syncRequest(new Adapter<T>(resource, assignableFrom));\r
- } catch (DatabaseException e) {\r
- Logger.defaultLogError(e);\r
- return null;\r
- }\r
- \r
- }\r
- \r
- public static <T> T filterSingleWorkbenchSelection(Class<T> assignableFrom) {\r
- return filterSingleSelection(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(), assignableFrom);\r
- }\r
-\r
-\r
- public static void asyncExecSWT(final Widget widget, final Runnable runnable) {\r
- SWTUtils.asyncExec(widget, delayedExecSWT(null, widget, runnable));\r
- }\r
-\r
- public static void asyncExecSWT(final Display display, final Runnable runnable) {\r
- SWTUtils.asyncExec(display, delayedExecSWT(display, null, runnable));\r
- }\r
-\r
- private static Runnable delayedExecSWT(final Display display, final Widget widget, final Runnable runnable) {\r
- if (display == null && widget == null)\r
- throw new IllegalArgumentException("both display and widget are null");\r
-\r
- return new Runnable() {\r
- @Override\r
- public void run() {\r
- if (display != null && display.isDisposed())\r
- return;\r
- if (widget != null && widget.isDisposed())\r
- return;\r
- if (DatabaseJob.inProgress()) {\r
- Display d = display != null ? display : widget.getDisplay();\r
- d.timerExec(50, this);\r
- return;\r
- }\r
- runnable.run();\r
- }\r
- };\r
- }\r
-\r
- private static long parseLongProperty(String propertyName, long defaultValue) {\r
- String value = System.getProperty(propertyName, null);\r
- try {\r
- return value != null ? Long.parseLong(value) : defaultValue;\r
- } catch (NumberFormatException e) {\r
- return defaultValue;\r
- }\r
- }\r
- \r
- public static boolean isLinuxGTK() {\r
- String ws = System.getProperty("osgi.ws");\r
- return ws != null && "gtk".equals(ws);\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010, 2018 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 org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Widget;
+import org.eclipse.ui.PlatformUI;
+import org.simantics.DatabaseJob;
+import org.simantics.Simantics;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.common.primitiverequest.Adapter;
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.common.utils.RequestUtil;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.management.ISessionContext;
+import org.simantics.db.management.ISessionContextProvider;
+import org.simantics.db.management.ISessionContextProviderSource;
+import org.simantics.project.IProject;
+import org.simantics.project.ProjectKeys;
+import org.simantics.utils.datastructures.Arrays;
+import org.simantics.utils.ui.BundleUtils;
+import org.simantics.utils.ui.ISelectionUtils;
+import org.simantics.utils.ui.SWTUtils;
+
+/**
+ */
+public class SimanticsUI {
+
+ public static final String PLUGIN_ID = "org.simantics.ui";
+
+ /**
+ * The maximum amount of time in milliseconds to wait for the execution of a
+ * database request to start when the request is executed synchronously in
+ * the UI thread. The timeout counting starts from the moment the request is
+ * first scheduled into the database {@link Session}. The purpose is to
+ * prevent synchronous UI thread database requests from locking the whole UI
+ * thread up.
+ *
+ * <p>
+ * The default value is 20. The default value can be customized at class
+ * load time by setting the system property
+ * <code>simantics.ui.request.start.timeout</code> to the desired value at
+ * JVM startup.
+ *
+ * @see RequestUtil
+ */
+ public static final long UI_THREAD_REQUEST_START_TIMEOUT;
+ /**
+ * The maximum amount of time in milliseconds to wait for the execution of a
+ * database request to complete when the request is executed synchronously
+ * in the UI thread. The timeout counting starts from the moment the request
+ * execution is scheduled. The purpose is to prevent synchronous UI thread
+ * database requests from locking the whole UI thread up.
+ *
+ * <p>
+ * The default value is 50. The default value can be customized at class
+ * load time by setting the system property
+ * <code>simantics.ui.request.execution.timeout</code> to the desired value
+ * at JVM startup.
+ *
+ * @see RequestUtil
+ */
+ public static final long UI_THREAD_REQUEST_EXECUTION_TIMEOUT;
+ /**
+ *
+ * <p>
+ * The default value is 100. The default value can be customized at class
+ * load time by setting the system property
+ * <code>simantics.ui.request.execution.timeout.long</code> to the desired
+ * value at JVM startup.
+ *
+ * @see RequestUtil
+ */
+ public static final long UI_THREAD_REQUEST_EXECUTION_TIMEOUT_LONG;
+
+ static {
+ UI_THREAD_REQUEST_START_TIMEOUT = parseLongProperty("simantics.ui.request.start.timeout", 500L);
+ UI_THREAD_REQUEST_EXECUTION_TIMEOUT = parseLongProperty("simantics.ui.request.exec.timeout", 50L);
+ UI_THREAD_REQUEST_EXECUTION_TIMEOUT_LONG = parseLongProperty("simantics.ui.request.exec.timeout.long", 100L);
+ }
+
+ /**
+ * Information of the currently open database session for the Simantics UI.
+ * Contains just the vital information to connect to the database. Is
+ * <code>null</code> when there is no open database session.
+ */
+ private static ISessionContextProviderSource providerSource = null;
+
+// /**
+// * TODO: support different contexts
+// * @deprecated no replacement
+// */
+// @Deprecated
+// public static void undo() {
+// try {
+// PlatformUI.getWorkbench().getOperationSupport().getOperationHistory().undo(
+// IOperationHistory.GLOBAL_UNDO_CONTEXT, null, null);
+// } catch (ExecutionException e) {
+// // TODO Auto-generated catch block
+// e.printStackTrace();
+// }
+// }
+//
+// /**
+// * TODO: support different contexts
+// * @deprecated no replacement
+// */
+// @Deprecated
+// public static void redo() {
+// try {
+// PlatformUI.getWorkbench().getOperationSupport().getOperationHistory().redo(
+// IOperationHistory.GLOBAL_UNDO_CONTEXT, null, null);
+// } catch (ExecutionException e) {
+// // TODO Auto-generated catch block
+// e.printStackTrace();
+// }
+// }
+
+ /**
+ * Only for use in application startup code such as the workbench window
+ * advisor. Must be invoked before calling any other methods in this class.
+ *
+ * @param manager the ISessionManager to be used by the application
+ * @throw IllegalArgumentException if manager is <code>null</code>
+ */
+ public static void setSessionContextProviderSource(ISessionContextProviderSource source) {
+ if (source == null)
+ throw new IllegalArgumentException("null provider source");
+ providerSource = source;
+ }
+
+ /**
+ * Asserts that the current context provider source has been initialized
+ * before allowing access to it.
+ *
+ * @return current context provider source
+ */
+ public static ISessionContextProviderSource getProviderSource() {
+ if (providerSource == null)
+ throw new IllegalStateException(
+ "providerSource must be initialized by the application before using SimanticsUI");
+ return providerSource;
+ }
+
+ /**
+ * Close and remove the current session contexts of the UI. Afterwards
+ * getSessionContext will return <code>null</code>.
+ *
+ * Not for client use, only for internal purposes.
+ */
+ public static synchronized void closeSessions() {
+ ISessionContextProviderSource source = providerSource;
+ if (source == null)
+ return;
+ for (ISessionContextProvider p : source.getAll()) {
+ ISessionContext ctx = p.setSessionContext(null);
+ if (ctx != null) {
+ ctx.dispose();
+ }
+ }
+ }
+
+ /**
+ * @return <code>true</code> if the session manager contains the specified
+ * session context
+ */
+ public static synchronized boolean isInUse(ISessionContext ctx) {
+ for (ISessionContextProvider p : getProviderSource().getAll()) {
+ if (p.getSessionContext() == ctx)
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * @param project the project to check
+ * @param excluding
+ * @return <code>true</code> if the session manager contains an
+ * ISessionContext that contains a reference to the specified
+ * project, disregarding the excluded ISessionContexts listed
+ */
+ public static synchronized boolean isInUse(IProject project, ISessionContext... excluding) {
+ for (ISessionContextProvider p : getProviderSource().getAll()) {
+ ISessionContext ctx = p.getSessionContext();
+ if (ctx != null) {
+ if (Arrays.indexOf(excluding, ctx) == -1) {
+ if (ctx.getHint(ProjectKeys.KEY_PROJECT) == project)
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+// /**
+// * Looks if there is an ISessionContextProvider within the Simantics workbench
+// * that is currently using a ProCore database server at the specified
+// * address.
+// *
+// * @param address the address to look for connections to
+// * @return <code>null</code> if there is currently no session in use to the
+// * specified address.
+// */
+// public static synchronized ISessionContext findSessionTo(ServerAddress address) {
+// if (address == null)
+// throw new IllegalArgumentException("null address");
+// for (ISessionContextProvider provider : getProviderSource().getAll()) {
+// ISessionContext ctx = provider.getSessionContext();
+// if (ctx != null) {
+// ServerAddress addr = ctx.getAddress();
+// if (address.equals(addr))
+// return ctx;
+// }
+// }
+// return null;
+// }
+
+ /**
+ * Returns the session context provider for the specified handle if one
+ * exists. Workbench windows (IWorkbenchWindow) are currently used as
+ * handles.
+ *
+ * @param handle the handle associated with the requested session context
+ * provider
+ * @return <code>null</code> if there is no session associated to the
+ * specified handle
+ */
+ public static ISessionContextProvider getSessionContextProvider(Object handle) {
+ return getProviderSource().get(handle);
+ }
+
+ /**
+ * Returns the database session context associated with the currently active
+ * workbench window. This method should be used to retrieve session contexts
+ * only when the client is sure that the correct workbench window has focus.
+ *
+ * <p>
+ * If the client knows the workbench window it is working with, but it isn't
+ * sure that the correct workbench window has focus, use
+ * {@link #getSessionContext(Object)} instead.
+ * </p>
+ *
+ * @return the session context associated with the currently active
+ * workbench window or <code>null</code> if the active window has no
+ * session context
+ */
+ @Deprecated
+ public static ISessionContext getSessionContext() {
+ return Simantics.getSessionContext();
+ }
+
+ /**
+ * Returns the database session context associated with the specified
+ * handle. Workbench windows (IWorkbenchWindow) are currently used as
+ * handles. This method should be used to retrieve session contexts in cases
+ * where the workbench window is known, but the thread of execution is such
+ * that the client cannot be certain that the same workbench window has
+ * focus.
+ *
+ * @return the session context associated with the specified handle
+ * (IWorkbenchWindow)
+ */
+ public static ISessionContext getSessionContext(Object handle) {
+ return getSessionContextProvider(handle).getSessionContext();
+ }
+
+ /**
+ * Associates the specified ISessionContext with the currently active
+ * workbench window. To remove an ISessionContext association from the
+ * active workbench window, specify <code>null</code> as ctx.
+ *
+ * <p>
+ * After invoking this method you should be able to retrieve the same
+ * ISessionContext through {@link #getSessionContext()}, provided that the
+ * same workbench window has focus at that time.
+ * </p>
+ *
+ * @param ctx the new UI database session context or <code>null</code> to
+ * replace the current UI session with no session.
+ * @return The previous session context if one existed, otherwise
+ * <code>null</code>. If the specified <code>ctx</code> matched the
+ * current session context (<code>null</code> or
+ * <code>non-null</code>), null is also returned and nothing is
+ * done.
+ */
+ public static synchronized ISessionContext setSessionContext(ISessionContext ctx) {
+ return Simantics.getSessionContextProvider().setSessionContext(ctx);
+ }
+
+ /**
+ * Associates the specified ISessionContext with the specified handle
+ * object.
+ *
+ * <p>
+ * Currently IWorkbenchWindow's are used as handles. This implies
+ * that each workbench window can only have one active ISessionContext bound
+ * to it. After invoking this method with a valid workbench window handle
+ * you should be able to retrieve the same ISessionContext through
+ * {@link #getSessionContext(Object)} with the same workbench window
+ * specified as the handle.
+ * </p>
+ *
+ * @param handle the handle to associate the specified ISessionContext with.
+ * @param ctx the new UI database session context or <code>null</code> to
+ * replace the current UI session with no session.
+ * @return The previous session context if one existed, otherwise
+ * <code>null</code>. If the specified <code>ctx</code> matched the
+ * current session context (<code>null</code> or
+ * <code>non-null</code>), null is also returned and nothing is
+ * done.
+ */
+ public static synchronized ISessionContext setSessionContext(Object handle, ISessionContext ctx) {
+ ISessionContextProvider provider = getProviderSource().get(handle);
+ if (provider != null)
+ return provider.setSessionContext(ctx);
+ return null;
+ }
+
+ /**
+ * @return the currently open and active project for the specified database
+ * session or <code>null</code> if there is no current project
+ */
+ @Deprecated
+ public static IProject peekProject(ISessionContext ctx) {
+ if (ctx == null)
+ return null;
+ return ctx.getHint(ProjectKeys.KEY_PROJECT);
+ }
+
+ /**
+ * TODO: refactor this out of here
+ *
+ * @param imageFilePath
+ * @return
+ */
+ public static ImageDescriptor getImageDescriptor(String imageFilePath) {
+ return BundleUtils.getImageDescriptorFromPlugin(PLUGIN_ID, imageFilePath);
+ }
+
+ /**
+ * TODO: [Tuukka] I'm really unsure this belongs here.
+ *
+ * @param <T>
+ * @param sel
+ * @param assignableFrom
+ * @return
+ */
+ public static <T> T filterSingleSelection(ISelection sel, Class<T> assignableFrom) {
+
+ T result = ISelectionUtils.filterSingleSelection(sel, assignableFrom);
+ if (result != null)
+ return result;
+
+ Resource resource = ISelectionUtils.filterSingleSelection(sel, Resource.class);
+ if(resource == null) return null;
+
+ try {
+ return Simantics.getSession().syncRequest(new Adapter<T>(resource, assignableFrom));
+ } catch (DatabaseException e) {
+ Logger.defaultLogError(e);
+ return null;
+ }
+
+ }
+
+ public static <T> T filterSingleWorkbenchSelection(Class<T> assignableFrom) {
+ return filterSingleSelection(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(), assignableFrom);
+ }
+
+
+ public static void asyncExecSWT(final Widget widget, final Runnable runnable) {
+ SWTUtils.asyncExec(widget, delayedExecSWT(null, widget, runnable));
+ }
+
+ public static void asyncExecSWT(final Display display, final Runnable runnable) {
+ SWTUtils.asyncExec(display, delayedExecSWT(display, null, runnable));
+ }
+
+ private static Runnable delayedExecSWT(final Display display, final Widget widget, final Runnable runnable) {
+ if (display == null && widget == null)
+ throw new IllegalArgumentException("both display and widget are null");
+
+ return new Runnable() {
+ @Override
+ public void run() {
+ if (display != null && display.isDisposed())
+ return;
+ if (widget != null && widget.isDisposed())
+ return;
+ if (DatabaseJob.inProgress()) {
+ Display d = display != null ? display : widget.getDisplay();
+ d.timerExec(50, this);
+ return;
+ }
+ runnable.run();
+ }
+ };
+ }
+
+ private static long parseLongProperty(String propertyName, long defaultValue) {
+ String value = System.getProperty(propertyName, null);
+ try {
+ return value != null ? Long.parseLong(value) : defaultValue;
+ } catch (NumberFormatException e) {
+ return defaultValue;
+ }
+ }
+
+ public static boolean isLinuxGTK() {
+ String ws = System.getProperty("osgi.ws");
+ return ws != null && "gtk".equals(ws);
+ }
+
+}