// BriefcaseView.java. 11/4/2023. package testbriefcasemcv; import java.awt.Font; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.text.DateFormat; import java.util.Date; import java.util.Vector; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import fwgeneral.ColorAux; import fwgeneral.FileObjectRead; import fwgeneral.FileUtility; import fwgeneral.Sorters; //import fwgeneral.swing.JFileChooserCheckApplication; import fwgeneral.swing.JFileSelector; import fwgeneral.swing.JFileSelectorInterface; import fwgeneral.swing.JTextPaneOutput; import fwgeneral.swing.JWindowUtility; public class BriefcaseView extends JFrame implements JFileSelectorInterface { // JFileSelectorInterface method public void selectedFilePaths(String[] strFilePaths) { if(strFilePaths == null) { return; } // Extract folder name String strHardDriveVolumePath = c.getHardDriveVolumePath(); String strFolderName = strFilePaths[0].substring(strHardDriveVolumePath.length()); c.setStrFolderName(strFolderName); //c.getStrFolderName(); // I believe I want to replace call with manageFoldersAndFiles) c.createNewBriefcaseFolder(); fileSelector.dispose(); setOKCancelButtons(); } private static final long serialVersionUID = 1L; private JPanel contentPane; BriefcaseController c; JList listFolderNames; JFileSelector fileSelector; JButton buttonAddFolder, buttonRemoveFolder, buttonCheckAll, buttonFromHD, buttonUSB, buttonOK, buttonCancel, buttonRefresh; JCheckBox checkBoxNoMetaData; JLabel labelToFrom; JTextPaneOutput textPaneOutput; JProgressBar progressBar; private JTextField textFieldBriefcaseVolume; FileObjectRead fileread; long lDateBriefcase; boolean bUpdateBriefcaseFolderInfoOK = true; String[] strFolderNames = new String[0]; String[] strHardDriveFolders; String[][] strHardDriveFileNames; long[][] lHardDriveFileDates; Vector vecDeleteFiles = new Vector(); Vector vecDeleteFolders = new Vector(); Vector vecCreateFolders = new Vector(); Vector vecCopyFiles = new Vector(); boolean bWarnings; int iCheckAllFolderNumber = Integer.MAX_VALUE; String strHardDriveDocumentsPath = FileUtility.getDocumentsPath(); int iTask; boolean bAllFoldersInSync = true; String strBriefcaseFolderDirectory, strHardDriveFolderDirectory; String strFromFolderDirectory, strToFolderDirectory; // Initialize inner classes CadOutput cadOutput = new CadOutput(); CadFired cadFired = new CadFired(); CadProgressBar viewProgressBar = new CadProgressBar(); public BriefcaseView(BriefcaseController c) { this.c = c; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 676, 532); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(null); setContentPane(contentPane); JScrollPane scrollPaneFolders = new JScrollPane(); scrollPaneFolders.setBounds(12, 28, 363, 179); contentPane.add(scrollPaneFolders); listFolderNames = new JList(); listFolderNames.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent arg0) { cadFired.listFolderFired(); } }); scrollPaneFolders.setViewportView(listFolderNames); JScrollPane scrollPaneOutput = new JScrollPane(); scrollPaneOutput.setBounds(12, 270, 648, 220); contentPane.add(scrollPaneOutput); buttonAddFolder = new JButton("Add Folder"); buttonAddFolder.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { buttonNewFolderFired(); iTask = 1; CadFired task = new CadFired(); task.start(); } }); buttonRemoveFolder = new JButton("Remove Folder"); buttonRemoveFolder.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { buttonRemoveFolderFired(); iTask = 0; CadFired task = new CadFired(); task.start(); } }); buttonRemoveFolder.setBounds(535, 30, 135, 25); contentPane.add(buttonRemoveFolder); buttonAddFolder.setBounds(388, 30, 135, 25); contentPane.add(buttonAddFolder); buttonCheckAll = new JButton("Check All"); buttonCheckAll.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { iTask = 2; CadFired task = new CadFired(); task.start(); //buttonCheckAllFired(); } }); buttonCheckAll.setBounds(403, 120, 257, 25); contentPane.add(buttonCheckAll); buttonFromHD = new JButton("From HD to USB"); buttonFromHD.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { iTask = 3; CadFired task = new CadFired(); task.start(); //buttonFromBriefcaseFired(); } }); buttonFromHD.setOpaque(false); buttonFromHD.setBounds(388, 60, 135, 25); contentPane.add(buttonFromHD); buttonUSB = new JButton("From USB to HD"); buttonUSB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { iTask = 4; CadFired task = new CadFired(); task.start(); //buttonToBriefcaseOrCancelFired(); } }); buttonUSB.setBounds(535, 60, 135, 25); contentPane.add(buttonUSB); buttonUSB.setOpaque(false); buttonOK = new JButton("OK"); buttonOK.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { iTask = 5; CadFired task = new CadFired(); task.start(); } }); buttonOK.setOpaque(false); buttonOK.setBounds(388, 90, 135, 25); contentPane.add(buttonOK); buttonCancel = new JButton("Cancel"); buttonCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { iTask = 6; CadFired task = new CadFired(); task.start(); } }); buttonCancel.setBounds(535, 90, 135, 25); contentPane.add(buttonCancel); buttonCancel.setOpaque(false); buttonRefresh = new JButton("Refresh Volume"); buttonRefresh.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setupScreen(); } }); buttonRefresh.setBounds(462, 182, 135, 25); contentPane.add(buttonRefresh); checkBoxNoMetaData = new JCheckBox("Do not report Eclipse metadata"); checkBoxNoMetaData.setSelected(true); checkBoxNoMetaData.setBounds(412, 240, 234, 25); contentPane.add(checkBoxNoMetaData); labelToFrom = new JLabel("Label"); labelToFrom.setHorizontalAlignment(SwingConstants.CENTER); labelToFrom.setForeground(ColorAux.red); labelToFrom.setFont(new Font("Tahoma", Font.BOLD, 13)); labelToFrom.setBounds(403, 150, 257, 20); contentPane.add(labelToFrom); progressBar = new JProgressBar(); progressBar.setBounds(12, 215, 363, 25); contentPane.add(progressBar); progressBar.setVisible(false); progressBar.setStringPainted(true); textPaneOutput = new JTextPaneOutput(scrollPaneOutput); textPaneOutput.setFont(new Font("Tahoma", Font.PLAIN, 12)); textPaneOutput.setFontFamily("Dialog"); textPaneOutput.paint(textPaneOutput.getGraphics()); // Tabs for the file creation dates int[] iTabs = { 350, 510 }; textPaneOutput.setTabs(iTabs); textFieldBriefcaseVolume = new JTextField(); textFieldBriefcaseVolume.setBounds(399, 210, 261, 26); contentPane.add(textFieldBriefcaseVolume); textFieldBriefcaseVolume.setColumns(10); if(FileUtility.isOSMac()) { textFieldBriefcaseVolume.setText("/Volumes/ESD-USB"); } else { textFieldBriefcaseVolume.setText("e:"); } setTitle(JWindowUtility.constructTitle("Briefcase", c.getVersion(), strHardDriveDocumentsPath)); JWindowUtility.center(this); /* String strBriefcaseMasterFolderDirectory = FileUtility.constructDirectory( textFieldBriefcaseVolume.getText(), c.getBriefcaseDirectoryName()); if(!FileUtility.isDirectory(strBriefcaseMasterFolderDirectory)) { FileUtility.createDirectory(strBriefcaseMasterFolderDirectory); } */ initializeButtons(); setupScreen(); setVisible(true); } protected void initializeButtons() { hideButtons(); buttonAddFolder.setVisible(true); buttonRefresh.setVisible(true); buttonCheckAll.setVisible(true); bWarnings = false; } private void setSelectedFolderButtons() { hideButtons(); buttonRemoveFolder.setVisible(true); buttonFromHD.setVisible(true); buttonUSB.setVisible(true); //buttonOK.setVisible(true); //buttonCancel.setVisible(true); buttonCheckAll.setVisible(true); buttonRefresh.setVisible(true); /* buttonAddFolder.setVisible(false); buttonAddFolder.setEnabled(true); buttonToBriefcase.setText("Cancel"); buttonToBriefcase.setForeground(ColorAux.black); buttonToBriefcase.setVisible(true); buttonToBriefcase.setEnabled(true); labelToFrom.setVisible(true); */ } private void setOKCancelButtons() { hideButtons(); buttonOK.setVisible(true); buttonCancel.setVisible(true); } private void hideButtons() { buttonAddFolder.setVisible(false); buttonRemoveFolder.setVisible(false); buttonUSB.setVisible(false); buttonFromHD.setVisible(false); buttonCheckAll.setVisible(false); buttonOK.setVisible(false); buttonCancel.setVisible(false); buttonRefresh.setVisible(false); } /* private void setEnabledButtons(boolean b) { buttonAddFolder.setEnabled(b); buttonToBriefcase.setEnabled(b); buttonFromBriefcase.setEnabled(b); buttonRemoveFolder.setEnabled(b); buttonCheckAll.setEnabled(b); } */ protected void setupScreen() { // Check for folder root directory **** this should be in Model c.checkForFolderRootDirectory(); String[] strFolderNames = c.getBriefcaseFolderNames(); // Alphabetize folder names Sorters.sortArray(strFolderNames); // Write list of folders in briefcase master to screen listFolderNames.setListData(strFolderNames); // List the folder names this.requestFocus(); } protected String getBriefcaseVolume() { return textFieldBriefcaseVolume.getText(); } public void buttonCheckAllFired() { iCheckAllFolderNumber = 0; String[] strFolderNames = c.getBriefcaseFolderNames(); for(int i=0; i 0) { // this.setOKCancelButtons(); } } private void setFromOrToButtonLabel(boolean bFromHD) { // Set buttons setSelectedFolderButtons(); // Label describing from of to briefcase c.setbFromHardDrive(bFromHD); if (bFromHD) { labelToFrom.setText("From HD"); } else { labelToFrom.setText("From USB"); } cadOutput.setWarnings(false); // Compare folders c.setFromToFolderFileInfo(); } private void buttonRemoveFolderFired() { String strSelectedFolderName = (String) listFolderNames.getSelectedValue(); c.setStrFolderName(strSelectedFolderName); c.removeExistingBriefcaseFolder(); } private void buttonNewFolderFired() { // Select folder from hard drive fileSelector = new JFileSelector(this, this, "D", null); // JFileSelector interface, selectedFilePaths(...), calls the method createNewBriefcaseFolder(...) } // Buttons fired class CadFired extends Thread { public void run() { // Clear output pane textPaneOutput.setText(""); if(iTask == 0) { buttonRemoveFolderFired(); } else if(iTask == 1) { buttonNewFolderFired(); } else if(iTask == 2) { buttonCheckAllFired(); } else if (iTask == 3) { buttonFromHDFired(); } else if(iTask == 4) { buttonFromUSBFired(); } else if(iTask == 5) { buttonOKFired(); } else if(iTask == 6) { buttonCancelFired(); } } protected void buttonRemoveFolderFired() { c.constructFolderAndFileVectors(); hideButtons(); setOKCancelButtons(); cadOutput.writeOperationsAndWarnings(); } protected void buttonFromHDFired() { c.setbFromHardDrive(true); buttonUSB.setForeground(ColorAux.black); buttonFromHD.setForeground(ColorAux.red); setFromOrToButtonLabel(true); c.constructFolderAndFileVectors(); cadOutput.writeOperationsAndWarnings(); hideButtons(); setOKCancelButtons(); } protected void buttonFromUSBFired() { c.setbFromHardDrive(false); buttonUSB.setForeground(ColorAux.red); buttonFromHD.setForeground(ColorAux.black); setFromOrToButtonLabel(false); c.constructFolderAndFileVectors(); cadOutput.writeOperationsAndWarnings(); hideButtons(); setOKCancelButtons(); } protected void buttonNewFolderFired() { c.constructFolderAndFileVectors(); hideButtons(); setOKCancelButtons(); cadOutput.writeOperationsAndWarnings(); } private void buttonOKFired() { c.updateFoldersAndFiles(); c.setupScreen(); textPaneOutput.setText(""); initializeButtons(); //checkForNextFolder(); } private void buttonCancelFired() { initializeButtons(); bAllFoldersInSync = false; //checkForNextFolder(); } protected void listFolderFired() { // Set buttons setSelectedFolderButtons(); String strSelectedFolderName = (String) listFolderNames.getSelectedValue(); if(strSelectedFolderName != null ) { c.setStrFolderName(strSelectedFolderName); } c.checkFolderTimes(); boolean bFromHardDriveMoreRecent = c.isbFromHardDriveMoreRecent(); if (bFromHardDriveMoreRecent) { buttonUSB.setForeground(ColorAux.black); buttonFromHD.setForeground(ColorAux.red); textPaneOutput.setForegroundColor(ColorAux.black); } else { buttonUSB.setForeground(ColorAux.black); buttonFromHD.setForeground(ColorAux.red); textPaneOutput.setForegroundColor(ColorAux.black); } } } class CadOutput { /* // Determine if hidden or visible operation and update counts bMetaDirectoryExcluded = isMetaDirectoryExcluded(strToFolderDirectory); if (bMetaDirectoryExcluded) { iNbrOfHiddenOperations++; return; } iNbrOfVisibleOperations++; */ final String strIndentBlanks = " "; String strIndent = ""; String strTopLevelFolderCurrent = "", strToFolderDirectoryCurrent = ""; boolean bMetaDirectoryExcluded; int iNbrOfVisibleOperations = 0; int iNbrOfHiddenOperations = 0; int iNbrOfWarnings = 0; DateFormat dateFormatDate, dateFormatTime; CadOutput() { dateFormatDate = DateFormat.getDateInstance(DateFormat.SHORT); dateFormatTime = DateFormat.getTimeInstance(DateFormat.SHORT); } // Write create or delete information for directory; called by Model via Controller protected void writeDirInfoToTextArea(String strTypeOfOperation, String strToFolderDirectory) { iNbrOfVisibleOperations = 0; iNbrOfHiddenOperations = 0; int iNbrOfWarnings = 0; // Determine if meta directory is legitimate and set boolean boolean bLegitimateOperation = strTypeOfOperation.equals("Create") || strTypeOfOperation.equals("Delete"); // Determine if meta directory excluded and if Directory set boolean boolean bMetaDirectoryExcluded = isMetaDirectoryExcluded(strToFolderDirectory); if (bMetaDirectoryExcluded) { if(bLegitimateOperation) { // Count "legitimate" operation iNbrOfHiddenOperations++; } return; } if(bLegitimateOperation) { // Count "legitimate" operation iNbrOfHiddenOperations++; } // Account for the first line writing the briefcase name if(!strTypeOfOperation.equals("Briefcase")) { iNbrOfVisibleOperations++; } // Prevents folder from being written multiple times // Write only if this is a hasn't been written previously if (!strToFolderDirectory.equals(strToFolderDirectoryCurrent)) { writeLineToTextArea("Folder " + strTypeOfOperation + ": " + strToFolderDirectory); strToFolderDirectoryCurrent = strToFolderDirectory; } } // Write creating or deleting introduction for file; called by Model via Controller // Calls textPaneOutput to perform the actual write protected void writeFileInfoToTextArea(String strTypeOfOperation, String strToFolderDirectory, String strToFileNameExt, long lDate) { // Must be a legitimate operation, either hidden or visible boolean bMetaDirectoryExcluded = isMetaDirectoryExcluded(strToFolderDirectory); if (bMetaDirectoryExcluded) { // Legitimate hidden operation iNbrOfHiddenOperations++; return; } // Legitimate visible operation iNbrOfVisibleOperations++; // Prevents folder from being written multiple times // Write only if this is a hasn't been written previously if (!strToFolderDirectory.equals(strToFolderDirectoryCurrent)) { writeLineToTextArea("Folder " + "Update" + ": " + strToFolderDirectory); strToFolderDirectoryCurrent = strToFolderDirectory; } String strToFileLine = strIndentBlanks + strTypeOfOperation + ": " + strToFileNameExt + "\t" + dateAndTimeToString(lDate); writeLineToTextArea(strToFileLine); } // Write update introduction information for file; called by Model via Controller // Calls textPaneOutput to perform the actual write protected void writeFileInfoToTextArea(String strTypeOfOperation, String strToFolderDirectory, String strToFileNameExt, long lDateFrom, long lDateTo) { // Must be a legitimate operation, either hidden or visible bMetaDirectoryExcluded = isMetaDirectoryExcluded(strToFolderDirectory); if (bMetaDirectoryExcluded) { return; } if(lDateFrom < lDateTo) { bWarnings = true; iNbrOfWarnings++; } String strUpdateLine = strIndentBlanks + strTypeOfOperation + ": " + strToFileNameExt + "\t" + dateAndTimeToString(lDateFrom) + "\t" + dateAndTimeToString(lDateTo); writeLineToTextArea(strUpdateLine); } // Called by all write's to write information to text area; does the actual write private void writeLineToTextArea(String strLine) { // Must be visible boolean bFolder = strLine.startsWith("Folder "); if (bFolder) { int iNbrOfIndents = calNbrOfFileSeparators(strLine) - 2; //if (iNbrOfIndents < 0) { //return; //} strIndent = ""; for (int i = 0; i < iNbrOfIndents; i++) { strIndent += strIndentBlanks; } } else { if (strLine.indexOf("\t.") >= 0) { return; } } int iBeginningIndex = strLine.indexOf(" ") + 1; int iEndingIndex = strLine.indexOf(":"); String strTypeOfOperation = strLine.substring(iBeginningIndex, iEndingIndex); setLineColor(strTypeOfOperation); // Indent all lines, except for Briefcase name (which ends with File.separator) if(strLine.endsWith(File.separator)) { // Briefcase name: Set bold font strLine = "Briefcase " + strLine.substring(strLine.indexOf(File.separator)); // Goes directory to textPaneOutput textPaneOutput.setBold(true); } else { strLine = strIndentBlanks + strLine; } // Write line to output pane textPaneOutput.append(strLine + "\n"); // Restore font: Set normal font textPaneOutput.setBold(false); } private boolean isMetaDirectoryExcluded(String strToFolderDirectory) { boolean bMetaData = strToFolderDirectory.indexOf(File.separator + ".") >= 0 || strToFolderDirectory.endsWith(File.separator + "bin" + File.separator); boolean bDirectoryExcluded = bMetaData && checkBoxNoMetaData.isSelected(); return bDirectoryExcluded; } public String dateAndTimeToString(long lDate) { Date date = new Date(lDate); return (dateFormatDate.format(date) + " " + dateFormatTime.format(date)); } public int calNbrOfFileSeparators(String strLine) { int iNbrOfOfFielSeparators = 0; String strTemp = strLine; int iFileSeparatorColumn = 0; while ((iFileSeparatorColumn = strTemp.indexOf(File.separator)) >= 0) { iNbrOfOfFielSeparators++; strTemp = strTemp.substring(iFileSeparatorColumn + 1); } return iNbrOfOfFielSeparators; } public void setWarnings(boolean bWarning) { if (bWarning) { iNbrOfWarnings++; textPaneOutput.setForegroundColor(ColorAux.red); } else { textPaneOutput.setForegroundColor(ColorAux.black); // textPaneOutput.setText(""); } textPaneOutput.setBold(bWarning); } private void setLineColor(String strTypeOfOperation) { if (strTypeOfOperation.indexOf("Create") >= 0) { textPaneOutput.setForegroundColor(ColorAux.darkGreen); } else if (strTypeOfOperation.indexOf("Copy") >= 0) { if (bWarnings) { textPaneOutput.setForegroundColor(ColorAux.red); bAllFoldersInSync = false; } else { textPaneOutput.setForegroundColor(ColorAux.darkGreen); } } else if (strTypeOfOperation.indexOf("Remove") >= 0 || strTypeOfOperation.indexOf("Delete") >= 0) { iNbrOfWarnings++; bAllFoldersInSync = false; textPaneOutput.setForegroundColor(ColorAux.red); } else if (strTypeOfOperation.indexOf("Update") >= 0) { if (bWarnings) { textPaneOutput.setForegroundColor(ColorAux.red); bAllFoldersInSync = false; } else { textPaneOutput.setForegroundColor(ColorAux.darkGreen); } } else { textPaneOutput.setForegroundColor(ColorAux.black); } } private void writeOperationsAndWarnings() { String strLine = "Number of visible operations=" + iNbrOfVisibleOperations + " " + "Number of hidden operations=" + iNbrOfHiddenOperations + " " + "Number of warnings=" + iNbrOfWarnings + "\n"; textPaneOutput.append(strLine); int iNumberOfOperationsAndWarnings = iNbrOfVisibleOperations + iNbrOfHiddenOperations + iNbrOfWarnings; if(iNumberOfOperationsAndWarnings == 0) { textPaneOutput.append("Folder(s) synchronized.\n"); } } } class CadProgressBar { // Progress bar methods protected void setVisible(boolean bVisible) { progressBar.setVisible(bVisible); } protected void setMaximum(int iValue) { progressBar.setMaximum(iValue); } protected void setValue(int iValue) { progressBar.setValue(iValue); } protected void setString(String strLine) { progressBar.setString(strLine); } } }