ide/debugger/src/main/java/org/apidesign/bck2brwsr/debugger/DebugBck2Brwsr.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 30 Mar 2013 07:55:27 +0100
branchdebugger
changeset 968 615c4158fbbb
permissions -rw-r--r--
Initial attempt to start and connect to NetBeans JavaScript debugger
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.debugger;
    19 
    20 import java.awt.event.ActionEvent;
    21 import java.awt.event.ActionListener;
    22 import java.net.MalformedURLException;
    23 import java.net.URL;
    24 import org.netbeans.api.debugger.Session;
    25 import org.netbeans.api.project.Project;
    26 import org.netbeans.modules.web.browser.api.BrowserSupport;
    27 import org.netbeans.modules.web.browser.api.WebBrowser;
    28 import org.netbeans.modules.web.browser.api.WebBrowsers;
    29 import org.netbeans.modules.web.webkit.debugging.api.WebKitDebugging;
    30 import org.netbeans.modules.web.webkit.debugging.spi.netbeansdebugger.NetBeansJavaScriptDebuggerFactory;
    31 import org.openide.awt.ActionID;
    32 import org.openide.awt.ActionReference;
    33 import org.openide.awt.ActionRegistration;
    34 import org.openide.awt.HtmlBrowser;
    35 import org.openide.util.Exceptions;
    36 import org.openide.util.Lookup;
    37 
    38 /**
    39  *
    40  * @author Jaroslav Tulach <jtulach@netbeans.org>
    41  */
    42 @ActionID(category = "Debug", id = "org.apidesign.bck2brwsr.Debug")
    43 @ActionRegistration(displayName = "Bck2brwsr debugger", asynchronous = true)
    44 @ActionReference(path = "Menu/DebugProject", position = 33333)
    45 public final class DebugBck2Brwsr implements ActionListener {
    46     private final Project project;
    47     
    48     public DebugBck2Brwsr(Project p) {
    49         this.project = p;
    50     }
    51     
    52 
    53     @Override
    54     public void actionPerformed(ActionEvent e) {
    55         try {
    56             URL url = new URL("http://localhost:8383/HTML5Application/index.html");
    57             BrowserSupport.getDefaultEmbedded().load(url, project.getProjectDirectory());
    58             /*
    59             for (WebBrowser web : WebBrowsers.getInstance().getAll(true)) {
    60             HtmlBrowser.Impl impl = web.getHtmlBrowserFactory().createHtmlBrowserImpl();
    61             WebKitDebugging wkd = impl.getLookup().lookup(WebKitDebugging.class);
    62             if (wkd != null) {
    63             impl.setURL(url);
    64             Session session = startSession(wkd, project.getLookup());
    65             System.err.println("debugging : " + session.getCurrentLanguage());
    66             return;
    67             }
    68             //            Collection<? extends Object> res = pane.getLookup().lookupAll(Object.class);
    69             System.err.println("res: " + wkd);
    70             }
    71              */
    72         } catch (MalformedURLException ex) {
    73             Exceptions.printStackTrace(ex);
    74         }
    75     }
    76 
    77     private static Session startSession(WebKitDebugging wkd, Lookup context) {
    78         final String sevenThree = "org.netbeans.modules.web.webkit.debugging.spi.netbeansdebugger.NetBeansJavaScriptDebuggerFactory";
    79         final String sevenFour = "org.netbeans.modules.web.webkit.debugging.spi.JavaScriptDebuggerFactory";
    80         
    81         Class<?> cls;
    82         try {
    83             cls = Class.forName(sevenFour);
    84         } catch (ClassNotFoundException ex1) {
    85             try {
    86                 cls = Class.forName(sevenThree);
    87             } catch (ClassNotFoundException ex) {
    88                 throw new IllegalStateException("Can't start debug session", ex);
    89             }
    90         }
    91         
    92         Object inst = Lookup.getDefault().lookup(cls);
    93 
    94         try {
    95             java.lang.reflect.Method m = cls.getMethod("createDebuggingSession", WebKitDebugging.class, Lookup.class);
    96             return (Session)m.invoke(inst, wkd, context);
    97         } catch (Exception ex) {
    98             throw new IllegalStateException(ex);
    99         }
   100     }
   101 }