View Javadoc

1   package org.catacomb.druid.util;
2   
3   import org.catacomb.druid.swing.DFileChooser;
4   import org.catacomb.interlish.service.AppPersist;
5   
6   import java.io.File;
7   import java.util.HashMap;
8   
9   import javax.swing.JFileChooser;
10  
11  
12  
13  public class FileChooser {
14  
15      static FileChooser chooser;
16  
17  
18      public static FileChooser getChooser() {
19          if (chooser == null) {
20              chooser = new FileChooser();
21          }
22          return chooser;
23      }
24  
25      DFileChooser dFileChooser;
26  
27      HashMap<String, File> hmap;
28  
29  
30  
31      public FileChooser() {
32          dFileChooser = new DFileChooser();
33  
34          hmap = new HashMap<String, File>();
35      }
36  
37  
38  
39      public void addExtensionFilter(String exts, String desc) {
40          dFileChooser.addExtensionFilter(exts, desc);
41      }
42  
43  
44      public File getFileToImport() {
45          return getFile("import", "import");
46      }
47  
48  
49      public File getFileToOpen(String mode) {
50          return getFile(mode, "open");
51      }
52  
53      public File getFileToWrite(String mode, String ext, String desc) {
54          addExtensionFilter(ext, desc);
55          dFileChooser.setFilter(ext);
56          return getFile(mode, "save");
57      }
58  
59      public File getFileToWrite(String mode) {
60          return getFile(mode, "save");
61      }
62  
63      public void setDefaultFolderForMode(String mode, File fdir) {
64          if (hmap.containsKey(mode)) {
65              // leave as is;
66          } else {
67              hmap.put(mode, fdir);
68          }
69      }
70  
71  
72  
73      private void applyMode(String mode) {
74          if (hmap.containsKey(mode)) {
75              File fdir = hmap.get(mode);
76              dFileChooser.setRootDirectory(fdir);
77  
78          } else {
79              String pel = "LastDir" + mode;
80              if (AppPersist.hasValueFor(pel)) {
81  
82                  String path = AppPersist.getValueFor(pel);
83                  File fpar = new File(path);
84                  if (fpar.exists() && fpar.isDirectory()) {
85                      dFileChooser.setRootDirectory(fpar);
86                  }
87              }
88          }
89      }
90  
91  
92      public File getFile(String mode, String approve) {
93          applyMode(mode);
94  
95          dFileChooser.setApproveButtonText(approve);
96  
97          int retval = dFileChooser.showDialog(null, approve);
98          File f = null;
99          if (retval == JFileChooser.APPROVE_OPTION) {
100             f = dFileChooser.getSelectedFile();
101         }
102 
103         cacheDir(f, mode);
104         return f;
105     }
106 
107     private void cacheDir(File f, String mode) {
108         if (f == null) {
109 
110         } else {
111             File fpar = f;
112             if (!f.isDirectory()) {
113                 fpar = f.getParentFile();
114             }
115 
116             String path = fpar.getAbsolutePath();
117             AppPersist.setValue("LastDir" + mode, path);
118 
119             hmap.put(mode, fpar);
120         }
121     }
122 
123 
124     public File getDirectory(File fdir, String approve) {
125         dFileChooser.setSelectDirectories();
126         if (fdir != null) {
127             // dFileChooser.setCurrentDirectory(new File(fdir.getParent()));
128             dFileChooser.setSelectedFile(fdir);
129         }
130 
131         File fret =  getDirectory(approve);
132         cacheDir(fret, "default");
133         return fret;
134     }
135 
136 
137     public File getDirectory(String mode, String approve) {
138         applyMode(mode);
139 
140         File fret = getDirectory(approve);
141         cacheDir(fret, mode);
142         return fret;
143     }
144 
145 
146     private File getDirectory(String approve) {
147         dFileChooser.setSelectDirectories();
148         dFileChooser.setApproveButtonText(approve);
149         int retval = dFileChooser.showDialog(null, approve);
150 
151         File f = null;
152         if (retval == JFileChooser.APPROVE_OPTION) {
153             f = dFileChooser.getSelectedFile();
154         }
155 
156         if (f != null && !f.isDirectory()) {
157             f = new File(f.getParent());
158         }
159         return f;
160     }
161 
162 
163 
164     public File getFolder(File fdef) {
165         return getDirectory(fdef, "select");
166     }
167 
168 
169 }