first commit
This commit is contained in:
@@ -0,0 +1,16 @@
|
|||||||
|
# Componentes_04
|
||||||
|
|
||||||
|
Cliente Swing que carga los DAO por reflexión.
|
||||||
|
|
||||||
|
### Requisitos
|
||||||
|
|
||||||
|
- Proyecto `Ficheros_09` en el classpath:
|
||||||
|
- Su `application.properties` debe incluir las claves de implementación:
|
||||||
|
|
||||||
|
```Plain text
|
||||||
|
|
||||||
|
continente.implementacion.clase=org.palomafp.aadd.dta.dao.impl.ContinenteDaoFichero
|
||||||
|
pais.implementacion.clase=org.palomafp.aadd.dta.dao.impl.PaisDaoFichero
|
||||||
|
```
|
||||||
|
|
||||||
|
Las claves de rutas y separadores (`continente.ruta.fichero`, `pais.ruta.fichero`, etc.) las usan los DAO de `Ficheros_09`. Deben ser adaptadas a cada equipo.
|
||||||
@@ -3,6 +3,7 @@ package prueba.aadd.swing;
|
|||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.Component;
|
import java.awt.Component;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
import javax.swing.DefaultListCellRenderer;
|
import javax.swing.DefaultListCellRenderer;
|
||||||
import javax.swing.DefaultListModel;
|
import javax.swing.DefaultListModel;
|
||||||
@@ -15,12 +16,10 @@ import javax.swing.JScrollPane;
|
|||||||
import javax.swing.JTextArea;
|
import javax.swing.JTextArea;
|
||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
import org.palomafp.aadd.inm.dao.IContinenteDAO;
|
import org.palomafp.aadd.dta.dao.IContinenteDAO;
|
||||||
import org.palomafp.aadd.inm.dao.IPaisDAO;
|
import org.palomafp.aadd.dta.dao.IPaisDAO;
|
||||||
import org.palomafp.aadd.inm.dao.impl.ContinenteDaoCSV;
|
import org.palomafp.aadd.dta.model.Continente;
|
||||||
import org.palomafp.aadd.inm.dao.impl.PaisDaoText;
|
import org.palomafp.aadd.dta.model.Pais;
|
||||||
import org.palomafp.aadd.inm.model.Continente;
|
|
||||||
import org.palomafp.aadd.inm.model.Pais;
|
|
||||||
|
|
||||||
public class VentanaContinentesPaises extends JFrame {
|
public class VentanaContinentesPaises extends JFrame {
|
||||||
/**
|
/**
|
||||||
@@ -45,9 +44,12 @@ public class VentanaContinentesPaises extends JFrame {
|
|||||||
setSize(500, 350);
|
setSize(500, 350);
|
||||||
setLocationRelativeTo(null);
|
setLocationRelativeTo(null);
|
||||||
|
|
||||||
// Inicializar DAO
|
// Carga por reflexión
|
||||||
continenteDAO = new ContinenteDaoCSV();
|
try {
|
||||||
paisDAO = new PaisDaoText(continenteDAO);
|
cargarImplementaciones();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IllegalStateException("No se pudieron cargar los DAO", e);
|
||||||
|
}
|
||||||
|
|
||||||
// Componentes
|
// Componentes
|
||||||
comboContinentes = new JComboBox<>();
|
comboContinentes = new JComboBox<>();
|
||||||
@@ -103,11 +105,43 @@ public class VentanaContinentesPaises extends JFrame {
|
|||||||
// Cargar continentes al iniciar
|
// Cargar continentes al iniciar
|
||||||
cargarContinentes();
|
cargarContinentes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Método para cargar por reflexión
|
||||||
|
private void cargarImplementaciones() throws Exception {
|
||||||
|
|
||||||
|
Properties config = new Properties();
|
||||||
|
|
||||||
|
try (var in = VentanaContinentesPaises.class.getResourceAsStream("/application.properties")) {
|
||||||
|
|
||||||
|
if (in == null) {
|
||||||
|
throw new IllegalStateException("No se encontró application.properties en el classpath");
|
||||||
|
}
|
||||||
|
|
||||||
|
config.load(in);
|
||||||
|
}
|
||||||
|
|
||||||
|
String claseContinente = config.getProperty("continente.implementacion.clase");
|
||||||
|
String clasePais = config.getProperty("pais.implementacion.clase");
|
||||||
|
|
||||||
|
if (claseContinente == null || clasePais == null) {
|
||||||
|
throw new IllegalStateException("Las propiedades de clase no se encuentran en application.properties");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reflexión para continenteDAO
|
||||||
|
continenteDAO = (IContinenteDAO) Class.forName(claseContinente.trim())
|
||||||
|
.getDeclaredConstructor()
|
||||||
|
.newInstance();
|
||||||
|
|
||||||
|
// Reflexión para paisDAO
|
||||||
|
paisDAO = (IPaisDAO) Class.forName(clasePais.trim())
|
||||||
|
.getDeclaredConstructor(IContinenteDAO.class)
|
||||||
|
.newInstance(continenteDAO);
|
||||||
|
}
|
||||||
|
|
||||||
private void cargarContinentes() {
|
private void cargarContinentes() {
|
||||||
|
|
||||||
// Sustituir por el nombre real del método de tu DAO
|
// Sustituir por el nombre real del método de tu DAO
|
||||||
List<Continente> continentes = continenteDAO.obtenerListaContinentes();
|
List<Continente> continentes = continenteDAO.obtenerTodos();
|
||||||
|
|
||||||
comboContinentes.removeAllItems();
|
comboContinentes.removeAllItems();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user