Search in sources :

Example 1 with AsynchronousProgressMonitorDialog

use of org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog in project Pydev by fabioz.

the class PySelectInterpreter method setInterpreterInfosWithProgressDialog.

public void setInterpreterInfosWithProgressDialog(IInterpreterManager interpreterManager, final IInterpreterInfo[] interpreterInfos, Shell shell) {
    // this is the default interpreter
    ProgressMonitorDialog monitorDialog = new AsynchronousProgressMonitorDialog(shell);
    monitorDialog.setBlockOnOpen(false);
    try {
        IRunnableWithProgress operation = new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                monitor.beginTask("Restoring PYTHONPATH", IProgressMonitor.UNKNOWN);
                try {
                    // i.e.: don't restore the PYTHONPATH (only order was changed).
                    Set<String> interpreterNamesToRestore = new HashSet<>();
                    interpreterManager.setInfos(interpreterInfos, interpreterNamesToRestore, monitor);
                } finally {
                    monitor.done();
                }
            }
        };
        monitorDialog.run(true, true, operation);
    } catch (Exception e) {
        Log.log(e);
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) AsynchronousProgressMonitorDialog(org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) AsynchronousProgressMonitorDialog(org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) HashSet(java.util.HashSet)

Example 2 with AsynchronousProgressMonitorDialog

use of org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog in project Pydev by fabioz.

the class PyShowHierarchy method perform.

@Override
protected String perform(IAction action, IProgressMonitor monitor) throws Exception {
    try {
        final PyHierarchyView view;
        IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        IWorkbenchPage page = workbenchWindow.getActivePage();
        view = (PyHierarchyView) page.showView("com.python.pydev.ui.hierarchy.PyHierarchyView", null, IWorkbenchPage.VIEW_VISIBLE);
        ProgressMonitorDialog monitorDialog = new AsynchronousProgressMonitorDialog(EditorUtils.getShell());
        try {
            IRunnableWithProgress operation = new IRunnableWithProgress() {

                @Override
                public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    try {
                        final HierarchyNodeModel model;
                        // set whatever is needed for the hierarchy
                        IPyRefactoring pyRefactoring = AbstractPyRefactoring.getPyRefactoring();
                        if (pyRefactoring instanceof IPyRefactoring2) {
                            RefactoringRequest refactoringRequest = getRefactoringRequest(monitor);
                            IPyRefactoring2 r2 = (IPyRefactoring2) pyRefactoring;
                            model = r2.findClassHierarchy(refactoringRequest, false);
                            if (monitor.isCanceled()) {
                                return;
                            }
                            Runnable r = new Runnable() {

                                @Override
                                public void run() {
                                    if (!monitor.isCanceled()) {
                                        view.setHierarchy(model);
                                    }
                                }
                            };
                            Display.getDefault().asyncExec(r);
                        }
                    } catch (Exception e) {
                        Log.log(e);
                    }
                }
            };
            boolean fork = true;
            monitorDialog.run(fork, true, operation);
        } catch (Throwable e) {
            Log.log(e);
        }
    } catch (Exception e) {
        Log.log(e);
    }
    return "";
}
Also used : PyHierarchyView(com.python.pydev.ui.hierarchy.PyHierarchyView) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) AsynchronousProgressMonitorDialog(org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog) RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) AsynchronousProgressMonitorDialog(org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog) IPyRefactoring(org.python.pydev.ast.refactoring.IPyRefactoring) HierarchyNodeModel(org.python.pydev.ast.refactoring.HierarchyNodeModel) IPyRefactoring2(org.python.pydev.ast.refactoring.IPyRefactoring2) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage)

Example 3 with AsynchronousProgressMonitorDialog

use of org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog in project Pydev by fabioz.

the class AbstractInterpreterEditor method restoreInterpreterInfos.

public void restoreInterpreterInfos(boolean editorChanged, Shell shell, IInterpreterManager iInterpreterManager) {
    final Set<String> interpreterNamesToRestore = this.getInterpreterExeOrJarToRestoreAndClear();
    final IInterpreterInfo[] exesList = this.getExesList();
    if (!editorChanged && interpreterNamesToRestore.size() == 0) {
        IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        SelectionDialog listDialog = AbstractInterpreterPreferencesPage.createChooseIntepreterInfoDialog(workbenchWindow, exesList, "Select interpreters to be restored", true);
        int open = listDialog.open();
        if (open != ListDialog.OK) {
            return;
        }
        Object[] result = listDialog.getResult();
        if (result == null || result.length == 0) {
            return;
        }
        for (Object o : result) {
            interpreterNamesToRestore.add(((IInterpreterInfo) o).getExecutableOrJar());
        }
    }
    // this is the default interpreter
    ProgressMonitorDialog monitorDialog = new AsynchronousProgressMonitorDialog(shell);
    monitorDialog.setBlockOnOpen(false);
    try {
        IRunnableWithProgress operation = new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                monitor.beginTask("Restoring PYTHONPATH", IProgressMonitor.UNKNOWN);
                try {
                    pushExpectedSetInfos();
                    // clear all but the ones that appear
                    iInterpreterManager.setInfos(exesList, interpreterNamesToRestore, monitor);
                } finally {
                    popExpectedSetInfos();
                    monitor.done();
                }
            }
        };
        monitorDialog.run(true, true, operation);
    } catch (Exception e) {
        Log.log(e);
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) AsynchronousProgressMonitorDialog(org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog) AsynchronousProgressMonitorDialog(org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) SelectionDialog(org.eclipse.ui.dialogs.SelectionDialog) InvocationTargetException(java.lang.reflect.InvocationTargetException) CancelException(org.python.pydev.shared_core.progress.CancelException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo)

Example 4 with AsynchronousProgressMonitorDialog

use of org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog in project Pydev by fabioz.

the class AbstractInterpreterEditor method doFillIntoGrid.

/**
 * @see org.eclipse.jface.preference.ListEditor#doFillIntoGrid(org.eclipse.swt.widgets.Composite, int)
 */
@Override
protected void doFillIntoGrid(Composite parent, int numColumns) {
    super.doFillIntoGrid(parent, numColumns);
    GridData gd = new GridData();
    tabFolder = new TabFolder(parent, SWT.None);
    gd = new GridData();
    gd.horizontalAlignment = SWT.FILL;
    gd.verticalAlignment = SWT.FILL;
    gd.grabExcessVerticalSpace = true;
    gd.horizontalSpan = numColumns;
    tabFolder.setLayoutData(gd);
    try {
        if (this.getShowPackageTab()) {
            packageTab.createPackageControlTab(tabFolder, exeOrJarOfInterpretersToRestore, interpreterManager);
        }
    } catch (Exception e1) {
        // Not really expected, just new code, so, let's protect until it matures.
        Log.log(e1);
    }
    createTreeLibsControlTab();
    // ----------------------- FORCED BUILTINS
    forcedBuiltins = new AbstractListWithNewRemoveControl(this) {

        @Override
        protected List<String> getStringsFromInfo(InterpreterInfo info) {
            ArrayList<String> ret = new ArrayList<String>();
            for (Iterator<String> iter = info.forcedLibsIterator(); iter.hasNext(); ) {
                ret.add(iter.next());
            }
            return ret;
        }

        @Override
        protected void removeSelectedFrominfo(InterpreterInfo info, String[] builtins) {
            for (String builtin : builtins) {
                info.removeForcedLib(builtin);
            }
            exeOrJarOfInterpretersWithBuiltinsChanged.add(info.getExecutableOrJar());
        }

        @Override
        protected String getInput() {
            IInputValidator validator = new IInputValidator() {

                @Override
                public String isValid(String newText) {
                    for (char c : newText.toCharArray()) {
                        if (!Character.isJavaIdentifierPart(c) && c != ' ' && c != ',' && c != '.') {
                            return "Can only accept valid python module names (char: '" + c + "' not accepted)";
                        }
                    }
                    return null;
                }
            };
            InputDialog d = new InputDialog(getShell(), "Builtin to add", "Builtin to add (comma separated)", "", validator);
            int retCode = d.open();
            String builtins = null;
            if (retCode == InputDialog.OK) {
                builtins = d.getValue();
            }
            return builtins;
        }

        @Override
        protected void addInputToInfo(InterpreterInfo info, String builtins) {
            java.util.List<String> split = StringUtils.splitAndRemoveEmptyTrimmed(builtins, ',');
            for (String string : split) {
                String trimmed = string.trim();
                if (trimmed.length() > 0) {
                    info.addForcedLib(trimmed);
                }
            }
            exeOrJarOfInterpretersWithBuiltinsChanged.add(info.getExecutableOrJar());
        }
    };
    forcedBuiltins.createTab("Forced Builtins", "Forced Builtins (check <a>Manual</a> for more info).");
    // ----------------------- PREDEFINED COMPLETIONS
    predefinedCompletions = new AbstractListWithNewRemoveControl(this) {

        private Button addAPIBt;

        @Override
        protected List<String> getStringsFromInfo(InterpreterInfo info) {
            return info.getPredefinedCompletionsPath();
        }

        @Override
        protected void removeSelectedFrominfo(InterpreterInfo info, String[] items) {
            for (String item : items) {
                info.removePredefinedCompletionPath(item);
            }
            exeOrJarOfInterpretersWithPredefinedChanged.add(info.getExecutableOrJar());
        }

        @Override
        protected String getInput() {
            DirectoryDialog dialog = new DirectoryDialog(getShell());
            dialog.setFilterPath(lastDirectoryDialogPath);
            String filePath = dialog.open();
            if (filePath != null) {
                lastDirectoryDialogPath = filePath;
            }
            return filePath;
        }

        @Override
        protected void addInputToInfo(InterpreterInfo info, String item) {
            info.addPredefinedCompletionsPath(item);
            exeOrJarOfInterpretersWithPredefinedChanged.add(info.getExecutableOrJar());
        }

        @Override
        protected void createButtons(AbstractInterpreterEditor interpreterEditor) {
            super.createButtons(interpreterEditor);
            // $NON-NLS-1$
            addAPIBt = interpreterEditor.createBt(box, "Add from QScintilla api file", this);
        }

        @Override
        public void widgetDisposed(DisposeEvent event) {
            super.widgetDisposed(event);
            if (addAPIBt != null) {
                addAPIBt.dispose();
                addAPIBt = null;
            }
        }

        @Override
        public void widgetSelected(SelectionEvent event) {
            super.widgetSelected(event);
            Widget widget = event.widget;
            if (widget == addAPIBt) {
                addAPIBt();
            }
        }

        private void addAPIBt() {
            final AbstractInterpreterEditor interpreterEditor = this.container.get();
            Assert.isNotNull(interpreterEditor);
            final InterpreterInfo info = interpreterEditor.getSelectedInfo();
            if (info != null) {
                FileDialog dialog = new FileDialog(getShell(), SWT.PRIMARY_MODAL | SWT.MULTI);
                dialog.setFilterExtensions(new String[] { "*.api" });
                dialog.setText("Select .api file to be converted to .pypredef.");
                dialog.setFilterPath(lastFileDialogPath);
                final String filePath = dialog.open();
                if (filePath != null) {
                    lastFileDialogPath = filePath;
                    File filePath1 = new File(filePath);
                    final String dir = filePath1.getParent();
                    IInputValidator validator = new IInputValidator() {

                        @Override
                        public String isValid(String newText) {
                            if (newText.length() == 0) {
                                return "Number not provided.";
                            }
                            try {
                                Integer.parseInt(newText);
                            } catch (NumberFormatException e) {
                                return "The string: " + newText + " is not a valid integer.";
                            }
                            return null;
                        }
                    };
                    final InputDialog d = new InputDialog(getShell(), "Number of tokens to consider module", "Please specify the number of tokens to consider a module from the .api file\n\n" + "i.e.: if there's a PyQt4.QtCore.QObject and PyQt4.QtCore is a module and QtObject " + "is the first class, the number of tokens to consider a module would be 2 (one for " + "PyQt4 and another for QtCore).", "", validator);
                    int retCode = d.open();
                    final ByteArrayOutputStream output = new ByteArrayOutputStream();
                    if (retCode == InputDialog.OK) {
                        ProgressMonitorDialog monitorDialog = new AsynchronousProgressMonitorDialog(getShell());
                        monitorDialog.setBlockOnOpen(false);
                        final Exception[] exception = new Exception[1];
                        try {
                            IRunnableWithProgress operation = new IRunnableWithProgress() {

                                @Override
                                public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                                    monitor.beginTask("Restoring PYTHONPATH", IProgressMonitor.UNKNOWN);
                                    IPythonInterpreter interpreter = JythonPlugin.newPythonInterpreter(false, false);
                                    interpreter.setErr(output);
                                    interpreter.setOut(output);
                                    HashMap<String, Object> locals = new HashMap<String, Object>();
                                    locals.put("api_file", filePath);
                                    locals.put("parts_for_module", d.getValue());
                                    locals.put("cancel_monitor", monitor);
                                    try {
                                        JythonPlugin.exec(locals, "convert_api_to_pypredef.py", interpreter);
                                    } catch (Exception e) {
                                        Log.log(e + "\n\n" + output.toString());
                                        exception[0] = e;
                                    }
                                    monitor.done();
                                }
                            };
                            monitorDialog.run(true, true, operation);
                        } catch (Exception e) {
                            Log.log(e);
                        }
                        Exception e = exception[0];
                        String contents = output.toString();
                        if (e == null && contents.indexOf("SUCCESS") != -1) {
                            addInputToInfo(info, dir);
                            interpreterEditor.updateTree();
                        } else {
                            if (e != null) {
                                MessageDialog.openError(getShell(), "Error creating .pypredef files", e.getMessage() + "\n\n" + contents);
                            } else {
                                MessageDialog.openError(getShell(), "Error creating .pypredef files", contents);
                            }
                        }
                    }
                }
            }
        }
    };
    predefinedCompletions.createTab("Predefined", "Predefined completions (check <a>Manual</a> for more info).");
    createEnvironmentVariablesTab();
    createStringSubstitutionTab();
}
Also used : AsynchronousProgressMonitorDialog(org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Widget(org.eclipse.swt.widgets.Widget) DisposeEvent(org.eclipse.swt.events.DisposeEvent) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) Button(org.eclipse.swt.widgets.Button) Iterator(java.util.Iterator) SelectionEvent(org.eclipse.swt.events.SelectionEvent) InterpreterInfo(org.python.pydev.ast.interpreter_managers.InterpreterInfo) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) List(java.util.List) ArrayList(java.util.ArrayList) DirectoryDialog(org.eclipse.swt.widgets.DirectoryDialog) InputDialog(org.eclipse.jface.dialogs.InputDialog) InterpreterInputDialog(org.python.pydev.ui.dialogs.InterpreterInputDialog) AsynchronousProgressMonitorDialog(org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) TabFolder(org.eclipse.swt.widgets.TabFolder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) CancelException(org.python.pydev.shared_core.progress.CancelException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IPythonInterpreter(org.python.pydev.shared_core.jython.IPythonInterpreter) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) GridData(org.eclipse.swt.layout.GridData) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File) IInputValidator(org.eclipse.jface.dialogs.IInputValidator)

Example 5 with AsynchronousProgressMonitorDialog

use of org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog in project Pydev by fabioz.

the class PyContainerAction method run.

/**
 * Act on the selection to do the needed action (will confirm and make a refresh before executing)
 */
@Override
public void run(IAction action) {
    // should not happen
    if (selectedContainers == null) {
        return;
    }
    if (!confirmRun()) {
        return;
    }
    beforeRun();
    final Integer[] nChanged = new Integer[] { 0 };
    ProgressMonitorDialog monitorDialog = new AsynchronousProgressMonitorDialog(EditorUtils.getShell());
    try {
        IRunnableWithProgress operation = new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                for (Iterator<IContainer> iter = selectedContainers.iterator(); iter.hasNext(); ) {
                    IContainer next = iter.next();
                    if (getRefreshBeforeExecute()) {
                        // as files are generated externally, if we don't refresh, it's very likely that we won't delete a bunch of files.
                        try {
                            next.refreshLocal(IResource.DEPTH_INFINITE, monitor);
                        } catch (Exception e) {
                            Log.log(e);
                        }
                    }
                    nChanged[0] += doActionOnContainer(next, monitor);
                }
            }
        };
        boolean fork = !needsUIThread();
        monitorDialog.run(fork, true, operation);
    } catch (Throwable e) {
        Log.log(e);
    }
    afterRun(nChanged[0]);
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) AsynchronousProgressMonitorDialog(org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) AsynchronousProgressMonitorDialog(org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog) IContainer(org.eclipse.core.resources.IContainer) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Aggregations

ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)8 AsynchronousProgressMonitorDialog (org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)7 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 File (java.io.File)2 HashSet (java.util.HashSet)2 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)2 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)2 CancelException (org.python.pydev.shared_core.progress.CancelException)2 PyHierarchyView (com.python.pydev.ui.hierarchy.PyHierarchyView)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ZipFile (java.util.zip.ZipFile)1 IContainer (org.eclipse.core.resources.IContainer)1 IResource (org.eclipse.core.resources.IResource)1 IInputValidator (org.eclipse.jface.dialogs.IInputValidator)1