198 lines
5.2 KiB
Java
198 lines
5.2 KiB
Java
package prueba.aadd.swing;
|
||
|
||
import java.awt.BorderLayout;
|
||
import java.awt.Component;
|
||
import java.util.List;
|
||
import java.util.Properties;
|
||
|
||
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.dta.dao.IContinenteDAO;
|
||
import org.palomafp.aadd.dta.dao.IPaisDAO;
|
||
import org.palomafp.aadd.dta.model.Continente;
|
||
import org.palomafp.aadd.dta.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);
|
||
|
||
// Carga por reflexión
|
||
try {
|
||
cargarImplementaciones();
|
||
} catch (Exception e) {
|
||
throw new IllegalStateException("No se pudieron cargar los DAO", 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();
|
||
}
|
||
|
||
// 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() {
|
||
|
||
// Sustituir por el nombre real del método de tu DAO
|
||
List<Continente> continentes = continenteDAO.obtenerTodos();
|
||
|
||
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.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);
|
||
});
|
||
}
|
||
}
|