Actividad 6.4: DAO por reflexión
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="module" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry combineaccessrules="false" kind="src" path="/Ficheros_09"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
bin/
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>Componentes_04</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
encoding/<project>=UTF-8
|
||||||
@@ -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<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
|
||||||
|
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<Continente> 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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user