// Path: node-projects/src/main/java/org/netbeans/modules/nodejs/api/NodeJSExecutable.java
// @Messages("UNKNOWN_VERSION=(unknown version)")
// public abstract class NodeJSExecutable {
// 
//     public String name () {
//         return getClass().getSimpleName();
//     }
//     
//     public String displayName() {
//         return name();
//     }
// 
//     public boolean isValid () {
//         return true;
//     }
//     
//     public String version() {
//         return Bundle.UNKNOWN_VERSION();
//     }
//     
//     public abstract String path();
//     
//     public ExecutionDescriptor.LineConvertorFactory newLineConverter() {
//         return new LineConverter(this.getSourcesLocation());
//     }
// 
//     public static NodeJSExecutable getDefault () {
//         NodeJSExecutable exe = Lookup.getDefault().lookup( NodeJSExecutable.class );
//         if (exe == null) {
//             exe = new NodeJSExecutable.DummyExectable();
//         }
//         return exe;
//     }
// 
//     public final Future<Integer>  run ( FileObject targetFile, String args ) throws IOException {
//         if (!targetFile.isValid() || !targetFile.isData()) {
//             StatusDisplayer.getDefault().setStatusText( NbBundle.getMessage(
//                     NodeJSExecutable.class, "MSG_CANNOT_RUN", targetFile.getPath() ) );
//             Toolkit.getDefaultToolkit().beep();
//         }
//         assert !EventQueue.isDispatchThread();
//         LifecycleManager.getDefault().saveAll();
//         return doRun( targetFile, args );
//     }
// 
//     protected abstract Future<Integer> doRun ( FileObject file, String args ) throws IOException;
// 
//     public abstract String getSourcesLocation ();
// 
//     public abstract void stopRunningProcesses ( Lookup.Provider owner );
// 
//     static final class DummyExectable extends NodeJSExecutable {
//         @Override
//         protected Future<Integer> doRun ( FileObject file, String args ) throws IOException {
//             return new Future<Integer>() {
//                 @Override
//                 public boolean cancel ( boolean bln ) {
//                     return false;
//                 }
// 
//                 @Override
//                 public boolean isCancelled () {
//                     return false;
//                 }
// 
//                 @Override
//                 public boolean isDone () {
//                     return true;
//                 }
// 
//                 @Override
//                 public Integer get () throws InterruptedException, ExecutionException {
//                     return 1;
//                 }
// 
//                 @Override
//                 public Integer get ( long l, TimeUnit tu ) throws InterruptedException, ExecutionException, TimeoutException {
//                     return 1;
//                 }
//             };
//         }
// 
//         @Override
//         public String getSourcesLocation () {
//             return null;
//         }
// 
//         @Override
//         public void stopRunningProcesses ( Lookup.Provider owner ) {
// 
//         }
// 
//         @Override
//         public String path () {
//             return "";
//         }
//     }
// }
// 
// Path: node-projects/src/main/java/org/netbeans/modules/nodejs/api/NodeJSPlatformProvider.java
// public abstract class NodeJSPlatformProvider {
//     public abstract NodeJSExecutable get ();
// 
//     public static NodeJSExecutable get ( Project project ) {
//         NodeJSPlatformProvider prov = project == null ? null : project.getLookup().lookup( NodeJSPlatformProvider.class );
//         if (prov == null) {
//             prov = DEFAULT;
//         }
//         return prov.get();
//     }
// 
//     private static NodeJSPlatformProvider DEFAULT = new NodeJSPlatformProvider() {
// 
//         @Override
//         public NodeJSExecutable get () {
//             return NodeJSExecutable.getDefault();
//         }
// 
//     };
// }


import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.filesystems.FileObject;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle.Messages;
import org.openide.util.RequestProcessor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import org.netbeans.api.actions.Savable;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.Project;
import org.netbeans.modules.nodejs.api.NodeJSExecutable;
import org.netbeans.modules.nodejs.api.NodeJSPlatformProvider;
import org.openide.loaders.DataObject;
import org.openide.LifecycleManager;
import org.openide.awt.ActionRegistration;
/* Copyright (C) 2012 Tim Boudreau

 Permission is hereby granted, free of charge, to any person obtaining a copy 
 of this software and associated documentation files (the "Software"), to 
 deal in the Software without restriction, including without limitation the 
 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 sell copies of the Software, and to permit persons to whom the Software is 
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all 
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
package org.netbeans.modules.nodejs.actions;



@ActionID (category = "Tools",
id = "org.netbeans.modules.nodejs.actions.RunAction")
@ActionRegistration (displayName = "#CTL_RunAction")
@ActionReferences ({
    @ActionReference (path = "Loaders/text/javascript/Actions", position = 250, separatorBefore = 225, separatorAfter = 275),
    @ActionReference (path = "Editors/text/javascript/Popup", position = 4950, separatorBefore = 4925, separatorAfter = 4975)
})
@Messages ("CTL_RunAction=Run With Node")
public final class RunAction implements ActionListener, Runnable {
    private final DataObject context;

    public RunAction ( DataObject context ) {
        this.context = context;
    }

    public void actionPerformed ( ActionEvent ev ) {
        RequestProcessor.getDefault().post( this );
    }

    @Override
    public void run () {
        try {
            LifecycleManager.getDefault().saveAll();
            FileObject fo = context.getPrimaryFile();
            Project p = FileOwnerQuery.getOwner( fo );
            NodeJSExecutable exe = null;
            if (p != null) {
// Path: node-projects/src/main/java/org/netbeans/modules/nodejs/api/NodeJSExecutable.java
// @Messages("UNKNOWN_VERSION=(unknown version)")
// public abstract class NodeJSExecutable {
// 
//     public String name () {
//         return getClass().getSimpleName();
//     }
//     
//     public String displayName() {
//         return name();
//     }
// 
//     public boolean isValid () {
//         return true;
//     }
//     
//     public String version() {
//         return Bundle.UNKNOWN_VERSION();
//     }
//     
//     public abstract String path();
//     
//     public ExecutionDescriptor.LineConvertorFactory newLineConverter() {
//         return new LineConverter(this.getSourcesLocation());
//     }
// 
//     public static NodeJSExecutable getDefault () {
//         NodeJSExecutable exe = Lookup.getDefault().lookup( NodeJSExecutable.class );
//         if (exe == null) {
//             exe = new NodeJSExecutable.DummyExectable();
//         }
//         return exe;
//     }
// 
//     public final Future<Integer>  run ( FileObject targetFile, String args ) throws IOException {
//         if (!targetFile.isValid() || !targetFile.isData()) {
//             StatusDisplayer.getDefault().setStatusText( NbBundle.getMessage(
//                     NodeJSExecutable.class, "MSG_CANNOT_RUN", targetFile.getPath() ) );
//             Toolkit.getDefaultToolkit().beep();
//         }
//         assert !EventQueue.isDispatchThread();
//         LifecycleManager.getDefault().saveAll();
//         return doRun( targetFile, args );
//     }
// 
//     protected abstract Future<Integer> doRun ( FileObject file, String args ) throws IOException;
// 
//     public abstract String getSourcesLocation ();
// 
//     public abstract void stopRunningProcesses ( Lookup.Provider owner );
// 
//     static final class DummyExectable extends NodeJSExecutable {
//         @Override
//         protected Future<Integer> doRun ( FileObject file, String args ) throws IOException {
//             return new Future<Integer>() {
//                 @Override
//                 public boolean cancel ( boolean bln ) {
//                     return false;
//                 }
// 
//                 @Override
//                 public boolean isCancelled () {
//                     return false;
//                 }
// 
//                 @Override
//                 public boolean isDone () {
//                     return true;
//                 }
// 
//                 @Override
//                 public Integer get () throws InterruptedException, ExecutionException {
//                     return 1;
//                 }
// 
//                 @Override
//                 public Integer get ( long l, TimeUnit tu ) throws InterruptedException, ExecutionException, TimeoutException {
//                     return 1;
//                 }
//             };
//         }
// 
//         @Override
//         public String getSourcesLocation () {
//             return null;
//         }
// 
//         @Override
//         public void stopRunningProcesses ( Lookup.Provider owner ) {
// 
//         }
// 
//         @Override
//         public String path () {
//             return "";
//         }
//     }
// }
// 
// Path: node-projects/src/main/java/org/netbeans/modules/nodejs/api/NodeJSPlatformProvider.java
// public abstract class NodeJSPlatformProvider {
//     public abstract NodeJSExecutable get ();
// 
//     public static NodeJSExecutable get ( Project project ) {
//         NodeJSPlatformProvider prov = project == null ? null : project.getLookup().lookup( NodeJSPlatformProvider.class );
//         if (prov == null) {
//             prov = DEFAULT;
//         }
//         return prov.get();
//     }
// 
//     private static NodeJSPlatformProvider DEFAULT = new NodeJSPlatformProvider() {
// 
//         @Override
//         public NodeJSExecutable get () {
//             return NodeJSExecutable.getDefault();
//         }
// 
//     };
// }


// Path: node-projects/src/main/java/org/netbeans/modules/nodejs/actions/RunAction.java
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.filesystems.FileObject;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle.Messages;
import org.openide.util.RequestProcessor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import org.netbeans.api.actions.Savable;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.Project;
import org.netbeans.modules.nodejs.api.NodeJSExecutable;
import org.netbeans.modules.nodejs.api.NodeJSPlatformProvider;
import org.openide.loaders.DataObject;
import org.openide.LifecycleManager;
import org.openide.awt.ActionRegistration;

/* Copyright (C) 2012 Tim Boudreau

 Permission is hereby granted, free of charge, to any person obtaining a copy 
 of this software and associated documentation files (the "Software"), to 
 deal in the Software without restriction, including without limitation the 
 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 sell copies of the Software, and to permit persons to whom the Software is 
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all 
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
package org.netbeans.modules.nodejs.actions;



@ActionID (category = "Tools",
id = "org.netbeans.modules.nodejs.actions.RunAction")
@ActionRegistration (displayName = "#CTL_RunAction")
@ActionReferences ({
    @ActionReference (path = "Loaders/text/javascript/Actions", position = 250, separatorBefore = 225, separatorAfter = 275),
    @ActionReference (path = "Editors/text/javascript/Popup", position = 4950, separatorBefore = 4925, separatorAfter = 4975)
})
@Messages ("CTL_RunAction=Run With Node")
public final class RunAction implements ActionListener, Runnable {
    private final DataObject context;

    public RunAction ( DataObject context ) {
        this.context = context;
    }

    public void actionPerformed ( ActionEvent ev ) {
        RequestProcessor.getDefault().post( this );
    }

    @Override
    public void run () {
        try {
            LifecycleManager.getDefault().saveAll();
            FileObject fo = context.getPrimaryFile();
            Project p = FileOwnerQuery.getOwner( fo );
            NodeJSExecutable exe = null;
            if (p != null) {
                NodeJSPlatformProvider pp = p.getLookup().lookup( NodeJSPlatformProvider.class );