public static URL select()

in remoting/ide/api/src/org/netbeans/modules/jackpot30/remotingapi/options/FSChooser.java [65:139]


    public static URL select(String caption, String okButton, URL preselect) {
        Node n = new RootNode();
        final ExplorerManager m = new ExplorerManager();

        m.setRootContext(n);

        class P extends JPanel implements ExplorerManager.Provider {
            public P() {
                setLayout(new BorderLayout());
                add(new BeanTreeView(), BorderLayout.CENTER);
            }
            @Override public ExplorerManager getExplorerManager() {
                return m;
            }
        }

        //XXX: asynchronously!
        FileObject toSelect = preselect != null ? URLMapper.findFileObject(preselect) : null;

        toSelect = toSelect != null ? toSelect : homeFO();

        if (toSelect != null) {
            try {
                Node toSelectNode = findFileNode(n, toSelect);

                if (toSelectNode == null) {
                    FileObject home = homeFO();

                    if (home != null) {
                        toSelectNode = findFileNode(n, home);
                    }
                }

                if (toSelectNode != null) {
                    m.setSelectedNodes(new Node[] {toSelectNode});
                }
            } catch (PropertyVetoException ex) {
                Exceptions.printStackTrace(ex);
            }
        }

        final boolean[] accepted = new boolean[1];
        final Dialog[] d = new Dialog[1];
        final JButton ok = new JButton(okButton);

        m.addPropertyChangeListener(new PropertyChangeListener() {
            @Override public void propertyChange(PropertyChangeEvent evt) {
                ok.setEnabled(m.getSelectedNodes().length == 1);
            }
        });

        ok.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                d[0].setVisible(false);
                accepted[0] = true;
            }
        });

        DialogDescriptor dd = new DialogDescriptor(new P(), caption, true, new Object[] {ok, DialogDescriptor.CANCEL_OPTION}, ok, DialogDescriptor.DEFAULT_ALIGN, null, null);

        d[0] = DialogDisplayer.getDefault().createDialog(dd);

        d[0].setVisible(true);

        if (accepted[0]) {
            try {
                return m.getSelectedNodes()[0].getLookup().lookup(FileObject.class).getURL();
            } catch (FileStateInvalidException ex) {
                Exceptions.printStackTrace(ex);
                return null;
            }
        } else {
            return null;
        }
    }