Files
tutorialjava2024-25/src/tutorialJava/examenes/examen20250616/vista/PanelCRUDSocio.java
2025-06-18 23:02:38 +02:00

439 lines
14 KiB
Java

package tutorialJava.examenes.examen20250616.vista;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JToolBar;
import tutorialJava.examenes.examen20250509.controlador.ControladorConcierto;
import tutorialJava.examenes.examen20250509.modelo.Concierto;
import tutorialJava.examenes.examen20250616.controlador.ControladorEquipo;
import tutorialJava.examenes.examen20250616.controlador.ControladorSocio;
import tutorialJava.examenes.examen20250616.modelo.Equipo;
import tutorialJava.examenes.examen20250616.modelo.Socio;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.GridBagConstraints;
import java.awt.Font;
import java.awt.Insets;
import javax.swing.JTextField;
import javax.swing.JSlider;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
public class PanelCRUDSocio extends JPanel {
private static final long serialVersionUID = 1L;
private JTextField jtfNombre;
private JTextField jtfApellido1;
private JTextField jtfApellido2;
private JTextField jtfFechaNacimiento;
private JTextField jtfId;
private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
JSlider sliderAntiguedad;
JLabel lblAntiguedad;
JComboBox<Equipo> jcbEquipo;
JCheckBox chckActivo;
/**
* Create the panel.
*/
public PanelCRUDSocio() {
setLayout(new BorderLayout(0, 0));
JToolBar toolBar = new JToolBar();
add(toolBar, BorderLayout.NORTH);
JButton btnPrimero = new JButton("");
btnPrimero.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mostrarSocioEnPantalla(ControladorSocio.getPrimero());
}
});
btnPrimero.setIcon(new ImageIcon(PanelCRUDSocio.class.getResource("/tutorialJava/capitulo9_AWT_SWING/res/gotostart.png")));
toolBar.add(btnPrimero);
JButton btnAnterior = new JButton("");
btnAnterior.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int idSocioActual = Integer.parseInt(jtfId.getText());
mostrarSocioEnPantalla(
ControladorSocio.getAnterior(idSocioActual));
}
});
btnAnterior.setIcon(new ImageIcon(PanelCRUDSocio.class.getResource("/tutorialJava/capitulo9_AWT_SWING/res/previous.png")));
toolBar.add(btnAnterior);
JButton btnSiguiente = new JButton("");
btnSiguiente.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int idSocioActual = Integer.parseInt(jtfId.getText());
mostrarSocioEnPantalla(
ControladorSocio.getSiguiente(idSocioActual));
}
});
btnSiguiente.setIcon(new ImageIcon(PanelCRUDSocio.class.getResource("/tutorialJava/capitulo9_AWT_SWING/res/next.png")));
toolBar.add(btnSiguiente);
JButton btnUltimo = new JButton("");
btnUltimo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mostrarSocioEnPantalla(ControladorSocio.getUltimo());
}
});
btnUltimo.setIcon(new ImageIcon(PanelCRUDSocio.class.getResource("/tutorialJava/capitulo9_AWT_SWING/res/gotoend.png")));
toolBar.add(btnUltimo);
JButton btnNuevo = new JButton("");
btnNuevo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nuevo();
}
});
btnNuevo.setIcon(new ImageIcon(PanelCRUDSocio.class.getResource("/tutorialJava/capitulo9_AWT_SWING/res/nuevo.png")));
toolBar.add(btnNuevo);
JButton btnGuardar = new JButton("");
btnGuardar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
guardar();
}
});
btnGuardar.setIcon(new ImageIcon(PanelCRUDSocio.class.getResource("/tutorialJava/capitulo9_AWT_SWING/res/guardar.png")));
toolBar.add(btnGuardar);
JButton btnEliminar = new JButton("");
btnEliminar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
eliminar();
}
});
btnEliminar.setIcon(new ImageIcon(PanelCRUDSocio.class.getResource("/tutorialJava/capitulo9_AWT_SWING/res/eliminar.png")));
toolBar.add(btnEliminar);
JPanel panel = new JPanel();
add(panel, BorderLayout.CENTER);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0, 0, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_panel.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JLabel lblNewLabel = new JLabel("Gestión de socios");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.gridwidth = 3;
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 0;
panel.add(lblNewLabel, gbc_lblNewLabel);
JLabel lblNewLabel_8 = new JLabel("Id:");
GridBagConstraints gbc_lblNewLabel_8 = new GridBagConstraints();
gbc_lblNewLabel_8.anchor = GridBagConstraints.EAST;
gbc_lblNewLabel_8.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel_8.gridx = 0;
gbc_lblNewLabel_8.gridy = 1;
panel.add(lblNewLabel_8, gbc_lblNewLabel_8);
jtfId = new JTextField();
GridBagConstraints gbc_jtfId = new GridBagConstraints();
gbc_jtfId.insets = new Insets(0, 0, 5, 5);
gbc_jtfId.fill = GridBagConstraints.HORIZONTAL;
gbc_jtfId.gridx = 1;
gbc_jtfId.gridy = 1;
panel.add(jtfId, gbc_jtfId);
jtfId.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("Nombre");
GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST;
gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel_1.gridx = 0;
gbc_lblNewLabel_1.gridy = 2;
panel.add(lblNewLabel_1, gbc_lblNewLabel_1);
jtfNombre = new JTextField();
GridBagConstraints gbc_jtfNombre = new GridBagConstraints();
gbc_jtfNombre.gridwidth = 2;
gbc_jtfNombre.insets = new Insets(0, 0, 5, 0);
gbc_jtfNombre.fill = GridBagConstraints.HORIZONTAL;
gbc_jtfNombre.gridx = 1;
gbc_jtfNombre.gridy = 2;
panel.add(jtfNombre, gbc_jtfNombre);
jtfNombre.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("Primer apellido:");
GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
gbc_lblNewLabel_2.anchor = GridBagConstraints.EAST;
gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel_2.gridx = 0;
gbc_lblNewLabel_2.gridy = 3;
panel.add(lblNewLabel_2, gbc_lblNewLabel_2);
jtfApellido1 = new JTextField();
GridBagConstraints gbc_jtfApellido1 = new GridBagConstraints();
gbc_jtfApellido1.gridwidth = 2;
gbc_jtfApellido1.insets = new Insets(0, 0, 5, 0);
gbc_jtfApellido1.fill = GridBagConstraints.HORIZONTAL;
gbc_jtfApellido1.gridx = 1;
gbc_jtfApellido1.gridy = 3;
panel.add(jtfApellido1, gbc_jtfApellido1);
jtfApellido1.setColumns(10);
JLabel lblNewLabel_3 = new JLabel("Segundo apellido:");
GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints();
gbc_lblNewLabel_3.anchor = GridBagConstraints.EAST;
gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel_3.gridx = 0;
gbc_lblNewLabel_3.gridy = 4;
panel.add(lblNewLabel_3, gbc_lblNewLabel_3);
jtfApellido2 = new JTextField();
GridBagConstraints gbc_jtfApellido2 = new GridBagConstraints();
gbc_jtfApellido2.gridwidth = 2;
gbc_jtfApellido2.insets = new Insets(0, 0, 5, 0);
gbc_jtfApellido2.fill = GridBagConstraints.HORIZONTAL;
gbc_jtfApellido2.gridx = 1;
gbc_jtfApellido2.gridy = 4;
panel.add(jtfApellido2, gbc_jtfApellido2);
jtfApellido2.setColumns(10);
JLabel lblNewLabel_4 = new JLabel("Fecha de nacimiento:");
GridBagConstraints gbc_lblNewLabel_4 = new GridBagConstraints();
gbc_lblNewLabel_4.anchor = GridBagConstraints.EAST;
gbc_lblNewLabel_4.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel_4.gridx = 0;
gbc_lblNewLabel_4.gridy = 5;
panel.add(lblNewLabel_4, gbc_lblNewLabel_4);
jtfFechaNacimiento = new JTextField();
GridBagConstraints gbc_jtfFechaNacimiento = new GridBagConstraints();
gbc_jtfFechaNacimiento.gridwidth = 2;
gbc_jtfFechaNacimiento.insets = new Insets(0, 0, 5, 0);
gbc_jtfFechaNacimiento.fill = GridBagConstraints.HORIZONTAL;
gbc_jtfFechaNacimiento.gridx = 1;
gbc_jtfFechaNacimiento.gridy = 5;
panel.add(jtfFechaNacimiento, gbc_jtfFechaNacimiento);
jtfFechaNacimiento.setColumns(10);
JLabel lblNewLabel_5 = new JLabel("Antigüedad (años):");
GridBagConstraints gbc_lblNewLabel_5 = new GridBagConstraints();
gbc_lblNewLabel_5.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel_5.gridx = 0;
gbc_lblNewLabel_5.gridy = 6;
panel.add(lblNewLabel_5, gbc_lblNewLabel_5);
sliderAntiguedad = new JSlider();
sliderAntiguedad.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (lblAntiguedad != null) {
lblAntiguedad.setText(
sliderAntiguedad.getValue() + " años");
}
}
});
sliderAntiguedad.setMaximum(150);
GridBagConstraints gbc_sliderAntiguedad = new GridBagConstraints();
gbc_sliderAntiguedad.fill = GridBagConstraints.HORIZONTAL;
gbc_sliderAntiguedad.insets = new Insets(0, 0, 5, 5);
gbc_sliderAntiguedad.gridx = 1;
gbc_sliderAntiguedad.gridy = 6;
panel.add(sliderAntiguedad, gbc_sliderAntiguedad);
lblAntiguedad = new JLabel("New label");
GridBagConstraints gbc_lblAntiguedad = new GridBagConstraints();
gbc_lblAntiguedad.insets = new Insets(0, 0, 5, 0);
gbc_lblAntiguedad.gridx = 2;
gbc_lblAntiguedad.gridy = 6;
panel.add(lblAntiguedad, gbc_lblAntiguedad);
chckActivo = new JCheckBox("Socio en activo");
GridBagConstraints gbc_chckActivo = new GridBagConstraints();
gbc_chckActivo.anchor = GridBagConstraints.WEST;
gbc_chckActivo.insets = new Insets(0, 0, 5, 5);
gbc_chckActivo.gridx = 1;
gbc_chckActivo.gridy = 7;
panel.add(chckActivo, gbc_chckActivo);
JLabel lblNewLabel_7 = new JLabel("Equipo:");
GridBagConstraints gbc_lblNewLabel_7 = new GridBagConstraints();
gbc_lblNewLabel_7.anchor = GridBagConstraints.EAST;
gbc_lblNewLabel_7.insets = new Insets(0, 0, 0, 5);
gbc_lblNewLabel_7.gridx = 0;
gbc_lblNewLabel_7.gridy = 8;
panel.add(lblNewLabel_7, gbc_lblNewLabel_7);
jcbEquipo = new JComboBox<Equipo>();
GridBagConstraints gbc_jcbEquipo = new GridBagConstraints();
gbc_jcbEquipo.gridwidth = 2;
gbc_jcbEquipo.fill = GridBagConstraints.HORIZONTAL;
gbc_jcbEquipo.gridx = 1;
gbc_jcbEquipo.gridy = 8;
panel.add(jcbEquipo, gbc_jcbEquipo);
cargarEquipos();
mostrarSocioEnPantalla(ControladorSocio.getPrimero());
}
/**
*
*/
private void cargarEquipos(){
List<Equipo> lista = ControladorEquipo.findAll();
for (Equipo e : lista) {
this.jcbEquipo.addItem(e);
}
}
/**
*
*/
private void mostrarSocioEnPantalla(Socio s) {
if (s != null) {
this.jtfId.setText("" + s.getId());
this.jtfNombre.setText(s.getNombre());
this.jtfApellido1.setText(s.getApellido1());
this.jtfApellido2.setText(s.getApellido2());
this.jtfFechaNacimiento.setText(
sdf.format(s.getFechaNacimiento()));
this.sliderAntiguedad.setValue(s.getAntiguedadAnios());
this.chckActivo.setSelected(s.isActivo());
for (int i = 0; i < this.jcbEquipo.getItemCount(); i++) {
if (this.jcbEquipo.getItemAt(i).getId() == s.getIdEquipo()) {
this.jcbEquipo.setSelectedIndex(i);
}
}
}
}
/**
*
* @return
*/
private Socio getSocioFromDatosDePantalla() {
Socio s = new Socio();
s.setId(Integer.parseInt(jtfId.getText()));
s.setNombre(jtfNombre.getText());
s.setApellido1(jtfApellido1.getText());
s.setApellido2(jtfApellido2.getText());
try {
s.setFechaNacimiento(sdf.parse(jtfFechaNacimiento.getText()));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "El formato de la fecha debe ser dd/MM/yyyy");
}
s.setAntiguedadAnios(this.sliderAntiguedad.getValue());
s.setActivo(this.chckActivo.isSelected());
Equipo equipoSeleccionado = (Equipo) this.jcbEquipo.getSelectedItem();
s.setIdEquipo(equipoSeleccionado.getId());
return s;
}
/**
*
*/
private void guardar() {
Socio s = getSocioFromDatosDePantalla();
int registrosAfectados = ControladorSocio.guardar(s);
if (registrosAfectados == 1) {
if (s.getId() == 0) { // Hemos insertado
mostrarSocioEnPantalla(ControladorSocio.getUltimo());
}
JOptionPane.showMessageDialog(null, "Guardado correctamente");
}
else {
JOptionPane.showMessageDialog(null, "Algo ha fallado");
}
}
/**
*
*/
private void nuevo() {
this.jtfId.setText("0");
this.jtfNombre.setText("");
this.jtfApellido1.setText("");
this.jtfApellido2.setText("");
this.jtfFechaNacimiento.setText(sdf.format(new Date()));
this.sliderAntiguedad.setValue(0);
this.chckActivo.setSelected(false);
this.jcbEquipo.setSelectedIndex(0);
}
/**
*
*/
private void eliminar() {
String [] opciones ={"","No"};
int eleccion = JOptionPane.showOptionDialog(null,
"¿Desea eliminar el socio?","Eliminar socio",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, opciones, "");
if (eleccion != JOptionPane.YES_OPTION) {
return;
}
int idSocio = Integer.parseInt(jtfId.getText());
int registrosAfectados = ControladorSocio.eliminar(idSocio);
if (registrosAfectados == 0) {
JOptionPane.showMessageDialog(null, "Error al eliminar");
}
else if (registrosAfectados == 1) {
JOptionPane.showMessageDialog(null, "Eliminado correctamente");
Socio anterior =
ControladorSocio.getAnterior(idSocio);
// Si elimino correctamente, intento mostrar el anterior
if (anterior != null) {
mostrarSocioEnPantalla(anterior);
}
else { // No existe un anterior, intento mostrar el siguiente
Socio siguiente =
ControladorSocio.getSiguiente(idSocio);
if (siguiente != null) {
mostrarSocioEnPantalla(siguiente);
}
else { // No tiene concierto anterior ni siguiente
nuevo();
}
}
}
}
}