package presenter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.openide.util.Exceptions;
import view.CenarioVolumeView;
import main.Main;
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.cenario.StrategyCenarioVolume;
public class CenarioVolumePresenter {
private CenarioVolumeView view;
private Main main;
private StrategyCenarioVolume strategy;
public CenarioVolumePresenter(Main pMain) {
this.main = pMain;
this.view = new CenarioVolumeView();
view.getTxtCenario().addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
view.getBtnCancelar().setText("Cancelar");
}
});
view.getBtnSalvar().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
strategy.salvarCenario(main);
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
}
}
});
view.getBtnCancelar().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
view.setVisible(false);
view.dispose();
}
});
view.getTxtCenario().setDocument(new FixedLengthDocument(30));
view.getTxtCenario().setName("Nome do cenário");
final ValidationPanel pnl = new ValidationPanel();
view.setContentPane(pnl);
pnl.setInnerComponent(view.getPnlCampos());
// 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.getTxtCenario(), vldVazio);
pnl.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
Problem p = pnl.getProblem();
boolean enable = p == null ? true : p.severity() != Severity.FATAL;
view.getBtnSalvar().setEnabled(enable);
}
});
}
public CenarioVolumeView getView() {
return view;
}
public Main getMain() {
return main;
}
public void setStrategy(final StrategyCenarioVolume strategy) {
this.strategy = strategy;
}
}