From 37aba86fd84eaccf2c703c5ef20a517980b5333a Mon Sep 17 00:00:00 2001 From: Frederico Palma Date: Fri, 14 Dec 2007 14:34:34 +0000 Subject: [PATCH] git-svn-id: https://svn.coded.pt/svn/SIPRP@600 bb69d46d-e84e-40c8-a05a-06db0d633741 --- .../processo/mail/ColorChooserDialog.java | 8 + .../medicina/processo/mail/MailPanel.java | 213 ++++++++++++++++-- .../medicina/processo/mail/MailSender.java | 58 +++++ 3 files changed, 262 insertions(+), 17 deletions(-) create mode 100644 trunk/SIPRPSoft/src/siprp/medicina/processo/mail/MailSender.java diff --git a/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/ColorChooserDialog.java b/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/ColorChooserDialog.java index fcf8f45a..c3ffbbaa 100644 --- a/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/ColorChooserDialog.java +++ b/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/ColorChooserDialog.java @@ -35,6 +35,14 @@ public class ColorChooserDialog extends CustomJDialog super( owner, true ); initialColor = color; setupComponents(); + if( owner != null ) + { + centerSuper(); + } + else + { + center(); + } } private void setupComponents() diff --git a/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/MailPanel.java b/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/MailPanel.java index 4d1701bb..74ef33d5 100644 --- a/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/MailPanel.java +++ b/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/MailPanel.java @@ -5,6 +5,7 @@ import info.clearthought.layout.TableLayoutConstraints; import java.awt.BorderLayout; import java.awt.Color; +import java.awt.FileDialog; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; @@ -13,6 +14,11 @@ import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FilenameFilter; +import java.io.IOException; import java.util.Arrays; import java.util.Comparator; import java.util.Vector; @@ -24,10 +30,13 @@ import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; +import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.JTextPane; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; import javax.swing.text.AttributeSet; import javax.swing.text.DefaultEditorKit; import javax.swing.text.StyleConstants; @@ -37,12 +46,15 @@ import javax.swing.text.html.HTMLEditorKit; import com.evolute.utils.images.ImageIconLoader; import com.evolute.utils.tables.BaseTable; import com.evolute.utils.tables.VectorTableModel; +import com.evolute.utils.ui.DialogException; public class MailPanel extends JPanel { protected StyledEditorKit.FontSizeAction FONT_SIZE_ACTIONS[]; protected int FONT_SIZES[]; + protected JFrame owner; + protected JTextField toText; protected JTextField bccText; protected JTextField subjectText; @@ -50,25 +62,62 @@ public class MailPanel extends JPanel protected VectorTableModel attachmentsModel; protected JTextPane bodyPane; + protected Action removeAttachmentAction; + public static void main( String args[] ) throws Exception { JFrame frm = new JFrame(); frm.getContentPane().setLayout( new GridLayout( 1, 1 ) ); - frm.getContentPane().add( new MailPanel() ); + MailPanel mailPanel = new MailPanel( frm ); + frm.getContentPane().add( mailPanel ); frm.addWindowListener( new WindowAdapter(){ public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); + mailPanel.setTo( "fredPalma@netcabo.pt" ); + mailPanel.setBcc( "fpalma@evolute.pt" ); + mailPanel.setSubject( "SIPRP - Marca\u00E7\u00E3o de consulta de 'Frederico Palma'" ); + mailPanel.setMessage( + "

" + + "Vimos pelo presente informar que 'Frederico Palma' deverá comparecer " + + "nas nossas instalações para a realização da consulta de Medicina " + + "do Trabalho, no dia '10-01-2008', pelas 08H30. " + + "

" + + "

" + + "Solicitamos, também, que o colaborador seja portador do Boletim de Vacinas e " + + "dos últimos exames complementares realizados." + + "

" + + "

" + + "Caso não seja possível a comparência deste colaborador na data " + + "indicada, contacte-nos, por favor, através do telefone 21 350 45 40 " + + "ou respondendo ao remetente desta mensagem." + + "

" + + "

" + + "Cumprimentos," + + "

" + + "

" + + "SIPRP" + + "

" + + "

" + + "ATRIUM SALDANHA" + + "

" + + "

" + + "Praça Duque de Saldanha, 1 - 9ºG" + + "

" + + "

" + + "1050-094 Lisboa" + + "

" ); frm.setSize( 1024, 768 ); frm.setVisible( true ); } - public MailPanel() + public MailPanel( JFrame owner ) throws Exception { + this.owner = owner; setupComponents(); } @@ -95,9 +144,10 @@ public class MailPanel extends JPanel public void actionPerformed( ActionEvent e ) { - + send(); } }; + sendAction.putValue( Action.SHORT_DESCRIPTION, "Enviar" ); JButton sendButton = createButton( sendAction, "siprp/medicina/processo/mail/icons/mail2.png" ); Action attachAction = new AbstractAction( "attach" ){ /** @@ -107,11 +157,30 @@ public class MailPanel extends JPanel public void actionPerformed( ActionEvent e ) { - + FileDialog fd = new FileDialog( owner, "Escolha um ficheiro", FileDialog.LOAD ); + fd.setDirectory( System.getProperty( "user.home" ) ); + fd.setFilenameFilter( new FilenameFilter() { + public boolean accept( File dir, String name ) + { + return (name!=null) && (name.indexOf( ".xls" ) != -1); + } + } ); + fd.setVisible( true ); + //String filename = "c:\\test.xls"; + String filename = fd.getFile(); + if( filename != null ) + { + String dir = fd.getDirectory(); + String full = filename + " (" + dir + ")"; + Vector values = attachmentsModel.getValues(); + values.add( full ); + attachmentsModel.setValues( values ); + } } }; + attachAction.putValue( Action.SHORT_DESCRIPTION, "Acrescentar anexo" ); JButton attachButton = createButton( attachAction, "siprp/medicina/processo/mail/icons/mail_attachment.png" ); - Action removeAttachmentAction = new AbstractAction( "remove-attachment" ){ + removeAttachmentAction = new AbstractAction( "remove-attachment" ){ /** * */ @@ -119,16 +188,28 @@ public class MailPanel extends JPanel public void actionPerformed( ActionEvent e ) { - + int selected = attachmentsTable.getSelectedRow(); + if( selected > -1 && selected < attachmentsTable.getRowCount() ) + { + attachmentsModel.removeRowAt( selected ); + } } }; + removeAttachmentAction.putValue( Action.SHORT_DESCRIPTION, "Remover anexo" ); JButton removeAttachmentButton = createButton( removeAttachmentAction, "siprp/medicina/processo/mail/icons/delete2.png" ); + removeAttachmentAction.setEnabled( false ); - JPanel buttonPanel = new JPanel(); - buttonPanel.setLayout( new FlowLayout( FlowLayout.LEFT ) ); - buttonPanel.add( sendButton ); - buttonPanel.add( attachButton ); - buttonPanel.add( removeAttachmentButton ); + JPanel buttonPanel = new JPanel(); + buttonPanel.setLayout( new GridLayout( 1, 2 ) ); + JPanel leftButtonPanel = new JPanel(); + buttonPanel.add( leftButtonPanel ); + JPanel rightButtonPanel = new JPanel(); + buttonPanel.add( rightButtonPanel ); + leftButtonPanel.setLayout( new FlowLayout( FlowLayout.LEFT ) ); + leftButtonPanel.add( sendButton ); + rightButtonPanel.setLayout( new FlowLayout( FlowLayout.RIGHT ) ); + rightButtonPanel.add( attachButton ); + rightButtonPanel.add( removeAttachmentButton ); JLabel toLabel = new JLabel( "Para:" ); toText = new JTextField(); @@ -141,7 +222,16 @@ public class MailPanel extends JPanel attachmentsModel = new VectorTableModel( new String[]{ "" } ); attachmentsTable = new BaseTable( attachmentsModel ); JScrollPane attachmentsScroll = new JScrollPane( attachmentsTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, - JScrollPane.HORIZONTAL_SCROLLBAR_NEVER ); + JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ); + attachmentsTable.getSelectionModel().addListSelectionListener( new ListSelectionListener(){ + + @Override + public void valueChanged(ListSelectionEvent e) + { + removeAttachmentAction.setEnabled( attachmentsTable.getSelectedRow() != -1 ); + } + + } ); TableLayout layout = new TableLayout( @@ -181,10 +271,6 @@ public class MailPanel extends JPanel } fontSizeActions.add( ( StyledEditorKit.FontSizeAction ) actions[ n ] ); } - else if( str.toLowerCase().indexOf( "font" ) != -1 ) - { - System.out.println( str ); - } } FONT_SIZE_ACTIONS = fontSizeActions.toArray( new StyledEditorKit.FontSizeAction[ fontSizeActions.size() ] ); @@ -209,12 +295,18 @@ public class MailPanel extends JPanel ActionMap actionMap = bodyPane.getActionMap(); Action cutAction = actionMap.get( DefaultEditorKit.cutAction ); + cutAction.putValue( Action.SHORT_DESCRIPTION, "Cortar" ); Action copyAction = actionMap.get( DefaultEditorKit.copyAction ); + copyAction.putValue( Action.SHORT_DESCRIPTION, "Copiar" ); Action pasteAction = actionMap.get( DefaultEditorKit.pasteAction ); + pasteAction.putValue( Action.SHORT_DESCRIPTION, "Colar" ); Action boldAction = actionMap.get( "font-bold" ); + boldAction.putValue( Action.SHORT_DESCRIPTION, "Bold" ); Action italicAction = actionMap.get( "font-italic" ); + italicAction.putValue( Action.SHORT_DESCRIPTION, "It\u00e1lico" ); Action underlineAction = actionMap.get( "font-underline" ); + underlineAction.putValue( Action.SHORT_DESCRIPTION, "Sublinhado" ); Action fontBiggerAction = new StyledEditorKit.FontSizeAction( "font-bigger", 30 ){ /** * @@ -238,6 +330,7 @@ public class MailPanel extends JPanel bodyPane.requestFocusInWindow(); } }; + fontBiggerAction.putValue( Action.SHORT_DESCRIPTION, "Fonte maior" ); Action fontSmallerAction = new StyledEditorKit.FontSizeAction( "font-smaller", 5 ){ /** @@ -262,6 +355,7 @@ public class MailPanel extends JPanel bodyPane.requestFocusInWindow(); } }; + fontSmallerAction.putValue( Action.SHORT_DESCRIPTION, "Fonte menor" ); Action colorAction = new StyledEditorKit.ForegroundAction( "color", Color.black ){ /** @@ -271,7 +365,7 @@ public class MailPanel extends JPanel public void actionPerformed( ActionEvent e ) { - ColorChooserDialog dialog = new ColorChooserDialog( null, Color.black ); + ColorChooserDialog dialog = new ColorChooserDialog( owner, Color.black ); dialog.setVisible( true ); Color color = dialog.getColor(); if( color != null ) @@ -284,6 +378,7 @@ public class MailPanel extends JPanel bodyPane.requestFocusInWindow(); } }; + colorAction.putValue( Action.SHORT_DESCRIPTION, "C\u00f4r" ); String fontFamilies[] = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); JComboBox fontCombo = new JComboBox(); for( int n = 0; n < fontFamilies.length; n++ ) @@ -306,12 +401,16 @@ public class MailPanel extends JPanel } ); Action alignLeftAction = actionMap.get( "left-justify" ); + alignLeftAction.putValue( Action.SHORT_DESCRIPTION, "Alinhado \u00e0 esquerda" ); Action alignCenterAction = actionMap.get( "center-justify" ); + alignCenterAction.putValue( Action.SHORT_DESCRIPTION, "Centrado" ); Action alignRightAction = actionMap.get( "right-justify" ); + alignRightAction.putValue( Action.SHORT_DESCRIPTION, "Alinhado \u00e0 direita" ); Action alignJustifyAction = new StyledEditorKit.AlignmentAction( "justify", javax.swing.text.StyleConstants.ALIGN_JUSTIFIED ); actionMap.put( "justify", alignJustifyAction ); + alignJustifyAction.putValue( Action.SHORT_DESCRIPTION, "Justificado" ); JButton exportButton = new JButton( "X" ); exportButton.addActionListener( new ActionListener(){ @@ -358,4 +457,84 @@ public class MailPanel extends JPanel return button; } + + public void setTo( String to ) + { + toText.setText( to ); + } + + public void setBcc( String bcc ) + { + bccText.setText( bcc ); + } + + public void setSubject( String subject ) + { + subjectText.setText( subject ); + } + + public void setMessage( String message ) + { + bodyPane.setText( message ); + } + + public void send() + { + String to = toText.getText(); + String bcc = bccText.getText(); + String subject = subjectText.getText(); + String body = bodyPane.getText(); + String attachmentList[] = new String[ attachmentsTable.getRowCount() ]; + byte attachments[][] = new byte[ attachmentsTable.getRowCount() ][]; + Vector values = attachmentsModel.getValues(); + for( int n = 0; n < attachmentList.length; n++ ) + { + String str = ( String ) values.elementAt( n ); + String name = str.substring( 0, str.indexOf( "(") - 1 ); + String path = str.substring( str.indexOf( "(") + 1, str.indexOf( ")") ); + try + { + FileInputStream fis = new FileInputStream( path + name ); + Vector bytes = new Vector(); + int available = 0; + int total = 0; + while( ( available = fis.available() ) > 0 ) + { + byte b[] = new byte[ available ]; + fis.read( b ); + bytes.add( b ); + total += available; + } + attachments[ n ] = new byte[ total ]; + int pos = 0; + for( byte[] chunk : bytes ) + { + System.arraycopy( chunk, 0, attachments[ n ], pos, chunk.length ); + pos += chunk.length; + } + } + catch( FileNotFoundException fnfex ) + { + JOptionPane.showMessageDialog( owner, + "O ficheiro " + path + name + " n\u00e3o existe.", + "Ficheiro inexistente", + JOptionPane.ERROR_MESSAGE ); + return; + } + catch( IOException ioex ) + { + DialogException.showExceptionMessage( ioex, "Erro a ler ficheiro " + path + name, true ); + return; + } + } + MailSender sender = new MailSender(); + try + { + sender.send( to, bcc, subject, body, attachmentList, attachments ); + } + catch( Exception ex ) + { + ex.printStackTrace(); + } + } } diff --git a/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/MailSender.java b/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/MailSender.java new file mode 100644 index 00000000..6702d8e4 --- /dev/null +++ b/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/MailSender.java @@ -0,0 +1,58 @@ +package siprp.medicina.processo.mail; + +import java.util.Date; +import java.util.Properties; + +import javax.mail.Authenticator; +import javax.mail.Message; +import javax.mail.PasswordAuthentication; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + +public class MailSender +{ + protected String from = "fpalma@evolute.pt"; + protected String mailServer = "mail2.evolute.pt"; + protected String userName = "fpalma@evolute.pt"; + protected String password = "UNIX.Luvs.U"; + + public MailSender() + { + + } + + public void send( String to, String bcc, String subject, String body, + String attachmentNames[], byte attachments[][] ) + throws Exception + { + Properties props = System.getProperties(); + props.put( "mail.smtp.host", mailServer ); + props.put( "mail.smtp.auth", "true" ); + + Authenticator auth = new SMTPAuthenticator(); + Session session = Session.getInstance( props, auth ); + + Message msg = new MimeMessage( session ); + + msg.setFrom( new InternetAddress( from ) ); + msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( to, true ) ); + msg.setRecipients( Message.RecipientType.BCC, InternetAddress.parse( bcc, true ) ); + msg.setSubject( subject ); + msg.setContent( body, "text/html" ); + + msg.setHeader( "X-Mailer", "Evolute Mailer" ); + msg.setSentDate( new Date() ); + Transport.send( msg ); + } + + private class SMTPAuthenticator extends javax.mail.Authenticator + { + + public PasswordAuthentication getPasswordAuthentication() + { + return new PasswordAuthentication(userName, password); + } + } +}