Actividad 6.4 – Cliente Swing: Carga de implementaciones por reflexión
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
bin/
|
||||
.classpath
|
||||
.project
|
||||
.settings/
|
||||
@@ -0,0 +1,9 @@
|
||||
continente.ruta.fichero=C:\\Users\\gonza\\aadd\\fic05\\informacion_continentes.csv
|
||||
|
||||
continente.csv.separador=#
|
||||
|
||||
continente.anyo=2026
|
||||
|
||||
continente.implementacion.clase=org.palomafp.aadd.gdo.dao.impl.ContinenteDaoFichero
|
||||
|
||||
pais.implementacion.clase=org.palomafp.aadd.gdo.dao.impl.PaisDaoFichero
|
||||
@@ -0,0 +1,45 @@
|
||||
package prueba.aadd.swing;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.palomafp.aadd.gdo.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.gdo.dao.IPaisDAO;
|
||||
|
||||
public class FabricaDAO {
|
||||
|
||||
private static final String RUTA_PROPERTIES = "config/application.properties";
|
||||
|
||||
private final Properties props = new Properties();
|
||||
|
||||
public FabricaDAO() {
|
||||
try (FileInputStream fis = new FileInputStream(RUTA_PROPERTIES)) {
|
||||
props.load(fis);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("No se pudo leer " + RUTA_PROPERTIES, e);
|
||||
}
|
||||
}
|
||||
|
||||
public IContinenteDAO crearContinenteDAO() {
|
||||
String clase = props.getProperty("continente.implementacion.clase");
|
||||
try {
|
||||
return (IContinenteDAO) Class.forName(clase)
|
||||
.getDeclaredConstructor()
|
||||
.newInstance();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("No se pudo crear el DAO de continentes: " + clase, e);
|
||||
}
|
||||
}
|
||||
|
||||
public IPaisDAO crearPaisDAO(IContinenteDAO continenteDAO) {
|
||||
String clase = props.getProperty("pais.implementacion.clase");
|
||||
try {
|
||||
return (IPaisDAO) Class.forName(clase)
|
||||
.getDeclaredConstructor(IContinenteDAO.class)
|
||||
.newInstance(continenteDAO);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("No se pudo crear el DAO de países: " + clase, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
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.gdo.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.gdo.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.gdo.model.Continente;
|
||||
import org.palomafp.aadd.gdo.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");
|
||||
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setSize(500, 350);
|
||||
setLocationRelativeTo(null);
|
||||
|
||||
// Inicializar DAO
|
||||
FabricaDAO fabrica = new FabricaDAO();
|
||||
continenteDAO = fabrica.crearContinenteDAO();
|
||||
paisDAO = fabrica.crearPaisDAO(continenteDAO);
|
||||
|
||||
// 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<Continente> continentes = continenteDAO.obtenerContinentes();
|
||||
|
||||
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.obtenerPaisesPorContinente(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.getNombreCapital(),
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user