E ai GUJianos... rsss
bom to estudando sobre desenvolvimento para Desktop...... e pesquise bastante sobre como separar Model, View e Controller..... porém achei poucos exemplos prático... e muito dizendo como deveria ser....... com base nisso tudo.... fiz um exemplo com base no que eu entendi, segue o codigo e mais abaixo as dúvidas.....
Model:public class TesteMVCModel {
private String nome;
private String cargo;
private String email;
//... getters & setters
}
public class TesteMVCView extends javax.swing.JFrame {
private TesteMVCController control = new TesteMVCController();
private TesteMVCModel model;
/** Creates new form TesteMVCView */
public TesteMVCView() {
initComponents();
if (model == null){
model = new TesteMVCModel();
}
this.atualizaView();
this.setVisible(true);
}
private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
this.atualizaModel();
String erro = control.salva(model);
if (erro.equalsIgnoreCase("OK")) {
JOptionPane.showConfirmDialog(this, "Salvo com sucesso");
}else{
JOptionPane.showConfirmDialog(this, erro);
}
this.atualizaView();
}//GEN-LAST:event_btnOkActionPerformed
private void btnLimpaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpaActionPerformed
this.atualizaModel();
String erro = control.limpa(model);
if (erro.equalsIgnoreCase("OK")) {
this.model = new TesteMVCModel();
}else{
JOptionPane.showConfirmDialog(this, erro);
}
this.atualizaView();
}//GEN-LAST:event_btnLimpaActionPerformed
private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
this.dispose();
}//GEN-LAST:event_btnExitActionPerformed
private void atualizaModel() {
this.model.setNome(txtNome.getText());
this.model.setCargo(txtCargo.getText());
this.model.setEmail(txtEmail.getText());
}
private void atualizaView() {
txtNome.setText(model.getNome());
txtCargo.setText(model.getCargo());
txtEmail.setText(model.getEmail());
}
}
public class TesteMVCController {
public String salva(TesteMVCModel model) {
String msg = "OK";
if (model.getNome().equals("") || model.getCargo().equals("")) {
msg = "Preencher Nome/Cargo";
}
if (model.getEmail().indexOf("@") == -1) {
msg = "E-mail Inválido. Verifique.";
}
return msg;
}
public String limpa(TesteMVCModel model) {
String msg = "OK";
//Verificações antes de limpar
return msg;
}
}
Agora as dúvidas:
1-Essa seria a maneira correta?
2-E se por acaso no Controle eu tivesse q mudar algo no Modelo, como devolveria isso para a View? (se vcs notarem apenas devolvo a msg de erro)
Muito obrigado pela ajuda!