Commit 2f68541c authored by Caelan Reese Wong's avatar Caelan Reese Wong
Browse files

Added the delete molecule function to the GUI

parent a06edd43
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ public class GUI extends JFrame {
    private JButton displayMoleculeButton;
    private JButton findSubgraphButton;
    private JButton downloadPubChemButton;
    private JButton deleteMoleculeButton;
    private JTextField filePathField;
    private static MDB moleculeDb;
    private Socket clientSocket;
@@ -42,6 +43,7 @@ public class GUI extends JFrame {
        JScrollPane scrollPane = new JScrollPane(outputTextArea);
        chooseFileButton = new JButton("Choose File");
        downloadPubChemButton = new JButton("Download PubChem");
        deleteMoleculeButton = new JButton("Delete Molecule");
        addMoleculeButton = new JButton("Add Molecule");
        findMoleculeButton = new JButton("Find Molecule");
        findSubgraphButton = new JButton("Find Subgraph");
@@ -58,6 +60,7 @@ public class GUI extends JFrame {
        controlPanel.setBackground(Color.BLACK);
        controlPanel.add(chooseFileButton);
        controlPanel.add(downloadPubChemButton);
        controlPanel.add(deleteMoleculeButton);
        controlPanel.add(addMoleculeButton);
        controlPanel.add(findMoleculeButton);
        controlPanel.add(findSubgraphButton);
@@ -109,6 +112,23 @@ public class GUI extends JFrame {
            }
        });


        //Action Listener for Delete Molecule button
        deleteMoleculeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Get the molecule path from the text field
                String moleculePath = filePathField.getText();
                // Execute the findMolecule command
                boolean delete= moleculeDb.deleteMolecule(new Molecule(moleculePath));
                if(delete)
                    outputTextArea.append("Successfully Deleted" + "\n\n");
                else
                    outputTextArea.append("Molecule not in the database" + "\n\n");

            }
        });

        // Action listener for Add Molecule button
        addMoleculeButton.addActionListener(new ActionListener() {
            @Override
+23 −0
Original line number Diff line number Diff line
@@ -145,6 +145,29 @@ public class MDB {
        return similar;
    }


    /**
     * Delete a given molecule if it is in the database
     */
    public boolean deleteMolecule(Molecule molecule){
        // Retrieve the partitioned array list based on the number of atoms
        int numAtoms = molecule.getNumAtoms();
        if (!db.containsKey(numAtoms)) {
            return false;
        }
        ArrayList<Molecule> moleculesWithSameNumAtoms = db.get(numAtoms);

        // Iterate through the array list of molecules with the same number of atoms
        for (Molecule dbMolecule : moleculesWithSameNumAtoms) {
            Molecule result = dbMolecule.areMoleculesEqual(molecule);
            if (result != null) {
                this.db.get(numAtoms).remove(dbMolecule);
                return true; // successfully delete the molecule
            }
        }
        return false; // Return false if molecule not in database
    }

    /**
     * Find subgraph
     */
+1 −1
Original line number Diff line number Diff line
@@ -127,7 +127,7 @@ public class Main {
                if(delete)
                    printVerbose("Successfully Deleted");
                else
                    printVerbose("Molecule not deleted or already not in the database");
                    System.out.println("Molecule not in the database");
                break;
            default:
                printVerbose("unrecognized command: " + cmd);
+1 −1
Original line number Diff line number Diff line
@@ -192,7 +192,7 @@ public class MoleculeDatabase {
        for (Molecule dbMolecule : moleculesWithSameNumAtoms) {
            Molecule result = dbMolecule.areMoleculesEqual(molecule);
            if (result != null) {
                db.get(numAtoms).remove(dbMolecule);
                this.db.get(numAtoms).remove(dbMolecule);
                return true; // successfully delete the molecule
            }
        }