Search in sources :

Example 1 with InputDialogWithLongMessage

use of org.python.pydev.shared_ui.dialogs.InputDialogWithLongMessage in project Pydev by fabioz.

the class Py2To3 method confirmRun.

@Override
protected boolean confirmRun() {
    clearRunInput();
    PythonNature nature = null;
    for (IResource c : selectedResources) {
        PythonNature n2 = PythonNature.getPythonNature(c);
        if (n2 != null) {
            if (nature == null) {
                nature = n2;
            } else {
                if (n2 != nature) {
                    MessageBox message = new MessageBox(EditorUtils.getShell(), SWT.OK | SWT.ICON_ERROR);
                    message.setText("Multiple python natures");
                    message.setMessage("This action can only be applied in one project at a time.");
                    message.open();
                    return false;
                }
            }
        }
    }
    if (nature == null) {
        MessageBox message = new MessageBox(EditorUtils.getShell(), SWT.OK | SWT.ICON_ERROR);
        message.setText("No nature found");
        message.setMessage("This action can only be applied in a project that is configured as a Pydev project.");
        message.open();
        return false;
    }
    AbstractRunner runner = UniversalRunner.getRunner(nature);
    Tuple<String, String> tup = runner.runCodeAndGetOutput(RUN_2_TO_3_CODE, new String[] { "--help" }, null, new NullProgressMonitor());
    if (tup.o1.indexOf("ImportError") != -1 || tup.o2.indexOf("ImportError") != -1) {
        MessageBox message = new MessageBox(EditorUtils.getShell(), SWT.OK | SWT.ICON_ERROR);
        message.setText("Unable to run 2to3");
        message.setMessage("Unable to run 2to3. Details: \n" + tup.o1 + "\n" + tup.o2 + "\n\nNotes: check if lib2to3 is properly installed in your Python install.");
        message.open();
        return false;
    }
    String msg = "Please enter the parameters to be passed for 2to3.py\n\n" + tup.o1 + "\n\n" + "E.g.: \n" + "Leave empty for preview\n" + "-w to apply with backup\n" + "-w -n to apply without backup.";
    if (tup.o2.length() > 0) {
        msg += "\n";
        msg += tup.o2;
    }
    final List<String> splitInLines = StringUtils.splitInLines(msg);
    int max = 10;
    for (String string : splitInLines) {
        max = Math.max(string.length(), max);
    }
    final int maxChars = max;
    InputDialogWithLongMessage d = new InputDialogWithLongMessage(EditorUtils.getShell(), "Parameters for 2to3.py", msg, "", null) {

        int averageCharWidth;

        int height;

        @Override
        protected boolean isResizable() {
            return true;
        }

        @Override
        protected String getOkButtonLabel() {
            return "Run with specified parameters";
        }

        @Override
        protected Control createDialogArea(Composite parent) {
            try {
                FontData labelFontData = FontUtils.getFontData(IFontUsage.DIALOG, false);
                Display display = parent.getDisplay();
                Font font = new Font(display, labelFontData);
                parent.setFont(font);
                GC gc = new GC(display);
                gc.setFont(font);
                FontMetrics fontMetrics = gc.getFontMetrics();
                averageCharWidth = fontMetrics.getAverageCharWidth();
                height = fontMetrics.getHeight();
                gc.dispose();
            } catch (Throwable e) {
            // ignore
            }
            return super.createDialogArea(parent);
        }

        @Override
        protected Point getInitialSize() {
            Point result = super.getInitialSize();
            // Check if we were able to get proper values before changing it.
            if (averageCharWidth > 0 && maxChars > 0) {
                result.x = (int) (averageCharWidth * maxChars * 1.15);
            }
            if (height > 0 && splitInLines.size() > 0) {
                // put some lines extra (we need the input line too)
                result.y = Math.max(height * (int) (splitInLines.size() * 1.5), result.y);
            }
            return result;
        }
    };
    int retCode = d.open();
    if (retCode != InputDialog.OK) {
        return false;
    }
    MessageConsole console = MessageConsoles.getConsole("2To3", UIConstants.PY_INTERPRETER_ICON);
    console.clearConsole();
    parameters = StringUtils.split(d.getValue(), " ");
    natureUsed = nature;
    return true;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) MessageConsole(org.eclipse.ui.console.MessageConsole) PythonNature(org.python.pydev.plugin.nature.PythonNature) Composite(org.eclipse.swt.widgets.Composite) InputDialogWithLongMessage(org.python.pydev.shared_ui.dialogs.InputDialogWithLongMessage) FontData(org.eclipse.swt.graphics.FontData) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) Font(org.eclipse.swt.graphics.Font) MessageBox(org.eclipse.swt.widgets.MessageBox) AbstractRunner(org.python.pydev.ast.runners.UniversalRunner.AbstractRunner) FontMetrics(org.eclipse.swt.graphics.FontMetrics) GC(org.eclipse.swt.graphics.GC) IResource(org.eclipse.core.resources.IResource) Display(org.eclipse.swt.widgets.Display)

Aggregations

IResource (org.eclipse.core.resources.IResource)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 Font (org.eclipse.swt.graphics.Font)1 FontData (org.eclipse.swt.graphics.FontData)1 FontMetrics (org.eclipse.swt.graphics.FontMetrics)1 GC (org.eclipse.swt.graphics.GC)1 Point (org.eclipse.swt.graphics.Point)1 Composite (org.eclipse.swt.widgets.Composite)1 Display (org.eclipse.swt.widgets.Display)1 MessageBox (org.eclipse.swt.widgets.MessageBox)1 MessageConsole (org.eclipse.ui.console.MessageConsole)1 AbstractRunner (org.python.pydev.ast.runners.UniversalRunner.AbstractRunner)1 PythonNature (org.python.pydev.plugin.nature.PythonNature)1 InputDialogWithLongMessage (org.python.pydev.shared_ui.dialogs.InputDialogWithLongMessage)1