From 7bd4089194178f3d0b61d3ecd1ac2eaf3dbaf0a0 Mon Sep 17 00:00:00 2001 From: "juanperoh11-lgtm." Date: Thu, 15 Oct 2026 16:35:23 +0200 Subject: [PATCH] Actividad 6.4 --- .gitignore | 16 ++ config/application.properties | 14 ++ .../aadd/swing/VentanaContinentesPaises.java | 194 ++++++++++++++++++ 3 files changed, 224 insertions(+) create mode 100644 .gitignore create mode 100644 config/application.properties create mode 100644 src/prueba/aadd/swing/VentanaContinentesPaises.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cbaae6f --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# Eclipse +.classpath +.project +.settings/ + +# Compilaci贸n Java +bin/ +target/ +*.class + +# Logs +*.log + +# Sistema operativo +.DS_Store +Thumbs.db \ No newline at end of file diff --git a/config/application.properties b/config/application.properties new file mode 100644 index 0000000..13cc235 --- /dev/null +++ b/config/application.properties @@ -0,0 +1,14 @@ +# Elegir nombres de entradas que sean SIGNIFICATIVOS +# y que no se repitan con otras entradas de la misma aplicaci髇 + +# Informaci髇 relativa Continentes +continente.ruta.fichero=C:/Users/aleja/aadd/fic05/informacion_continentes.csv +continente.fichero.ruta=C:/Users/aleja/aadd/fic05/informacion_continentes.csv +continente.csv.separador=# +continente.fichero.separador=# +continente.implementacion.clase=org.palomafp.aadd.aez.dao.impl.ContinenteDaoCSV + +# Informaci髇 relativa Pa韘es +pais.ruta.fichero=C:/Users/aleja/aadd/fic05/informacion_paises.txt +pais.txt.separador=# +pais.implementacion.clase=org.palomafp.aadd.aez.dao.impl.PaisDaoFichero \ No newline at end of file diff --git a/src/prueba/aadd/swing/VentanaContinentesPaises.java b/src/prueba/aadd/swing/VentanaContinentesPaises.java new file mode 100644 index 0000000..35ee249 --- /dev/null +++ b/src/prueba/aadd/swing/VentanaContinentesPaises.java @@ -0,0 +1,194 @@ +package prueba.aadd.swing; + +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +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 javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; + +// Importaciones de interfaces, modelos y gestor (sin importar clases de implementaci贸n directa) +import org.palomafp.aadd.aez.dao.IContinenteDAO; +import org.palomafp.aadd.aez.dao.IPaisDAO; +import org.palomafp.aadd.aez.gestores.GestorConfiguracion; +import org.palomafp.aadd.aez.model.Continente; +import org.palomafp.aadd.aez.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 - Carga por Reflexi贸n"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(600, 400); + setLocationRelativeTo(null); + setLayout(new BorderLayout(10, 10)); + + try { + // 1. Obtener los nombres completos de las clases desde el fichero .properties + String claseContinenteNombre = GestorConfiguracion.getProperty("continente.implementacion.clase"); + String clasePaisNombre = GestorConfiguracion.getProperty("pais.implementacion.clase"); + + // 2. Cargar e instanciar el DAO de Continentes por reflexi贸n + if (claseContinenteNombre != null) { + Class claseContinente = Class.forName(claseContinenteNombre); + this.continenteDAO = (IContinenteDAO) claseContinente.getDeclaredConstructor().newInstance(); + } + + // 3. Cargar e instanciar el DAO de Pa铆ses por reflexi贸n + if (clasePaisNombre != null) { + Class clasePais = Class.forName(clasePaisNombre); + this.paisDAO = (IPaisDAO) clasePais.getDeclaredConstructor().newInstance(); + } + + } catch (Exception e) { + System.err.println("Error al cargar e instanciar las clases DAO por reflexi贸n: " + e.getMessage()); + e.printStackTrace(); + } + + // --- Panel Superior (Selecci贸n de Continente) --- + JPanel panelSuperior = new JPanel(); + panelSuperior.add(new JLabel("Selecciona un continente: ")); + + comboContinentes = new JComboBox<>(); + + // Renderer para mostrar 煤nicamente el 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; + } + }); + + panelSuperior.add(comboContinentes); + add(panelSuperior, BorderLayout.NORTH); + + // --- Panel Central (Lista de Pa铆ses) --- + modeloPaises = new DefaultListModel<>(); + listaPaises = new JList<>(modeloPaises); + + // Renderer para dar formato de lista a cada Pa铆s + listaPaises.setCellRenderer(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 Pais) { + Pais p = (Pais) value; + setText("Pais [identificador=" + p.getIdentificador() + ", nombre=" + p.getNombre() + "]"); + } + return this; + } + }); + + JScrollPane scrollLista = new JScrollPane(listaPaises); + add(scrollLista, BorderLayout.CENTER); + + // --- Panel Inferior (脕rea de Texto e Informaci贸n) --- + areaInfo = new JTextArea(3, 40); + areaInfo.setEditable(false); + areaInfo.setLineWrap(true); + areaInfo.setWrapStyleWord(true); + JScrollPane scrollArea = new JScrollPane(areaInfo); + add(scrollArea, BorderLayout.SOUTH); + + // --- Cargar Continentes en el desplegable --- + cargarContinentes(); + + // --- Escuchadores de Eventos --- + comboContinentes.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + actualizarListaPaises(); + } + }); + + listaPaises.addListSelectionListener(new ListSelectionListener() { + @Override + public void valueChanged(ListSelectionEvent e) { + if (!e.getValueIsAdjusting()) { + mostrarDetallePais(); + } + } + }); + + // Cargar selecci贸n inicial si existen continentes + if (comboContinentes.getItemCount() > 0) { + comboContinentes.setSelectedIndex(0); + actualizarListaPaises(); + } + } + + private void cargarContinentes() { + if (continenteDAO != null) { + List continentes = continenteDAO.obtenerTodos(); + comboContinentes.removeAllItems(); + if (continentes != null) { + for (Continente c : continentes) { + comboContinentes.addItem(c); + } + } + } + } + + private void actualizarListaPaises() { + modeloPaises.clear(); + areaInfo.setText(""); + Continente cSeleccionado = (Continente) comboContinentes.getSelectedItem(); + if (cSeleccionado != null && paisDAO != null) { + paisesActuales = paisDAO.obtenerPorContinente(cSeleccionado.getCodigo()); + if (paisesActuales != null) { + for (Pais p : paisesActuales) { + modeloPaises.addElement(p); + } + } + } + } + + private void mostrarDetallePais() { + Pais pSeleccionado = listaPaises.getSelectedValue(); + if (pSeleccionado != null && pSeleccionado.getContinente() != null) { + areaInfo.setText(pSeleccionado.getCapital() + ", capital de " + pSeleccionado.getNombre() + " - " + + pSeleccionado.getIdentificador() + ", pertenece al continente " + + pSeleccionado.getContinente().getNombre() + " (" + pSeleccionado.getContinente().getCodigo() + + ")"); + } else if (pSeleccionado != null) { + areaInfo.setText(pSeleccionado.toString()); + } + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + new VentanaContinentesPaises().setVisible(true); + } + }); + } +} \ No newline at end of file