Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions TP3_GUI/src/gui/ButtonEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,36 @@ public class ButtonEditor extends DefaultCellEditor {
private boolean clicked;
private int row;

/**
* Procura na lista principal a Pessoa correspondente aos valores da linha
* atualmente editada na tabela.
*/
private Pessoa findPessoa(JTable table, int modelRow) {
String nome = table.getModel().getValueAt(modelRow, 0).toString();
String generoStr = table.getModel().getValueAt(modelRow, 1).toString();
char genero;
if (generoStr.toLowerCase().startsWith("masculino")) {
genero = 'M';
} else if (generoStr.toLowerCase().startsWith("feminino")) {
genero = 'F';
} else {
genero = '?';
}
String data = table.getModel().getValueAt(modelRow, 2).toString();
String tele = table.getModel().getValueAt(modelRow, 3).toString();

for (Pessoa pessoa : MainFrame.gestor.getLista()) {
boolean nomeEq = pessoa.getNome().equals(nome);
boolean generoEq = pessoa.getGenero() == genero;
boolean dataEq = java.util.Objects.equals(pessoa.getDataNascimento(), data);
boolean teleEq = java.util.Objects.equals(pessoa.getNumeroTelemovel(), tele);
if (nomeEq && generoEq && dataEq && teleEq) {
return pessoa;
}
}
return null;
}

public ButtonEditor(JCheckBox checkBox, JTable table) {
super(checkBox);
button = new JButton();
Expand All @@ -38,15 +68,7 @@ public ButtonEditor(JCheckBox checkBox, JTable table) {
JOptionPane.QUESTION_MESSAGE, null, opcoes, opcoes[0]);

if (escolha == 0) {
Pessoa p = null;
int i = 0;
for (Pessoa pessoa : MainFrame.gestor.getLista()) {
if (i == modelRow) {
p = pessoa;
break;
}
i++;
}
Pessoa p = findPessoa(table, modelRow);
if (p == null) return; // Segurança: não encontrado


Expand Down Expand Up @@ -85,15 +107,7 @@ public ButtonEditor(JCheckBox checkBox, JTable table) {


} else if (escolha == 1) {
Pessoa aRemover = null;
int i = 0;
for (Pessoa pessoa : MainFrame.gestor.getLista()) {
if (i == modelRow) {
aRemover = pessoa;
break;
}
i++;
}
Pessoa aRemover = findPessoa(table, modelRow);

if (aRemover != null) {
boolean removido = MainFrame.gestor.getLista().remover(aRemover);
Expand Down