Search in sources :

Example 1 with TreeNodeContentProvider

use of org.python.pydev.shared_ui.tree.TreeNodeContentProvider in project Pydev by fabioz.

the class SelectNDialog method selectElements.

/**
 * @param labelProvider: usually a TreeNodeLabelProvider
 * @param title: message in shell
 * @param message: message above tree
 * @param checkElapsedBeforeClose: if true we'll only accept a click after some secs (to prevent an accidental click
 * in the case of an unrequested dialog).
 */
public static List<TreeNode> selectElements(TreeNode root, ILabelProvider labelProvider, String title, String message, final boolean checkElapsedBeforeClose, List initialSelection) {
    Shell shell = EditorUtils.getShell();
    CheckedTreeSelectionDialog dialog = new CheckedTreeSelectionDialog(shell, labelProvider, new TreeNodeContentProvider()) {

        private final DialogButtonEnablementHelper helper = new DialogButtonEnablementHelper(!checkElapsedBeforeClose);

        @Override
        public boolean close() {
            if (!helper.areButtonsEnabled()) {
                return false;
            }
            return super.close();
        }

        @Override
        protected void cancelPressed() {
            if (!helper.areButtonsEnabled()) {
                return;
            }
            super.cancelPressed();
        }

        @Override
        protected void okPressed() {
            if (!helper.areButtonsEnabled()) {
                return;
            }
            super.okPressed();
        }

        @Override
        protected void constrainShellSize() {
            helper.onConstrainShellSize();
            super.constrainShellSize();
        }

        @Override
        protected Button createButton(Composite parent, int id, String label, boolean defaultButton) {
            Button button = super.createButton(parent, id, label, defaultButton);
            helper.onCreateButton(button, id);
            return button;
        }

        @Override
        protected void createButtonsForButtonBar(Composite parent) {
            // create OK and Cancel buttons by default
            createButton(parent, IDialogConstants.OK_ID, "Apply selected changes (Ignore unselected)", true);
            createButton(parent, IDialogConstants.CANCEL_ID, "Don't ask again (Ignore all)", false);
        }
    };
    dialog.setTitle(title);
    dialog.setMessage(message);
    dialog.setInput(root);
    dialog.setInitialElementSelections(initialSelection);
    dialog.setExpandedElements(initialSelection.toArray());
    if (dialog.open() == CheckedTreeSelectionDialog.OK) {
        Object[] result = dialog.getResult();
        if (result != null) {
            ArrayList<TreeNode> ret = new ArrayList<TreeNode>(result.length);
            for (Object o : result) {
                ret.add((TreeNode) o);
            }
            return ret;
        }
    }
    return null;
}
Also used : Shell(org.eclipse.swt.widgets.Shell) Composite(org.eclipse.swt.widgets.Composite) CheckedTreeSelectionDialog(org.eclipse.ui.dialogs.CheckedTreeSelectionDialog) Button(org.eclipse.swt.widgets.Button) TreeNode(org.python.pydev.shared_core.structure.TreeNode) ArrayList(java.util.ArrayList) TreeNodeContentProvider(org.python.pydev.shared_ui.tree.TreeNodeContentProvider)

Aggregations

ArrayList (java.util.ArrayList)1 Button (org.eclipse.swt.widgets.Button)1 Composite (org.eclipse.swt.widgets.Composite)1 Shell (org.eclipse.swt.widgets.Shell)1 CheckedTreeSelectionDialog (org.eclipse.ui.dialogs.CheckedTreeSelectionDialog)1 TreeNode (org.python.pydev.shared_core.structure.TreeNode)1 TreeNodeContentProvider (org.python.pydev.shared_ui.tree.TreeNodeContentProvider)1