]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2016 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
8  *\r
9  * Contributors:\r
10  *     THTH ry - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.debug.browser.ui;\r
13 \r
14 import java.net.URL;\r
15 \r
16 import javax.inject.Named;\r
17 \r
18 import org.eclipse.core.runtime.IStatus;\r
19 import org.eclipse.core.runtime.Platform;\r
20 import org.eclipse.core.runtime.Status;\r
21 import org.eclipse.e4.core.di.annotations.CanExecute;\r
22 import org.eclipse.e4.core.di.annotations.Execute;\r
23 import org.eclipse.e4.ui.services.IServiceConstants;\r
24 import org.eclipse.jface.dialogs.ErrorDialog;\r
25 import org.eclipse.jface.viewers.ISelection;\r
26 import org.eclipse.swt.widgets.Shell;\r
27 import org.eclipse.ui.PlatformUI;\r
28 import org.eclipse.ui.browser.IWebBrowser;\r
29 import org.eclipse.ui.browser.IWorkbenchBrowserSupport;\r
30 import org.simantics.db.Resource;\r
31 import org.simantics.db.layer0.SelectionHints;\r
32 import org.simantics.debug.browser.internal.DebugBrowserServer;\r
33 import org.simantics.utils.ui.ISelectionUtils;\r
34 \r
35 /**\r
36  * @author Jani Simomaa / Semantum Oy\r
37  * @author Tuukka Lehtonen / Semantum Oy\r
38  * @since 1.22\r
39  */\r
40 public class LaunchDebugBrowser {\r
41 \r
42     private static int browserCounter = 0;\r
43 \r
44     @CanExecute\r
45     public boolean canExecute() {\r
46         return Platform.inDevelopmentMode();\r
47     }\r
48 \r
49     @Execute\r
50     public void execute(\r
51             @Named(IServiceConstants.ACTIVE_SHELL) Shell activeShell,\r
52             @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection)\r
53     {\r
54         try {\r
55             // Start Jetty server for graph debugging\r
56             DebugBrowserServer server = org.simantics.debug.browser.internal.Activator.getDefault().startDebugServer();\r
57             URL url = server.getURL();\r
58 \r
59             // Get default input from current workbench selection\r
60             Resource input = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MAIN, Resource.class, true);\r
61             URL inputUrl = input != null ? new URL(url.toExternalForm() + "/" + input.getResourceId()) : url;\r
62 \r
63             // Open browser\r
64             IWorkbenchBrowserSupport bs = PlatformUI.getWorkbench().getBrowserSupport();\r
65             if (bs.isInternalWebBrowserAvailable()) {\r
66                 IWebBrowser browser = bs.createBrowser(\r
67                         IWorkbenchBrowserSupport.AS_EDITOR\r
68                         | IWorkbenchBrowserSupport.NAVIGATION_BAR\r
69                         | IWorkbenchBrowserSupport.LOCATION_BAR\r
70                         , "graph.debug.browser." + (browserCounter++),\r
71                         "Database Debug Browser",\r
72                         url.toString());\r
73                 browser.openURL(inputUrl);\r
74             } else {\r
75                 bs.getExternalBrowser().openURL(inputUrl);\r
76             }\r
77         } catch (Exception e) {\r
78             Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to open external browser.", e));\r
79             ErrorDialog.openError(activeShell,\r
80                     "Failed to Open External Browser",\r
81                     "Failed to open debug browser in the default external web browser. See exception for details.",\r
82                     new Status(Status.ERROR, Activator.PLUGIN_ID, "", e));\r
83         }\r
84     }\r
85 \r
86 }\r