Search in sources :

Example 1 with Select1Dialog

use of org.python.pydev.ui.dialogs.Select1Dialog in project Pydev by fabioz.

the class AttachToProcess method doIt.

protected void doIt() throws Exception {
    IProcessList processList = PlatformUtils.getProcessList();
    IProcessInfo[] processList2 = processList.getProcessList();
    TreeNode<Object> root = new TreeNode<Object>(null, null);
    for (IProcessInfo iProcessInfo : processList2) {
        new TreeNode<>(root, iProcessInfo);
    }
    TreeNode<Object> element = new Select1Dialog() {

        @Override
        protected String getInitialFilter() {
            return "*python*";
        }

        @Override
        protected ILabelProvider getLabelProvider() {
            return new TreeNodeLabelProvider() {

                @Override
                public Image getImage(Object element) {
                    return ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.PUBLIC_ATTR_ICON));
                }

                @SuppressWarnings("unchecked")
                @Override
                public String getText(Object element) {
                    if (element == null) {
                        return "null";
                    }
                    TreeNode<Object> node = (TreeNode<Object>) element;
                    Object data = node.data;
                    if (data instanceof IProcessInfo) {
                        IProcessInfo iProcessInfo = (IProcessInfo) data;
                        return iProcessInfo.getPid() + " - " + iProcessInfo.getName();
                    }
                    return "Unexpected: " + data;
                }
            };
        }
    }.selectElement(root);
    if (element != null) {
        IProcessInfo p = (IProcessInfo) element.data;
        int pid = p.getPid();
        if (!PydevRemoteDebuggerServer.isRunning()) {
            // I.e.: the remote debugger server must be on so that we can attach to it.
            PydevRemoteDebuggerServer.startServer();
        }
        // Select interpreter
        IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        IInterpreterManager interpreterManager = InterpreterManagersAPI.getPythonInterpreterManager();
        if (interpreterManager == null) {
            MessageDialog.openError(workbenchWindow.getShell(), "No interpreter manager.", "No interpreter manager was available for attaching to a process.");
        }
        IInterpreterInfo[] interpreters = interpreterManager.getInterpreterInfos();
        if (interpreters == null || interpreters.length == 0) {
            MessageDialog.openError(workbenchWindow.getShell(), "No interpreters for creating console", "An interpreter that matches the architecture of the target process must be configured in the interpreter preferences.");
            return;
        }
        SelectionDialog listDialog = AbstractInterpreterPreferencesPage.createChooseIntepreterInfoDialog(workbenchWindow, interpreters, "Select interpreter which matches the architecture of the target process (i.e.: 32/64 bits).", false);
        int open = listDialog.open();
        if (open != ListDialog.OK || listDialog.getResult().length != 1) {
            return;
        }
        Object[] result = listDialog.getResult();
        IInterpreterInfo interpreter;
        if (result == null || result.length == 0) {
            interpreter = interpreters[0];
        } else {
            interpreter = ((IInterpreterInfo) result[0]);
        }
        SimplePythonRunner runner = new SimplePythonRunner();
        IPath relative = new Path("pysrc").append("pydevd_attach_to_process").append("attach_pydevd.py");
        String script = CorePlugin.getBundleInfo().getRelativePath(relative).getAbsolutePath();
        String[] args = new String[] { "--port", "" + DebugPluginPrefsInitializer.getRemoteDebuggerPort(), "--pid", "" + pid, "--protocol", "http" };
        IPythonNature nature = new SystemPythonNature(interpreterManager, interpreter);
        String[] s = SimplePythonRunner.preparePythonCallParameters(interpreter.getExecutableOrJar(), script, args);
        Tuple<Process, String> run = runner.run(s, (File) null, nature, new NullProgressMonitor());
        if (run.o1 != null) {
            ShowProcessOutputDialog dialog = new ShowProcessOutputDialog(UIUtils.getActiveShell(), run.o1);
            dialog.open();
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IProcessList(org.python.pydev.shared_core.utils.IProcessList) IPythonNature(org.python.pydev.core.IPythonNature) Image(org.eclipse.swt.graphics.Image) SelectionDialog(org.eclipse.ui.dialogs.SelectionDialog) TreeNode(org.python.pydev.shared_core.structure.TreeNode) TreeNodeLabelProvider(org.python.pydev.ui.dialogs.TreeNodeLabelProvider) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IPath(org.eclipse.core.runtime.IPath) Select1Dialog(org.python.pydev.ui.dialogs.Select1Dialog) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) IProcessInfo(org.python.pydev.shared_core.utils.IProcessInfo) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider) IInterpreterManager(org.python.pydev.core.IInterpreterManager) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) SimplePythonRunner(org.python.pydev.ast.runners.SimplePythonRunner)

Aggregations

IPath (org.eclipse.core.runtime.IPath)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 Path (org.eclipse.core.runtime.Path)1 ILabelProvider (org.eclipse.jface.viewers.ILabelProvider)1 Image (org.eclipse.swt.graphics.Image)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1 SelectionDialog (org.eclipse.ui.dialogs.SelectionDialog)1 SimplePythonRunner (org.python.pydev.ast.runners.SimplePythonRunner)1 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)1 IInterpreterManager (org.python.pydev.core.IInterpreterManager)1 IPythonNature (org.python.pydev.core.IPythonNature)1 SystemPythonNature (org.python.pydev.plugin.nature.SystemPythonNature)1 TreeNode (org.python.pydev.shared_core.structure.TreeNode)1 IProcessInfo (org.python.pydev.shared_core.utils.IProcessInfo)1 IProcessList (org.python.pydev.shared_core.utils.IProcessList)1 Select1Dialog (org.python.pydev.ui.dialogs.Select1Dialog)1 TreeNodeLabelProvider (org.python.pydev.ui.dialogs.TreeNodeLabelProvider)1