Search in sources :

Example 11 with TreeNode

use of org.python.pydev.shared_core.structure.TreeNode 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)

Example 12 with TreeNode

use of org.python.pydev.shared_core.structure.TreeNode in project Pydev by fabioz.

the class SyncSystemModulesManager method createInitialSelectionForDialogConsideringPreviouslyIgnored.

public List<TreeNode> createInitialSelectionForDialogConsideringPreviouslyIgnored(DataAndImageTreeNode root, IEclipsePreferences iPreferenceStore) {
    List<TreeNode> initialSelection = new ArrayList<>();
    for (DataAndImageTreeNode<IInterpreterInfo> interpreterNode : (List<DataAndImageTreeNode<IInterpreterInfo>>) root.getChildren()) {
        IInterpreterInfo info = interpreterNode.getData();
        String key = createKeyForInfo(info);
        String ignoredValue = iPreferenceStore.get(key, "");
        if (ignoredValue != null && ignoredValue.length() > 0) {
            Set<String> previouslyIgnored = new HashSet(StringUtils.split(ignoredValue, "|||"));
            boolean added = false;
            for (TreeNode<PythonpathChange> pathNode : interpreterNode.getChildren()) {
                if (!previouslyIgnored.contains(pathNode.data.path)) {
                    initialSelection.add(pathNode);
                    added = true;
                } else {
                    if (SyncSystemModulesManager.DEBUG) {
                        System.out.println("Removed from initial selection: " + pathNode);
                    }
                }
            }
            if (added) {
                initialSelection.add(interpreterNode);
            }
        } else {
            // Node and children all selected initially (nothing ignored).
            initialSelection.add(interpreterNode);
            initialSelection.addAll(interpreterNode.getChildren());
        }
    }
    if (SyncSystemModulesManager.DEBUG) {
        for (TreeNode treeNode : initialSelection) {
            System.out.println("Initial selection: " + treeNode.getData());
        }
    }
    return initialSelection;
}
Also used : IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode) TreeNode(org.python.pydev.shared_core.structure.TreeNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 13 with TreeNode

use of org.python.pydev.shared_core.structure.TreeNode in project Pydev by fabioz.

the class SyncSystemModulesManager method computeChanges.

/**
 * Given the tree structure we created initially with all the changes (root) and the elements
 * that the user selected in the tree (selectElements), return a list of infos updated with the
 * proper pythonpath.
 */
private List<IInterpreterInfo> computeChanges(final DataAndImageTreeNode root, List<TreeNode> selectElements) {
    List<IInterpreterInfo> changedInfos = new ArrayList<>();
    HashSet<TreeNode> set = new HashSet<TreeNode>(selectElements.size());
    set.addAll(selectElements);
    for (Object n : root.getChildren()) {
        DataAndImageTreeNode interpreterNode = (DataAndImageTreeNode) n;
        if (set.contains(interpreterNode)) {
            IInterpreterInfo info = (IInterpreterInfo) interpreterNode.getData();
            List<String> pythonPath = info.getPythonPath();
            boolean changed = false;
            OrderedSet<String> newPythonPath = new OrderedSet<String>(pythonPath);
            for (Object entryNode : interpreterNode.getChildren()) {
                DataAndImageTreeNode pythonpathNode = (DataAndImageTreeNode) entryNode;
                if (set.contains(pythonpathNode)) {
                    PythonpathChange change = (PythonpathChange) pythonpathNode.data;
                    change.apply(newPythonPath);
                    changed = true;
                }
            }
            if (changed) {
                InterpreterInfo copy = (InterpreterInfo) info.makeCopy();
                copy.libs.clear();
                copy.libs.addAll(newPythonPath);
                changedInfos.add(copy);
            }
        }
    }
    return changedInfos;
}
Also used : OrderedSet(org.python.pydev.shared_core.structure.OrderedSet) ArrayList(java.util.ArrayList) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode) TreeNode(org.python.pydev.shared_core.structure.TreeNode) DefaultPathsForInterpreterInfo(org.python.pydev.ast.interpreter_managers.DefaultPathsForInterpreterInfo) InterpreterInfo(org.python.pydev.ast.interpreter_managers.InterpreterInfo) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) HashSet(java.util.HashSet)

Example 14 with TreeNode

use of org.python.pydev.shared_core.structure.TreeNode in project Pydev by fabioz.

the class SyncSystemModulesManager method saveUnselected.

/**
 * When the user selects changes in selectElementsInDialog, it's possible that he doesn't check some of the
 * proposed changes, thus, in this case, we should save the unselected items in the preferences and the next
 * time such a change is proposed, it should appear unchecked (and if all changes are unchecked, we shouldn't
 * present the user with a dialog).
 *
 * @param root this is the initial structure, containing all the proposed changes.
 * @param selectedElements this is a structure which will hold only the selected changes.
 * @param iPreferenceStore this is the store where we'll keep the selected changes.
 */
public void saveUnselected(DataAndImageTreeNode root, List<TreeNode> selectedElements, IEclipsePreferences iPreferenceStore) {
    // root has null data, level 1 has IInterpreterInfo and level 2 has PythonpathChange.
    HashSet<TreeNode> selectionSet = new HashSet<>();
    if (selectedElements != null && selectedElements.size() > 0) {
        selectionSet.addAll(selectedElements);
    }
    boolean changed = false;
    for (DataAndImageTreeNode<IInterpreterInfo> interpreterNode : (List<DataAndImageTreeNode<IInterpreterInfo>>) root.getChildren()) {
        Set<TreeNode> addToIgnore = new HashSet<>();
        if (!selectionSet.contains(interpreterNode)) {
            // ignore all the entries below this interpreter.
            addToIgnore.addAll(interpreterNode.getChildren());
        } else {
            // check each entry and only add the ones not selected.
            for (TreeNode<PythonpathChange> pathNode : interpreterNode.getChildren()) {
                if (!selectionSet.contains(pathNode)) {
                    addToIgnore.add(pathNode);
                }
            }
        }
        if (addToIgnore.size() > 0) {
            IInterpreterInfo info = interpreterNode.getData();
            String key = createKeyForInfo(info);
            ArrayList<String> addToIgnorePaths = new ArrayList<String>(addToIgnore.size());
            for (TreeNode<PythonpathChange> node : addToIgnore) {
                PythonpathChange data = node.getData();
                addToIgnorePaths.add(data.path);
            }
            if (DEBUG) {
                System.out.println("Setting key: " + key);
                System.out.println("Paths ignored: " + addToIgnorePaths);
            }
            changed = true;
            iPreferenceStore.put(key, StringUtils.join("|||", addToIgnorePaths));
        }
    }
    if (changed) {
        try {
            iPreferenceStore.flush();
        } catch (BackingStoreException e) {
            Log.log(e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) BackingStoreException(org.osgi.service.prefs.BackingStoreException) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode) TreeNode(org.python.pydev.shared_core.structure.TreeNode) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 15 with TreeNode

use of org.python.pydev.shared_core.structure.TreeNode in project Pydev by fabioz.

the class SearchIndexLabelProvider method getImage.

/* (non-Javadoc)
     * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
     */
@Override
public Image getImage(Object element) {
    if (element instanceof TreeNode) {
        TreeNode treeNode = (TreeNode) element;
        element = treeNode.data;
    }
    if (element instanceof ICustomLineElement) {
        return fLineMatchImage;
    }
    if (element instanceof ICustomModule) {
        return ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.PY_FILE_ICON));
    }
    if (!(element instanceof IResource)) {
        return null;
    }
    IResource resource = (IResource) element;
    Image image = fLabelProvider.getImage(resource);
    return image;
}
Also used : TreeNode(org.python.pydev.shared_core.structure.TreeNode) Image(org.eclipse.swt.graphics.Image) IResource(org.eclipse.core.resources.IResource)

Aggregations

TreeNode (org.python.pydev.shared_core.structure.TreeNode)23 ArrayList (java.util.ArrayList)9 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)8 DataAndImageTreeNode (org.python.pydev.shared_core.structure.DataAndImageTreeNode)7 HashSet (java.util.HashSet)6 IResource (org.eclipse.core.resources.IResource)5 IAdaptable (org.eclipse.core.runtime.IAdaptable)5 InterpreterInfo (org.python.pydev.ast.interpreter_managers.InterpreterInfo)5 HashMap (java.util.HashMap)4 List (java.util.List)4 IProject (org.eclipse.core.resources.IProject)4 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 SyncSystemModulesManager (org.python.pydev.ast.codecompletion.revisited.SyncSystemModulesManager)4 IInterpreterManager (org.python.pydev.core.IInterpreterManager)4 AdditionalSystemInterpreterInfo (com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo)3 Collection (java.util.Collection)3 Map (java.util.Map)3 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)3 IWrappedResource (org.python.pydev.navigator.elements.IWrappedResource)3