Le protocole IMAP: Classe Java GUI
Cette mini application imap utilise la classe connectionManager, elle montre une petite implementation d'un lecteur d'email via une connection imap. Elle dispose d'un petite interface graphique en swing.
:so $VIMRUNTIME/syntax/2html.vim
kimport java.util.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Gimap extends JFrame
{
class imapMessage {
public String message;
private String title;
public imapMessage (String titleW, String msg)
{
title = titleW;
message = msg;
}
public String toString()
{
return title;
}
}
public class imapServerDialog extends JDialog
{
JTextField hostTF, portTF, userTF;
JPasswordField passwordTF;
public imapServerDialog(Frame parent, boolean modal)
{
super(parent, modal);
JPanel panel1, panel2, panel3;
JButton okButton, cancelButton;
Container contentPane = this.getContentPane();
panel1 = new JPanel();
panel1.setLayout(new FlowLayout(FlowLayout.CENTER));
contentPane.add(panel1, BorderLayout.SOUTH);
okButton = new JButton("OK");
panel1.add(okButton);
cancelButton = new JButton("Cancel");
panel1.add(cancelButton);
panel3 = new JPanel();
panel3.setLayout(new FlowLayout(FlowLayout.CENTER));
contentPane.add(panel3, BorderLayout.CENTER);
hostTF = new JTextField(imapServer, 20);
portTF = new JTextField("" + port, 5);
panel3.add(new JLabel("Server"));
panel3.add(hostTF);
panel3.add(new JLabel("Port"));
panel3.add(portTF);
panel2 = new JPanel();
panel2.setLayout(new FlowLayout(FlowLayout.CENTER));
contentPane.add(panel2, BorderLayout.NORTH);
userTF = new JTextField(user, 20);
passwordTF = new JPasswordField(password, 10);
panel2.add(new JLabel("User"));
panel2.add(userTF);
panel2.add(new JLabel("Passwd"));
panel2.add(passwordTF);
setTitle("imap Server info");
pack();
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
dispose();
setVisible(false);
}
});
okButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
imapServer = hostTF.getText();
user = userTF.getText();
password = new String(passwordTF.getPassword());
port = Integer.parseInt(portTF.getText());
dispose();
setVisible(false);
}
});
}
}
private String imapServer = "pop.salemioche.com";
private String user = "" , password = "";
private int port = 143;
private JLabel status = new JLabel("Ready...");
private JTextArea Jbody = new JTextArea(10,20);
private Vector messages = new Vector();
private JList msgList = new JList();
public Gimap ()
{
JMenuBar menuBar = new JMenuBar();
JPanel statusbar = new JPanel();
JPanel Core = new JPanel();
JScrollPane SPbody, SPlist;
Core.setLayout(new BorderLayout());
Container contentPane = this.getContentPane();
contentPane.setLayout(new BorderLayout());
setJMenuBar(menuBar);
JMenu fMenu = new JMenu("File");
menuBar.add(fMenu);
JMenuItem imapServerItem = new JMenuItem("Set imap Server", 2);
fMenu.add(imapServerItem);
JMenuItem popMsgItem = new JMenuItem("Get Messages", 2);
fMenu.add(popMsgItem);
fMenu.addSeparator();
JMenuItem exitItem = new JMenuItem("Exit", 2);
fMenu.add(exitItem);
statusbar.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
statusbar.setBorder(new BevelBorder(BevelBorder.LOWERED));
contentPane.add(statusbar,BorderLayout.SOUTH );
statusbar.add(status);
SPbody = new JScrollPane(Jbody,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
SPlist = new JScrollPane(msgList,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
Core.add(SPlist, BorderLayout.WEST);
Core.add(SPbody, BorderLayout.EAST);
contentPane.add(Core);
// add listeners for the window and the various buttons
this.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
dispose();
System.exit(0);
}});
exitItem.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}});
popMsgItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
getMessages();
}});
imapServerItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
changeimapServer();
}});
msgList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int index = msgList.locationToIndex(e.getPoint());
if ( index >= 0 && index < messages.size())
Jbody.setText(((imapMessage)messages.elementAt(index)).message);
}
});
pack();
}
protected void getMessages()
{
try {
ConnectionManager cm = new ConnectionManager();
cm.init(user,password,"imap",imapServer,port);
cm.openMailBox("INBOX");
for ( int i =0; i <= cm.nbMessage(); i++ ) {
messages.add(new imapMessage(cm.getSubject(i), cm.getText(i)));
}
msgList.setListData(messages);
cm.close();
status.setText("Ready ...");
} catch ( Exception e ) {System.err.println(e);}
}
protected void changeimapServer()
{
new imapServerDialog(this, true).show();
}
public static void main ( String args[] )
{
(new Gimap()).setVisible(true);
|