Example usage for com.intellij.openapi.fileChooser FileChooser chooseFile

List of usage examples for com.intellij.openapi.fileChooser FileChooser chooseFile

Introduction

In this page you can find the example usage for com.intellij.openapi.fileChooser FileChooser chooseFile.

Prototype

public static void chooseFile(@NotNull final FileChooserDescriptor descriptor, @Nullable final Project project,
        @Nullable final VirtualFile toSelect, @NotNull final Consumer<? super VirtualFile> callback) 

Source Link

Document

Shows file/folder open dialog, allows user to choose file/folder and then passes result to callback in EDT.

Usage

From source file:bazaar4idea.action.BzrInit.java

License:Apache License

private static void doInit(final Project project, FileChooserDescriptor fcd, VirtualFile baseDir,
        final VirtualFile finalBaseDir) {
    FileChooser.chooseFile(fcd, project, baseDir, new Consumer<VirtualFile>() {
        @Override//from  w w w. ja v a 2s. c  o  m
        public void consume(final VirtualFile root) {
            if (BzrUtil.isUnderBzr(root) && Messages.showYesNoDialog(project,
                    BzrBundle.message("init.warning.already.under.bzr",
                            StringUtil.escapeXml(root.getPresentableUrl())),
                    BzrBundle.getString("init.warning.title"), Messages.getWarningIcon()) != Messages.YES) {
                return;
            }

            BzrCommandResult result = ServiceManager.getService(Bzr.class).init(project, root);
            if (!result.success()) {
                BzrVcs vcs = BzrVcs.getInstance(project);
                if (vcs != null && vcs.getExecutableValidator().checkExecutableAndNotifyIfNeeded()) {
                    BzrUIUtil.notify(BzrVcs.IMPORTANT_ERROR_NOTIFICATION, project, "Bazaar init failed",
                            result.getErrorOutputAsHtmlString(), NotificationType.ERROR, null);
                }
                return;
            }

            if (project.isDefault()) {
                return;
            }
            final String path = root.equals(finalBaseDir) ? "" : root.getPath();
            BzrVcs.runInBackground(new Task.Backgroundable(project, BzrBundle.getString("common.refreshing")) {
                @Override
                public void run(@NotNull ProgressIndicator indicator) {
                    refreshAndConfigureVcsMappings(project, root, path);
                }
            });
        }
    });
}

From source file:com.intellij.execution.scratch.JavaScratchConfigurable.java

License:Apache License

public JavaScratchConfigurable(final Project project) {
    myMainClass = new LabeledComponent<JTextField>();
    myMainClass.setLabelLocation(BorderLayout.WEST);
    myMainClass.setText("Main &class:");
    myMainClass.setComponent(new JTextField());

    myScratchPathField = new LabeledComponent<TextFieldWithBrowseButton>();
    myScratchPathField.setLabelLocation(BorderLayout.WEST);
    myScratchPathField.setText("&Path to scratch file:");
    myScratchPathField.setComponent(new TextFieldWithBrowseButton(new ActionListener() {
        @Override/*from   w  w  w.  ja  v  a  2  s  .co m*/
        public void actionPerformed(ActionEvent e) {
            VirtualFile toSelect = getVFileFromEditor();
            if (toSelect == null) {
                final String scratchesRoot = ScratchFileService.getInstance()
                        .getRootPath(ScratchRootType.getInstance());
                toSelect = LocalFileSystem.getInstance().findFileByPath(scratchesRoot);
            }
            final VirtualFile file = FileChooser.chooseFile(
                    FileChooserDescriptorFactory.createSingleFileNoJarsDescriptor(),
                    myScratchPathField.getComponent(), project, toSelect);
            if (file != null) {
                setVFileToEditor(file);
            }
        }
    }, this));

    myModule = new LabeledComponent<ModulesComboBox>();
    myModule.setLabelLocation(BorderLayout.WEST);
    myModule.setComponent(new ModulesComboBox());
    myModule.setText("Use classpath of &module:");
    myModuleSelector = new ConfigurationModuleSelector(project, myModule.getComponent());

    myCommonProgramParameters = new CommonJavaParametersPanel();
    myCommonProgramParameters.setModuleContext(myModuleSelector.getModule());
    myModule.getComponent().addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            myCommonProgramParameters.setModuleContext(myModuleSelector.getModule());
        }
    });
    myJrePathEditor = new JrePathEditor(DefaultJreSelector.projectSdk(project));

    myWholePanel = new JPanel(new GridBagLayout());
    myWholePanel.add(myMainClass,
            new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, NORTHWEST, HORIZONTAL, JBUI.insetsTop(6), 0, 0));
    myWholePanel.add(myScratchPathField, new GridBagConstraints(RELATIVE, 1, 1, 1, 1.0, 0.0, NORTHWEST,
            HORIZONTAL, JBUI.insetsTop(6), 0, 0));
    myWholePanel.add(myCommonProgramParameters,
            new GridBagConstraints(RELATIVE, 2, 1, 1, 1.0, 1.0, NORTHWEST, BOTH, JBUI.insets(12, 0), 0, 0));
    myWholePanel.add(myModule, new GridBagConstraints(RELATIVE, 3, 1, 1, 1.0, 0.0, NORTHWEST, HORIZONTAL,
            JBUI.emptyInsets(), 0, 0));
    myWholePanel.add(myJrePathEditor, new GridBagConstraints(RELATIVE, 4, 1, 1, 1.0, 0.0, NORTHWEST, HORIZONTAL,
            JBUI.insetsTop(6), 0, 0));

    myAnchor = UIUtil.mergeComponentsWithAnchor(myMainClass, myScratchPathField, myCommonProgramParameters,
            myJrePathEditor, myModule);
}

From source file:com.intellij.execution.ui.CommonProgramParametersPanel.java

License:Apache License

protected void initComponents() {
    myProgramParametersComponent = LabeledComponent.create(new RawCommandLineEditor(),
            ExecutionBundle.message("run.configuration.program.parameters"));

    final JPanel panel = new JPanel(new BorderLayout());
    myWorkingDirectoryField = new TextFieldWithBrowseButton(new ActionListener() {
        @Override/*from www . j  a v a  2 s . c om*/
        public void actionPerformed(ActionEvent e) {
            FileChooserDescriptor fileChooserDescriptor = FileChooserDescriptorFactory
                    .createSingleFolderDescriptor();
            fileChooserDescriptor.setTitle(ExecutionBundle.message("select.working.directory.message"));
            fileChooserDescriptor.putUserData(LangDataKeys.MODULE_CONTEXT, myModuleContext);
            Project project = myModuleContext != null ? myModuleContext.getProject() : null;
            VirtualFile file = FileChooser.chooseFile(fileChooserDescriptor, myWorkingDirectoryComponent,
                    project, null);
            if (file != null) {
                setWorkingDirectory(file.getPresentableUrl());
            }
        }
    }) {
        @Override
        protected void installPathCompletion(FileChooserDescriptor fileChooserDescriptor) {
            super.installPathCompletion(FileChooserDescriptorFactory.createSingleFolderDescriptor());
        }
    };
    panel.add(myWorkingDirectoryField, BorderLayout.CENTER);

    final FixedSizeButton button = new FixedSizeButton(myWorkingDirectoryField);
    button.setIcon(AllIcons.RunConfigurations.Variables);
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            final List<String> macros = new ArrayList<String>(PathMacros.getInstance().getUserMacroNames());
            if (myHaveModuleContext)
                macros.add("MODULE_DIR");

            final JList list = new JBList(ArrayUtil.toStringArray(macros));
            final JBPopup popup = JBPopupFactory.getInstance().createListPopupBuilder(list)
                    .setItemChoosenCallback(new Runnable() {
                        @Override
                        public void run() {
                            final Object value = list.getSelectedValue();
                            if (value instanceof String) {
                                setWorkingDirectory("$" + value + "$");
                            }
                        }
                    }).setMovable(false).setResizable(false).createPopup();
            popup.showUnderneathOf(button);
        }
    });
    panel.add(button, BorderLayout.EAST);

    myWorkingDirectoryComponent = LabeledComponent.create(panel,
            ExecutionBundle.message("run.configuration.working.directory.label"));
    myEnvVariablesComponent = new EnvironmentVariablesComponent();

    myEnvVariablesComponent.setLabelLocation(BorderLayout.WEST);
    myProgramParametersComponent.setLabelLocation(BorderLayout.WEST);
    myWorkingDirectoryComponent.setLabelLocation(BorderLayout.WEST);

    addComponents();

    setPreferredSize(new Dimension(10, 10));

    setAnchor(myEnvVariablesComponent.getLabel());
}

From source file:com.intellij.internal.encodings.EncodingViewer.java

License:Apache License

public EncodingViewer() {
    super(false);
    initEncodings();//from ww  w .j  a va 2s .c  om
    myLoadFile.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            VirtualFile file = FileChooser.chooseFile(
                    FileChooserDescriptorFactory.createSingleFileNoJarsDescriptor(), myPanel, null, null);
            if (file != null) {
                loadFrom(file);
            }
        }
    });
    init();
}

From source file:com.intellij.plugins.haxe.config.sdk.ui.HaxeAdditionalConfigurablePanel.java

License:Apache License

public HaxeAdditionalConfigurablePanel() {
    myNekoTextField.getButton().addActionListener(new ActionListener() {
        @Override/*  w  ww  . j  a  v  a 2  s .  c  om*/
        public void actionPerformed(ActionEvent e) {
            final FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, false, false, false,
                    false);
            final VirtualFile file = FileChooser.chooseFile(descriptor, myPanel, null, null);
            if (file != null) {
                setNekoBinPath(FileUtil.toSystemIndependentName(file.getPath()));
            }
        }
    });
    myNekoLabel.setLabelFor(myNekoTextField.getTextField());
    myHaxelibTextField.getButton().addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            final FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, false, false, false,
                    false);
            final VirtualFile file = FileChooser.chooseFile(descriptor, myPanel, null, null);
            if (file != null) {
                setHaxelibPath(FileUtil.toSystemIndependentName(file.getPath()));
            }
        }
    });
    myHaxelibLabel.setLabelFor(myHaxelibTextField.getTextField());
}

From source file:com.intellij.plugins.haxe.runner.ui.HaxeRunConfigurationEditorForm.java

License:Apache License

@Override
protected void resetEditorFrom(HaxeApplicationConfiguration configuration) {
    myComboModules.removeAllItems();//www.  j  ava 2  s .c  o m

    final Module[] modules = ModuleManager.getInstance(configuration.getProject()).getModules();
    for (final Module module : modules) {
        if (ModuleType.get(module) == HaxeModuleType.getInstance()) {
            myComboModules.addItem(module);
        }
    }
    myComboModules.setSelectedItem(configuration.getConfigurationModule().getModule());

    myComboModules.setRenderer(new ListCellRendererWrapper() {
        @Override
        public void customize(JList list, Object value, int index, boolean selected, boolean hasFocus) {
            if (value instanceof Module) {
                final Module module = (Module) value;
                setText(module.getName());
            }
        }
    });

    myCustomPathCheckBox.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!myCustomPathCheckBox.isSelected()) {
                customPathToFile = myPathToFileTextField.getText();
            }
            updateComponents();
        }
    });

    myPathToFileTextField.getButton().addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            final FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, false, true, false,
                    false);
            final VirtualFile file = FileChooser.chooseFile(descriptor, component, null, null);
            if (file != null) {
                customPathToFile = FileUtil.toSystemIndependentName(file.getPath());
                updateComponents();
            }
        }
    });

    myAlternativeExecutable.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!myAlternativeExecutable.isSelected()) {
                customPathToExecutable = myExecutableField.getText();
            }
            updateComponents();
        }
    });

    myExecutableField.getButton().addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            final FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, false, true, false,
                    false);
            final VirtualFile file = FileChooser.chooseFile(descriptor, component, null, null);
            if (file != null) {
                customPathToExecutable = FileUtil.toSystemIndependentName(file.getPath());
                updateComponents();
            }
        }
    });

    myDebugListenPort.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String portString = myDebugListenPort.getText();
            if (portString == "") {
                portString = "6972";
            }
            Integer port;
            try {
                port = Integer.parseInt(portString);
            } catch (NumberFormatException ex) {
                port = 6972;
            }
            customDebugListenPort = port;
            myDebugListenPort.setText("" + port);
        }
    });

    myRemoteDebuggingCheckBox.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            customRemoteDebugging = myRemoteDebuggingCheckBox.isSelected();
        }
    });

    myCustomPathCheckBox.setSelected(configuration.isCustomFileToLaunch());
    myAlternativeExecutable.setSelected(configuration.isCustomExecutable());

    String launchPath = configuration.getCustomFileToLaunchPath();
    launchPath = !launchPath.contains("://") ? FileUtil.toSystemDependentName(launchPath) : launchPath;
    customPathToFile = launchPath;

    launchPath = configuration.getCustomExecutablePath();
    launchPath = !launchPath.contains("://") ? FileUtil.toSystemDependentName(launchPath) : launchPath;
    customPathToExecutable = launchPath;

    customDebugListenPort = configuration.getCustomDebugPort();
    myDebugListenPort.setText("" + customDebugListenPort);

    customRemoteDebugging = configuration.isCustomRemoteDebugging();
    myRemoteDebuggingCheckBox.setSelected(customRemoteDebugging);

    updateComponents();
}

From source file:com.intellij.ui.GuiUtils.java

License:Apache License

public static JPanel constructDirectoryBrowserField(final JTextField aTextField,
        final String aSearchedObjectName) {
    return constructFieldWithBrowseButton(aTextField, new ActionListener() {
        @Override//from ww  w  .jav  a2  s  . c om
        public void actionPerformed(ActionEvent e) {
            FileChooserDescriptor descriptor = FileChooserDescriptorFactory
                    .getDirectoryChooserDescriptor(aSearchedObjectName);
            VirtualFile file = FileChooser.chooseFile(descriptor, aTextField, null, null);
            if (file != null) {
                aTextField.setText(FileUtil.toSystemDependentName(file.getPath()));
                aTextField.postActionEvent();
            }
        }
    });
}

From source file:com.intellij.ui.GuiUtils.java

License:Apache License

public static JPanel constructFileURLBrowserField(final TextFieldWithHistory aFieldWithHistory,
        final String aSearchedObjectName) {
    return constructFieldWithBrowseButton(aFieldWithHistory, new ActionListener() {
        @Override/*from  ww  w  .  ja v  a 2s.co  m*/
        public void actionPerformed(ActionEvent e) {
            FileChooserDescriptor descriptor = FileChooserDescriptorFactory
                    .getFileChooserDescriptor(aSearchedObjectName);
            VirtualFile file = FileChooser.chooseFile(descriptor, aFieldWithHistory, null, null);
            if (file != null) {
                try {
                    aFieldWithHistory.setText(VfsUtil.virtualToIoFile(file).toURL().toString());
                } catch (MalformedURLException e1) {
                    aFieldWithHistory.setText("");
                }
            }
        }
    });
}

From source file:com.microsoft.intellij.forms.ImportSubscriptionForm.java

License:Open Source License

public ImportSubscriptionForm(Project project) {
    super(project, true);
    myProject = project;//www . java  2 s . c  o  m

    lblPolicy.addMouseListener(new LinkListener("http://msdn.microsoft.com/en-us/vstudio/dn425032.aspx"));
    lblDownload.addMouseListener(new LinkListener("http://go.microsoft.com/fwlink/?LinkID=301775"));

    this.setModal(true);
    this.setTitle("Import Microsoft Azure Subscriptions");

    final ImportSubscriptionForm form = this;
    this.setOKActionEnabled(false);
    this.setOKButtonText("Import");

    browseButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {

            FileChooserDescriptor fileChooserDescriptor = new FileChooserDescriptor(true, false, false, false,
                    false, false) {
                @Override
                public boolean isFileVisible(VirtualFile file, boolean showHiddenFiles) {
                    try {
                        return file.isDirectory() || (file.getExtension() != null
                                && file.getExtension().equals("publishsettings"));
                    } catch (Throwable t) {
                        return super.isFileVisible(file, showHiddenFiles);
                    }
                }

                @Override
                public boolean isFileSelectable(VirtualFile file) {
                    return (file.getExtension() != null && file.getExtension().equals("publishsettings"));
                }
            };
            fileChooserDescriptor.setTitle("Choose Subscriptions File");

            FileChooser.chooseFile(fileChooserDescriptor, null, null, new Consumer<VirtualFile>() {
                @Override
                public void consume(VirtualFile virtualFile) {
                    if (virtualFile != null) {
                        form.setOKActionEnabled(true);
                        txtFile.setText(virtualFile.getPath());
                    }
                }
            });
        }
    });

    DocumentListener documentListener = new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent e) {
            setOKActionEnabled(txtFile.getText() != null && !txtFile.getText().trim().isEmpty());
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            setOKActionEnabled(txtFile.getText() != null && !txtFile.getText().trim().isEmpty());
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            setOKActionEnabled(txtFile.getText() != null && !txtFile.getText().trim().isEmpty());
        }
    };

    txtFile.getDocument().addDocumentListener(documentListener);

    init();
}

From source file:com.microsoft.intellij.forms.OpenSSLFinderForm.java

License:Open Source License

public OpenSSLFinderForm(Project project) {
    super(project, true);
    setModal(true);/*from   w w  w . jav a 2s . co  m*/

    btnBrowse.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {

            FileChooserDescriptor fileChooserDescriptor = new FileChooserDescriptor(true, false, false, false,
                    false, false) {
                @Override
                public boolean isFileVisible(VirtualFile file, boolean showHiddenFiles) {
                    try {
                        return file.isDirectory()
                                || file.getNameWithoutExtension().toLowerCase().equals("openssl");
                    } catch (Throwable t) {
                        return super.isFileVisible(file, showHiddenFiles);
                    }
                }

                @Override
                public boolean isFileSelectable(VirtualFile file) {
                    return file.getNameWithoutExtension().toLowerCase().equals("openssl");
                }
            };

            fileChooserDescriptor.setTitle("Choose OpenSSL executable");

            FileChooser.chooseFile(fileChooserDescriptor, null, null, new Consumer<VirtualFile>() {
                @Override
                public void consume(VirtualFile virtualFile) {
                    if (virtualFile != null) {
                        txtFile.setText(virtualFile.getParent().getPath());
                    }
                }
            });
        }
    });

    init();
}