]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.debug.browser.ui/src/org/simantics/debug/browser/ui/LaunchDebugBrowser.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.debug.browser.ui / src / org / simantics / debug / browser / ui / LaunchDebugBrowser.java
diff --git a/bundles/org.simantics.debug.browser.ui/src/org/simantics/debug/browser/ui/LaunchDebugBrowser.java b/bundles/org.simantics.debug.browser.ui/src/org/simantics/debug/browser/ui/LaunchDebugBrowser.java
new file mode 100644 (file)
index 0000000..e704153
--- /dev/null
@@ -0,0 +1,86 @@
+/*******************************************************************************\r
+ * Copyright (c) 2016 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
+ *     THTH ry - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.debug.browser.ui;\r
+\r
+import java.net.URL;\r
+\r
+import javax.inject.Named;\r
+\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.e4.core.di.annotations.CanExecute;\r
+import org.eclipse.e4.core.di.annotations.Execute;\r
+import org.eclipse.e4.ui.services.IServiceConstants;\r
+import org.eclipse.jface.dialogs.ErrorDialog;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.eclipse.ui.browser.IWebBrowser;\r
+import org.eclipse.ui.browser.IWorkbenchBrowserSupport;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.layer0.SelectionHints;\r
+import org.simantics.debug.browser.internal.DebugBrowserServer;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+/**\r
+ * @author Jani Simomaa / Semantum Oy\r
+ * @author Tuukka Lehtonen / Semantum Oy\r
+ * @since 1.22\r
+ */\r
+public class LaunchDebugBrowser {\r
+\r
+    private static int browserCounter = 0;\r
+\r
+    @CanExecute\r
+    public boolean canExecute() {\r
+        return Platform.inDevelopmentMode();\r
+    }\r
+\r
+    @Execute\r
+    public void execute(\r
+            @Named(IServiceConstants.ACTIVE_SHELL) Shell activeShell,\r
+            @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection)\r
+    {\r
+        try {\r
+            // Start Jetty server for graph debugging\r
+            DebugBrowserServer server = org.simantics.debug.browser.internal.Activator.getDefault().startDebugServer();\r
+            URL url = server.getURL();\r
+\r
+            // Get default input from current workbench selection\r
+            Resource input = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MAIN, Resource.class, true);\r
+            URL inputUrl = input != null ? new URL(url.toExternalForm() + "/" + input.getResourceId()) : url;\r
+\r
+            // Open browser\r
+            IWorkbenchBrowserSupport bs = PlatformUI.getWorkbench().getBrowserSupport();\r
+            if (bs.isInternalWebBrowserAvailable()) {\r
+                IWebBrowser browser = bs.createBrowser(\r
+                        IWorkbenchBrowserSupport.AS_EDITOR\r
+                        | IWorkbenchBrowserSupport.NAVIGATION_BAR\r
+                        | IWorkbenchBrowserSupport.LOCATION_BAR\r
+                        , "graph.debug.browser." + (browserCounter++),\r
+                        "Database Debug Browser",\r
+                        url.toString());\r
+                browser.openURL(inputUrl);\r
+            } else {\r
+                bs.getExternalBrowser().openURL(inputUrl);\r
+            }\r
+        } catch (Exception e) {\r
+            Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to open external browser.", e));\r
+            ErrorDialog.openError(activeShell,\r
+                    "Failed to Open External Browser",\r
+                    "Failed to open debug browser in the default external web browser. See exception for details.",\r
+                    new Status(Status.ERROR, Activator.PLUGIN_ID, "", e));\r
+        }\r
+    }\r
+\r
+}\r