@@ -325,7 +325,7 @@ public void changedUpdate(DocumentEvent e) {} // Not used
325325 userAgentPanel .add (fullAgentPanel , BorderLayout .NORTH );
326326 userAgentPanel .add (userAgentInputPanel , BorderLayout .CENTER );
327327
328- // Add subpanels to the main panel
328+ // Add sub-panels to the main panel
329329 panel .add (userAgentPanel );
330330 panel .add (fontPanel );
331331
@@ -385,90 +385,44 @@ public static void clearIdList() {
385385 private void clickSearch (int row , int col , JTable table ) {
386386 String typeOfTable = table .getColumnName (0 );
387387 System .out .println ("Clicked at: " + row + ", " + col );
388- switch (typeOfTable ) {
389- case "Disc Name" -> {
390- if (col == 0 && row >= 0 ) {
391- setSearchStatus ("Fetching Tracks..." , "blue" );
392- System .out .println ("This kinda works" );
393- String response = getDiscTrackListResponseString (row );
394- MusicBrainzJSONReader reader = new MusicBrainzJSONReader (response );
395- DefaultTableModel model = reader .getTracksAsTableModel (reader .getTracks ());
396-
397- String title = table .getValueAt (row , 0 ).toString ();
398- String artist = table .getValueAt (row , 1 ).toString ();
399- int trackCount = Integer .parseInt (table .getValueAt (row , 2 ).toString ());
400-
401- JPanel mainPanel = new JPanel (new BorderLayout ());
402- JPanel panel = new JPanel (new BorderLayout ());
403- panel .setLayout (new BoxLayout (panel , BoxLayout .Y_AXIS ));
404-
405- panel .add (new JLabel ("Title: " + title ));
406- panel .add (new JLabel ("Artist: " + artist ));
407- panel .add (new JLabel ("Track Count: " + trackCount ));
408-
409- mainPanel .add (panel , BorderLayout .NORTH );
410-
411- JTable trackTable = new JTable (model );
412- resizeColumnWidths (trackTable );
413- JScrollPane trackScrollPane = new JScrollPane (trackTable );
388+ if (row < 0 || col < 0 ) {return ;}
414389
415- mainPanel .add (trackScrollPane , BorderLayout .CENTER );
416- mainPanel .add (new JLabel ("Would you like to add this record to your CD label?" ), BorderLayout .SOUTH );
390+ setSearchStatus ("Fetching Tracks..." , "blue" );
391+ String response ;
392+ DefaultTableModel model ;
393+ String title = table .getValueAt (row , 0 ).toString ();
394+ String artist = table .getValueAt (row , 1 ).toString ();
395+ int trackCount = Integer .parseInt (table .getValueAt (row , 2 ).toString ());
417396
418- setSearchStatus ("All Done!" , "green" );
419- int result = JOptionPane .showConfirmDialog (null , mainPanel , "Tracks" , JOptionPane .YES_NO_OPTION );
420- if (result == JOptionPane .YES_OPTION ) {
421- System .out .println ("give me redbull" );
422- } else if (result == JOptionPane .NO_OPTION ) {
423- System .out .println ("give me beer" );
424- }
397+ switch (typeOfTable ) {
398+ case "Disc Name" -> {
399+ if (col == 0 ) {
400+ response = getTrackListResponseString (row , "tracks" );
401+ model = new MusicBrainzJSONReader (response ).getTracksAsTableModel (new MusicBrainzJSONReader (response ).getTracks ());
402+ createTrackDialog (title , artist , trackCount , null , model );
425403 }
426404 }
427405 case "Release Name" -> {
428- if (col == 0 && row >= 0 ) {
429- setSearchStatus ("Fetching Tracks..." , "blue" );
430- System .out .println ("This also kinda works" );
431- String response = getReleaseTrackListResponseString (row );
406+ if (col == 0 ) {
407+ response = getTrackListResponseString (row , "release" );
432408 MusicBrainzJSONReader reader = new MusicBrainzJSONReader (response );
433- MusicBrainzTrack [] tracks = reader .getReleaseTracks ();
434- DefaultTableModel model = reader .getTracksAsTableModel (tracks );
435-
436- String title = table .getValueAt (row , 0 ).toString ();
437- String artist = table .getValueAt (row , 1 ).toString ();
438- int trackCount = Integer .parseInt (table .getValueAt (row , 2 ).toString ());
409+ model = reader .getTracksAsTableModel (reader .getReleaseTracks ());
439410 String date = table .getValueAt (row , 3 ).toString ();
440-
441- JPanel mainPanel = new JPanel (new BorderLayout ());
442- JPanel panel = new JPanel (new BorderLayout ());
443- panel .setLayout (new BoxLayout (panel , BoxLayout .Y_AXIS ));
444-
445- panel .add (new JLabel ("Title: " + title ));
446- panel .add (new JLabel ("Artist: " + artist ));
447- panel .add (new JLabel ("Track Count: " + trackCount ));
448- panel .add (new JLabel ("Date: " + date ));
449- JTable trackTable = new JTable (model );
450- resizeColumnWidths (trackTable );
451- JScrollPane trackScrollPane = new JScrollPane (trackTable );
452-
453- mainPanel .add (panel , BorderLayout .NORTH );
454- mainPanel .add (trackScrollPane , BorderLayout .CENTER );
455- mainPanel .add (new JLabel ("Would you like to add this record to your CD label?" ), BorderLayout .SOUTH );
456-
457- setSearchStatus ("All Done!" , "green" );
458- // the worst she can say is no...
459- int result = JOptionPane .showConfirmDialog (null , mainPanel , "Tracks" , JOptionPane .YES_NO_OPTION );
460- if (result == JOptionPane .YES_OPTION ) {
461- System .out .println ("give me redbull" );
462- } else if (result == JOptionPane .NO_OPTION ) {
463- System .out .println ("give me beer" );
464- }
411+ createTrackDialog (title , artist , trackCount , date , model );
465412 }
466413 }
467414 }
415+ setSearchStatus ("All Done!" , "green" );
468416 }
469417
470- private String getDiscTrackListResponseString (int row ) {
471- MusicBrainzRequest request = new MusicBrainzRequest ("tracks" , idList .get (row ));
418+ /**
419+ * Helper method to get the track list from the MusicBrainz API.
420+ * @param row The row of the table. Used to look up the ID from the ID list.
421+ * @param key The key to use for the request. (Release or CDStub)
422+ * @return The response from the API.
423+ */
424+ private String getTrackListResponseString (int row , String key ) {
425+ MusicBrainzRequest request = new MusicBrainzRequest (key , idList .get (row ));
472426 WebRequest webRequest = new WebRequest (request .buildTrackListURL (), userAgent );
473427 String response ;
474428 try {
@@ -479,16 +433,44 @@ private String getDiscTrackListResponseString(int row) {
479433 return response ;
480434 }
481435
482- private String getReleaseTrackListResponseString (int row ) {
483- MusicBrainzRequest request = new MusicBrainzRequest ("release" , idList .get (row ));
484- WebRequest webRequest = new WebRequest (request .buildTrackListURL (), userAgent );
485- String response ;
486- try {
487- response = webRequest .sendRequest ();
488- } catch (IOException | URISyntaxException e ) {
489- throw new RuntimeException (e );
436+ /**
437+ * Helper method to create a track dialog. This is what the user uses to make the final labels
438+ * from a search.
439+ * @param title The title of the CD / Release.
440+ * @param artist The artist of the CD / Release.
441+ * @param trackCount The number of tracks on the CD / Release.
442+ * @param date The date of the release. CDStubs typically do not have a date.
443+ * @param model The table model to use for the track list.
444+ */
445+ private void createTrackDialog (String title , String artist , int trackCount , String date , DefaultTableModel model ) {
446+ // Set up panels
447+ JPanel mainPanel = new JPanel (new BorderLayout ());
448+ JPanel panel = new JPanel ();
449+ panel .setLayout (new BoxLayout (panel , BoxLayout .Y_AXIS ));
450+
451+ // Add labels to panel
452+ panel .add (new JLabel ("Title: " + title ));
453+ panel .add (new JLabel ("Artist: " + artist ));
454+ panel .add (new JLabel ("Track Count: " + trackCount ));
455+ if (date != null ) {
456+ panel .add (new JLabel ("Date: " + date ));
490457 }
491- return response ;
492- }
493458
459+ // Add label panel to main panel
460+ mainPanel .add (panel , BorderLayout .NORTH );
461+ // Add table to main panel
462+ JTable trackTable = new JTable (model );
463+ JScrollPane trackScrollPane = new JScrollPane (trackTable );
464+ mainPanel .add (trackScrollPane , BorderLayout .CENTER );
465+ // Add bottom (question) label to main panel
466+ mainPanel .add (new JLabel ("Would you like to add this record to your CD label?" ), BorderLayout .SOUTH );
467+
468+ // Show dialog
469+ int result = JOptionPane .showConfirmDialog (null , mainPanel , "Tracks" , JOptionPane .YES_NO_OPTION );
470+ if (result == JOptionPane .YES_OPTION ) {
471+ System .out .println ("ask her out" );
472+ } else if (result == JOptionPane .NO_OPTION ) {
473+ System .out .println ("get rejected idiot" );
474+ }
475+ }
494476}
0 commit comments