package siprp.higiene.gestao.email; import static com.evolute.utils.strings.UnicodeLatin1Map.atilde; import static com.evolute.utils.strings.UnicodeLatin1Map.ccedil; import javax.swing.JOptionPane; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreePath; import leaf.ui.LeafDialog; import siprp.database.cayenne.objects.Estabelecimentos; import siprp.database.cayenne.objects.HsEmail; import siprp.database.cayenne.objects.HsEmailEstabelecimento; import siprp.higiene.gestao.AdicionarPanel; import siprp.logic.HigieneSegurancaLogic; import com.evolute.adt.TreeTools; public class AdicionarEmailsPanel extends AdicionarPanel { private static final long serialVersionUID = 1L; private Estabelecimentos estabelecimento = null; public void setEstabelecimento( Estabelecimentos estabelecimento ) { this.estabelecimento = estabelecimento; refresh(); setEnabled(); } @Override protected void add() { try { if( estabelecimento != null ) { String emailString = JOptionPane.showInputDialog( this, "Email", "Adicionar novo Email", JOptionPane.QUESTION_MESSAGE ); if( emailString != null ) { HsEmail email = new HsEmail(); email.setEmail( emailString ); email.save(); HsEmailEstabelecimento rel = new HsEmailEstabelecimento(); rel.setToHsEmail( email ); rel.setToEstabelecimento( estabelecimento ); rel.save(); refresh(); } } } catch (Exception e) { LeafDialog.error( e ); } } private boolean confirmDelete() { return JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog( null, "Tem a certeza que deseja remover o email seleccionado?", "Confirmar remo" + ccedil + atilde + "o", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null ); } @Override protected void rem() { try { TreePath path = tree.getSelectionPath(); if( path != null ) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); if( node != null && confirmDelete() ) { Object obj = node.getUserObject(); if( obj instanceof HsEmailEstabelecimento ) { ((HsEmailEstabelecimento) obj).delete(); refresh(); } } } } catch (Exception e) { LeafDialog.error( e ); } } @Override public void refresh() { root.removeAllChildren(); TreeTools.merge( root, HigieneSegurancaLogic.getEmailsForEstabelecimento( estabelecimento ) ); TreeTools.refreshTree( tree, root, false ); } @Override protected void setEnabled() { buttonAdicionar.setEnabled( estabelecimento != null ); buttonRemover.setEnabled( tree.getSelectionCount() > 0 ); } }