// BriefcaseModel.java. 11/4/2023. //c.setProgressBarMaximum(1); //c.setProgressBarVisible(true); //c.setProgressBarString("Folder Paths: " + c.getStrFolderName()); //c.setProgressBarValue(0); package testbriefcasemcv; import java.io.File; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Vector; import fwgeneral.DateTimeAux; import fwgeneral.FileObjectRead; import fwgeneral.FileObjectWrite; import fwgeneral.FileUtility; import fwgeneral.swing.JWindowUtility; public class BriefcaseModel { BriefcaseController c; Vector vecCreateFolders = new Vector(); Vector vecDeleteFolders = new Vector(); Vector vecDeleteFiles = new Vector(); Vector vecCopyFiles = new Vector(); String strFromRootFolderPath = ""; String[] strFromFolderNames = new String[0]; String[][] strFromFileNameExts = new String[0][]; long[][] lFromFileTimes = new long[0][]; String strToRootFolderPath = ""; String[] strToFolderNames = new String[0]; String[][] strToFileNameExts = new String[0][]; long[][] lToFileTimes = new long[0][]; boolean bUpdateBriefcaseFolderInfoOK = true; int iProgressBarMax = 0; int iProgressBarValue = 0; public BriefcaseModel(BriefcaseController c) { this.c = c; } protected void checkForFolderRootDirectory() { String strBriefcaseRootDirectory = c.getBriefcaseRootFolderPath(); if (!FileUtility.isDirectory(strBriefcaseRootDirectory)) { FileUtility.createDirectory(strBriefcaseRootDirectory); return; } } protected void createNewBriefcaseFolder() { // Called from Selected File Paths in View via Model // (in the seledtedFilePaths method) c.setbFromHardDrive(true); String strNewHardDriveFolderPath = c.constructHardDriveSubFolderPath(); // Check to be certain file is on the hard drive if (!FileUtility.isDirectory(strNewHardDriveFolderPath)) { JWindowUtility.reportMessage("createNewBriefcaseFolder", strNewHardDriveFolderPath + " is NOT a directory."); strNewHardDriveFolderPath = null; return; } getHDInfo(); getUSBInfo(); c.setProgressBarMaximum(strFromFileNameExts.length); constructFolderAndFileVectors(); } protected void removeExistingBriefcaseFolder() { // Called from buttonRemoveFolderFired() in View via Model getUSBInfo(); // Set up progress bar c.setProgressBarVisible(true); c.setupScreen(); c.initializeButtons(); } // Not needed //protected void setupUpdate() { // readAndSetFileInfo(); // constructFolderAndFileVectors(); //} protected void readAndSetFileInfo() { // Called from View via Controller getHDInfo(); getUSBInfo(); } private void getHDInfo() { // Called from readAndSetFileInfo // Hard drive info: Get all folders names on HardDrive DirectoryTree folderTree = new DirectoryTree(); String[] strHardDriveFolderNames = folderTree.getFolderNames(c.constructHardDriveSubFolderPath()); String strFolderPath = FileUtility.constructDirectory(FileUtility.getDocumentsPath(), strHardDriveFolderNames[0]); c.setProgressBarMaximum(strHardDriveFolderNames.length); int iProgressBar = 0; c.setProgressBarVisible(true); // Hard drive info: Get all folders names on HardDrive folderTree = new DirectoryTree(); strHardDriveFolderNames = folderTree.getFolderNames(c.constructHardDriveSubFolderPath()); // Alphabetize the HardDrive folder names replace(strHardDriveFolderNames); sortArray(strHardDriveFolderNames); // Initialize subfolder file names.exts and dates String[][] strHardDriveFileNameExts = new String[strHardDriveFolderNames.length][]; long[][] lHardDriveFileTimes = new long[strHardDriveFolderNames.length][]; for (int i = 0; i < strHardDriveFolderNames.length; i++) { // Construct directory path to strHardDriveFolderNames[i] //if(strHardDriveFolderNames[i].endsWith(File.separator)) { // strHardDriveFolderNames[i] = // strHardDriveFolderNames[i].substring(0, strHardDriveFolderNames[i].length() -1); //} strFolderPath = FileUtility.constructDirectory(FileUtility.getDocumentsPath(), strHardDriveFolderNames[i]); // Get all files.extensions in HardDrive folder i String[] strFileNameExts = FileUtility.getListOfFiles(strFolderPath); strFileNameExts = eliminateDotUnderScoreFiles(strFileNameExts); if (strFileNameExts.length > 0) { strHardDriveFileNameExts[i] = strFileNameExts; } else { strHardDriveFileNameExts[i] = new String[0]; } // Alphabetize strHardDriveFileNameExts[i] sortArray(strHardDriveFileNameExts[i]); // Initialize lHardDriveFileTimes[i] lHardDriveFileTimes[i] = new long[strHardDriveFileNameExts[i].length]; // Get the last modified date for all files in strHardDriveFileNameExts[i] for (int j = 0; j < strHardDriveFileNameExts[i].length; j++) { sortArray(strHardDriveFileNameExts[i]); String strFilePath = FileUtility.constructFilePath(strFolderPath, strHardDriveFileNameExts[i][j]); lHardDriveFileTimes[i][j] = FileUtility.lastModified(strFilePath); } iProgressBar++; c.setProgressBarValue(iProgressBar); } c.setProgressBarVisible(false); // Set from and to directories, file name.exts, and file dates if(c.isbFromHardDrive()) { strFromRootFolderPath = c.getHardDriveRootFolderPath(); strFromFolderNames = strHardDriveFolderNames; strFromFileNameExts = strHardDriveFileNameExts; lFromFileTimes = lHardDriveFileTimes; } else { strToRootFolderPath = c.getHardDriveRootFolderPath(); strToFolderNames = strHardDriveFolderNames; strToFileNameExts = strHardDriveFileNameExts; lToFileTimes = lHardDriveFileTimes; } } private String[] eliminateDotUnderScoreFiles(String[] strFileNameExts) { // Called from getHDInfo to fix a Windows problem Vector vecTemp = new Vector(0); for(int i=0; i= 0; i--) { String strSubFolderPath = vecDeleteFolders.elementAt(i); String strFolderPath = FileUtility.constructDirectory(strBriefcaseRootFolderPath, strSubFolderPath); FileUtility.deleteDirectory(strFolderPath); // Update progress bar iProgressBarValue++; c.setProgressBarValue(iProgressBarValue); } // Create directories // NB: vecCreateFolders.elementAt(0) is already created for (int i = 0; i < vecCreateFolders.size(); i++) { String strSubFolderPath = vecCreateFolders.elementAt(i); String strFolderPath = FileUtility.constructDirectory(strToRootFolderPath, strSubFolderPath); FileUtility.createDirectory(strFolderPath); // Update progress bar iProgressBarValue++; c.setProgressBarValue(iProgressBarValue); } // Copy files for (int i = 0; i < vecCopyFiles.size(); i++) { String[] strFolderFile = vecCopyFiles.elementAt(i); String strFolderSubPath = strFolderFile[0]; String strFileNameExt = strFolderFile[1]; String strFromFolderPath = FileUtility.constructDirectory(strFromRootFolderPath, strFolderSubPath); String strToFolderPath = FileUtility.constructDirectory(strToRootFolderPath, strFolderSubPath); String strFromFilePath = FileUtility.constructFilePath(strFromFolderPath, strFileNameExt); String strToFilePath = FileUtility.constructFilePath(strToFolderPath, strFileNameExt); try { FileUtility.copyFile(strFromFilePath, strToFilePath); } catch (Exception e) { JWindowUtility.reportException("updateFoldersAndFiles()", e); e.printStackTrace(); } // Update progress bar // Update progress bar iProgressBarValue++; c.setProgressBarValue(iProgressBarValue); } if (bUpdateBriefcaseFolderInfoOK) { if (c.isbFromHardDrive()) { c.setProgressBarValue(iProgressBarValue); } } else { JWindowUtility.reportMessage("updateFolder()", "As a consequence of the encountered error(s), this update was aborted."); bUpdateBriefcaseFolderInfoOK = true; } // If To directory is the USB is being update, copy From information to To folder if(c.isbFromHardDrive()) { // Create from folder or subfolder information file path String strBriefcaseSubFolderPath = c.constructBriefcaseSubFolderPath(); updateBriefcaseFolderInfoFile(strBriefcaseSubFolderPath); } // Set subfolders to the current time: Needed to initial From and To buttons long lCurrentTime = DateTimeAux.getTime(); String strHardDrivesubFolderPath = c.constructHardDriveSubFolderPath(); FileUtility.setLastModified(strHardDrivesubFolderPath, lCurrentTime); String strSubFolderPath = c.constructBriefcaseSubFolderPath(); FileUtility.setLastModified(strSubFolderPath, lCurrentTime); // Hide progress bar c.setProgressBarVisible(false); // Clear vectors vecDeleteFolders.clear(); vecDeleteFiles.clear(); vecCreateFolders.clear(); vecCopyFiles.clear(); } protected void checkFolderTimes() { // Called from View via Controller // Needed to initialize From and To folder buttons String strBriefcaseName = c.constructBriefcaseSubFolderPath(); String strHardDriveName = c.constructHardDriveSubFolderPath(); long lBriefcaseTime = FileUtility.lastModified(strBriefcaseName); long lHardDriveTime = FileUtility.lastModified(strHardDriveName); if(lHardDriveTime >= lBriefcaseTime) { c.setbFromHardDriveMoreRecent(true); c.setbFromHardDrive(true); } else { c.setbFromHardDriveMoreRecent(false); c.setbFromHardDrive(false); } } // Alphabetizes private void sortArray(String[] strArray) { char cBackSlash = '\\'; char cSlash = '/'; // Replace separation char with slash for(int i=0; i list = Arrays.asList(strArray); Collections.sort(list, String.CASE_INSENSITIVE_ORDER); // Replace slash with separation char for(int i=0; i vectorFolderPaths = new Vector(0); public String[] getFolderNames(String strMainFolderPath) { // Call by getHDInfo in Model (this file) String[] strFolderPaths = getFolderPaths(strMainFolderPath); String[] strFolderNames = new String[strFolderPaths.length]; for(int i=0; i