package presenter;
import business.Projeto;
import view.ProjetoView;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import javax.swing.JOptionPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import main.Main;
import org.jdesktop.swingx.JXErrorPane;
import org.jdesktop.swingx.error.ErrorInfo;
import org.netbeans.validation.api.Problem;
import org.netbeans.validation.api.Severity;
import org.netbeans.validation.api.Validator;
import org.netbeans.validation.api.builtin.Validators;
import org.netbeans.validation.api.ui.ValidationPanel;
import util.FixedLengthDocument;
import presenter.strategy.projeto.StrategyProjeto;
/**
*
* @author Usuario
*/
public class ProjetoPresenter {
private ProjetoView view;
private Projeto projeto;
private StrategyProjeto strategy;
public ProjetoPresenter(final Main main) {
this.view = new ProjetoView();
this.view.setTitle("Novo Projeto");
this.view.setVisible(true);
view.getBtnConfirmar().addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
strategy.salvarProjeto(main);
} catch (Exception ex) {
ErrorInfo info = new ErrorInfo("Erro", ex.getMessage(), null, "category", ex, Level.SEVERE, null);
JXErrorPane.showDialog(null, info);
}
}
});
view.getBtnCancelar().addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (JOptionPane.showConfirmDialog(view, "Cancela inclusão do projeto?", "Cancela", JOptionPane.YES_NO_OPTION) == 0) {
projeto = null;
view.setVisible(false);
view.dispose();
}
}
});
view.getTextNomeProj().setDocument(new FixedLengthDocument(30));
view.getTextNomeProj().setName("Nome do projeto");
view.getTextAutorProj().setName("Autor do projeto");
view.getTextDescProj().setName("Descrição do projeto");
final ValidationPanel pnl = new ValidationPanel();
view.setContentPane(pnl);
pnl.setInnerComponent(view.getPanel());
// O campo Nome não pode ser vazio
Validator<javax.swing.text.Document> vldVazio = Validators.forDocument(true, Validators.REQUIRE_NON_EMPTY_STRING);
pnl.getValidationGroup().add(view.getTextNomeProj(), vldVazio);
pnl.getValidationGroup().add(view.getTextAutorProj(), vldVazio);
pnl.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
Problem p = pnl.getProblem();
boolean enable = p == null ? true : p.severity() != Severity.FATAL;
view.getBtnConfirmar().setEnabled(enable);
}
});
}
public void setStrategy(StrategyProjeto strategy) {
this.strategy = strategy;
}
public void salvaProjeto() {
}
public ProjetoView getView() {
return view;
}
}