/src/mpv5/ui/dialogs/DialogForFile.java
Java | 428 lines | 288 code | 44 blank | 96 comment | 59 complexity | 04bed4dfa1fd90c06f82580454a97513 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
1/* 2 * This file is part of YaBS. 3 * 4 * YaBS is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * YaBS is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with YaBS. If not, see <http://www.gnu.org/licenses/>. 16 */ 17package mpv5.ui.dialogs; 18 19import java.awt.Container; 20import java.io.File; 21import java.io.FileNotFoundException; 22import java.io.IOException; 23import java.util.List; 24import javax.swing.JComponent; 25import javax.swing.JFileChooser; 26import javax.swing.JTextField; 27import javax.swing.filechooser.FileFilter; 28import mpv5.Main; 29import mpv5.globals.Constants; 30import mpv5.globals.Messages; 31import mpv5.logging.Log; 32import mpv5.ui.frames.MPView; 33import mpv5.utils.export.Export; 34import mpv5.utils.files.FileDirectoryHandler; 35import mpv5.utils.jobs.Waiter; 36 37/** 38 * This class is useful for selecting files and directories 39 * 40 */ 41public class DialogForFile extends JFileChooser implements Waiter { 42 43 private static final long serialVersionUID = 1L; 44 public static FileFilter DIRECTORIES = new FileFilter() { 45 46 public boolean accept(File f) { 47 return f.isDirectory(); 48 } 49 50 public String getDescription() { 51 return "Directories"; 52 } 53 }; 54 public static FileFilter TEMPLATE_FILES = new FileFilter() { 55 56 public boolean accept(File f) { 57 return f.getName().toLowerCase().endsWith(".odt") || f.getName().toLowerCase().endsWith(".pdf") || f.isDirectory(); 58 } 59 60 public String getDescription() { 61 return "Templates"; 62 } 63 }; 64 public static FileFilter HTML_FILES = new FileFilter() { 65 66 public boolean accept(File f) { 67 return f.getName().toLowerCase().endsWith(".html") || f.getName().toLowerCase().endsWith(".htm") || f.isDirectory(); 68 } 69 70 public String getDescription() { 71 return "HTML Format(*.htm[l])"; 72 } 73 }; 74 public static FileFilter XML_FILES = new FileFilter() { 75 76 public boolean accept(File f) { 77 return f.getName().toLowerCase().endsWith(".xml") || f.isDirectory(); 78 } 79 80 public String getDescription() { 81 return "XML Format(*.xml)"; 82 } 83 }; 84 public static FileFilter CSV_FILES = new FileFilter() { 85 86 public boolean accept(File f) { 87 return f.getName().toLowerCase().endsWith(".csv") || f.isDirectory(); 88 } 89 90 public String getDescription() { 91 return "CSV Format(*.csv)"; 92 } 93 }; 94 public static FileFilter PDF_FILES = new FileFilter() { 95 96 public boolean accept(File f) { 97 return f.getName().toLowerCase().endsWith(".pdf") || f.isDirectory(); 98 } 99 100 public String getDescription() { 101 return "PDF Format(*.pdf)"; 102 } 103 }; 104 public static FileFilter SQL_FILES = new FileFilter() { 105 106 public boolean accept(File f) { 107 return f.getName().toLowerCase().endsWith(".sql") || f.isDirectory(); 108 } 109 110 public String getDescription() { 111 return "SQL Format(*.sql)"; 112 } 113 }; 114 public static FileFilter OOO_FILES = new FileFilter() { 115 116 public boolean accept(File f) { 117 return f.getName().toLowerCase().matches(".*sxw$|.*doc$|.*xls$|.*odt$|.*ods$|.*pps$|.*odt$|.*ppt$|.*odp$") || f.isDirectory(); 118 } 119 120 public String getDescription() { 121 return "Open Office Formats(*.odt etc.)"; 122 } 123 }; 124 private File file = null; 125 public static File CURRENT_DIR = new File(Main.USER_HOME); 126 private Container mparent; 127 128 /** 129 * Create a new dialog for files and dirs 130 */ 131 public DialogForFile() { 132 super(); 133 this.setFileSelectionMode(DialogForFile.FILES_AND_DIRECTORIES); 134 this.setSelectedFile(CURRENT_DIR); 135 } 136 137 /** 138 * Create a new dialog for the given selection mode 139 * 140 * @param selectionMode DialogForFile.MODE 141 */ 142 public DialogForFile(int selectionMode) { 143 super(); 144 this.setFileSelectionMode(selectionMode); 145 this.setSelectedFile(CURRENT_DIR); 146 } 147 148 /** 149 * Create a new dialog for files and dirs with the given file selected 150 * 151 * @param file 152 */ 153 public DialogForFile(File file) { 154 super(); 155 if (!file.isDirectory()) { 156 this.setFileSelectionMode(DialogForFile.FILES_AND_DIRECTORIES); 157 this.setSelectedFile(file); 158 } else { 159 this.setFileSelectionMode(DialogForFile.FILES_ONLY); 160 this.setSelectedFile(new File(file.getPath() + File.separator + "file")); 161 } 162 } 163 164 /** 165 * Create a new dialog for the given selection mode with the given file 166 * seleced 167 * 168 * @param mode 169 * @param filename 170 */ 171 public DialogForFile(int mode, String filename) { 172 super(); 173 this.setFileSelectionMode(mode); 174 this.setSelectedFile(new File(filename)); 175 } 176 177 /** 178 * Create a new dialog for the given selection mode with the given file 179 * seleced 180 * 181 * @param mode 182 * @param file 183 */ 184 public DialogForFile(int mode, File file) { 185 super(); 186 this.setFileSelectionMode(mode); 187 this.setSelectedFile(file); 188 } 189 190 /** 191 * Show a file selection dialog 192 * 193 * @return true if a file/dir was selected 194 */ 195 public boolean chooseFile() { 196 try { 197 if (this.showOpenDialog(mparent == null ? mpv5.YabsViewProxy.instance().getIdentifierFrame() : mparent) == JFileChooser.APPROVE_OPTION) { 198 try { 199 this.file = this.getSelectedFile(); 200 setCurrentDir(file); 201 return true; 202 } catch (Exception ex) { 203 Log.Debug(this, ex); 204 } 205 } 206 } catch (Exception n) { 207 Log.Debug(this, n.getMessage()); 208 } 209 return false; 210 } 211 212 /** 213 * Show a file save dialog 214 * 215 * @return true if a file/dir was selected 216 */ 217 public boolean saveFile() { 218 try { 219 if (this.showSaveDialog(mparent == null ? mpv5.YabsViewProxy.instance().getIdentifierFrame() : mparent) == JFileChooser.APPROVE_OPTION) { 220 try { 221 if (!this.getSelectedFile().exists()) { 222 this.file = this.getSelectedFile(); 223 setCurrentDir(file); 224 return true; 225 } else { 226 if (!Popup.Y_N_dialog(Messages.FILE_EXISTS + "\n" + getSelectedFile())) { 227 saveFile(); 228 } else { 229 this.file = this.getSelectedFile(); 230 CURRENT_DIR = file.getParentFile(); 231 return true; 232 } 233 } 234 } catch (Exception ex) { 235 Log.Debug(this, ex); 236 } 237 } 238 } catch (Exception n) { 239 Log.Debug(this, n.getMessage()); 240 } 241 return false; 242 } 243 244 /** 245 * Show a file save dialog 246 * 247 * @param fileToSave 248 */ 249 public File saveFile(File fileToSave) { 250 setSelectedFile(new File(fileToSave.getName())); 251 if (this.showSaveDialog(mparent == null ? mpv5.YabsViewProxy.instance().getIdentifierFrame() : mparent) == JFileChooser.APPROVE_OPTION) { 252 try { 253 this.file = this.getSelectedFile(); 254 if (!file.exists()) { 255 try { 256 file.createNewFile(); 257 } catch (IOException ex) { 258 mpv5.logging.Log.Debug(ex); //Logger.getLogger(DialogForFile.class.getName()).log(Level.SEVERE, null, ex); 259 } 260 } else { 261 if (!Popup.Y_N_dialog(Messages.FILE_EXISTS + "\n" + file)) { 262 saveFile(file); 263 } 264 } 265 FileDirectoryHandler.copyFile2(fileToSave, file); 266 } catch (FileNotFoundException ex) { 267 Log.Debug(ex); 268 } catch (IOException ex) { 269 Log.Debug(ex); 270 } 271 return file; 272 } 273 274 return null; 275 } 276 277 /** 278 * 279 * @return The selected file 280 */ 281 public File getFile() { 282 return file; 283 } 284 285 /** 286 * Show a file selection dialog 287 * 288 * @param field This gets the selected files' canonical path 289 * @return True if a file was selected 290 */ 291 public boolean getFilePath(JTextField field) { 292 if (this.showOpenDialog(mparent == null ? mpv5.YabsViewProxy.instance().getIdentifierFrame() : mparent) == JFileChooser.APPROVE_OPTION) { 293 try { 294 field.setText(this.getSelectedFile().getCanonicalPath()); 295 this.file = this.getSelectedFile(); 296 setCurrentDir(file); 297 return true; 298 } catch (IOException ex) { 299 Log.Debug(this, ex); 300 } 301 } 302 return false; 303 } 304 305 /** 306 * Calls saveFile((File) object) 307 * 308 * @param object 309 * @param e 310 * @throws Exception 311 */ 312 @Override 313 @SuppressWarnings("unchecked") 314 public void set(Object object, Exception e) throws Exception { 315 316 if (e == null) { 317 if (object instanceof List) { 318 319 if (!((List) object).isEmpty()) { 320 if (((List) object).get(0) instanceof File) { 321 saveFiles((List) object); 322 } else if (((List) object).get(0) instanceof Export) { 323 saveFiles2((List) object); 324 } 325 } 326 } else if (object instanceof File) { 327 saveFile((File) object); 328 } else if (object instanceof Export) { 329 File d = getCurrentDirectory(); 330 setSelectedFile(((Export) object).getTargetFile()); 331 setCurrentDirectory(d); 332 if (saveFile()) { 333 FileDirectoryHandler.copyFile2(((Export) object).getTargetFile(), file, false); 334 ((Export) object).getTargetFile().delete(); 335 } 336 } 337 } else { 338 Popup.error(e); 339 } 340 } 341 342 /** 343 * Shows a choose directory dialog and saves a files in the list to it. 344 * 345 * @param <T> 346 * @param list 347 */ 348 public <T extends Export> void saveFiles2(List<T> list) { 349 try { 350 this.setFileSelectionMode(DialogForFile.DIRECTORIES_ONLY); 351 this.setSelectedFile(CURRENT_DIR); 352 353 354 if (this.showSaveDialog(mparent == null ? mpv5.YabsViewProxy.instance().getIdentifierFrame() : mparent) == JFileChooser.APPROVE_OPTION) { 355 if (!this.getSelectedFile().exists()) { 356 this.file = this.getSelectedFile(); 357 setCurrentDir(file); 358 file.mkdirs(); 359 } else { 360 this.file = this.getSelectedFile(); 361 setCurrentDir(file); 362 } 363 364 for (int i = 0; i < list.size(); i++) { 365 File f3 = list.get(i).getTargetFile(); 366 FileDirectoryHandler.copyFile2(f3, new File(CURRENT_DIR.getPath() + File.separator + f3.getName()), true); 367 } 368 } 369 370 } catch (Exception n) { 371 Log.Debug(this, n.getMessage()); 372 Popup.error(n); 373 } 374 } 375 376 /** 377 * Shows a choose directory dialog and saves a files in the list to it. 378 * 379 * @param <T> 380 * @param list 381 */ 382 public <T extends File> void saveFiles(List<T> list) { 383 try { 384 this.setFileSelectionMode(DialogForFile.DIRECTORIES_ONLY); 385 this.setSelectedFile(CURRENT_DIR); 386 387 if (this.showSaveDialog(mparent == null ? mpv5.YabsViewProxy.instance().getIdentifierFrame() : mparent) == JFileChooser.APPROVE_OPTION) { 388 if (!this.getSelectedFile().exists()) { 389 this.file = this.getSelectedFile(); 390 setCurrentDir(file); 391 file.mkdirs(); 392 } else { 393 this.file = this.getSelectedFile(); 394 setCurrentDir(file); 395 } 396 397 for (int i = 0; i 398 < list.size(); i++) { 399 File f3 = list.get(i); 400 FileDirectoryHandler.copyFile2(f3, new File(CURRENT_DIR.getPath() + File.separator + f3.getName()), true); 401 } 402 } 403 404 } catch (Exception n) { 405 Log.Debug(this, n.getMessage()); 406 Popup.error(n); 407 } 408 } 409 410 /** 411 * Set the modality parent 412 * 413 * @param mparent 414 */ 415 public void setModalityParent(JComponent mparent) { 416 this.mparent = mparent; 417 } 418 419 private void setCurrentDir(File file) { 420 if (file != null) { 421 if (file.isDirectory()) { 422 CURRENT_DIR = file; 423 } else { 424 CURRENT_DIR = file.getParentFile(); 425 } 426 } 427 } 428}