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