From e04740e1cfc85be5085daa11059649faa2421a1c Mon Sep 17 00:00:00 2001 From: hades Date: Thu, 21 May 2020 13:35:34 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-31866=E3=80=90=E5=86=92=E7=83=9F?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E3=80=91=E5=88=87=E6=8D=A2=E8=BF=9C=E7=A8=8B?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=EF=BC=8C=E6=96=B0=E5=BB=BA=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E6=8D=AE=E9=9B=86=E5=B1=95=E7=A4=BA=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=20&&=20REPORT-28701=20=E5=90=8C=E6=AD=A5release?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../design/file/HistoryTemplateListCache.java | 18 ++- .../gui/ifilechooser/AbstractFileChooser.java | 103 ++++++++++++ .../gui/ifilechooser/UINativeFileChooser.java | 153 ++++++++++++++++++ .../com/fr/design/os/impl/SupportOSImpl.java | 12 ++ .../mainframe/FormWidgetDetailPane.java | 34 +++- 5 files changed, 310 insertions(+), 10 deletions(-) create mode 100644 designer-base/src/main/java/com/fr/design/gui/ifilechooser/AbstractFileChooser.java create mode 100644 designer-base/src/main/java/com/fr/design/gui/ifilechooser/UINativeFileChooser.java diff --git a/designer-base/src/main/java/com/fr/design/file/HistoryTemplateListCache.java b/designer-base/src/main/java/com/fr/design/file/HistoryTemplateListCache.java index f8b089fe6a..cb68bb6e47 100644 --- a/designer-base/src/main/java/com/fr/design/file/HistoryTemplateListCache.java +++ b/designer-base/src/main/java/com/fr/design/file/HistoryTemplateListCache.java @@ -2,6 +2,7 @@ package com.fr.design.file; import com.fr.base.chart.chartdata.CallbackEvent; import com.fr.base.io.BaseBook; +import com.fr.design.DesignModelAdapter; import com.fr.design.DesignerEnvManager; import com.fr.design.base.mode.DesignModeContext; import com.fr.design.data.DesignTableDataManager; @@ -384,9 +385,14 @@ public class HistoryTemplateListCache implements CallbackEvent { historyList.set(i, template); // 替换当前正在编辑的模板,使用添加并激活的方式,以便使用统一的入口来处理监听事件 if (isCurrentEditingFile(template.getPath())) { - DesignerContext.getDesignerFrame().addAndActivateJTemplate(template); - setCurrentEditingTemplate(template); - FineLoggerFactory.getLogger().info("Env Change Current Editing Template."); + loadCurrentTemplate(template); + } + } else { + // 当模板为空时 说明是一个新建的未保存模板 但是内存中保存了该模板 可以从中获取 + JTemplate jt = historyList.get(i); + // 另外如果该模板是正在编辑的模板,需要要激活 + if (jt != null && isCurrentEditingFile(jt.getPath())) { + loadCurrentTemplate(jt); } } } catch (Exception e) { @@ -400,6 +406,12 @@ public class HistoryTemplateListCache implements CallbackEvent { FineLoggerFactory.getLogger().info("Env Change Template Loaded."); } + private void loadCurrentTemplate(JTemplate template) { + DesignerContext.getDesignerFrame().addAndActivateJTemplate(template); + setCurrentEditingTemplate(template); + FineLoggerFactory.getLogger().info("Env Change Current Editing Template " + template.getTemplateName()); + } + /** * 重新载入当前模板,刷新数据/对象 */ diff --git a/designer-base/src/main/java/com/fr/design/gui/ifilechooser/AbstractFileChooser.java b/designer-base/src/main/java/com/fr/design/gui/ifilechooser/AbstractFileChooser.java new file mode 100644 index 0000000000..ee61fd81f5 --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/gui/ifilechooser/AbstractFileChooser.java @@ -0,0 +1,103 @@ +package com.fr.design.gui.ifilechooser; + +import javax.swing.filechooser.FileFilter; +import java.awt.*; +import java.io.File; + +/** + * @author hades + * @version 10.0 + * Created by hades on 2020/3/31 + */ +public abstract class AbstractFileChooser { + + /** + * 返回当前目录 + * + */ + public abstract File getCurrentDirectory(); + + /** + * 返回当前的文件选择过滤器 + * + */ + public abstract FileFilter getFileFilter(); + + /** + * 返回选择的文件 + * + */ + public abstract File getSelectedFile(); + + /** + * 多文件选择模式下 返回选择的多个文件 + * + */ + public abstract File[] getSelectedFiles(); + + /** + * 是否可以选择多个文件 + * + */ + public abstract boolean isMultiSelectionEnabled(); + + /** + * 设置当前选择的目录 + * + */ + public abstract void setCurrentDirectory(File dir); + + /** + * 设置左上角标题 + * + */ + public abstract void setDialogTitle(String title); + + /** + * 设置当前的文件过滤器 + * + */ + public abstract void setFileFilter(final FileFilter filter); + + /** + * 设置文件选择器模式 + * + * JFileChooser.FILES_ONLY + * JFileChooser.DIRECTORIES_ONLY + * JFileChooser.FILES_AND_DIRECTORIES + */ + public abstract void setFileSelectionMode(int selectionMode); + + /** + * 设置是否允许选择多个文件 + * + */ + public abstract void setMultiSelectionEnabled(boolean multiple); + + /** + * 设置选择的文件 用于showSaveDialog + * + */ + public abstract void setSelectedFile(File file); + + /** + * 弹出文件选择器 打开文件 + * + */ + public abstract int showOpenDialog(Component parent); + + /** + * 弹出文件选择器 保存文件 + * + */ + 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/UINativeFileChooser.java b/designer-base/src/main/java/com/fr/design/gui/ifilechooser/UINativeFileChooser.java new file mode 100644 index 0000000000..597f733f1e --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/gui/ifilechooser/UINativeFileChooser.java @@ -0,0 +1,153 @@ +package com.fr.design.gui.ifilechooser; + +import com.fr.design.mainframe.DesignerContext; +import com.fr.stable.os.OperatingSystem; + +import javax.swing.*; +import javax.swing.filechooser.FileFilter; +import java.awt.*; +import java.io.File; +import java.io.FilenameFilter; + + +/** + * 系统原生风格的文件选择器 + * + * jdk问题: + * https://bugs.openjdk.java.net/browse/JDK-4811090 不支持文件扩展选择 + * https://stackoverflow.com/questions/40713398/filter-not-working-in-filedialog windows下 setFilenameFilter不work + * + * @author hades + * @version 10.0 + * Created by hades on 2020/3/31 + */ +public class UINativeFileChooser extends AbstractFileChooser { + + private final FileDialog fileDialog; + private FileFilter fileFilter; + private int selectionMode; + + public UINativeFileChooser(File file) { + fileDialog = new FileDialog(DesignerContext.getDesignerFrame()); + if (file != null) { + fileDialog.setDirectory(file.getAbsolutePath()); + fileDialog.setFile(file.toString()); + } + } + + public UINativeFileChooser() { + this(null); + } + + @Override + public File getCurrentDirectory() { + return new File(fileDialog.getDirectory()); + } + + @Override + public FileFilter getFileFilter() { + return fileFilter; + } + + @Override + public File getSelectedFile() { + return new File(fileDialog.getDirectory() + fileDialog.getFile()); + } + + @Override + public File[] getSelectedFiles() { + return fileDialog.getFiles(); + } + + @Override + public boolean isMultiSelectionEnabled() { + return fileDialog.isMultipleMode(); + } + + @Override + public void setCurrentDirectory(File f) { + fileDialog.setDirectory(f.toString()); + } + + @Override + public void setDialogTitle(String title) { + fileDialog.setTitle(title); + } + + @Override + public void setFileFilter(final FileFilter cff) { + FilenameFilter filter = new FilenameFilter() { + @Override + public boolean accept(File Directory, String fileName) { + return cff.accept(new File(Directory.getAbsolutePath() + fileName)); + } + }; + fileDialog.setFilenameFilter(filter); + fileFilter = cff; + } + + @Override + public void setFileSelectionMode(int selectionMode) { + this.selectionMode = selectionMode; + } + + @Override + public void setMultiSelectionEnabled(boolean multiple) { + fileDialog.setMultipleMode(multiple); + } + + @Override + public void setSelectedFile(File file) { + fileDialog.setDirectory(file.getAbsolutePath()); + fileDialog.setFile(file.getName()); + } + + @Override + public int showOpenDialog(Component parent) { + boolean appleProperty = OperatingSystem.isMacos() && selectionMode == JFileChooser.DIRECTORIES_ONLY; + if (appleProperty) { + System.setProperty("apple.awt.fileDialogForDirectories", "true"); + } + try { + fileDialog.setLocale(JComponent.getDefaultLocale()); + fileDialog.setMode(FileDialog.LOAD); + fileDialog.setVisible(true); + return fileDialog.getFile() == null ? JFileChooser.CANCEL_OPTION : JFileChooser.APPROVE_OPTION; + } finally { + if (appleProperty) { + System.setProperty("apple.awt.fileDialogForDirectories", "false"); + } + } + } + + @Override + public int showSaveDialog(Component parent) { + fileDialog.setLocale(JComponent.getDefaultLocale()); + fileDialog.setMode(FileDialog.SAVE); + fileDialog.setVisible(true); + return fileDialog.getFile() == null ? JFileChooser.CANCEL_OPTION : JFileChooser.APPROVE_OPTION; + } + + @Override + public void setExtensionFilter(String file) { + fileDialog.setFile(file); + } + + /** + * 确认本地文件选择器是否支持选择模式 + * @param selectionMode 选择模式 + * @return 是否支持选择模式 + */ + public static boolean supportsSelectionMode(int selectionMode) { + switch (selectionMode) { + case JFileChooser.FILES_AND_DIRECTORIES: + return false; + case JFileChooser.DIRECTORIES_ONLY: + return OperatingSystem.isMacos(); + case JFileChooser.FILES_ONLY: + default: + return true; + } + } + +} diff --git a/designer-base/src/main/java/com/fr/design/os/impl/SupportOSImpl.java b/designer-base/src/main/java/com/fr/design/os/impl/SupportOSImpl.java index d799d00466..366e7d565c 100644 --- a/designer-base/src/main/java/com/fr/design/os/impl/SupportOSImpl.java +++ b/designer-base/src/main/java/com/fr/design/os/impl/SupportOSImpl.java @@ -105,6 +105,18 @@ public enum SupportOSImpl implements SupportOS { public boolean support() { return OperatingSystem.isWindows(); } + }, + + /** + * 原生文件选择器弹窗 + * https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8019464 mac下原生弹窗国际化设置无效,jdk11中有修复 + * + */ + NATIVE_CHOOSER { + @Override + public boolean support() { + return OperatingSystem.isWindows(); + } } } diff --git a/designer-form/src/main/java/com/fr/design/mainframe/FormWidgetDetailPane.java b/designer-form/src/main/java/com/fr/design/mainframe/FormWidgetDetailPane.java index ecb3faa706..311ea4cb76 100644 --- a/designer-form/src/main/java/com/fr/design/mainframe/FormWidgetDetailPane.java +++ b/designer-form/src/main/java/com/fr/design/mainframe/FormWidgetDetailPane.java @@ -5,8 +5,10 @@ import com.fr.base.FRContext; import com.fr.design.gui.ibutton.UIButton; import com.fr.design.gui.icombobox.UIComboBox; import com.fr.design.gui.icontainer.UIScrollPane; +import com.fr.design.gui.ifilechooser.UINativeFileChooser; import com.fr.design.gui.ilable.UILabel; import com.fr.design.layout.FRGUIPaneFactory; +import com.fr.design.os.impl.SupportOSImpl; import com.fr.design.widget.FRWidgetFactory; import com.fr.form.share.SharableWidgetProvider; import com.fr.form.share.ShareLoader; @@ -333,13 +335,31 @@ public class FormWidgetDetailPane extends FormDockView{ new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - JFileChooser fileChooser = new JFileChooser(); - fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); - fileChooser.setFileFilter(new FileNameExtensionFilter(".reu", "reu")); - int returnValue = fileChooser.showDialog(new UILabel(), com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Select")); - if (returnValue == JFileChooser.APPROVE_OPTION) { - final File chosenFile = fileChooser.getSelectedFile(); - installFromDiskZipFile(chosenFile); + if (SupportOSImpl.NATIVE_CHOOSER.support()) { + UINativeFileChooser nativeFileChooser = new UINativeFileChooser(); + nativeFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); + nativeFileChooser.setMultiSelectionEnabled(true); + nativeFileChooser.setExtensionFilter("*.reu"); + nativeFileChooser.setDialogTitle(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Select")); + int returnValue = nativeFileChooser.showOpenDialog(new UILabel()); + if (returnValue == JFileChooser.APPROVE_OPTION) { + final File[] chosenFiles = nativeFileChooser.getSelectedFiles(); + for (File file : chosenFiles) { + installFromDiskZipFile(file); + } + } + } else { + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); + fileChooser.setMultiSelectionEnabled(true); + fileChooser.setFileFilter(new FileNameExtensionFilter(".reu", "reu")); + int returnValue = fileChooser.showDialog(new UILabel(), com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Select")); + if (returnValue == JFileChooser.APPROVE_OPTION) { + final File[] chosenFiles = fileChooser.getSelectedFiles(); + for (File file : chosenFiles) { + installFromDiskZipFile(file); + } + } } } }