Source: http://myjava.cgilinks.com/java/Toshiba_eclipse-SearchWorkProj-javaSource.txt Eclipse 2024 03 Project on jre22 SearchWork Project on Toshiba external drive 6/13/2024 DESKTOP Sheet Music Search System ================================================================ === CurrentDriveLetter.java import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; public class CurrentDriveLetter { public String getRelativePath() { String relativePath = null; Path currRelativePath = Paths.get(""); String currAbsolutePathString = currRelativePath.toAbsolutePath().toString(); System.out.println("Current absolute path is - " + currAbsolutePathString); relativePath = currAbsolutePathString; return relativePath; } public String getPathRoot() { File file = new File("test.txt"); Path dllPath = Paths.get(file.getAbsolutePath()); Path driveLetter = dllPath.getRoot(); System.out.println("Current root is - " + driveLetter.toString()); return driveLetter.toString(); } } ================================================================ === DirectoryFileWriter.java import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; public class DirectoryFileWriter { //Open new file public void writeDirectoryFiles(ArrayList listOfFiles, String filePath ) { filePath="F:/text.txt"; try (var fw = new FileWriter(filePath, StandardCharsets.UTF_8,false)) { //LocalDate myDTS = LocalDate.now(); if(listOfFiles.size() > 0) { // int iPatternNo = 0; for (String fileName : listOfFiles) { // iPatternNo++; fileName = fileName + "\n"; fw.write(fileName); } } fw.close(); } catch (IOException e) { e.printStackTrace(); } } //writeDirectory } ================================================================ === FilePathResource.java public class FilePathResource { public String getURLResource() { return "http://cisackson.cgilinks.com/COTFCatalog/jComboBox-composersLN.txt"; } public String getComposersResource() { return getDrive() + "Categories/Categories.txt"; } public String getListFileWriterResource() { return getDrive() + "Root/MusicScores/pdf/"; } public String getIndexFileWriterResource() { return getDrive() + "Root/SheetMusicLinks/index.html"; } public String getSheetMusicLinksDirectoryResource() { return getDrive() + "Root/SheetMusicLinks/"; } public String getMusicScoresReadResource() { return getDrive() + "Root/MusicScores/pdf/"; } public String getSearchResultsFileResource() { return getDrive() + "SearchResultsFile/SearchResults.html"; } private String getDrive() { CurrentDriveLetter currentDriveLetter = new CurrentDriveLetter(); return currentDriveLetter.getPathRoot().toString(); } } ================================================================ === HTMLFileWriter.java import java.io.FileWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.time.LocalDate; public class HTMLFileWriter { //Open new file and add the html headings up to table row entries. FilePathResource writeHTMLFile = new FilePathResource(); String filePath = writeHTMLFile.getSearchResultsFileResource(); public void writeNewHTMLPreamble( ) { LocalDate myDTS = LocalDate.now(); try (var fw = new FileWriter(filePath, StandardCharsets.UTF_8,false)) { fw.write("\n"); fw.write("\n"); fw.write("Music Scores Search Results -OFFICE VERSION-"); fw.write("\n"); fw.write("\n"); fw.write("\n"); fw.write("\r\n"); fw.write("\n"); fw.write("\n"); fw.close(); } catch (IOException e) { e.printStackTrace(); } } public void writeHTMLTableRow(String tableRow) { try (var fw = new FileWriter(filePath, StandardCharsets.UTF_8,true)) { fw.write(tableRow); fw.close(); } catch (IOException e) { e.printStackTrace(); } } public void writeHTMLClosing() { try (var fw = new FileWriter(filePath, StandardCharsets.UTF_8,true)) { fw.write("\n"); fw.write("
Sheet Music PDF File Library Search run on " + myDTS + "
Pattern Tokens#File Names
End Pattern TokensEnd #End File Names
\n"); fw.write("\n"); fw.write("\n"); fw.close(); } catch (IOException e) { e.printStackTrace(); } } } ================================================================ === Launcher.java import java.awt.Desktop; import java.io.File; import java.io.IOException; public class Launcher { public void launchHTML() throws IOException { FilePathResource filePathResource = new FilePathResource(); File file = new File(filePathResource.getSearchResultsFileResource()); Desktop.getDesktop().open(file); } } ================================================================ === ListDirectoryFiles.java import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; public class ListDirectoryFiles { public ArrayList getFilesMatchingTokens(String tokens, String dir) throws IOException { //TODO: need to match the array of tokens with a file name, make true if all match with filename, add to returned fileArrayList. 1/26/23 //Create a File object for directory with all files System.out.println("Dir: " + dir); File directoryPath = new File(dir); //List of all files and directories File filesList[] = directoryPath.listFiles(); System.out.println("List of files and directories in directory (" + dir + ":"); ArrayList fileArrayList = new ArrayList(); for(File file : filesList) { String inputString = file.getName().toLowerCase(); if(containsWords(inputString ,tokens)) { fileArrayList.add(file.getPath()); System.out.println("Matched: " + file.getPath()); } } return fileArrayList; } private boolean containsWords(String inputString, String tokens) { //tokens is just a comma-delimited string of items to match. It must be converted to a String Array //with the split() String function. boolean found = true; String[] items = tokens.split(","); for (String item : items) { if (!inputString.contains(item.toLowerCase())) { found = false; break; } } return found; } } =============================================================== === ReadCategoriesSourceFile.java import java.net.*; import java.util.ArrayList; import java.io.*; public class ReadCategoriesSourceFile { public ArrayList getCategoriesResourceFileArray() throws IOException { //Read lines of text from a file into an ArrayList of String items. FilePathResource filePath = new FilePathResource(); String resourceFilePath = filePath.getComposersResource(); ArrayList arrayList = new ArrayList(); URL cisackson; BufferedReader in; String inputLine; try { in = new BufferedReader(new FileReader(resourceFilePath)); while ((inputLine = in.readLine()) != null) { arrayList.add(inputLine); } in.close(); arrayList.sort(null); return arrayList; } catch (IOException e) { e.printStackTrace(); } return arrayList; } } ================================================================ ==== SearchFrame.java import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.JTextField; import java.awt.Font; import javax.swing.JButton; import java.awt.Color; import java.awt.event.ActionListener; import java.io.IOException; import java.io.File; import java.net.URISyntaxException; import java.net.URL; import java.util.ArrayList; import java.awt.event.ActionEvent; import javax.swing.JComboBox; public class SearchFrame extends JFrame { private static final int String = 0; private JPanel contentPane; private JTextField txtTokens; private JTextField txtDefaultFilePath; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { SearchFrame frame = new SearchFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. * @throws IOException */ public SearchFrame() throws IOException { setTitle("Sheet Music File Search"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 807, 399); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("Enter Tokens (comma separated):"); lblNewLabel.setBounds(10, 11, 182, 24); contentPane.add(lblNewLabel); txtTokens = new JTextField(); txtTokens.setFont(new Font("Tahoma", Font.BOLD, 12)); txtTokens.setBounds(10, 31, 780, 30); contentPane.add(txtTokens); txtTokens.setColumns(10); JLabel lblNewLabel_1 = new JLabel("Default Search Path:"); lblNewLabel_1.setBounds(10, 72, 120, 24); contentPane.add(lblNewLabel_1); txtDefaultFilePath = new JTextField(); txtDefaultFilePath.setBounds(140, 74, 650, 22); contentPane.add(txtDefaultFilePath); txtDefaultFilePath.setColumns(10); // FilePathResource filePaths = new FilePathResource(); txtDefaultFilePath.setText(filePaths.getMusicScoresReadResource()); // JLabel lblStatus = new JLabel("Status: "); lblStatus.setBounds(364, 325, 417, 24); contentPane.add(lblStatus); JButton btnExecuteSearch = new JButton("Execute Search"); btnExecuteSearch.addActionListener(new ActionListener() { //btnExecuteSearch clicked - send all necessary data to the search // then obtain the results public void actionPerformed(ActionEvent e) { lblStatus.setText("Status: "); if(txtDefaultFilePath.getText().length() > 0 && txtTokens.getText().length() > 0) { lblStatus.setText("Status: Successful initiation"); SearchValues searchValues = new SearchValues(); searchValues.setSearchPath(txtDefaultFilePath.getText()); searchValues.setSearchTokens(txtTokens.getText()); searchValues.getTokenArray(); try { searchValues.launchFileSearch(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { lblStatus.setText("Status: Path or Tokens are missing!"); } } }); btnExecuteSearch.setForeground(new Color(255, 0, 0)); btnExecuteSearch.setFont(new Font("Tahoma", Font.BOLD, 11)); btnExecuteSearch.setBackground(new Color(192, 192, 192)); btnExecuteSearch.setBounds(140, 98, 135, 23); contentPane.add(btnExecuteSearch); JButton btnLaunchHTMLFile = new JButton("Launch HTML File"); btnLaunchHTMLFile.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub Launcher launchHTML = new Launcher(); try { launchHTML.launchHTML(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); btnLaunchHTMLFile.setBounds(364, 291, 154, 23); contentPane.add(btnLaunchHTMLFile); ReadCategoriesSourceFile categoryFileReader = new ReadCategoriesSourceFile(); ArrayList arrayList; arrayList = categoryFileReader.getCategoriesResourceFileArray(); String[] array = new String[arrayList.size()]; for(int i = 0; i < array.length; i++) { array[i] = arrayList.get(i); System.out.println("Item = " + arrayList.get(i)); } JComboBox comboBoxCategories = new JComboBox(array); comboBoxCategories.setBounds(20, 158, 294, 24); contentPane.add(comboBoxCategories); JButton btnComboChoice = new JButton("Execute Search on Selected Item"); btnComboChoice.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String selectedComboItem; selectedComboItem = comboBoxCategories.getSelectedItem().toString(); lblStatus.setText("Selection from dropdown list: " + selectedComboItem); txtTokens.setText(selectedComboItem); } }); btnComboChoice.setBounds(334, 159, 223, 23); contentPane.add(btnComboChoice); } String getDriveLetter() { CurrentDriveLetter driveLetter = new CurrentDriveLetter(); return driveLetter.getPathRoot(); } } =============================================================== === SearchValues.java import java.io.IOException; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; public class SearchValues { private String searchPath; private String searchTokens; public void setSearchPath(String pSearchPath) { this.searchPath = pSearchPath; } public String getSearchPath() { return this.searchPath; } public void setSearchTokens(String pSearchTokens) { this.searchTokens = pSearchTokens; } public String getSearchTokens() { return this.searchTokens; } ArrayList getTokenArray() { ArrayList strList = new ArrayList(); System.out.println(this.searchTokens); String str = ""; str = this.getSearchTokens(); String[] strSplit = str.split(","); Arrays.asList(strSplit); System.out.println("Size: " + strSplit.length); // Now print the ArrayList for (String s : strSplit) { strList.add(s); System.out.println(s); } return strList; } public void launchFileSearch() throws IOException { ListDirectoryFiles listDirectoryFiles = new ListDirectoryFiles(); ArrayList returnedList = new ArrayList(); returnedList = listDirectoryFiles.getFilesMatchingTokens(this.getSearchTokens(), this.getSearchPath()); if(returnedList.size() > 0) { // MATCHES FOUND System.out.println("Array List size: " + returnedList.size()); HTMLFileWriter htmlFileWriter = new HTMLFileWriter(); htmlFileWriter.writeNewHTMLPreamble(); int ix = 0; for (String s : returnedList) { System.out.println("Matching " + this.getSearchTokens() + ": " + s); htmlFileWriter.writeHTMLTableRow("" + this.getSearchTokens() + "" + ++ix + "" + s + "\n\n"); } htmlFileWriter.writeHTMLClosing(); } else { // NO MATCHES FOUND System.out.println("No Matches Found!"); } } } =============================================================== === -END -