mini client NNTP java avec interface utilisateur
Un client nntp extrêmement simple en java/swing
import 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 GNntp extends JFrame
{
class NntpMessage {
public String message;
private String title;
public NntpMessage (String titleW, String msg)
{
title = titleW;
message = msg;
}
public String toString()
{
return title;
}
}
public class NntpServerDialog extends JDialog
{
JTextField hostTF, portTF, groupTF;
public NntpServerDialog(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(popServer, 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);
groupTF = new JTextField(group, 20);
panel2.add(new JLabel("User"));
panel2.add(groupTF);
setTitle("Nntp Server info");
pack();
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
dispose();
setVisible(false);
}
});
okButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
popServer = hostTF.getText();
group = groupTF.getText();
port = Integer.parseInt(portTF.getText());
dispose();
setVisible(false);
}
});
}
}
private String popServer = "news.wanadoo.fr";
private String group = "alt.tv.alias";
private int port = 119;
private JLabel status = new JLabel("Ready...");
private JTextArea Jbody = new JTextArea(10,20);
private Vector messages = new Vector();
private JList msgList = new JList();
public GNntp ()
{
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 popServerItem = new JMenuItem("Set Nntp Server", 2);
fMenu.add(popServerItem);
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();
}});
popServerItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
changeNntpServer();
}});
msgList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int index = msgList.locationToIndex(e.getPoint());
if ( index >= 0 && index < messages.size())
Jbody.setText(((NntpMessage)messages.elementAt(index)).message);
}
});
pack();
}
protected void getMessages()
{
PrintWriter to;
BufferedReader from;
String str, msg;
try {
Socket socket = new Socket(InetAddress.getByName(popServer),port);
to = new PrintWriter(
new BufferedWriter (
new OutputStreamWriter (
socket.getOutputStream())),true);
from = new BufferedReader(
new InputStreamReader (
socket.getInputStream()));
while ( ! (from.readLine()).startsWith("200") );
System.out.println("now group");
to.println("GROUP "+group+"\r\n");
while ( ! (msg = from.readLine()).startsWith("211") );
StringTokenizer st = new StringTokenizer(msg);
st.nextToken(); st.nextToken();
int deb=(new Integer(st.nextToken())).intValue();
int fin=(new Integer(st.nextToken())).intValue();
for(int i=deb; i<=fin; i++) {
System.out.println(i);
to.println("ARTICLE "+i+"\r\n");
while ( ! (from.readLine()).startsWith("220") );
msg = "";
do {
msg += from.readLine() + "\n";
} while ( ! msg.endsWith("\n.\n") );
messages.add(new NntpMessage(""+i, msg));
}
msgList.setListData(messages);
status.setText("Ready ...");
socket.close();
} catch ( Exception e ) {System.err.println(e);}
}
protected void changeNntpServer()
{
new NntpServerDialog(this, true).show();
}
public static void main ( String args[] )
{
(new GNntp()).setVisible(true);
}
}
|