Search in sources :

Example 1 with DialogMemento

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

the class Select1Dialog method selectElement.

public TreeNode<Object> selectElement(TreeNode<Object> emptyRoot) {
    Shell shell = EditorUtils.getShell();
    final DialogMemento memento = new DialogMemento(shell, "org.python.pydev.ui.dialogs.Select1Dialog.shell");
    TreeSelectionDialog dialog = new TreeSelectionDialog(shell, getLabelProvider(), getContentProvider()) {

        @Override
        public boolean close() {
            memento.writeSettings(getShell());
            return super.close();
        }

        @Override
        public Control createDialogArea(Composite parent) {
            memento.readSettings();
            Control ret = super.createDialogArea(parent);
            ret.addTraverseListener(new TraverseListener() {

                @Override
                public void keyTraversed(TraverseEvent e) {
                    if (e.detail == SWT.TRAVERSE_RETURN) {
                        okPressed();
                    }
                }
            });
            return ret;
        }

        /* (non-Javadoc)
             * @see org.python.pydev.ui.dialogs.TreeSelectionDialog#createButtonBar(org.eclipse.swt.widgets.Composite)
             */
        @Override
        protected Control createButtonBar(Composite parent) {
            Composite buttonBar = new Composite(parent, 0);
            GridLayout layout = new GridLayout();
            layout.numColumns = 2;
            buttonBar.setLayout(layout);
            afterCreateButtonBar(parent, buttonBar);
            GridData data = new GridData();
            data.horizontalAlignment = SWT.FILL;
            data.grabExcessHorizontalSpace = true;
            buttonBar.setLayoutData(data);
            return buttonBar;
        }

        @Override
        protected Point getInitialSize() {
            return memento.getInitialSize(super.getInitialSize(), getShell());
        }

        @Override
        protected Point getInitialLocation(Point initialSize) {
            return memento.getInitialLocation(initialSize, super.getInitialLocation(initialSize), getShell());
        }

        /*
             * @see SelectionStatusDialog#computeResult()
             */
        @Override
        @SuppressWarnings("unchecked")
        protected void computeResult() {
            doFinalUpdateBeforeComputeResult();
            IStructuredSelection selection = (IStructuredSelection) getTreeViewer().getSelection();
            List<Object> list = selection.toList();
            if (list.size() == 1) {
                setResult(list);
            } else {
                Tree tree = getTreeViewer().getTree();
                TreeItem[] items = tree.getItems();
                list = new ArrayList<Object>();
                // Now, if he didn't select anything, let's create tests with all that is currently filtered
                // in the interface
                createListWithLeafs(items, list);
                if (list.size() == 1) {
                    setResult(list);
                }
            }
        }

        private void createListWithLeafs(TreeItem[] items, List<Object> leafObjectsList) {
            for (TreeItem item : items) {
                TreeItem[] children = item.getItems();
                if (children.length == 0) {
                    leafObjectsList.add(item.getData());
                } else {
                    createListWithLeafs(children, leafObjectsList);
                }
            }
        }
    };
    dialog.setTitle("PyDev: Select entry");
    dialog.setMessage("Select entry");
    dialog.setInitialFilter(getInitialFilter());
    dialog.setAllowMultiple(false);
    dialog.setInput(emptyRoot);
    int open = dialog.open();
    if (open != Window.OK) {
        return null;
    }
    Object[] result = dialog.getResult();
    if (result != null && result.length == 1) {
        return (TreeNode<Object>) result[0];
    }
    return null;
}
Also used : TraverseEvent(org.eclipse.swt.events.TraverseEvent) Composite(org.eclipse.swt.widgets.Composite) TraverseListener(org.eclipse.swt.events.TraverseListener) TreeItem(org.eclipse.swt.widgets.TreeItem) Point(org.eclipse.swt.graphics.Point) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DialogMemento(org.python.pydev.shared_ui.dialogs.DialogMemento) Point(org.eclipse.swt.graphics.Point) Shell(org.eclipse.swt.widgets.Shell) Control(org.eclipse.swt.widgets.Control) GridLayout(org.eclipse.swt.layout.GridLayout) TreeNode(org.python.pydev.shared_core.structure.TreeNode) GridData(org.eclipse.swt.layout.GridData) Tree(org.eclipse.swt.widgets.Tree) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with DialogMemento

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

the class SelectTestTreeContentProvider method run.

@Override
public void run(IAction action) {
    PyEdit pyEdit = getPyEdit();
    final Tuple<String, IInterpreterManager> launchConfigurationTypeAndInterpreterManager = this.getLaunchConfigurationTypeAndInterpreterManager(pyEdit, true);
    Shell shell = EditorUtils.getShell();
    final DialogMemento memento = new DialogMemento(shell, "org.python.pydev.debug.ui.actions.RunEditorAsCustomUnitTestAction");
    SimpleNode ast = pyEdit.getAST();
    final ShiftListener shiftListener = new ShiftListener();
    Display d = shell.getDisplay();
    d.addFilter(SWT.KeyDown, shiftListener);
    d.addFilter(SWT.KeyUp, shiftListener);
    try {
        final TreeSelectionDialog dialog = new TreeSelectionDialog(shell, new SelectTestLabelProvider(), new SelectTestTreeContentProvider(pyEdit)) {

            private Label labelShiftToDebug;

            @Override
            public boolean close() {
                memento.writeSettings(getShell());
                return super.close();
            }

            @Override
            public Control createDialogArea(Composite parent) {
                memento.readSettings();
                Control ret = super.createDialogArea(parent);
                ret.addTraverseListener(new TraverseListener() {

                    @Override
                    public void keyTraversed(TraverseEvent e) {
                        if (e.detail == SWT.TRAVERSE_RETURN) {
                            okPressed();
                        }
                    }
                });
                return ret;
            }

            /* (non-Javadoc)
                 * @see org.python.pydev.ui.dialogs.TreeSelectionDialog#createButtonBar(org.eclipse.swt.widgets.Composite)
                 */
            @Override
            protected Control createButtonBar(Composite parent) {
                Composite buttonBar = new Composite(parent, 0);
                GridLayout layout = new GridLayout();
                layout.numColumns = 2;
                buttonBar.setLayout(layout);
                GridData data = new GridData();
                data.horizontalAlignment = SWT.FILL;
                data.grabExcessHorizontalSpace = true;
                buttonBar.setLayoutData(data);
                Link configTestRunner = new Link(buttonBar, SWT.PUSH);
                configTestRunner.setText(" <a>Configure test runner</a>");
                configTestRunner.addSelectionListener(new SelectionAdapter() {

                    @Override
                    public void widgetSelected(SelectionEvent e) {
                        PyUnitPrefsPage2.showPage();
                    }
                });
                data = new GridData();
                data.horizontalAlignment = GridData.BEGINNING;
                data.grabExcessHorizontalSpace = true;
                configTestRunner.setLayoutData(data);
                labelShiftToDebug = new Label(buttonBar, 0);
                labelShiftToDebug.setText("Run: Normal   (Press Shift to Debug)");
                data = new GridData();
                data.horizontalAlignment = GridData.END;
                data.grabExcessHorizontalSpace = true;
                labelShiftToDebug.setLayoutData(data);
                shiftListener.onChanged.registerListener(new ICallbackListener<Boolean>() {

                    @Override
                    public Object call(Boolean shiftPressed) {
                        if (shiftPressed) {
                            labelShiftToDebug.setText("Run: Debug   (Release Shift for Normal)");
                        } else {
                            labelShiftToDebug.setText("Run: Normal   (Press Shift to Debug)");
                        }
                        labelShiftToDebug.getParent().layout(true);
                        return null;
                    }
                });
                Tree tree = getTreeViewer().getTree();
                Menu menu = new Menu(tree.getShell(), SWT.POP_UP);
                MenuItem runItem = new MenuItem(menu, SWT.PUSH);
                final TreeSelectionDialog outerDialog = this;
                runItem.addSelectionListener(new SelectionAdapter() {

                    @Override
                    public void widgetSelected(SelectionEvent e) {
                        ILaunchConfiguration conf = null;
                        IStructuredSelection selection = (IStructuredSelection) getTreeViewer().getSelection();
                        IEditorInput editorInput = pyEdit.getEditorInput();
                        IFile resource = editorInput != null ? editorInput.getAdapter(IFile.class) : null;
                        FileOrResource[] fileOrResource;
                        if (resource != null) {
                            fileOrResource = new FileOrResource[] { new FileOrResource(resource) };
                        } else {
                            fileOrResource = new FileOrResource[] { new FileOrResource(pyEdit.getEditorFile()) };
                        }
                        String testNames = getFullArgumentsRepresentation(selection.toArray());
                        UnittestLaunchShortcut shortcut = new UnittestLaunchShortcut(launchConfigurationTypeAndInterpreterManager, testNames);
                        List<ILaunchConfiguration> configurations = shortcut.findExistingLaunchConfigurations(fileOrResource);
                        boolean newConfiguration = false;
                        if (configurations.isEmpty()) {
                            conf = shortcut.createDefaultLaunchConfiguration(fileOrResource);
                            newConfiguration = true;
                        } else {
                            // assume that there's only one matching configuration
                            conf = configurations.get(0);
                        }
                        int retVal = DebugUITools.openLaunchConfigurationDialog(shell, conf, shiftListener.shiftPressed ? IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP : IDebugUIConstants.ID_RUN_LAUNCH_GROUP, null);
                        if (retVal == Window.CANCEL && newConfiguration) {
                            // user cancelled operation on newly created configuration
                            try {
                                conf.delete();
                            } catch (CoreException e1) {
                            // ignore
                            }
                        }
                        if (retVal == Window.OK) {
                            outerDialog.cancel();
                        }
                    }
                });
                runItem.setText("Customize run configuration...");
                tree.setMenu(menu);
                return buttonBar;
            }

            @Override
            protected Point getInitialSize() {
                return memento.getInitialSize(super.getInitialSize(), getShell());
            }

            @Override
            protected Point getInitialLocation(Point initialSize) {
                return memento.getInitialLocation(initialSize, super.getInitialLocation(initialSize), getShell());
            }

            /*
                 * @see SelectionStatusDialog#computeResult()
                 */
            @Override
            @SuppressWarnings("unchecked")
            protected void computeResult() {
                doFinalUpdateBeforeComputeResult();
                IStructuredSelection selection = (IStructuredSelection) getTreeViewer().getSelection();
                List<Object> list = selection.toList();
                if (list.size() > 0) {
                    setResult(list);
                } else {
                    Tree tree = getTreeViewer().getTree();
                    TreeItem[] items = tree.getItems();
                    list = new ArrayList<Object>();
                    // Now, if he didn't select anything, let's create tests with all that is currently filtered
                    // in the interface
                    createListWithLeafs(items, list);
                    setResult(list);
                }
            }

            private void createListWithLeafs(TreeItem[] items, List<Object> leafObjectsList) {
                for (TreeItem item : items) {
                    TreeItem[] children = item.getItems();
                    if (children.length == 0) {
                        leafObjectsList.add(item.getData());
                    } else {
                        createListWithLeafs(children, leafObjectsList);
                    }
                }
            }
        };
        dialog.setTitle("PyDev: Select tests to run");
        dialog.setMessage("Select the tests to run (press enter to run tests shown/selected)");
        PySelection ps = pyEdit.createPySelection();
        String selectedText;
        try {
            selectedText = ps.getSelectedText();
        } catch (BadLocationException e) {
            selectedText = "";
        }
        if (selectedText.length() > 0 && PyStringUtils.isValidIdentifier(selectedText, false)) {
            // Space in the end == exact match
            dialog.setInitialFilter(selectedText + " ");
        } else {
            dialog.setInitialFilter("test");
        }
        dialog.setAllowMultiple(true);
        dialog.setInput(ast);
        int open = dialog.open();
        if (open != Window.OK) {
            return;
        }
        Object[] result = dialog.getResult();
        final String arguments = getFullArgumentsRepresentation(result);
        AbstractLaunchShortcut shortcut = new UnittestLaunchShortcut(launchConfigurationTypeAndInterpreterManager, arguments);
        if (shiftListener.shiftPressed) {
            shortcut.launch(pyEdit, "debug");
        } else {
            shortcut.launch(pyEdit, "run");
        }
    } finally {
        d.removeFilter(SWT.KeyDown, shiftListener);
        d.removeFilter(SWT.KeyUp, shiftListener);
    }
}
Also used : TraverseEvent(org.eclipse.swt.events.TraverseEvent) IFile(org.eclipse.core.resources.IFile) TreeItem(org.eclipse.swt.widgets.TreeItem) Label(org.eclipse.swt.widgets.Label) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Shell(org.eclipse.swt.widgets.Shell) Control(org.eclipse.swt.widgets.Control) GridLayout(org.eclipse.swt.layout.GridLayout) AbstractLaunchShortcut(org.python.pydev.debug.ui.launching.AbstractLaunchShortcut) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Tree(org.eclipse.swt.widgets.Tree) List(java.util.List) ArrayList(java.util.ArrayList) Menu(org.eclipse.swt.widgets.Menu) PyEdit(org.python.pydev.editor.PyEdit) TreeSelectionDialog(org.python.pydev.ui.dialogs.TreeSelectionDialog) ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) Composite(org.eclipse.swt.widgets.Composite) TraverseListener(org.eclipse.swt.events.TraverseListener) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) MenuItem(org.eclipse.swt.widgets.MenuItem) Point(org.eclipse.swt.graphics.Point) IInterpreterManager(org.python.pydev.core.IInterpreterManager) DialogMemento(org.python.pydev.shared_ui.dialogs.DialogMemento) Point(org.eclipse.swt.graphics.Point) CoreException(org.eclipse.core.runtime.CoreException) FileOrResource(org.python.pydev.debug.ui.launching.FileOrResource) GridData(org.eclipse.swt.layout.GridData) PySelection(org.python.pydev.core.docutils.PySelection) Link(org.eclipse.swt.widgets.Link) IEditorInput(org.eclipse.ui.IEditorInput) BadLocationException(org.eclipse.jface.text.BadLocationException) Display(org.eclipse.swt.widgets.Display)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 TraverseEvent (org.eclipse.swt.events.TraverseEvent)2 TraverseListener (org.eclipse.swt.events.TraverseListener)2 Point (org.eclipse.swt.graphics.Point)2 GridData (org.eclipse.swt.layout.GridData)2 GridLayout (org.eclipse.swt.layout.GridLayout)2 Composite (org.eclipse.swt.widgets.Composite)2 Control (org.eclipse.swt.widgets.Control)2 Shell (org.eclipse.swt.widgets.Shell)2 Tree (org.eclipse.swt.widgets.Tree)2 TreeItem (org.eclipse.swt.widgets.TreeItem)2 DialogMemento (org.python.pydev.shared_ui.dialogs.DialogMemento)2 IFile (org.eclipse.core.resources.IFile)1 CoreException (org.eclipse.core.runtime.CoreException)1 ILaunchConfiguration (org.eclipse.debug.core.ILaunchConfiguration)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1