1 package jp.sourceforge.stigmata.ui.swing;
7 import java.awt.Component;
9 import java.text.MessageFormat;
11 import javax.swing.JFileChooser;
12 import javax.swing.SwingUtilities;
13 import javax.swing.filechooser.FileFilter;
15 import jp.sourceforge.stigmata.BirthmarkEnvironment;
19 * @author Haruaki TAMADA
22 public class FileIOManager{
23 private Component parent;
24 private File currentDirectory;
25 private BirthmarkEnvironment env;
27 public FileIOManager(Component parent, BirthmarkEnvironment env){
30 if(env.getProperty(".current.directory") != null){
31 currentDirectory = new File(env.getProperty(".current.directory"));
33 if(env.getProperty("startup.directory") != null){
34 currentDirectory = new File(env.getProperty("startup.directory"));
36 if(currentDirectory == null && System.getProperty("execution.directory") != null){
37 currentDirectory = new File(System.getProperty("execution.directory"));
39 if(currentDirectory == null){
40 currentDirectory = new File(".");
44 public File getCurrentDirectory(){
45 return currentDirectory;
48 public void setCurrentDirectory(File directory) throws IllegalArgumentException{
49 if(!directory.isDirectory()){
50 throw new IllegalArgumentException(directory.getName() + " is not directory");
52 this.currentDirectory = directory;
53 env.addProperty(".current.directory", directory.getAbsolutePath());
56 public File findFile(boolean open){
57 return findFile(open, new String[0], "");
60 public File[] openFiles(FileFilter[] filters, boolean multi, boolean directory){
61 JFileChooser chooser = new JFileChooser(getCurrentDirectory());
63 for(int i = 0; i < filters.length; i++){
64 chooser.addChoosableFileFilter(filters[i]);
67 chooser.setMultiSelectionEnabled(multi);
69 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
72 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
74 int returnValue = chooser.showOpenDialog(SwingUtilities.getRootPane(parent));
75 if(returnValue == JFileChooser.APPROVE_OPTION){
76 setCurrentDirectory(chooser.getCurrentDirectory());
77 return chooser.getSelectedFiles();
82 public File findFile(boolean open, String[] exts, String desc){
83 JFileChooser chooser = new JFileChooser(getCurrentDirectory());
85 MessageFormat formatter = new MessageFormat(desc);
86 for(int i = 0; i < exts.length; i++){
87 chooser.addChoosableFileFilter(
88 new ExtensionFilter(exts[i], formatter.format(new Object[] { exts[i], }))
94 returnValue = chooser.showOpenDialog(SwingUtilities.getRootPane(parent));
97 returnValue = chooser.showSaveDialog(SwingUtilities.getRootPane(parent));
99 if(returnValue == JFileChooser.APPROVE_OPTION){
100 setCurrentDirectory(chooser.getCurrentDirectory());
101 File file = chooser.getSelectedFile();
103 FileFilter filter = chooser.getFileFilter();
104 if(filter instanceof ExtensionFilter){
105 ExtensionFilter ef = (ExtensionFilter)filter;
106 if(!filter.accept(file)){
107 String[] extensions = ef.getExtensions();
108 file = setExtension(file, extensions[0]);
117 private File setExtension(File file, String ext){
118 String name = file.getName();
119 int index = name.lastIndexOf('.');
122 n = n.substring(0, index);
124 name = n + '.' + ext;
125 return new File(file.getParentFile(), name);