From 3ae83b3681015233e1301d728cb1d3fa6c4631d2 Mon Sep 17 00:00:00 2001 From: "rocio.montero1" Date: Fri, 16 Oct 2026 14:59:59 +0200 Subject: [PATCH] =?UTF-8?q?Actividad=206.4:=20DAO=20por=20reflexi=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .classpath | 11 ++ .gitignore | 1 + .project | 17 ++ .settings/org.eclipse.core.resources.prefs | 2 + .../aadd/swing/VentanaContinentesPaises.java | 172 ++++++++++++++++++ 5 files changed, 203 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 src/prueba/aadd/swing/VentanaContinentesPaises.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..4f66160 --- /dev/null +++ b/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e660fd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..800dcdd --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Componentes_04 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/src/prueba/aadd/swing/VentanaContinentesPaises.java b/src/prueba/aadd/swing/VentanaContinentesPaises.java new file mode 100644 index 0000000..2e02511 --- /dev/null +++ b/src/prueba/aadd/swing/VentanaContinentesPaises.java @@ -0,0 +1,172 @@ +package prueba.aadd.swing; + +import java.awt.BorderLayout; +import java.awt.Component; +import java.util.List; + +import javax.swing.DefaultListCellRenderer; +import javax.swing.DefaultListModel; +import javax.swing.JComboBox; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; +import javax.swing.SwingUtilities; + +import org.palomafp.aadd.rmr.GestorPropiedades; +import org.palomafp.aadd.rmr.dao.IContinenteDAO; +import org.palomafp.aadd.rmr.dao.IPaisDAO; +import org.palomafp.aadd.rmr.model.Continente; +import org.palomafp.aadd.rmr.model.Pais; + +public class VentanaContinentesPaises extends JFrame { + /** + * + */ + + private JComboBox comboContinentes; + private JList listaPaises; + private DefaultListModel modeloPaises; + private JTextArea areaInfo; + + private List paisesActuales; + + private IContinenteDAO continenteDAO; + private IPaisDAO paisDAO; + + public VentanaContinentesPaises() { + + super("Continentes y países"); + + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(500, 350); + setLocationRelativeTo(null); + + // Inicializar DAO + try { + String claseContinente = GestorPropiedades.get("continente.implementacion.clase"); + String clasePais = GestorPropiedades.get("pais.implementacion.clase"); + + continenteDAO = (IContinenteDAO) Class.forName(claseContinente) + .getDeclaredConstructor().newInstance(); + paisDAO = (IPaisDAO) Class.forName(clasePais) + .getDeclaredConstructor().newInstance(); + + } catch (ReflectiveOperationException e) { + throw new RuntimeException("No se pudieron crear los DAO por reflexión: " + e.getMessage(), e); + } + + // Componentes + comboContinentes = new JComboBox<>(); + modeloPaises = new DefaultListModel<>(); + listaPaises = new JList<>(modeloPaises); + + areaInfo = new JTextArea(4, 30); + areaInfo.setEditable(false); + areaInfo.setLineWrap(true); + areaInfo.setWrapStyleWord(true); + + // Mostrar nombre del continente en el desplegable + comboContinentes.setRenderer(new DefaultListCellRenderer() { + @Override + public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, + boolean cellHasFocus) { + + super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); + + if (value instanceof Continente) { + setText(((Continente) value).getNombre()); + } + + return this; + } + }); + + // Diseño + JPanel panel = new JPanel(new BorderLayout(5, 5)); + + JPanel panelSuperior = new JPanel(new BorderLayout(5, 5)); + panelSuperior.add(new JLabel("Selecciona un continente:"), BorderLayout.WEST); + panelSuperior.add(comboContinentes, BorderLayout.CENTER); + + panel.add(panelSuperior, BorderLayout.NORTH); + panel.add(new JScrollPane(listaPaises), BorderLayout.CENTER); + panel.add(new JScrollPane(areaInfo), BorderLayout.SOUTH); + + add(panel); + + // Eventos + comboContinentes.addActionListener(e -> cargarPaises()); + listaPaises.addListSelectionListener(e -> { + if (!e.getValueIsAdjusting()) { + Pais seleccionado = listaPaises.getSelectedValue(); + + if (seleccionado != null) { + mostrarInfo(seleccionado); + } + } + }); + + // Cargar continentes al iniciar + cargarContinentes(); + } + + private void cargarContinentes() { + + // Sustituir por el nombre real del método de tu DAO + List continentes = continenteDAO.mostrarContinentes(); + + comboContinentes.removeAllItems(); + + for (Continente continente : continentes) { + comboContinentes.addItem(continente); + } + } + + private void cargarPaises() { + + Continente seleccionado = (Continente) comboContinentes.getSelectedItem(); + + modeloPaises.clear(); + areaInfo.setText(""); + + if (seleccionado == null) { + return; + } + + paisesActuales = paisDAO.obtenerListaPaisesPorContinente(seleccionado.getCodigo()); + + if (paisesActuales == null || paisesActuales.isEmpty()) { + areaInfo.setText("No hay países disponibles para el continente seleccionado."); + return; + } + + for (Pais pais : paisesActuales) { + modeloPaises.addElement(pais); + } + + if (!paisesActuales.isEmpty()) { + listaPaises.setSelectedIndex(0); + } + } + + private void mostrarInfo(Pais pais) { + + String texto = String.format("%s, capital de %s – %d, pertenece al continente %s (%s)", pais.getCapital(), + pais.getNombre(), pais.getIdentificador(), pais.getContinente().getNombre(), + pais.getContinente().getCodigo()); + + areaInfo.setText(texto); + } + + public static void main(String[] args) { + + SwingUtilities.invokeLater(() -> { + VentanaContinentesPaises ventana = new VentanaContinentesPaises(); + + ventana.setVisible(true); + }); + } +}