From 791cae8d111f70cc640dad3e8a1d0806948f518b Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Mon, 4 Nov 2013 12:06:59 +0100 Subject: [PATCH] =?UTF-8?q?simplify=20API=20chooseFile=20=E2=80=94=20use?= =?UTF-8?q?=20native=20Mac=20OS=20X=20file=20chooser=20Dart=20=E2=80=94=20?= =?UTF-8?q?use=20native=20Mac=20OS=20X=20file=20chooser=20+review?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fileChooser/ChooseFileHandler.java | 31 ++++++++ .../openapi/fileChooser/FileChooser.java | 60 ++++++++++++++-- .../plugins/InstalledPluginsManagerMain.java | 72 +++++++++---------- 3 files changed, 119 insertions(+), 44 deletions(-) create mode 100644 platform/platform-api/src/com/intellij/openapi/fileChooser/ChooseFileHandler.java diff --git a/platform/platform-api/src/com/intellij/openapi/fileChooser/ChooseFileHandler.java b/platform/platform-api/src/com/intellij/openapi/fileChooser/ChooseFileHandler.java new file mode 100644 index 000000000000..1300cf52c85b --- /dev/null +++ b/platform/platform-api/src/com/intellij/openapi/fileChooser/ChooseFileHandler.java @@ -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}, 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 { + protected void cancelled() { + } + + @Override + public abstract void consume(@NotNull VirtualFile file); +} \ No newline at end of file diff --git a/platform/platform-api/src/com/intellij/openapi/fileChooser/FileChooser.java b/platform/platform-api/src/com/intellij/openapi/fileChooser/FileChooser.java index b31a276ebcf7..35c5bed22b2c 100644 --- a/platform/platform-api/src/com/intellij/openapi/fileChooser/FileChooser.java +++ b/platform/platform-api/src/com/intellij/openapi/fileChooser/FileChooser.java @@ -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>() { @Override public void consume(final List 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>() { @Override public void consume(final List 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 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 callback) { + LOG.assertTrue(!descriptor.isChooseMultiple()); + chooseFiles(descriptor, project, parent, toSelect, new Consumer>() { + @Override + public void consume(List files) { + if (files.isEmpty()) { + if (callback instanceof ChooseFileHandler) { + ((ChooseFileHandler)callback).cancelled(); + } + else { + callback.consume(null); + } + } + else { + callback.consume(files.get(0)); + } + } + }); + } } \ No newline at end of file diff --git a/platform/platform-impl/src/com/intellij/ide/plugins/InstalledPluginsManagerMain.java b/platform/platform-impl/src/com/intellij/ide/plugins/InstalledPluginsManagerMain.java index 57edd4894f7b..a50e33a48509 100644 --- a/platform/platform-impl/src/com/intellij/ide/plugins/InstalledPluginsManagerMain.java +++ b/platform/platform-impl/src/com/intellij/ide/plugins/InstalledPluginsManagerMain.java @@ -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 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()); } } });