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.data.outer.EstabelecimentosData; import siprp.data.outer.HsEmailData; import siprp.data.outer.HsEmailEstabelecimentoData; import siprp.higiene.gestao.AdicionarPanel; import siprp.logic.HigieneSegurancaLogic; import com.evolute.utils.ui.trees.TreeTools; public class AdicionarEmailsPanel extends AdicionarPanel { private static final long serialVersionUID = 1L; private EstabelecimentosData estabelecimento = null; public void setEstabelecimento( EstabelecimentosData estabelecimento ) { this.estabelecimento = estabelecimento; doRefresh(); setEnabled(); } @Override protected void add() { try { if( estabelecimento != null ) { String emailString = JOptionPane.showInputDialog( this, "Email", "Adicionar novo Email", JOptionPane.QUESTION_MESSAGE ); if( emailString != null ) { HsEmailData email = new HsEmailData(); email.setEmail( emailString ); email.save(); HsEmailEstabelecimentoData rel = new HsEmailEstabelecimentoData(); rel.setToEmail_id( email ); rel.setToEstabelecimento_id( estabelecimento ); rel.save(); doRefresh(); } } } 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 HsEmailEstabelecimentoData ) { ((HsEmailEstabelecimentoData) obj).delete(); doRefresh(); } } } } 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 ); } }