KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > hello > HelloApplet


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.hello;
32
33 import org.objectweb.proactive.core.config.ProActiveConfiguration;
34
35 public class HelloApplet extends org.objectweb.proactive.examples.StandardFrame {
36
37   /** The active Hello object */
38   private Hello activeHello;
39   
40   /** The remote node locator if runnning over the network */
41   private org.objectweb.proactive.core.node.Node node;
42
43   /** The label */
44   private javax.swing.JTextArea JavaDoc lMessage;
45   
46   private boolean shouldRun = true;
47   
48
49   public HelloApplet(String JavaDoc name, int width, int height, String JavaDoc nodeURL) {
50     super(name, width, height);
51     if (nodeURL == null)
52       node = null; // We'll create the objects locally
53
else {
54       try {
55         node = org.objectweb.proactive.core.node.NodeFactory.getNode(nodeURL); // We'll create the objects on the specified node
56
} catch (org.objectweb.proactive.core.node.NodeException e) {
57         node = null;
58       }
59     }
60   }
61
62
63   public static void main(String JavaDoc args[]) {
64     // checks for the server's URL
65
String JavaDoc nodeURL = null;
66     if (args.length > 0) {
67       // Lookups the server object
68
nodeURL = args[0];
69     }
70     ProActiveConfiguration.load();
71     new HelloApplet("Hello applet", 300, 200, nodeURL);
72   }
73
74
75   public void start() {
76     receiveMessage("Applet creating active objects");
77     receiveMessage("on node "+(node == null ? "local" : node.getNodeInformation().getURL()));
78     try {
79       activeHello = (Hello)org.objectweb.proactive.ProActive.newActive(Hello.class.getName(), null, node);
80     } catch (Exception JavaDoc e) {
81       // There has been a problem...
82
receiveMessage("Error while initializing");
83       lMessage.setText("Error... Did you set the security attributes?");
84       e.printStackTrace(); // Print the exception to stderr
85
return;
86     }
87     receiveMessage("Ok");
88     Thread JavaDoc t = new Thread JavaDoc(new HelloTimer(), "Hello clock");
89     t.start();
90   }
91
92
93   public void stop() {
94     shouldRun = false;
95     activeHello = null;
96     lMessage.setText("Applet stopped");
97   }
98
99
100   protected javax.swing.JPanel JavaDoc createRootPanel() {
101     javax.swing.JPanel JavaDoc rootPanel = new javax.swing.JPanel JavaDoc();
102     // Layout
103
rootPanel.setBackground(java.awt.Color.white);
104     rootPanel.setForeground(java.awt.Color.blue);
105     lMessage = new javax.swing.JTextArea JavaDoc("Please wait...........");
106     rootPanel.add(lMessage);
107     return rootPanel;
108   }
109
110
111   private class HelloTimer implements Runnable JavaDoc {
112
113     public void run() {
114       while (shouldRun) {
115         lMessage.setText(activeHello.sayHello());
116         try {
117           Thread.sleep(1000);
118         } catch (InterruptedException JavaDoc e) {}
119       }
120     }
121   }
122 }
123
Popular Tags