Documentation Index
Fetch the complete documentation index at: https://mintlify.com/efrain-svg/Potes_Freddy_ProgInterfacesG_U3/llms.txt
Use this file to discover all available pages before exploring further.
logica_ventana is the single controller in the Contact Management App’s MVC architecture. It receives a reference to the ventana view at construction, registers all event listeners, sets up the table sorter and keyboard shortcuts, then loads contacts from disk — all before the window becomes interactive. Every user action flows through this class, which coordinates between the ventana view and the personaDAO persistence layer while keeping all UI mutations safely on the Event Dispatch Thread.
Class declaration
src/controlador/logica_ventana.java
ActionListener), table row selection changes (ListSelectionListener), and combo box / checkbox state changes (ItemListener) without anonymous inner classes for those events.
Constructor
src/controlador/logica_ventana.java
configurarEventos()
Adds
this as an ActionListener on all buttons and as an ItemListener on all combo boxes and the checkbox. Also registers the context-menu items.configurarTablaYFiltro()
Creates a
TableRowSorter, attaches it to the table, wires a DocumentListener on txt_buscar to trigger async search on every keystroke, and adds a MouseAdapter that toggles favorites on star-column clicks and shows the context menu on right-click.configurarAtajos()
Binds five keyboard shortcuts to the root pane’s
InputMap/ActionMap so they work regardless of which component has focus.configurarMenuContextual()
Calls
tbl_contactos.setComponentPopupMenu(delegado.menuContextual) so the popup appears on right-click automatically.configurarCierre()
Attaches a
WindowAdapter that calls shutdown() on both executor services when the window closes, preventing thread leaks.aplicarIdioma(I18n.LANG_ES)
Sets Spanish as the initial language, translates every visible label and combo item, and refreshes the table column headers.
Key fields
| Field | Type | Purpose |
|---|---|---|
delegado | ventana | The view instance — all UI reads/writes go through this reference |
dao | personaDAO | Data access object for CSV read/write |
i18n | I18n | Internationalisation helper; holds current language and formats dates |
contactos | List<persona> | In-memory contact list; the authoritative source for table data |
sorter | TableRowSorter<DefaultTableModel> | Attached to the table for live text/category filtering and column sorting |
editLock | ReentrantLock | Prevents two concurrent edit operations on the same row |
exportExecutor | ExecutorService | Fixed thread pool (size 2) for background CSV export tasks |
notificationExecutor | ExecutorService | Single-thread executor that serialises status-bar update messages |
searchSeq | AtomicInteger | Monotonically-increasing sequence number; stale SwingWorker results are discarded when seq != searchSeq.get() |
busyCount | AtomicInteger | Reference-counted busy flag; UI controls are disabled while count > 0 |
Setup methods
configurarEventos()
Registers listeners on every interactive component exposed by ventana. Buttons and menu items get addActionListener(this); combos and the checkbox get addItemListener(this); the table selection model gets addListSelectionListener(this).
configurarTablaYFiltro()
src/controlador/logica_ventana.java
MouseAdapter on the table dispatches single clicks on the favorite column to alternarFavoritoSeleccionado() and right-click to the context menu.
configurarAtajos()
| Shortcut | Action |
|---|---|
Ctrl + S | Save (add or update depending on selection) |
Ctrl + N | Clear form fields |
Delete | Delete selected contact |
Ctrl + E | Export visible contacts to CSV |
Ctrl + F | Focus and select-all in the search field |
configurarMenuContextual()
Sets the popup menu on the table via setComponentPopupMenu, which handles both the right-click trigger and the platform-specific popup gesture automatically.
configurarCierre()
src/controlador/logica_ventana.java
CRUD methods
agregarContacto()
Reads and validates the form fields, then calls validarContactoAsync(-1, ...) to check for duplicates on a background thread. If no duplicate is found, a new persona is appended to contactos, persisted via dao.actualizarContactos(), and the table is refreshed.
modificarContactoSeleccionado()
src/controlador/logica_ventana.java
editLock via tryLock() before proceeding. If the lock is already held by another edit operation, the user sees an “edit locked” message. On success the contact at the selected model index is mutated in place, saved, and the lock is released.
eliminarSeleccionado()
Shows a localised confirmation dialog before removing the contact from contactos under synchronized(contactosLock), then calls guardarCambiosDisco() and refreshes the table and statistics panels.
alternarFavoritoSeleccionado()
Flips persona.isFavorito() for the selected row under contactosLock, persists, and refreshes without clearing form fields — so the edit form remains populated.
Async methods
solicitarBusquedaAsync()
src/controlador/logica_ventana.java
SwingWorker and increments searchSeq. The done() callback drops stale results by comparing the local seq snapshot with the current atomic value — only the most recent search applies the filter.
validarContactoAsync(int indexIgnorado, Runnable onValid)
Runs duplicate detection on a background thread under contactosLock. A contact is considered a duplicate if it shares an email address or phone number with any existing contact (excluding the one at indexIgnorado for update operations). Calls onValid.run() on the EDT if no duplicate is found, otherwise shows a warning dialog.
cargarContactosRegistrados()
Sets the progress bar to indeterminate, then delegates to a SwingWorker that calls dao.leerArchivo(). The done() callback populates contactos, refreshes the table, and updates the statistics panel.
exportarCsv()
Opens a JFileChooser, sets UI busy, then submits the export task to exportExecutor. The export runs under synchronized(exportLock) to prevent concurrent writes to the same destination. The EDT is notified on completion via SwingUtilities.invokeLater.
notificarAsync(String mensaje)
src/controlador/logica_ventana.java
notificationExecutor so messages are always applied in submission order, even when multiple async operations produce notifications simultaneously.
Language method: aplicarIdioma(String code)
Accepts one of "es", "en", or "pt". Updates i18n, sets the window title, translates every label, button, menu item, and table column header, rebuilds the combo box items while preserving selection indices, then calls refrescarTabla() and actualizarEstadisticas() to redraw localised content. The actualizandoCombos flag prevents the ItemListener from triggering spurious filter resets during the repopulation loop.
Interface methods
actionPerformed(ActionEvent e)
Dispatches to the appropriate CRUD or export method by comparing e.getSource() against the known button and menu item references.
| Source | Dispatches to |
|---|---|
btn_add | agregarContacto() |
btn_modificar | modificarContactoSeleccionado() |
mnu_editar | prepararEdicionSeleccionada() |
btn_eliminar, mnu_eliminar | eliminarSeleccionado() |
btn_exportar, mnu_exportar | exportarCsv() |
mnu_favorito | alternarFavoritoSeleccionado() |
valueChanged(ListSelectionEvent e)
Ignores adjusting events (e.getValueIsAdjusting()). When a stable selection exists, calls cargarContacto(index) to populate the form fields with the selected contact’s data.
itemStateChanged(ItemEvent e)
Handles three sources:
chb_favorito— updates the localfavoritoboolean immediately on any state change.cmb_categoria— updates the localcategoriacode whenSELECTEDand not rebuilding combos.cmb_filtro_categoria— callsaplicarFiltro()(→solicitarBusquedaAsync()) whenSELECTED.cmb_idioma— callsaplicarIdioma()with the newly selected language code whenSELECTED.