filebrowser.java
来自「Petri网分析工具PIPE is open-source」· Java 代码 · 共 68 行
JAVA
68 行
package pipe.gui.widgets;import java.io.File;import java.io.IOException;import javax.swing.JFileChooser;import javax.swing.JOptionPane;import pipe.gui.ExtensionFilter;/** * @author Maxim * * Opens a file browser with appropriate settings for the given filetype/extension */public class FileBrowser extends JFileChooser { private String ext; public FileBrowser(String filetype,String ext,String path) { super(); if (filetype==null) filetype="file"; if (path!=null) { File f=new File(path); if(f.exists())setCurrentDirectory(f); if(!f.isDirectory())setSelectedFile(f); } this.ext=ext; ExtensionFilter filter=new ExtensionFilter(ext,filetype); setFileFilter(filter); } public FileBrowser(String path) { this("Petri net","xml",path); // default parameters } public FileBrowser() { this(null); } public File openFile() { if (showOpenDialog(null)==JFileChooser.APPROVE_OPTION) try { return getSelectedFile().getCanonicalFile(); } catch (IOException e) {/* gulp */}; return null; } public String saveFile() { if (showSaveDialog(null)==JFileChooser.APPROVE_OPTION) try { File f=getSelectedFile(); if(!f.getName().endsWith("."+ext)) f=new File(f.getCanonicalPath()+"."+ext); // force extension if (f.exists() && JOptionPane.showConfirmDialog( this, f.getCanonicalPath()+"\nDo you want to overwrite this file?" )!=JOptionPane.YES_OPTION ) return null; return f.getCanonicalPath(); } catch (IOException e) {/* gulp */}; return null; } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?