/** * Copyright 1999-2009 The Pegadi Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * A simple mailing dialog. Includes error checking before sending. * * @author Øystein Handegard <handegar@underdusken.no> * @version $Revision$, $Date$ */ package org.pegadi.maildialog; import org.pegadi.model.LoginContext; import org.pegadi.sources.Source; import org.pegadi.sources.SourceSearchPanel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import javax.swing.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.awt.*; import java.awt.event.*; import java.io.File; import java.io.IOException; import java.net.URL; import java.rmi.RemoteException; import java.text.MessageFormat; import java.util.ResourceBundle; public class MailDialog extends JDialog { JButton sendMailButton; JButton cancelButton; JTextArea mailBodyArea; JTextField mailSubjectField; JTextField mailToField; JTextField mailFromField; JLabel mailSubjectLabel; JLabel mailToLabel; JLabel mailFromLabel; JScrollPane scrollPane; private File logFile; boolean accepted = false; GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); Container container = this.getContentPane(); /** All user-visible strings. */ ResourceBundle strings; JDialog orgSearchDialog = new JDialog(); MouseListener toLabelListener; private final Logger log = LoggerFactory.getLogger(getClass()); public MailDialog(Frame owner, String title, boolean modal) { super(owner, title, modal); Point p = owner.getLocation(); this.setLocation(p.x + 50, p.y+50); strings = ResourceBundle.getBundle("org.pegadi.maildialog.MailDialogStrings"); createUI(); } /** * Loads an email template from the given <code>url</code> into the form. * Occurences of {0},{1} etc. are replaced with the String with the * corresponding index in <code>replaceWith</code> array. * <p> * Here is an example of an email template<br> * <pre> * <email> * <from>{0} <{1}></from> * <to>pegadi-dev@underdusken.no</to> * <subject>Bug report/feature request</subject> * <text> * Please describe your problem here: * * -- * {0} * {1} * </text> * </email> * </pre> */ public void loadTemplate(String url, Object[] replaceWith) { Document maildoc = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; factory.setIgnoringElementContentWhitespace(false); builder = factory.newDocumentBuilder(); maildoc = builder.parse(new InputSource((new URL(url)).openStream())); } catch (IOException ioe) { log.error("Error loading template", ioe); log.error("Could not get template {}", url, ioe); return; } catch (SAXException se) { log.error("Problem while parsing template {} ", url, se); return; } catch (ParserConfigurationException e) { log.error("Error configuring parser", e); } String fromRaw = ""; if(maildoc.getElementsByTagName("from").getLength()>0) fromRaw = ((org.w3c.dom.Text)maildoc.getElementsByTagName("from").item(0).getFirstChild()).getData(); String toRaw = ""; if(maildoc.getElementsByTagName("to").getLength()>0) toRaw = ((org.w3c.dom.Text)maildoc.getElementsByTagName("to").item(0).getFirstChild()).getData(); String subjectRaw = ""; if(maildoc.getElementsByTagName("subject").getLength()>0) subjectRaw = ((org.w3c.dom.Text)maildoc.getElementsByTagName("subject").item(0).getFirstChild()).getData(); String textRaw = ""; if(maildoc.getElementsByTagName("text").getLength()>0) textRaw = ((org.w3c.dom.Text)maildoc.getElementsByTagName("text").item(0).getFirstChild()).getData(); for(int i=0;i<replaceWith.length;i++) { if(replaceWith[i] == null) replaceWith[i] = "Unknown"; } setSubjectText(MessageFormat.format(subjectRaw,replaceWith)); setFromAddress(MessageFormat.format(fromRaw,replaceWith)); setToAddress(MessageFormat.format(toRaw,replaceWith)); setBodyText(MessageFormat.format(textRaw,replaceWith)); } private void createUI() { setTitle(strings.getString("window.title")); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); mailBodyArea = new JTextArea(20,70); mailBodyArea.setLineWrap(true); mailBodyArea.setWrapStyleWord(true); mailBodyArea.setFont(new Font("monospaced",Font.PLAIN,12)); scrollPane = new JScrollPane(mailBodyArea); mailSubjectField = new JTextField(20); mailToField = new JTextField(20); mailFromField = new JTextField(20); mailSubjectLabel = new JLabel(strings.getString("subject.label")); mailToLabel = new JLabel(strings.getString("to.label")); setToFieldEditable(false); toLabelListener = new MouseAdapter() { public void mouseClicked(MouseEvent e) { mouseClicked_searchSource(); } public void mouseEntered(MouseEvent e) { setCursor(new Cursor(Cursor.HAND_CURSOR)); } public void mouseExited(MouseEvent e) { setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } }; mailFromLabel = new JLabel(strings.getString("from.label")); sendMailButton = new JButton(strings.getString("sendmail.text")); sendMailButton.setIcon(new ImageIcon(getClass().getResource(strings.getString("sendmail.icon")))); sendMailButton.setToolTipText(strings.getString("sendmail.tooltip")); cancelButton = new JButton(strings.getString("cancel.text")); cancelButton.setIcon(new ImageIcon(getClass().getResource(strings.getString("cancel.icon")))); cancelButton.setToolTipText(strings.getString("cancel.tooltip")); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { actionPerformed_cancel(); } } ); sendMailButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { actionPerformed_sendMail(); } } ); Container pane = this.getContentPane(); container.setLayout(gridbag); c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(5, 5, 0, 5); c.gridx = 0; c.gridy = 0; c.anchor = GridBagConstraints.EAST; pane.add(mailFromLabel,c); c.gridx = 1; pane.add(mailFromField,c); c.gridx = 0; c.gridy = 1; pane.add(mailToLabel,c); c.gridx = 1; pane.add(mailToField,c); c.gridx = 0; c.gridy = 2; pane.add(mailSubjectLabel,c); c.gridx = 1; pane.add(mailSubjectField,c); c.gridx = 0; c.gridy = 3; c.gridwidth = 2; pane.add(scrollPane,c); JPanel bPane = new JPanel(); bPane.add(sendMailButton); bPane.add(cancelButton); c.gridx = 0; c.gridy = 4; pane.add(bPane,c); mailToField.requestFocus(); } private void actionPerformed_cancel() { this.accepted = false; MailDialog.this.dispose(); } private void mouseClicked_searchSource() { SourceSearchPanel ssp = new SourceSearchPanel(); Point p = getLocation(); orgSearchDialog.setLocation(p.x + 50, p.y + 50); ssp.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Source source = (Source)e.getSource(); setToAddress(source.getName() +" <" +source.getEmail() +">"); orgSearchDialog.dispose(); } }); orgSearchDialog.getContentPane().add(ssp); orgSearchDialog.pack(); orgSearchDialog.setVisible(true); } private void actionPerformed_sendMail() { int fileSave; fileSave = JOptionPane.showConfirmDialog(this, strings.getString("confirm.label"), strings.getString("confirm.title"), JOptionPane.YES_NO_OPTION); if (fileSave == JOptionPane.YES_OPTION){ this.accepted = true; if(!sendMail()) return; } else { return; } MailDialog.this.dispose(); } private boolean sendMail() { if(mailToField.getText().trim().length() == 0){ JOptionPane.showMessageDialog(this,strings.getString("error_emptytofield"),"",JOptionPane.ERROR_MESSAGE); return(false); } if(!checkMailAddress(mailToField.getText())){ JOptionPane.showMessageDialog(this,strings.getString("error_invalidtofield"),"",JOptionPane.ERROR_MESSAGE); return(false); } if(mailFromField.getText().trim().length() == 0){ JOptionPane.showMessageDialog(this,strings.getString("error_emptyfromfield"),"",JOptionPane.ERROR_MESSAGE); return(false); } if(!checkMailAddress(mailFromField.getText())){ JOptionPane.showMessageDialog(this,strings.getString("error_invalidfromfield"),"",JOptionPane.ERROR_MESSAGE); return(false); } String mailText = mailBodyArea.getText(); boolean value; try { value = LoginContext.server.sendmail(mailFromField.getText(), mailToField.getText(), mailSubjectField.getText(), mailText, LoginContext.sessionKey); } catch(RemoteException ex) { JOptionPane.showMessageDialog(this,strings.getString("error_sendingfailed"),"Error",JOptionPane.ERROR_MESSAGE); log.error("Could not send email beacuse of a server-side error", ex); return false; } if (!value) { JOptionPane.showMessageDialog(this,strings.getString("error_sendingfailed"),"Error",JOptionPane.ERROR_MESSAGE); log.error("Could not send email beacuse of a server-side error"); return false; } return true; } private boolean checkMailAddress(String email) { if(email.length() < 6) // Minimum email-size; 'a@b.cd' return(false); if(!email.contains("@")) return(false); if(!email.contains(".")) return(false); return(true); } public boolean wasAccepted() { return this.accepted; } public void setBodyText(String text) { mailBodyArea.setText(text); mailBodyArea.setCaretPosition(0); } public void setSubjectText(String subject) { mailSubjectField.setText(subject); } public void setFromAddress(String address) { mailFromField.setText(address); } public void setToAddress(String address) { mailToField.setText(address); } public void setToLabelActive(boolean bool) { if(bool) { mailToLabel.addMouseListener(toLabelListener); mailToLabel.setText("<html><font color=blue><u>" +strings.getString("to.label") +"</u></font></html>"); } else { mailToLabel.setText(strings.getString("to.label")); mailToLabel.removeMouseListener(toLabelListener); } } public void setToFieldEditable(boolean bool) { mailToField.setEditable(bool); } public void setFromFieldEditable(boolean bool) { mailFromField.setEditable(bool); } public void setSubjectFieldEditable(boolean bool) { mailSubjectField.setEditable(bool); } }