//: c13:TextPane.java // CLIENT SOFTWARE WITH GUI // <applet code=TextPane width=475 height=425> // </applet> Java client software with Graphic User Interface --------------------------------------- --------------------------------------- --------------------------------------- import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.border.*; import com.bruceeckel.swing.*; import com.bruceeckel.util.*; import java.io.*; import java.util.*; import java.net.*; public class menu extends JApplet { JMenu[] menus = { new JMenu("FILE"), new JMenu("Exit"), new JMenu("Options")}; JMenuItem[] items = { new JMenuItem("New"), new JMenuItem("Open"), new JMenuItem("Open With Search"), new JMenuItem("Exit"), new JMenuItem("Settings"), new JMenuItem("See Settings")}; // NEW dlg = new NEW(null); static int PORT=1025; static int HOSTNUM=1; public void init() { items[0].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { NEW dlg = new NEW(null); dlg.show(); } }); items[1].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String Filename = JOptionPane.showInputDialog("Enter File Name"); String str1=new String(),str2=new String(); try { BufferedReader ll1 = new BufferedReader( new FileReader(Filename)); while((str1 = ll1.readLine())!= null) str2 += str1 + "\n"; ll1.close(); NEW dlg = new NEW(null,str2); dlg.show(); } catch(IOException zzz) { JOptionPane.showMessageDialog(null,"File Not Found","File Not Found",JOptionPane.ERROR_MESSAGE); } } }); items[3].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); items[2].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser ch = new JFileChooser(); int rVal = ch.showOpenDialog(menu.this); String s; if(rVal == JFileChooser.APPROVE_OPTION) //OK { s = (ch.getSelectedFile().getName()); /* s+= ( ch.getCurrentDirectory().toString()); */ String str1=new String(),str2=new String(); try { BufferedReader ll1 = new BufferedReader( new FileReader(s)); while((str1 = ll1.readLine())!= null) str2 += str1 + "\n"; ll1.close(); NEW dlg = new NEW(null,str2); dlg.show(); } catch(IOException zzz) { JOptionPane.showMessageDialog(null,"File Not Found","File Not Found",JOptionPane.ERROR_MESSAGE); } } if(rVal == JFileChooser.CANCEL_OPTION) //CANCEL { } } }); items[4].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { SETTINGS dlg = new SETTINGS(null); dlg.show(); } }); items[5].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { LOOKSET dlg = new LOOKSET(null); dlg.show(); } }); menus[0].add(items[0]); menus[0].add(items[1]); menus[0].add(items[2]); menus[0].add(items[3]); menus[2].add(items[4]); menus[2].add(items[5]); JMenuBar mb = new JMenuBar(); mb.add(menus[0]); mb.add(menus[1]); mb.add(menus[2]); setJMenuBar(mb); } public static void main(String[] args) { Console.run(new menu(), 475, 425); } } class LOOKSET extends JDialog { JButton ok = new JButton("OK"); public LOOKSET(JFrame parent) { super(parent,"SET",true); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); } }); JTextField jt = new JTextField(30); jt.setText("Port : " + menu.PORT + " Host : HOST" + menu.HOSTNUM); jt.setEditable(false); Container cp1 = getContentPane(); cp1.setLayout( new FlowLayout() ); cp1.add(ok); cp1.add(jt); setSize(400,150); } } class SETTINGS extends JDialog { JButton ok = new JButton("OK"); JRadioButton port1 = new JRadioButton("1025",menu.PORT == 1025), port2 = new JRadioButton("1026",menu.PORT == 1026), port3 = new JRadioButton("HOST1",menu.HOSTNUM == 1), port4 = new JRadioButton("HOST2",menu.HOSTNUM == 2); public SETTINGS(JFrame parent) { super(parent,"PORT",true); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); } }); port1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { menu.PORT=1025; } }); port2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { menu.PORT=1026; } }); port3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { menu.HOSTNUM=1; } }); port4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { menu.HOSTNUM=2; } }); ButtonGroup bg = new ButtonGroup(); ButtonGroup bg1 = new ButtonGroup(); bg.add(port1); bg.add(port2); bg1.add(port3); bg1.add(port4); Container cp1 = getContentPane(); cp1.setLayout( new FlowLayout() ); cp1.add(port1); cp1.add(port2); cp1.add(port3); cp1.add(port4); cp1.add(ok); setSize(220,170); } } class NEW extends JDialog { String s = new String(); JButton b = new JButton("Copy Text"), c = new JButton("Paste Text"), d = new JButton("Save Text"), w = new JButton("SEND Text"); JCheckBox cb1 = new JCheckBox("Editor Lock"); JTextPane tp = new JTextPane(); public NEW(JFrame parent) { super(parent,"NEW FILE",true); w.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { /* try{ new JabberClient( tp.getText()); } catch(IOException yyyyy) {} */ } }); b.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { s = tp.getSelectedText(); } }); cb1.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (cb1.isSelected()) tp.setEditable(false); else tp.setEditable(true); } }); c.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tp.setText(s + tp.getText()); } }) ; d.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String filename = JOptionPane.showInputDialog("Enter File Name"); s = tp.getText(); try { try { // BufferedReader in = new BufferedReader(new StringReader(s)); PrintWriter out = new PrintWriter (new BufferedWriter(new FileWriter(filename) ) ); // while ( (s2=in.readLine())!=null ) out.print(s); out.close(); } catch(EOFException zuzu) { System.err.println(""); } } catch(IOException zuzu) { System.err.println(""); } dispose(); } }) ; Container cp = getContentPane(); JPanel jp = new JPanel(); cp.add(new JScrollPane(tp)); jp.add(b); jp.add(c); jp.add(d); jp.add(w); cp.add(BorderLayout.NORTH,cb1); cp.add(BorderLayout.SOUTH,jp); setSize(500,500); } public NEW(JFrame parent, String openfilestring) { super(parent,"LOAD FILE",true); w.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try{ // Console.run( new JabberClient( tp.getText() ), 200, 200 ); JabberClient dlg = new JabberClient( tp.getText() ); dlg.show(); // new JabberClient( tp.getText() ); } catch (IOException yyyyyy) {}; } }); b.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { s = tp.getSelectedText(); } }); cb1.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (cb1.isSelected()) tp.setEditable(false); else tp.setEditable(true); } }); c.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tp.setText(s + tp.getText()); } }) ; d.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String filename = JOptionPane.showInputDialog("Enter File Name"); s = tp.getText(); try { try { // BufferedReader in = new BufferedReader(new StringReader(s)); PrintWriter out = new PrintWriter (new BufferedWriter(new FileWriter(filename) ) ); // while ( (s2=in.readLine())!=null ) out.print(s); out.close(); } catch(EOFException zuzu) { System.err.println(""); } } catch(IOException zuzu) { System.err.println(""); } dispose(); } }) ; Container cp = getContentPane(); JPanel jp = new JPanel(); cp.add(new JScrollPane(tp)); jp.add(b); jp.add(c); jp.add(d); jp.add(w); cp.add(BorderLayout.NORTH,cb1); cp.add(BorderLayout.SOUTH,jp); tp.setText(openfilestring); setSize(500,500); } } ///:~ class JabberClient extends JDialog { public JabberClient(String string_for_send) throws IOException { // super(parent,"NEW FILE",true); JTextField t = new JTextField (30); String str_from_recv = new String(); Container cp_old = getContentPane(); // Passing null to getByName() produces the // special "Local Loopback" IP address, for // testing on one machine w/o a network: InetAddress addr = InetAddress.getByName(null); // Alternatively, you can use // the address or name: // InetAddress addr = // InetAddress.getByName("127.0.0.1"); // InetAddress addr = // InetAddress.getByName("localhost"); t.setText("addr = " + addr + "\n"); Socket socket = new Socket(addr, JabberServer.PORT); // Guard everything in a try-finally to make // sure that the socket is closed: try { t.setText(t.getText() + "socket = " + socket + "\n"); BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); // Output is automatically flushed // by PrintWriter: PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream())),true); System.out.println(string_for_send); out.println(string_for_send); // System.out.println(string_for_send); str_from_recv = in.readLine(); out.println("END"); } finally { t.setText(t.getText() + str_from_recv + "\n" + "closing"); System.out.println(str_from_recv); cp_old.add(t); socket.close(); } } } //----------------------------- // MULTITHREAD SERVER SOFTWARE //----------------------------- import java.io.*; import java.net.*; class ServeOneJabber extends Thread { private Socket socket; private BufferedReader in; private PrintWriter out; public ServeOneJabber(Socket s) throws IOException { socket = s; in = new BufferedReader( new InputStreamReader( socket.getInputStream())); // Enable auto-flush: out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream())), true); // If any of the above calls throw an // exception, the caller is responsible for // closing the socket. Otherwise the thread // will close it. start(); // Calls run() } public void run() { try { while (true) { String str = in.readLine(); if (str.equals("END")) break; System.out.println("Echoing: " + str); out.println("Length : " + str.length()); } System.out.println("closing..."); } catch(IOException e) { System.err.println("IO Exception"); } finally { try { socket.close(); } catch(IOException e) { System.err.println("Socket not closed"); } } } } public class MultiJabberServer { static final int PORT = 8080; public static void main(String[] args) throws IOException { ServerSocket s = new ServerSocket(PORT); System.out.println("Server Started"); try { while(true) { // Blocks until a connection occurs: Socket socket = s.accept(); try { new ServeOneJabber(socket); } catch(IOException e) { // If it fails, close the socket, // otherwise the thread will close it: socket.close(); } } } finally { s.close(); } } }