TheConnectDialogclass has a number of accessor methods for retrieving the connection settings entered in the dialog box. Each of thegetType( ),getServer( ),getUsername( ),
getPassword( ), andgetSmtpServer( )methods simply returns the value entered into its corresponding control.
The DownloadingDialog Class
Immediately after the Connect dialog box has been disposed, the Downloading dialog box illustrated in Figure 5-3 is launched. This dialog box tells the user that e-mail messages are being downloaded. Additionally, because the Downloading dialog box is modal, it prevents the user from using any other part of E-mail Client while downloading is under way.
TheDownloadingDialogclass is shown here. Notice that it extendsJDialog:
import java.awt.*; import javax.swing.*;
/* This class displays a simple dialog box instructing the user that messages are being downloaded. */
public class DownloadingDialog extends JDialog {
// Constructor for dialog box.
public DownloadingDialog(Frame parent) {
// Call super constructor, specifying that dialog box is modal. super(parent, true);
// Set dialog box title. setTitle("E-mail Client");
// Instruct window not to close when the "X" is clicked. setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
// Put a message with a nice border in this dialog box. JPanel contentPane = new JPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
contentPane.add(new JLabel("Downloading messages...")); setContentPane(contentPane);
// Size dialog box to components. pack();
// Center dialog box over application. setLocationRelativeTo(parent);
} }
TheDownloadingDialogclass is clear cut in that it contains only a constructor method. The constructor begins by mirroring theConnectDialogconstructor withsuper( )and
setTitle( )method calls. Next, thesetDefaultCloseOperation( )method is called to change the dialog box’s behavior so that it doesn’t close when the close box is clicked. This prevents the user from being able to close the window prematurely. After that, the message label is added to the display with an empty (invisible) border of 5 pixels for spacing. Next,pack( )
is invoked to size the dialog box to the minimum size required by its controls. Finally, a call tosetLocationRelativeTo( )centers the dialog box over the parent window.
C h a p t e r 5 : I m p l e m e n t i n g a n E - m a i l C l i e n t i n J a v a
1 3 3
AppDev TIGHT/ The Art of Java / Schildt/Holmes / 222971-3 / Chapter 5
P:\010Comp\ApDev\971-3\ch05.vp Monday, July 07, 2003 10:03:51 AM
Color profile: Generic CMYK printer profile Composite Default screen
The MessageDialog Class
The Message dialog box, shown in Figure 5-4, is used to enter messages that will be sent by theEmailClientclass. It is created by theMessageDialogclass. This class is used for new messages, as shown in Figure 5-4, as well as reply to and forward messages.
TheMessageDialogclass is shown here. Notice that it extendsJDialog:
import java.awt.*; import java.awt.event.*; import javax.mail.*; import javax.swing.*;
// This class displays the dialog box used for creating messages. public class MessageDialog extends JDialog
{
// Dialog box message identifiers. public static final int NEW = 0; public static final int REPLY = 1; public static final int FORWARD = 2;
// Message From, To, and Subject text fields. private JTextField fromTextField, toTextField; private JTextField subjectTextField;
// Message content text area. private JTextArea contentTextArea;
// Flag specifying whether or not dialog box was cancelled. private boolean cancelled;
// Constructor for dialog box.
public MessageDialog(Frame parent, int type, Message message) throws Exception
{
// Call super constructor, specifying that dialog box is modal. super(parent, true);
/* Set dialog box title and get message's "To", "Subject", and "content" values based on message type. */
String to = "", subject = "", content = ""; switch (type) {
// Reply message. case REPLY:
// Get message "To" value.
Address[] senders = message.getFrom(); if (senders != null || senders.length > 0) {
to = senders[0].toString(); }
to = message.getFrom()[0].toString();
// Get message subject.
subject = message.getSubject();
if (subject != null && subject.length() > 0) { subject = "RE: " + subject;
} else {
subject = "RE:"; }
// Get message content and add "REPLIED TO" notation. content = "\n--- " + "REPLIED TO MESSAGE" + " ---\n" + EmailClient.getMessageContent(message); break; // Forward message. case FORWARD: setTitle("Forward Message");
// Get message subject.
subject = message.getSubject();
if (subject != null && subject.length() > 0) { subject = "FWD: " + subject;
} else {
subject = "FWD:"; }
// Get message content and add "FORWARDED" notation. content = "\n--- " + "FORWARDED MESSAGE" + " ---\n" + EmailClient.getMessageContent(message); break; // New message. default: setTitle("New Message"); } C h a p t e r 5 : I m p l e m e n t i n g a n E - m a i l C l i e n t i n J a v a
1 3 5
AppDev TIGHT/ The Art of Java / Schildt/Holmes / 222971-3 / Chapter 5
P:\010Comp\ApDev\971-3\ch05.vp Monday, July 07, 2003 10:03:52 AM
Color profile: Generic CMYK printer profile Composite Default screen
// Handle closing events.
addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {
actionCancel(); }
});
// Set up fields panel.
JPanel fieldsPanel = new JPanel(); GridBagConstraints constraints;
GridBagLayout layout = new GridBagLayout(); fieldsPanel.setLayout(layout);
JLabel fromLabel = new JLabel("From:"); constraints = new GridBagConstraints(); constraints.anchor = GridBagConstraints.EAST; constraints.insets = new Insets(5, 5, 0, 0); layout.setConstraints(fromLabel, constraints); fieldsPanel.add(fromLabel);
fromTextField = new JTextField(); constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL; constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.insets = new Insets(5, 5, 0, 0);
layout.setConstraints(fromTextField, constraints); fieldsPanel.add(fromTextField);
JLabel toLabel = new JLabel("To:"); constraints = new GridBagConstraints(); constraints.anchor = GridBagConstraints.EAST; constraints.insets = new Insets(5, 5, 0, 0); layout.setConstraints(toLabel, constraints); fieldsPanel.add(toLabel);
toTextField = new JTextField(to); constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL; constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.insets = new Insets(5, 5, 0, 0);
constraints.weightx = 1.0D;
layout.setConstraints(toTextField, constraints); fieldsPanel.add(toTextField);
JLabel subjectLabel = new JLabel("Subject:"); constraints = new GridBagConstraints(); constraints.insets = new Insets(5, 5, 5, 0); layout.setConstraints(subjectLabel, constraints); fieldsPanel.add(subjectLabel);
subjectTextField = new JTextField(subject); constraints = new GridBagConstraints();
constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.insets = new Insets(5, 5, 5, 0);
layout.setConstraints(subjectTextField, constraints); fieldsPanel.add(subjectTextField);
// Set up content panel.
JScrollPane contentPanel = new JScrollPane(); contentTextArea = new JTextArea(content, 10, 50); contentPanel.setViewportView(contentTextArea);
// Set up buttons panel.
JPanel buttonsPanel = new JPanel(); JButton sendButton = new JButton("Send");
sendButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
actionSend(); }
});
buttonsPanel.add(sendButton);
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
actionCancel(); }
});
buttonsPanel.add(cancelButton);
// Add panels to display.
getContentPane().setLayout(new BorderLayout());
getContentPane().add(fieldsPanel, BorderLayout.NORTH); getContentPane().add(contentPanel, BorderLayout.CENTER); getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
// Size dialog box to components. pack();
// Center dialog box over application. setLocationRelativeTo(parent);
}
// Validate message fields and close dialog box. private void actionSend() {
if (fromTextField.getText().trim().length() < 1 || toTextField.getText().trim().length() < 1 || subjectTextField.getText().trim().length() < 1 || contentTextArea.getText().trim().length() < 1) {
C h a p t e r 5 : I m p l e m e n t i n g a n E - m a i l C l i e n t i n J a v a
1 3 7
AppDev TIGHT/ The Art of Java / Schildt/Holmes / 222971-3 / Chapter 5
P:\010Comp\ApDev\971-3\ch05.vp Monday, July 07, 2003 10:03:52 AM
Color profile: Generic CMYK printer profile Composite Default screen
JOptionPane.showMessageDialog(this, "One or more fields is missing.",
"Missing Field(s)", JOptionPane.ERROR_MESSAGE); return;
}
// Close dialog box. dispose();
}
// Cancel creation of this message and close dialog box. private void actionCancel() {
cancelled = true;
// Close dialog box. dispose();
}
// Show dialog box.
public boolean display() { show();
// Return whether or not display was successful. return !cancelled;
}
// Get message's "From" field value. public String getFrom() {
return fromTextField.getText(); }
// Get message's "To" field value. public String getTo() {
return toTextField.getText(); }
// Get message's "Subject" field value. public String getSubject() {
return subjectTextField.getText(); }
// Get message's "content" field value. public String getContent() {
return contentTextArea.getText(); }
The MessageDialog Variables
MessageDialogbegins by declaring threestatic finalvariables,NEW,REPLY, and
FORWARD, that specify the types of messages that the dialog box handles. Next, several GUI control variables are declared. Finally, thecancelledflag is declared for tracking whether or not the dialog box was cancelled.
The MessageDialog Constructor
Similar to the previous two dialog classes, theMessageDialogconstructor begins by calling its parent class constructor to specify that the dialog box is modal. Next, aswitchstatement is used to set the dialog box’s title based on the type of message being created. Theswitch
statement also serves to retrieve message fields from an original message supplied as an argument to the constructor (REPLYandFORWARDtypes). These original message fields are then used to populateMessageDialog’s fields later in the constructor. Notice that theREPLY
andFORWARDmessages’ content is prefixed with text to denote the original message. After theswitchstatement has concluded, the fields panel is created, and each field’s control is initialized and added to the panel. The content and buttons panels are set up next, and then all the panels are added to the display. Next,pack( )is called to size the dialog box window to the minimum size required by its GUI controls. Finally, a call tosetLocation- RelativeTo( )centers the dialog box over the parent window.