From b68c0a25fa7295b37129c8f2d97061f620e94464 Mon Sep 17 00:00:00 2001 From: hades Date: Tue, 7 Apr 2020 14:08:59 +0800 Subject: [PATCH] fix --- .../{swing => }/AbstractFileChooser.java | 11 +- .../ifilechooser/FRFileChooserFactory.java | 27 -- .../{swing => }/UINativeFileChooser.java | 9 +- .../javafx/UINativeFileChooser.java | 348 ------------------ 4 files changed, 17 insertions(+), 378 deletions(-) rename designer-base/src/main/java/com/fr/design/gui/ifilechooser/{swing => }/AbstractFileChooser.java (87%) delete mode 100644 designer-base/src/main/java/com/fr/design/gui/ifilechooser/FRFileChooserFactory.java rename designer-base/src/main/java/com/fr/design/gui/ifilechooser/{swing => }/UINativeFileChooser.java (95%) delete mode 100644 designer-base/src/main/java/com/fr/design/gui/ifilechooser/javafx/UINativeFileChooser.java diff --git a/designer-base/src/main/java/com/fr/design/gui/ifilechooser/swing/AbstractFileChooser.java b/designer-base/src/main/java/com/fr/design/gui/ifilechooser/AbstractFileChooser.java similarity index 87% rename from designer-base/src/main/java/com/fr/design/gui/ifilechooser/swing/AbstractFileChooser.java rename to designer-base/src/main/java/com/fr/design/gui/ifilechooser/AbstractFileChooser.java index 682f60e35c..ee61fd81f5 100644 --- a/designer-base/src/main/java/com/fr/design/gui/ifilechooser/swing/AbstractFileChooser.java +++ b/designer-base/src/main/java/com/fr/design/gui/ifilechooser/AbstractFileChooser.java @@ -1,4 +1,4 @@ -package com.fr.design.gui.ifilechooser.swing; +package com.fr.design.gui.ifilechooser; import javax.swing.filechooser.FileFilter; import java.awt.*; @@ -91,4 +91,13 @@ public abstract class AbstractFileChooser { * */ public abstract int showSaveDialog(Component parent); + + + /** + * https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4031440 + * + * 设置文件名后缀 起到文件过滤的作用 形如 "*.jpg;*.jpeg" + * + */ + public abstract void setExtensionFilter(String file); } diff --git a/designer-base/src/main/java/com/fr/design/gui/ifilechooser/FRFileChooserFactory.java b/designer-base/src/main/java/com/fr/design/gui/ifilechooser/FRFileChooserFactory.java deleted file mode 100644 index 0bc57dd438..0000000000 --- a/designer-base/src/main/java/com/fr/design/gui/ifilechooser/FRFileChooserFactory.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fr.design.gui.ifilechooser; - -import com.fr.design.gui.ifilechooser.javafx.UINativeFileChooser; - -import javax.swing.*; - -/** - * @author hades - * @version 10.0 - * Created by hades on 2020/4/7 - */ -public class FRFileChooserFactory { - - /** - * 默认提供原生风格 - * - * @return 文件选择器 - */ - public static JFileChooser createFileChooser() { - if (UINativeFileChooser.isAvailable()) { - return new UINativeFileChooser(); - } else { - return new JFileChooser(); - } - } - -} diff --git a/designer-base/src/main/java/com/fr/design/gui/ifilechooser/swing/UINativeFileChooser.java b/designer-base/src/main/java/com/fr/design/gui/ifilechooser/UINativeFileChooser.java similarity index 95% rename from designer-base/src/main/java/com/fr/design/gui/ifilechooser/swing/UINativeFileChooser.java rename to designer-base/src/main/java/com/fr/design/gui/ifilechooser/UINativeFileChooser.java index dd2394f793..c1418f834b 100644 --- a/designer-base/src/main/java/com/fr/design/gui/ifilechooser/swing/UINativeFileChooser.java +++ b/designer-base/src/main/java/com/fr/design/gui/ifilechooser/UINativeFileChooser.java @@ -1,6 +1,6 @@ -package com.fr.design.gui.ifilechooser.swing; +package com.fr.design.gui.ifilechooser; -import com.fr.design.gui.ifilechooser.swing.AbstractFileChooser; +import com.fr.design.gui.ifilechooser.AbstractFileChooser; import com.fr.design.mainframe.DesignerContext; import com.fr.stable.os.OperatingSystem; @@ -129,6 +129,11 @@ public class UINativeFileChooser extends AbstractFileChooser { return fileDialog.getFile() == null ? JFileChooser.CANCEL_OPTION : JFileChooser.APPROVE_OPTION; } + @Override + public void setExtensionFilter(String file) { + fileDialog.setFile(file); + } + /** * 确认本地文件选择器是否支持选择模式 * @param selectionMode 选择模式 diff --git a/designer-base/src/main/java/com/fr/design/gui/ifilechooser/javafx/UINativeFileChooser.java b/designer-base/src/main/java/com/fr/design/gui/ifilechooser/javafx/UINativeFileChooser.java deleted file mode 100644 index b1a1576af8..0000000000 --- a/designer-base/src/main/java/com/fr/design/gui/ifilechooser/javafx/UINativeFileChooser.java +++ /dev/null @@ -1,348 +0,0 @@ -package com.fr.design.gui.ifilechooser.javafx; - -import java.awt.*; -import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; -import java.util.concurrent.CountDownLatch; -import javafx.application.Platform; -import javafx.embed.swing.JFXPanel; -import javafx.stage.DirectoryChooser; -import javafx.stage.FileChooser; - -import javax.swing.*; -import javax.swing.filechooser.FileFilter; -import javax.swing.filechooser.FileNameExtensionFilter; -import javax.swing.filechooser.FileSystemView; - - -/** - * 基于javafx的原生风格文件选择器 - * 解决swing原生文件选择器不支持文件过滤的问题 - * - * @author hades - * @version 10.0 - * Created by hades on 2020/4/2 - */ -public class UINativeFileChooser extends JFileChooser { - - private static final boolean FX_AVAILABLE; - private List currentFiles; - private FileChooser fileChooser; - private File currentFile; - private DirectoryChooser directoryChooser; - - static { - boolean isFx; - try { - Class.forName("javafx.stage.FileChooser"); - isFx = true; - JFXPanel jfxPanel = new JFXPanel(); - } catch (ClassNotFoundException e) { - isFx = false; - } - - FX_AVAILABLE = isFx; - } - - public static boolean isAvailable() { - return FX_AVAILABLE; - } - - public UINativeFileChooser() { - initFxFileChooser(null); - } - - public UINativeFileChooser(String currentDirectoryPath) { - super(currentDirectoryPath); - initFxFileChooser(new File(currentDirectoryPath)); - } - - public UINativeFileChooser(File currentDirectory) { - super(currentDirectory); - initFxFileChooser(currentDirectory); - } - - public UINativeFileChooser(FileSystemView fsv) { - super(fsv); - initFxFileChooser(fsv.getDefaultDirectory()); - } - - public UINativeFileChooser(File currentDirectory, FileSystemView fsv) { - super(currentDirectory, fsv); - initFxFileChooser(currentDirectory); - } - - public UINativeFileChooser(String currentDirectoryPath, FileSystemView fsv) { - super(currentDirectoryPath, fsv); - initFxFileChooser(new File(currentDirectoryPath)); - } - - @Override - public int showOpenDialog(final Component parent) throws HeadlessException { - if (!FX_AVAILABLE) { - return super.showOpenDialog(parent); - } - - final CountDownLatch latch = new CountDownLatch(1); - Platform.runLater(new Runnable() { - @Override - public void run() { - - if (parent != null) { - parent.setEnabled(false); - } - - if (DIRECTORIES_ONLY == getFileSelectionMode()) { - currentFile = directoryChooser.showDialog(null); - } else { - if (isMultiSelectionEnabled()) { - currentFiles = fileChooser.showOpenMultipleDialog(null); - } else { - currentFile = fileChooser.showOpenDialog(null); - } - } - latch.countDown(); - } - - }); - try { - latch.await(); - } catch (InterruptedException ex) { - throw new RuntimeException(ex); - } finally { - if (parent != null) { - parent.setEnabled(true); - } - } - - if (isMultiSelectionEnabled()) { - if (currentFiles != null) { - return JFileChooser.APPROVE_OPTION; - } else { - return JFileChooser.CANCEL_OPTION; - } - } else { - if (currentFile != null) { - return JFileChooser.APPROVE_OPTION; - } else { - return JFileChooser.CANCEL_OPTION; - } - } - - } - - @Override - public int showSaveDialog(final Component parent) throws HeadlessException { - if (!FX_AVAILABLE) { - return super.showSaveDialog(parent); - } - - final CountDownLatch latch = new CountDownLatch(1); - - Platform.runLater(new Runnable() { - @Override - public void run() { - - if (parent != null) { - parent.setEnabled(false); - } - - if (DIRECTORIES_ONLY == getFileSelectionMode()) { - currentFile = directoryChooser.showDialog(null); - } else { - currentFile = fileChooser.showSaveDialog(null); - } - latch.countDown(); - } - - }); - try { - latch.await(); - } catch (InterruptedException ex) { - throw new RuntimeException(ex); - } finally { - if (parent != null) { - parent.setEnabled(true); - } - } - - if (currentFile != null) { - return JFileChooser.APPROVE_OPTION; - } else { - return JFileChooser.CANCEL_OPTION; - } - } - - @Override - public int showDialog(Component parent, String approveButtonText) { - if (!FX_AVAILABLE) { - return super.showDialog(parent, approveButtonText); - } - return showOpenDialog(parent); - } - - @Override - public File[] getSelectedFiles() { - if (!FX_AVAILABLE) { - return super.getSelectedFiles(); - } - if (currentFiles == null) { - return null; - } - return currentFiles.toArray(new File[currentFiles.size()]); - } - - @Override - public File getSelectedFile() { - if (!FX_AVAILABLE) { - return super.getSelectedFile(); - } - return currentFile; - } - - @Override - public void setSelectedFiles(File[] selectedFiles) { - if (!FX_AVAILABLE) { - super.setSelectedFiles(selectedFiles); - return; - } - if (selectedFiles == null || selectedFiles.length == 0) { - currentFiles = null; - } else { - setSelectedFile(selectedFiles[0]); - currentFiles = new ArrayList<>(Arrays.asList(selectedFiles)); - } - } - - @Override - public void setSelectedFile(File file) { - if (!FX_AVAILABLE) { - super.setSelectedFile(file); - return; - } - currentFile = file; - if (file != null) { - if (file.isDirectory()) { - fileChooser.setInitialDirectory(file.getAbsoluteFile()); - - if (directoryChooser != null) { - directoryChooser.setInitialDirectory(file.getAbsoluteFile()); - } - } else if (file.isFile()) { - fileChooser.setInitialDirectory(file.getParentFile()); - fileChooser.setInitialFileName(file.getName()); - - if (directoryChooser != null) { - directoryChooser.setInitialDirectory(file.getParentFile()); - } - } - - } - } - - @Override - public void setFileSelectionMode(int mode) { - super.setFileSelectionMode(mode); - if (!FX_AVAILABLE) { - return; - } - if (mode == DIRECTORIES_ONLY) { - if (directoryChooser == null) { - directoryChooser = new DirectoryChooser(); - } - setSelectedFile(currentFile); - setDialogTitle(getDialogTitle()); - } - } - - @Override - public void setDialogTitle(String dialogTitle) { - if (!FX_AVAILABLE) { - super.setDialogTitle(dialogTitle); - return; - } - fileChooser.setTitle(dialogTitle); - if (directoryChooser != null) { - directoryChooser.setTitle(dialogTitle); - } - } - - @Override - public String getDialogTitle() { - if (!FX_AVAILABLE) { - return super.getDialogTitle(); - } - return fileChooser.getTitle(); - } - - @Override - public void changeToParentDirectory() { - if (!FX_AVAILABLE) { - super.changeToParentDirectory(); - return; - } - File parentDir = fileChooser.getInitialDirectory().getParentFile(); - if (parentDir.isDirectory()) { - fileChooser.setInitialDirectory(parentDir); - if (directoryChooser != null) { - directoryChooser.setInitialDirectory(parentDir); - } - } - } - - @Override - public void addChoosableFileFilter(FileFilter filter) { - super.addChoosableFileFilter(filter); - if (!FX_AVAILABLE || filter == null) { - return; - } - if (filter.getClass().equals(FileNameExtensionFilter.class)) { - FileNameExtensionFilter f = (FileNameExtensionFilter) filter; - - List ext = new ArrayList<>(); - for (String extension : f.getExtensions()) { - ext.add(extension.replaceAll("^\\*?\\.?(.*)$", "*.$1")); - } - fileChooser.getExtensionFilters() - .add(new FileChooser.ExtensionFilter(f.getDescription(), ext)); - } - } - - @Override - public void setAcceptAllFileFilterUsed(boolean bool) { - boolean differs = isAcceptAllFileFilterUsed() ^ bool; - super.setAcceptAllFileFilterUsed(bool); - if (!FX_AVAILABLE) { - return; - } - if (!differs) { - return; - } - if (bool) { - fileChooser.getExtensionFilters() - .add(new FileChooser.ExtensionFilter("All files", "*.*")); - } else { - for (Iterator it - = fileChooser.getExtensionFilters().iterator(); it.hasNext();) { - FileChooser.ExtensionFilter filter = it.next(); - if (filter.getExtensions().size() == 1 - && filter.getExtensions().contains("*.*")) { - it.remove(); - } - } - } - - } - - private void initFxFileChooser(File currentFile) { - if (FX_AVAILABLE) { - fileChooser = new FileChooser(); - this.currentFile = currentFile; - setSelectedFile(currentFile); - } - } - -} \ No newline at end of file