1515
1616import control .MusicaControl ;
1717import model .Musica ;
18+ import pExceptions .EmptyFieldException ;
1819import pExceptions .MusicNotFoundException ;
1920
2021/**
@@ -76,56 +77,35 @@ private void trimAll() {
7677 txtAlbum .setText (txtAlbum .getText ().trim ());
7778 txtDuracao .setText (txtDuracao .getText ().trim ());
7879 txtDataPub .setText (txtDataPub .getText ().trim ());
80+ txtBuscaNome .setText (txtBuscaNome .getText ());
7981 txtBuscaAutor .setText (txtBuscaAutor .getText ().trim ());
8082 }
8183
8284 private boolean checkCampos () {
8385 trimAll ();
84- String errMessage = "" ;
85-
86- if (txtNome .getText ().isBlank ()) {
87- errMessage = "O campo nome não pode estar vazio." ;
88- txtNome .requestFocus ();
89- } else if (txtAutor .getText ().isBlank ()) {
90- errMessage = "O campo autor não pode estar vazio." ;
91- txtAutor .requestFocus ();
92- } else if (txtAlbum .getText ().isBlank ()) {
93- errMessage = "O campo álbum não pode estar vazio." ;
94- txtAlbum .requestFocus ();
95- } else if (txtDuracao .getText ().isBlank ()) {
96- errMessage = "O campo duração não pode estar vazio." ;
97- txtDuracao .requestFocus ();
98- // } else if (!Musica.durationCheck.matcher(txtDuracao.getText()).matches()) {
99- // errMessage = "Formato de data incorreto: Deve estar como dd/MM/AAAA";
100- // txtDuracao.requestFocus();
101- } else if (txtDataPub .getText ().isBlank ()) {
102- errMessage = "O campo data de publicação não pode estar vazio." ;
103- txtDataPub .requestFocus ();
104- // } else if (!Musica.dataCheck.matcher(txtDataPub.getText()).matches()) {
105- // errMessage = "Formato de duração incorreto: Deve estar como MM:SS";
106- // txtDataPub.requestFocus();
107- }
10886
109- if (!errMessage .isBlank ())
110- JOptionPane .showMessageDialog (null , errMessage );
87+ EmptyFieldException .checkComponent (txtNome , "nome" );
88+ EmptyFieldException .checkComponent (txtAutor , "autor" );
89+ EmptyFieldException .checkComponent (txtAlbum , "álbum" );
90+ EmptyFieldException .checkDuracao (txtDuracao );
91+ EmptyFieldException .checkDataPub (txtDataPub );
11192
112- return ! errMessage . isBlank () ;
93+ return true ;
11394 }
11495
11596 private ArrayList <Musica > checkCamposBusca () throws SQLException , MusicNotFoundException {
11697 trimAll ();
11798
118- if (txtBuscaNome .getText ().isBlank () && txtBuscaAutor .getText ().isBlank ()) {
119- txtBuscaNome .requestFocus ();
120- throw new IllegalArgumentException ("Os campos nome e autor não podem estar vazios." );
99+ if (EmptyFieldException .isEmpty (txtBuscaNome ) && EmptyFieldException .isEmpty (txtBuscaAutor )) {
100+ EmptyFieldException .checkComponent (txtBuscaNome , "nome e autor" );
121101 }
122102
123103 String nome = txtBuscaNome .getText ();
124104 String autor = txtBuscaAutor .getText ();
125105
126- if (!nome .isBlank () && autor .isBlank ()) { // Nesse caso, fazer pesquisa por nome.
106+ if (!nome .isEmpty () && autor .isEmpty ()) { // Nesse caso, fazer pesquisa por nome.
127107 return lstMusica .findByName (nome );
128- } else if (nome .isBlank () && !autor .isBlank ()) { // Nesse caso, fazer pesquisa por autor.
108+ } else if (nome .isEmpty () && !autor .isEmpty ()) { // Nesse caso, fazer pesquisa por autor.
129109 return lstMusica .findByAuthor (autor );
130110 } else {
131111 return lstMusica .findAll (nome , autor ); // Nesse caso, fazer pesquisa por nome e autor.
@@ -158,7 +138,7 @@ private void fillTable() {
158138 private void showMusica (ArrayList <Musica > e ) throws MusicNotFoundException {
159139 if (e .size () < 1 ) {
160140 limparBusca ();
161- throw new MusicNotFoundException ();
141+ throw new MusicNotFoundException (MusicNotFoundException . EmptySearchSet );
162142 }
163143
164144 txtBuscaNome .setText (e .get (0 ).getNome ());
@@ -424,8 +404,12 @@ private void setBusca() {
424404 }
425405
426406 private void btnCadastrarActionPerformed (java .awt .event .ActionEvent evt ) {// GEN-FIRST:event_btnCadastrarActionPerformed
427- if (checkCampos ())
407+ try {
408+ checkCampos ();
409+ } catch (EmptyFieldException e ) {
410+ JOptionPane .showMessageDialog (null , e .getMessage ());
428411 return ;
412+ }
429413
430414 String nome = txtNome .getText ();
431415 String album = txtAlbum .getText ();
@@ -439,10 +423,10 @@ private void btnCadastrarActionPerformed(java.awt.event.ActionEvent evt) {// GEN
439423 limparCampos ();
440424 fillTable ();
441425 JOptionPane .showMessageDialog (null , "A música \" " + e .getNome () + "\" foi inserida com sucesso." );
442- } catch (IllegalArgumentException e ) {
443- JOptionPane .showMessageDialog (null , e .getMessage ());
444426 } catch (SQLException e ) {
445427 JOptionPane .showMessageDialog (null , "Houve um erro no banco de dados :(" + e .getMessage ());
428+ } catch (Exception e ) {
429+ JOptionPane .showMessageDialog (null , e .getMessage ());
446430 }
447431 }// GEN-LAST:event_btnCadastrarActionPerformed
448432
0 commit comments