Actividad 6.4
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
||||
# Eclipse
|
||||
.classpath
|
||||
.project
|
||||
.settings/
|
||||
|
||||
# Compilación Java
|
||||
bin/
|
||||
target/
|
||||
*.class
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
|
||||
# Sistema operativo
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
@@ -0,0 +1,14 @@
|
||||
# Elegir nombres de entradas que sean SIGNIFICATIVOS
|
||||
# y que no se repitan con otras entradas de la misma aplicación
|
||||
|
||||
# Información 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ón relativa Países
|
||||
pais.ruta.fichero=C:/Users/aleja/aadd/fic05/informacion_paises.txt
|
||||
pais.txt.separador=#
|
||||
pais.implementacion.clase=org.palomafp.aadd.aez.dao.impl.PaisDaoFichero
|
||||
@@ -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<Continente> comboContinentes;
|
||||
private JList<Pais> listaPaises;
|
||||
private DefaultListModel<Pais> modeloPaises;
|
||||
private JTextArea areaInfo;
|
||||
|
||||
private List<Pais> 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<Continente> 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user