001/** 002 * 003 */ 004package jmri.jmrit.operations.setup; 005 006import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 007import java.io.File; 008import java.util.Date; 009 010/** 011 * Represents the set of Operations files that is considered a "Backup" of the 012 * current Operations files. 013 * 014 * It can facilitate the display and selection of backup sets using a GUI. 015 * 016 * This class needs tests....... 017 * 018 * @author Gregory Madsen Copyright (C) 2012 019 * 020 */ 021public class BackupSet { 022 023 private String _setName; 024 025 public String getSetName() { 026 return _setName; 027 } 028 029 private Date _lastModifiedDate; 030 private File _dir; 031 032 public BackupSet(File dir) { 033 _dir = dir; 034 _setName = dir.getName(); 035 _lastModifiedDate = new Date(dir.lastModified()); 036 } 037 038 public void delete() { 039 deleteDirectoryAndFiles(_dir); 040 } 041 042 @SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", justification = "not possible") 043 private void deleteDirectoryAndFiles(File dir) { 044 // Deletes all of the files in a directory, and then the directory 045 // itself. 046 // This is NOT a general purpose method, as it only handles directories 047 // with only files and no sub directories. 048 // This probably needs to handle failures. delete() returns false if it fails. 049 Boolean ok; 050 for (File f : dir.listFiles()) { 051 // Delete files first 052 if (f.isFile()) { 053 ok = f.delete(); 054 if (!ok) { 055 throw new RuntimeException("Failed to delete file: " + f.getAbsolutePath()); // NOI18N 056 } 057 } 058 } 059 060 ok = dir.delete(); 061 if (!ok) { 062 throw new RuntimeException("Failed to delete directory: " + dir.getAbsolutePath()); // NOI18N 063 } 064 } 065 066 @Override 067 public String toString() { 068 return String.format("%s (%s)", _setName, _lastModifiedDate); // NOI18N 069 } 070}