169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage sample.rmi;
269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.applet.*;
469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.awt.*;
569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.awt.event.*;
669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.tools.rmi.ObjectImporter;
769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.tools.rmi.ObjectNotFoundException;
869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.tools.web.Viewer;
969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class CountApplet extends Applet implements ActionListener {
1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private Font font;
1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private ObjectImporter importer;
1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private Counter counter;
1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private AlertDialog dialog;
1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private String message;
1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private String paramButton;
1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private String paramName;
1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void init() {
2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	paramButton = getParameter("button");
2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	paramName = getParameter("name");
2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	importer = new ObjectImporter(this);
2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	commonInit();
2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /* call this method instead of init() if this program is not run
2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * as an applet.
2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void applicationInit() {
3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	paramButton = "OK";
3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	paramName = "counter";
3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	Viewer cl = (Viewer)getClass().getClassLoader();
3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	importer = new ObjectImporter(cl.getServer(), cl.getPort());
3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	commonInit();
3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private void commonInit() {
3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	font = new Font("SansSerif", Font.ITALIC, 40);
4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	Button b = new Button(paramButton);
4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	b.addActionListener(this);
4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	add(b);
4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	dialog = new AlertDialog();
4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	message = "???";
4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void destroy() {
4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	dialog.dispose();
4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void start() {
5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	try {
5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	    counter = (Counter)importer.lookupObject(paramName);
5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	    message = Integer.toString(counter.get());
5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	}
5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	catch (ObjectNotFoundException e) {
5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	    dialog.show(e.toString());
5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	}
5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void actionPerformed(ActionEvent e) {
6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	counter.increase();
6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	message = Integer.toString(counter.get());
6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	repaint();
6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void paint(Graphics g) {
6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	g.setFont(font);
6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	g.drawRect(50, 50, 100, 100);
7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	g.setColor(Color.blue);
7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	g.drawString(message, 60, 120);
7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static void main(String[] args) {
7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	Frame f = new Frame("CountApplet");
7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	CountApplet ca = new CountApplet();
7769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	f.add(ca);
7869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	f.setSize(300, 300);
7969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	ca.applicationInit();
8069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	ca.start();
8169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	f.setVisible(true);
8269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
8369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
84