simplify API chooseFile — use native Mac OS X file chooser

Dart — use native Mac OS X file chooser +review
This commit is contained in:
Vladimir Krivosheev
2013-11-04 12:07:58 +01:00
parent 3a94882633
commit 791cae8d11
3 changed files with 119 additions and 44 deletions
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.fileChooser;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.Consumer;
import org.jetbrains.annotations.NotNull;
/**
* You can use {@link Consumer<VirtualFile>}, but this class allows you to avoid null-check of passed file ({@link #cancelled()}) will be called in this case.
*/
public abstract class ChooseFileHandler implements Consumer<VirtualFile> {
protected void cancelled() {
}
@Override
public abstract void consume(@NotNull VirtualFile file);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@ package com.intellij.openapi.fileChooser;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Consumer;
@@ -174,7 +174,7 @@ public class FileChooser {
chooseFiles(descriptor, project, toSelect, new Consumer<List<VirtualFile>>() {
@Override
public void consume(final List<VirtualFile> files) {
callback.consume(VfsUtil.toVirtualFileArray(files));
callback.consume(VfsUtilCore.toVirtualFileArray(files));
}
});
}
@@ -195,7 +195,7 @@ public class FileChooser {
chooseFiles(descriptor, project, parent, toSelect, new Consumer<List<VirtualFile>>() {
@Override
public void consume(final List<VirtualFile> files) {
callback.consume(VfsUtil.toVirtualFileArray(files));
callback.consume(VfsUtilCore.toVirtualFileArray(files));
}
});
}
@@ -237,4 +237,56 @@ public class FileChooser {
final PathChooserDialog pathChooser = factory.createPathChooser(descriptor, project, parent);
pathChooser.choose(toSelect, callback);
}
/**
* Shows file/folder open dialog, allows user to choose file/folder and then passes result to callback in EDT.
* On MacOS Open Dialog will be shown with slide effect if Macish UI is turned on.
*
* @param descriptor file chooser descriptor
* @param project project
* @param toSelect file to preselect
* @param callback callback will be invoked after user have closed dialog, see {@link com.intellij.openapi.fileChooser.ChooseFileHandler}
* @since 13
*/
public static void chooseFile(@NotNull final FileChooserDescriptor descriptor,
@Nullable final Project project,
@Nullable final VirtualFile toSelect,
@NotNull final Consumer<VirtualFile> callback) {
chooseFile(descriptor, project, null, toSelect, callback);
}
/**
* Shows file/folder open dialog, allows user to choose file/folder and then passes result to callback in EDT.
* On MacOS Open Dialog will be shown with slide effect if Macish UI is turned on.
*
* @param descriptor file chooser descriptor
* @param project project
* @param parent parent component
* @param toSelect file to preselect
* @param callback callback will be invoked after user have closed dialog, see {@link com.intellij.openapi.fileChooser.ChooseFileHandler}
* @since 13
*/
public static void chooseFile(@NotNull final FileChooserDescriptor descriptor,
@Nullable final Project project,
@Nullable final Component parent,
@Nullable final VirtualFile toSelect,
@NotNull final Consumer<VirtualFile> callback) {
LOG.assertTrue(!descriptor.isChooseMultiple());
chooseFiles(descriptor, project, parent, toSelect, new Consumer<List<VirtualFile>>() {
@Override
public void consume(List<VirtualFile> files) {
if (files.isEmpty()) {
if (callback instanceof ChooseFileHandler) {
((ChooseFileHandler)callback).cancelled();
}
else {
callback.consume(null);
}
}
else {
callback.consume(files.get(0));
}
}
});
}
}
@@ -21,6 +21,7 @@ import com.intellij.ide.startup.StartupActionScriptManager;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.ex.ComboBoxAction;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.fileChooser.ChooseFileHandler;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.options.ex.SingleConfigurableEditor;
@@ -83,49 +84,40 @@ public class InstalledPluginsManagerMain extends PluginManagerMain {
};
descriptor.setTitle("Choose Plugin File");
descriptor.setDescription("JAR and ZIP archives are accepted");
FileChooser.chooseFiles(descriptor, null, myActionsPanel, null, new FileChooser.FileChooserConsumer() {
FileChooser.chooseFile(descriptor, null, myActionsPanel, null, new ChooseFileHandler() {
@Override
public void cancelled() {
}
@Override
public void consume(List<VirtualFile> files) {
if (files != null && files.size() == 1) {
VirtualFile virtualFile = files.get(0);
if (virtualFile != null) {
final File file = VfsUtilCore.virtualToIoFile(virtualFile);
try {
final IdeaPluginDescriptorImpl pluginDescriptor = PluginDownloader.loadDescriptionFromJar(file);
if (pluginDescriptor == null) {
Messages.showErrorDialog("Fail to load plugin descriptor from file " + file.getName(), CommonBundle.getErrorTitle());
return;
}
if (PluginManagerCore.isIncompatible(pluginDescriptor)) {
Messages.showErrorDialog("Plugin " + pluginDescriptor.getName() + " is incompatible with current installation", CommonBundle.getErrorTitle());
return;
}
final IdeaPluginDescriptor alreadyInstalledPlugin = PluginManager.getPlugin(pluginDescriptor.getPluginId());
if (alreadyInstalledPlugin != null) {
final File oldFile = alreadyInstalledPlugin.getPath();
if (oldFile != null) {
StartupActionScriptManager.addActionCommand(new StartupActionScriptManager.DeleteCommand(oldFile));
}
}
if (((InstalledPluginsTableModel)pluginsModel).appendOrUpdateDescriptor(pluginDescriptor)) {
PluginDownloader.install(file, file.getName(), false);
select(pluginDescriptor);
checkInstalledPluginDependencies(pluginDescriptor);
setRequireShutdown(true);
}
else {
Messages.showInfoMessage(myActionsPanel, "Plugin " + pluginDescriptor.getName() + " was already installed",
CommonBundle.getWarningTitle());
}
}
catch (IOException ex) {
Messages.showErrorDialog(ex.getMessage(), CommonBundle.getErrorTitle());
public void consume(@NotNull VirtualFile virtualFile) {
final File file = VfsUtilCore.virtualToIoFile(virtualFile);
try {
final IdeaPluginDescriptorImpl pluginDescriptor = PluginDownloader.loadDescriptionFromJar(file);
if (pluginDescriptor == null) {
Messages.showErrorDialog("Fail to load plugin descriptor from file " + file.getName(), CommonBundle.getErrorTitle());
return;
}
if (PluginManagerCore.isIncompatible(pluginDescriptor)) {
Messages.showErrorDialog("Plugin " + pluginDescriptor.getName() + " is incompatible with current installation", CommonBundle.getErrorTitle());
return;
}
final IdeaPluginDescriptor alreadyInstalledPlugin = PluginManager.getPlugin(pluginDescriptor.getPluginId());
if (alreadyInstalledPlugin != null) {
final File oldFile = alreadyInstalledPlugin.getPath();
if (oldFile != null) {
StartupActionScriptManager.addActionCommand(new StartupActionScriptManager.DeleteCommand(oldFile));
}
}
if (((InstalledPluginsTableModel)pluginsModel).appendOrUpdateDescriptor(pluginDescriptor)) {
PluginDownloader.install(file, file.getName(), false);
select(pluginDescriptor);
checkInstalledPluginDependencies(pluginDescriptor);
setRequireShutdown(true);
}
else {
Messages.showInfoMessage(myActionsPanel, "Plugin " + pluginDescriptor.getName() + " was already installed",
CommonBundle.getWarningTitle());
}
}
catch (IOException ex) {
Messages.showErrorDialog(ex.getMessage(), CommonBundle.getErrorTitle());
}
}
});