📄 emailclient.java
字号:
/*
* WebLogic Server Unleashed
*
*/
package com.wlsunleashed.xml.sax;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import com.wlsunleashed.xml.sax.email.EMail;
import com.wlsunleashed.xml.sax.email.EMailAddress;
/**
* This class Creates a *simple* e-mail parsing client.
*
* @version 1.0
*/
public class EMailClient extends JFrame {
/** The default parser class */
private String DEFAULT_PARSER =
"weblogic.apache.xerces.parsers.SAXParser";
/** DOCUMENT ME! */
private String xmlFile = "";
/**
* Constructor for EMailClient.
*
* @param title The title of the application
* @param xmlFile The xml file to be used
*
* @throws SAXException Thrown when any SAX Parsing exception occurs
* @throws IOException Thrown when any IO exception occurs
*/
public EMailClient(String title, String xmlFile)
throws SAXException, IOException {
super(title);
this.xmlFile = xmlFile;
JPanel mainPanel = new JPanel();
populatePanel(mainPanel);
getContentPane().setLayout(new FlowLayout());
getContentPane().add(mainPanel);
setSize(400, 300);
}
/**
* The entry point to the application
*
* @param args The arguments to the invocation
*/
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage EMailClient <EMail document>.xml");
System.exit(1);
}
try {
EMailClient anEmailClient = new EMailClient(
"Email Client (Using SAX Parser)",
args[0]);
anEmailClient.setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Populates the header panel with the contents of the EMail.
*
* @param headerPanel JPanel to be populated
* @param anEmail EMail : The email object
*/
private void populatePanel(JPanel headerPanel, EMail anEmail) {
Vector toAddresses = anEmail.getToAddresses();
Vector ccAddresses = anEmail.getCcAddresses();
int numLines = 5 + toAddresses.size() + ccAddresses.size();
GridBagConstraints gbc = new GridBagConstraints();
GridBagLayout gbl = new GridBagLayout();
gbl.columnWidths = new int[] { 30, 70 };
gbl.rowHeights = new int[numLines];
for (int i = 0; i < numLines; i++) {
gbl.rowHeights[i] = 1;
}
headerPanel.setLayout(gbl);
gbc.anchor = GridBagConstraints.NORTHWEST;
// From address
gbc.gridy = 0;
gbc.gridx = 0;
headerPanel.add(new JLabel("From:"), gbc);
gbc.gridx = 1;
headerPanel.add(new JLabel(anEmail.getFromAddress().getDisplayableId()),
gbc);
// To addresses
gbc.gridy++;
gbc.gridx = 0;
headerPanel.add(new JLabel("To:"), gbc);
gbc.gridy--;
for (int i = 0; i < toAddresses.size(); i++) {
EMailAddress anAddress = (EMailAddress) toAddresses.get(i);
gbc.gridy++;
gbc.gridx = 1;
headerPanel.add(new JLabel(anAddress.getDisplayableId()), gbc);
}
// CC Addresses
if (ccAddresses.size() > 0) {
gbc.gridy++;
gbc.gridx = 0;
headerPanel.add(new JLabel("Cc:"), gbc);
gbc.gridy--;
for (int i = 0; i < ccAddresses.size(); i++) {
EMailAddress anAddress = (EMailAddress) ccAddresses.get(i);
gbc.gridy++;
gbc.gridx = 1;
headerPanel.add(new JLabel(anAddress.getDisplayableId()), gbc);
}
}
// Options : Read Receipt
gbc.gridy++;
gbc.gridx = 0;
headerPanel.add(new JLabel("Read Receipt "), gbc);
JCheckBox aCheckBox = new JCheckBox("");
aCheckBox.setEnabled(false);
if (anEmail.getOptions().isReadReceipt()) {
aCheckBox.setSelected(true);
} else {
aCheckBox.setSelected(false);
}
gbc.gridx = 1;
headerPanel.add(aCheckBox, gbc);
// Options : Importance
gbc.gridy++;
gbc.gridx = 0;
headerPanel.add(new JLabel("Importance: "), gbc);
gbc.gridx = 1;
headerPanel.add(new JLabel(anEmail.getOptions().getImportance()), gbc);
// Subject
gbc.gridy++;
gbc.gridx = 0;
headerPanel.add(new JLabel("Subject: "), gbc);
gbc.gridx = 1;
headerPanel.add(new JLabel(anEmail.getSubject()), gbc);
// Body of the EMAil
gbc.gridy += 3;
gbc.gridx = 0;
headerPanel.add(new JLabel("Body: "), gbc);
gbc.gridx = 1;
JTextArea textArea = new JTextArea(anEmail.getBody(), 5, 25);
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setBorder(new LineBorder(Color.black));
JScrollPane aScrollPane = new JScrollPane(textArea);
aScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
aScrollPane.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
headerPanel.add(aScrollPane, gbc);
gbc.gridy++;
gbc.gridx = 0;
JButton aButton = new JButton("Close");
aButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
headerPanel.add(aButton, gbc);
}
/**
* Method populatePanel Populates the created panel
*
* @param headerPanel The header panel
*
* @throws SAXException Thrown when any SAX exception occurs
* @throws IOException Thrown when any IO Exception occurs
*/
private void populatePanel(JPanel headerPanel)
throws SAXException, IOException {
// Create an XML Reader
XMLReader aReader = XMLReaderFactory.createXMLReader(DEFAULT_PARSER);
aReader.setContentHandler(new EMailContentHandler());
aReader.setErrorHandler(new EMailErrorHandler());
// Parse the input source
InputSource inputSource = new InputSource(xmlFile);
aReader.parse(inputSource);
EMail anEmail = ((EMailContentHandler) aReader.getContentHandler()).getEMail();
populatePanel(headerPanel, anEmail);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -