KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > c3d > C3DUser


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.c3d;
32
33 import java.awt.Button JavaDoc;
34 import java.awt.Canvas JavaDoc;
35 import java.awt.Color JavaDoc;
36 import java.awt.Dialog JavaDoc;
37 import java.awt.Dimension JavaDoc;
38 import java.awt.Font JavaDoc;
39 import java.awt.Frame JavaDoc;
40 import java.awt.Graphics JavaDoc;
41 import java.awt.GridBagConstraints JavaDoc;
42 import java.awt.GridBagLayout JavaDoc;
43 import java.awt.GridLayout JavaDoc;
44 import java.awt.Image JavaDoc;
45 import java.awt.Insets JavaDoc;
46 import java.awt.Label JavaDoc;
47 import java.awt.List JavaDoc;
48 import java.awt.Menu JavaDoc;
49 import java.awt.MenuBar JavaDoc;
50 import java.awt.MenuItem JavaDoc;
51 import java.awt.Panel JavaDoc;
52 import java.awt.TextArea JavaDoc;
53 import java.awt.TextField JavaDoc;
54 import java.awt.event.ActionEvent JavaDoc;
55 import java.awt.event.ActionListener JavaDoc;
56 import java.awt.event.ItemEvent JavaDoc;
57 import java.awt.event.ItemListener JavaDoc;
58 import java.awt.event.MouseEvent JavaDoc;
59 import java.awt.event.MouseListener JavaDoc;
60 import java.awt.event.WindowAdapter JavaDoc;
61 import java.awt.event.WindowEvent JavaDoc;
62 import java.awt.image.MemoryImageSource JavaDoc;
63 import java.net.InetAddress JavaDoc;
64 import java.net.UnknownHostException JavaDoc;
65 import java.util.Hashtable JavaDoc;
66
67 import org.apache.log4j.Logger;
68 import org.objectweb.proactive.ProActive;
69 import org.objectweb.proactive.core.config.ProActiveConfiguration;
70 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
71 import org.objectweb.proactive.core.descriptor.data.VirtualNode;
72 import org.objectweb.proactive.core.node.Node;
73 import org.objectweb.proactive.core.node.NodeException;
74 import org.objectweb.proactive.examples.c3d.geom.Vec;
75 import org.objectweb.proactive.examples.c3d.prim.Light;
76 import org.objectweb.proactive.examples.c3d.prim.Primitive;
77 import org.objectweb.proactive.examples.c3d.prim.Sphere;
78 import org.objectweb.proactive.ext.migration.MigrationStrategyManagerImpl;
79
80 public class C3DUser implements org.objectweb.proactive.RunActive, java.io.Serializable JavaDoc {
81     
82     static Logger logger = Logger.getLogger(C3DUser.class.getName());
83     
84     private MigrationStrategyManagerImpl myStrategyManager;
85     private boolean onMigration;
86     private String JavaDoc dispatcher_host;
87     
88     protected VirtualNode vnDispatcher;
89   /**
90    * Uses active objects if set to true
91    */

92   private final boolean ACTIVE = true;
93   private C3DUser me;
94   /**
95    * The about box
96    */

97   private MyDialog about;
98   /**
99    * Status log, displays messages from other users concerning action
100    * requests
101    */

102    TextArea JavaDoc ta_log;
103   /**
104    * SpyEvent log
105    */

106    TextArea JavaDoc ta_mess;
107   /**
108    * Name (number) of this user
109    */

110    Label JavaDoc l_user;
111   /**
112    * OS String of the C3Ddispatcher
113    */

114    Label JavaDoc l_c3ddispatcher;
115   /**
116    * Height of the <code>Image</code> to be rendered
117    */

118   final int i_height = 270;
119   /**
120    * Width of the <code>Image</code> to be rendered
121    */

122   final int i_width = 270;
123   /**
124    * <code>MemoryImagesource</code> as pixel source for the
125    * <code>Image</code> to be rendered
126    */

127    transient MemoryImageSource JavaDoc mis;
128   /**
129    * Destination array for the calculated pixels of the <code>Image</code>
130    */

131   transient int pix[];
132   /**
133    * <code>C3DDispatcher</code>, decouples the m user frames and the
134    * n rendering engines. There is always exactly one C3DDispatcher object
135    * in the Java// rendering system.
136    */

137   C3DDispatcher c3ddispatcher;
138   /**
139    * Number of this user in the set of users registered at the
140    * <code>C3DDispatcher</Code>, used to distinguish the action requests of
141    * several users
142    */

143
144   //@@ADDON Florian 13/8/98
145
/**
146    * Hashtable containing the name and ID of each user<br>
147    * Used for direct messaging
148    */

149   private Hashtable JavaDoc h_users = new Hashtable JavaDoc();
150   /**
151    * An AWT list allowing the User to select its locutors [??]
152    */

153   private transient List JavaDoc li_users;
154   /**
155    * The ID# of the selected locutor [-1=broadcast]
156    */

157   private int talkId = -1;
158   /**
159    * The array of random colors
160    */

161   private static Vec color[] = {
162     // new Vec(0,0,0), // Black
163
new Vec(1, 1, 1), // White
164
new Vec(0, 0, 0.5), // Navy
165
new Vec(0, 0.5, 0), // darkgreen
166
new Vec(0.5, 0, 0), // dark red
167
new Vec(1, 0.6, 0), // Orange
168
new Vec(1, 0.7, 0.8),// Pink
169
new Vec(0.8, 0.8, 1)// Lavender
170
};
171
172   
173   //@@END_ADDON Florian 13/8/98
174

175   /**
176    * The number of rotations so far
177    */

178   public int nRotate = 0;
179   public transient UserFrame userframe;
180   boolean b_isApplet;
181   boolean b_isBot;
182   String JavaDoc botUrl;
183   String JavaDoc s_username;
184   int i_user;
185
186
187   public C3DUser() {
188   }
189
190
191   public C3DUser(Boolean JavaDoc b_isApplet, Boolean JavaDoc b_isBot, String JavaDoc url) {
192     this.b_isApplet = b_isApplet.booleanValue();
193     this.b_isBot = b_isBot.booleanValue();
194     this.botUrl = url;
195     this.onMigration = false;
196   }
197
198
199   public void go(ProActiveDescriptor pad) {
200     
201             
202     me = (C3DUser)org.objectweb.proactive.ProActive.getStubOnThis();
203     
204     // Creates the user Frame
205
vnDispatcher = pad.getVirtualNode("Dispatcher");
206     userframe = new UserFrame(me);
207     userframe.createWelcomePanel();
208     
209     setOnMigration(true);
210   }
211
212     public void rebuild(){
213         me = (C3DUser)org.objectweb.proactive.ProActive.getStubOnThis();
214         userframe = new UserFrame(me);
215         userframe.createPanelAfterMigration(dispatcher_host,s_username);
216     }
217     
218     public void clean(){
219         if (userframe != null){
220             userframe.dispose();
221             userframe = null;
222         }
223     }
224     
225     public void setOnMigration(boolean value){
226         this.onMigration = value;
227     }
228     
229     public boolean getOnMigration(){
230         return this.onMigration;
231     }
232     
233   public void runActivity(org.objectweb.proactive.Body body) {
234    logger.info("Starting custom live in C3DUser");
235     //System.out.println("migration "+getOnMigration());
236
org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body);
237     myStrategyManager = new MigrationStrategyManagerImpl((org.objectweb.proactive.core.body.migration.Migratable) body);
238     myStrategyManager.onDeparture("clean");
239     if(getOnMigration()) {
240         //System.out.println("on migration");
241
//System.out.println("width "+i_width);
242
//System.out.println("height "+i_height);
243
rebuild();
244         
245       }
246         
247     
248 // while (body.isActive()) {
249
// service.blockingServeOldest();
250
// }
251
service.fifoServing();
252         
253     
254     if(getOnMigration()) clean();
255     //if the activity is restarted it is a migration
256
//to be improved
257

258   }
259
260     
261
262   /**
263    * Entry point of the program
264    */

265   public static void main(String JavaDoc argv[]) throws NodeException{
266     
267     ProActiveDescriptor proActiveDescriptor = null;
268     
269     ProActiveConfiguration.load();
270         try
271         {
272             proActiveDescriptor =
273                 ProActive.getProactiveDescriptor(
274                     "file:"+argv[0]);
275             proActiveDescriptor.activateMappings();
276             
277         }
278         catch (Exception JavaDoc e)
279         {
280             e.printStackTrace();
281             logger.fatal("Pb in main");
282         }
283  
284     VirtualNode user =
285             proActiveDescriptor.getVirtualNode("User");
286             
287         Node node = user.getNode();
288     Object JavaDoc params[] = { new Boolean JavaDoc(false),new Boolean JavaDoc(false), ""};
289     try {
290         
291       C3DUser c3duser = (C3DUser)org.objectweb.proactive.ProActive.newActive("org.objectweb.proactive.examples.c3d.C3DUser", params, node.getNodeInformation().getURL());
292       
293       c3duser.go(proActiveDescriptor);
294     } catch (Exception JavaDoc e) {
295       e.printStackTrace();
296     }
297   }
298
299
300   public void log(String JavaDoc s_message) {
301     ta_log.append(s_message + "\n");
302   }
303
304
305   public void message(String JavaDoc s_message) {
306     ta_mess.append(s_message + "\n");
307   }
308
309
310   public UserFrame getUserFrame() {
311     return userframe;
312   }
313
314   
315   //@@ADDON Florian 13/8/98
316
/**
317    * Informs the user that a new user has joined the party!!
318    * @param nUser The new user's ID
319    * @param sNAme The new user's name
320    * @version ADDON 13/8/98
321    * @author Florian DOYON
322    */

323   public void informNewUser(int nUser, String JavaDoc sName) {
324     li_users.add(sName);
325     h_users.put(sName, new Integer JavaDoc(nUser));
326   }
327
328
329   /**
330    * Informs the user that another user left
331    * @param nUser The id of the old user
332    * @version ADDON 16/8/98
333    * @author Florian DOYON
334    */

335   public void informUserLeft(String JavaDoc sName) {
336
337     // remove the user from the users list
338
try {
339       li_users.remove(sName);
340     } catch (Exception JavaDoc e) {
341         e.printStackTrace();
342     }
343
344     // Remove the user from the hash table
345
h_users.remove(sName);
346
347     if (h_users.isEmpty()) {
348       talkId = -1;
349       li_users.select(0);
350     }
351   }
352   //@@END_ADDON Florian 13/8/98
353

354   /**
355    * The pinging function called by <code>C3DDispatcher</code>
356    * to get the avg. pinging time
357    */

358   public int ping() {
359     return 0;
360   }
361
362
363   /**
364    * Create and initialize the scene for the rendering picture.
365    * @return The scene just created
366    */

367   private Scene createScene() {
368     int x = 0;
369     int y = 0;
370
371     Scene scene = new Scene();
372     
373     /* Creates three objects (spheres) */
374     Primitive p = new Sphere(new Vec(10, -5.77, 0), 7);
375     p.setColor(1.0, 0.0, 0.0);
376     p.surf.shine = 14.0;
377     p.surf.kd = 0.7;
378     p.surf.ks = 0.3;
379     p.surf.ior = 0.3;
380     scene.addObject(p);
381
382     p = new Sphere(new Vec(0, 11.55, 0), 7);
383     p.setColor(0.0, 1.0, 0.0);
384     p.surf.shine = 14.0;
385     p.surf.kd = 0.7;
386     p.surf.ks = 0.3;
387     scene.addObject(p);
388
389     p = new Sphere(new Vec(-10, -5.77, 0), 7);
390     p.setColor(0.0, 0.0, 1.0);
391     p.surf.shine = 14.0;
392     p.surf.kd = 0.7;
393     p.surf.ks = 0.3;
394     scene.addObject(p);
395
396     /*
397         p = new Sphere(new Vec(0,0,0), 3);
398         p.setColor(1.0,1.0,1.0);
399         p.surf.shine = 15.0;
400         p.surf.ks = 0.5;
401         p.surf.kt=0.5;
402         scene.addObject(p);
403  
404         p = new Sphere(new Vec(-10,5.77,0), 4);
405         p.setColor(0.0,1.0,1.0);
406         p.surf.shine = 14.0;
407         p.surf.kd = 0.7;
408         p.surf.ks = 0.3;
409         scene.addObject(p);
410
411         p = new Sphere(new Vec(10,5.77,0), 4);
412         p.setColor(1.0,1.0,0.0);
413         p.surf.shine = 14.0;
414         p.surf.kd = 0.7;
415         p.surf.ks = 0.3;
416         scene.addObject(p);
417  
418         p = new Sphere(new Vec(0,-11.55,0), 4);
419         p.setColor(1.0,0.0,1.0);
420         p.surf.shine = 14.0;
421         p.surf.kd = 0.7;
422         p.surf.ks = 0.3;
423         scene.addObject(p);
424
425         p = new Sphere(new Vec(0,0,14), 8);
426         p.setColor(0.0,0.0,0.0);
427         p.surf.shine = 14.0;
428         p.surf.kd = 0.7;
429         p.surf.ks = 0.3;
430         scene.addObject(p);
431
432     */

433     /* Creates five lights for the scene */
434     scene.addLight(new Light(100, 100, -50, 1.0));
435     scene.addLight(new Light(-100, 100, -50, 1.0));
436     scene.addLight(new Light(100, -100, -50, 1.0));
437     // scene.addLight(new Light(-100,-100,-50, 1.0));
438
// scene.addLight(new Light(200, 200, 0, 1.0));
439
// scene.addLight(new Light(0,0, 0, 1.0));
440

441     /* Creates a View (viewing point) for the rendering scene */
442     View v = new View();
443     // v.from = new Vec(x, y, -40);
444
v.from = new Vec(x, y, -30);
445     // v.from = new Vec(x, y, -5);
446
v.at = new Vec(x, y, -15);
447     v.up = new Vec(0, 1, 0);
448     v.angle = 35.0 * 3.14159265 / 180.0;
449     v.aspect = 1.0; /* 4.0/3.0; */
450     v.dist = 1.0;
451     scene.addView(v);
452
453     return scene;
454   }
455
456
457   /**
458    * Display an interval of newly calculated pixels
459    * @param newpix The pixels as int array
460    * @param interval The interval
461    */

462   public void setPixels(int[] newpix, Interval interval) {
463     int from = interval.width * interval.yfrom;
464     
465     /* Copy new pixels among the old ones */
466     System.arraycopy(newpix, 0, pix, from, newpix.length);
467     
468     /* Inform the image observers of the update */
469     mis.newPixels(0, interval.yfrom, interval.width, interval.yto);
470
471     /* Refresh the image displayed on the frame */
472     userframe.repaint();
473   }
474
475
476   /**
477    * Displays a message from the <code>C3DDispatcher</code> in the status log
478    *
479    * @param message message to be displayed
480    */

481   public void showMessage(String JavaDoc message) {
482     ta_log.append(message + "\n");
483     if (b_isBot) {
484       // if all the intervals are rendered
485
if (message.substring(0, 3).compareTo("All") == 0) {
486         // 16 renderings
487
if (nRotate < 15) {
488           nRotate++;
489           c3ddispatcher.rotateLeft(i_user);
490         } else {
491           c3ddispatcher.doBenchmarks();
492           exit();
493         }
494       }
495     }
496   }
497
498
499   public void showUserMessage(String JavaDoc message) {
500     ta_mess.append(message + '\n');
501   }
502
503
504   public void dialogMessage(String JavaDoc title, String JavaDoc content) {
505     MsgDialog md = new MsgDialog(userframe, title, content);
506     md.setVisible(true);
507   }
508
509
510   /**
511    * OOP encapsulation method for 'int width'
512    * @return width of the applet window, i.e. width of the rendering image
513    */

514
515   public Integer JavaDoc getWidth() {
516     //System.out.println("Dispatcher asks for width ");
517
return new Integer JavaDoc(i_width);
518   }
519
520
521   /**
522    * OOP encapsulation method for 'int height'
523    * @return height of the applet window, i.e. height of the rendering image
524    */

525   public Integer JavaDoc getHeight() {
526     return new Integer JavaDoc(i_height);
527   }
528
529
530   /**
531    * Exit the application
532    */

533
534   void exit() {
535     try {
536       c3ddispatcher.unregisterConsumer(i_user);
537       userframe.setVisible(false);
538       userframe.dispose();
539       userframe = null;
540       me.terminate();
541      
542     } catch (NullPointerException JavaDoc ex) {
543         ex.printStackTrace();
544     }
545 // if (!b_isApplet) {
546
//
547
// System.exit(0);
548
// }
549
}
550
551
552   private void trace(String JavaDoc s_message) {
553     logger.info("C3DUser: " + s_message);
554   }
555
556
557     public void terminate(){
558         org.objectweb.proactive.ProActive.getBodyOnThis().terminate();
559     }
560
561   public class UserFrame extends Frame JavaDoc implements ActionListener JavaDoc, ItemListener JavaDoc {
562
563     /**
564      * Button UP
565      */

566     
567     private Button JavaDoc b_up = new EqualButton("Up");
568     /**
569      * Button UP
570      */

571     private Button JavaDoc b_down = new EqualButton("Down");
572     /**
573      * Button to request the 'rotate left' action
574      */

575     private Button JavaDoc b_left = new EqualButton("Left");
576     /**
577      * Button to request the 'rotate right' action
578      */

579     private Button JavaDoc b_right = new EqualButton("Right");
580     /**
581      * Button to request the spin clock action
582      */

583     private Button JavaDoc b_clock = new EqualButton("Spin right");
584     /**
585      * Button to request the spin unclock action
586      */

587     private Button JavaDoc b_unclock = new EqualButton("Spin left");
588     /**
589      * Button to exit the application
590      */

591     private Button JavaDoc b_exit = new EqualButton("Exit");
592     /***
593      * Button to add a Sphere
594      */

595     private Button JavaDoc b_addSphere = new Button JavaDoc("Add random sphere");
596     /**
597      * Button to conenct to the dispatcher
598      */

599     private Button JavaDoc b_connect = new EqualButton("Connect");
600     /**
601      * Button to reset the scene
602      */

603     private Button JavaDoc b_reset = new EqualButton("Reset");
604     /**
605      * Button used for sending messages
606      */

607     private Button JavaDoc b_send = new Button JavaDoc("Send");
608     private TextField JavaDoc tf_mess, tf_name, tf_host;
609     private GridBagLayout JavaDoc gridbag = new GridBagLayout JavaDoc();;
610     private GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
611     private String JavaDoc s_localhost;
612     private C3DUser c3duser;
613     private MenuItem JavaDoc mi_exit, mi_clear, mi_list,mi_about;
614     private Font JavaDoc f_standard = new Font JavaDoc("SansSerif", Font.PLAIN, 12);
615     private int i_top;
616     private MenuBar JavaDoc mb;
617
618
619     /**
620      * One and only constructor for <code>UserFrame</code>. Creates and
621      * displays this frame and the GUI objects, creates the image object
622      * to take the rendered picture.
623      */

624     public UserFrame(C3DUser c3d) {
625       super("Collaborative 3D Environment - User Window");
626       setBackground(Color.lightGray);
627       setLayout(gridbag);
628       setFont(f_standard);
629       addWindowListener(new MyWindowListener());
630       c3duser = c3d;
631
632       s_localhost = "";
633       try {
634         s_localhost = InetAddress.getLocalHost().getCanonicalHostName();
635       } catch (UnknownHostException JavaDoc e) {
636         s_localhost = "unknown!";
637       }
638     }
639
640         public UserFrame(C3DUser c3d, boolean value) {
641             super("Collaborative 3D Environment - User Window");
642       setBackground(Color.lightGray);
643       setLayout(gridbag);
644       setFont(f_standard);
645       addWindowListener(new MyWindowListener());
646       c3duser = c3d;
647       s_localhost = "";
648       try {
649         s_localhost = InetAddress.getLocalHost().getCanonicalHostName();
650       } catch (UnknownHostException JavaDoc e) {
651         s_localhost = "unknown!";
652       }
653     
654         }
655         
656     public Button JavaDoc getB_up() {
657       return b_up;
658     }
659
660
661     public Button JavaDoc getB_down() {
662       return b_down;
663     }
664
665
666     public Button JavaDoc getB_left() {
667       return b_left;
668     }
669
670
671     public Button JavaDoc getB_right() {
672       return b_right;
673     }
674
675
676     public Button JavaDoc getB_clock() {
677       return b_clock;
678     }
679
680
681     public Button JavaDoc getB_unclock() {
682       return b_unclock;
683     }
684
685
686     /**
687      * Creates the display for a benchmarking bot
688      */

689     public void createBot(String JavaDoc url) {
690       s_username = "Benchmarking bot";
691       //createFinalPanel(url, s_username);
692
}
693
694
695     private void createWelcomePanel() {
696       Label JavaDoc l1 = new Label JavaDoc("Welcome to the Collaborative 3D Environment -", Label.CENTER);
697       c.insets = new Insets JavaDoc(5, 5, 0, 5);
698       c.gridwidth = GridBagConstraints.REMAINDER;
699       c.weightx = 1.0;
700       gridbag.setConstraints(l1, c);
701       add(l1);
702
703       Label JavaDoc l2 = new Label JavaDoc("a Java// test application", Label.CENTER);
704       c.insets = new Insets JavaDoc(0, 5, 0, 5);
705       gridbag.setConstraints(l2, c);
706       add(l2);
707
708       Label JavaDoc l3 = new Label JavaDoc("Please enter your name and the host of the C3DDispatcher.", Label.CENTER);
709       c.insets = new Insets JavaDoc(15, 5, 10, 5);
710       gridbag.setConstraints(l3, c);
711       add(l3);
712
713       Label JavaDoc l4 = new Label JavaDoc("Name:", Label.RIGHT);
714       Label JavaDoc l5 = new Label JavaDoc("Host:", Label.RIGHT);
715       c.insets = new Insets JavaDoc(5, 5, 5, 0);
716       c.gridwidth = 1;
717       c.anchor = GridBagConstraints.EAST;
718       gridbag.setConstraints(l4, c);
719       gridbag.setConstraints(l5, c);
720
721       tf_name = new TextField JavaDoc(20);
722       tf_name.addActionListener(this);
723
724       tf_host = new TextField JavaDoc(s_localhost, 20);
725       tf_host.addActionListener(this);
726       c.insets = new Insets JavaDoc(5, 0, 5, 5);
727       c.anchor = GridBagConstraints.CENTER;
728       c.gridwidth = GridBagConstraints.REMAINDER;
729       gridbag.setConstraints(tf_name, c);
730       gridbag.setConstraints(tf_host, c);
731
732       add(l4);
733       add(tf_name);
734       add(l5);
735       add(tf_host);
736
737       c.gridwidth = GridBagConstraints.RELATIVE;
738       c.insets = new Insets JavaDoc(15, 5, 10, 5);
739       c.anchor = GridBagConstraints.EAST;
740       gridbag.setConstraints(b_connect, c);
741       b_connect.addActionListener(this);
742       add(b_connect);
743       c.gridwidth = GridBagConstraints.REMAINDER;
744       c.anchor = GridBagConstraints.CENTER;
745       gridbag.setConstraints(b_exit, c);
746       b_exit.addActionListener(this);
747       add(b_exit);
748
749       pack();
750       setVisible(true);
751       toFront();
752     }
753
754         public void createPanel(String JavaDoc s_host,String JavaDoc s_name){
755             setVisible(false);
756       removeAll();
757
758       mb = new MenuBar JavaDoc();
759       Menu JavaDoc m_file = new Menu JavaDoc("File", false);
760       m_file.setFont(f_standard);
761       mi_exit = new MenuItem JavaDoc("Exit");
762       mi_clear = new MenuItem JavaDoc("Clear log");
763       mi_list = new MenuItem JavaDoc("List users");
764       mi_clear.setFont(f_standard);
765       mi_exit.setFont(f_standard);
766       mi_list.setFont(f_standard);
767       mi_exit.addActionListener(this);
768       mi_clear.addActionListener(this);
769       mi_list.addActionListener(this);
770       m_file.add(mi_list);
771       m_file.add(mi_clear);
772       m_file.addSeparator();
773       m_file.add(mi_exit);
774       mb.add(m_file);
775
776       Menu JavaDoc m_c3d = new Menu JavaDoc("C3D ProActive PDC", false);
777       mi_about = new MenuItem JavaDoc("About ProActive PDC");
778       mi_about.setFont(f_standard);
779       mi_about.addActionListener(this);
780       m_c3d.add(mi_about);
781
782       mb.setHelpMenu(m_c3d);
783       setMenuBar(mb);
784
785       /* Instanciates the destination pixel array */
786       pix = new int[i_width * i_height];
787    
788       /* Creates a MemoryImageSource as a pixel source for the image */
789       mis = new MemoryImageSource JavaDoc(i_width, i_height, pix, 0, i_width);
790
791       /* Accomodates the changing nature (rotation) of the image */
792       mis.setAnimated(true);
793
794       /* Creates an image component from the image from the
795        * MemoryImageSource; avoids drawing issues in the user frame
796        * and places it in a panel
797        */

798       Panel JavaDoc p_image = new Panel JavaDoc(new GridLayout JavaDoc(1, 1));
799       //System.out.println("p image "+p_image.getSize().width);
800

801       // PANEL CONSTRAINTS
802
c.insets = new Insets JavaDoc(5, 5, 0, 5);
803       
804       // position
805
c.gridx = 0;
806       c.gridy = 0;
807       c.weightx = 1;
808       c.weighty = 1;
809       
810       // size;
811
c.gridwidth = 1;
812       c.gridheight = 1;
813
814       //fill
815
c.fill = GridBagConstraints.BOTH; // components will fill their whole area
816

817       gridbag.setConstraints(p_image, c);
818       //System.out.println("p image "+p_image.getSize().width);
819
p_image.add(new MyImageContainer(createImage(mis), i_width, i_height, p_image));
820       add(p_image);
821
822       /**
823        * The log and buttons area
824        */

825       
826       // Creates the panel and places it in the main frame
827
Panel JavaDoc p_log = new Panel JavaDoc();
828       
829       // adds the panel
830
c.gridx = 1;
831       gridbag.setConstraints(p_log, c);
832       add(p_log);
833       
834       // Creates the panel's layout manager
835
GridBagLayout JavaDoc gb_panel = new GridBagLayout JavaDoc();
836       p_log.setLayout(gb_panel);
837       
838       // Label displaying the username
839
l_user = new Label JavaDoc("User " + s_name, Label.CENTER);
840       l_user.setFont(new Font JavaDoc("SansSerif", Font.ITALIC + Font.BOLD, 18));
841
842       GridBagConstraints JavaDoc pc = new GridBagConstraints JavaDoc();
843
844       pc.fill = GridBagConstraints.BOTH;
845       pc.insets = new Insets JavaDoc(5, 5, 0, 5);
846       pc.gridx = 0;
847       pc.gridy = 0;
848       pc.weightx = 1;
849       pc.weighty = 1;
850       pc.gridwidth = GridBagConstraints.REMAINDER;
851       gb_panel.setConstraints(l_user, pc);
852       p_log.add(l_user);
853       
854       // Label displaying local machine informations
855
Label JavaDoc l_machine = new Label JavaDoc("Local host: " + s_localhost + " (" + System.getProperty("os.name") + " " + System.getProperty("os.version") + ")");
856
857       // Adds the label in the panel
858
pc.gridy = 1;
859       gb_panel.setConstraints(l_machine, pc);
860       p_log.add(l_machine);
861       
862       
863       // Label displaying C3DDispatcher informations
864
l_c3ddispatcher = new Label JavaDoc("C3DDispatcher host: " + s_host);
865       pc.gridy = 2;
866       gb_panel.setConstraints(l_c3ddispatcher, pc);
867       p_log.add(l_c3ddispatcher);
868       
869       // Mesage log
870
pc.gridy = 3;
871       pc.weighty = 2;
872       ta_log = new TextArea JavaDoc(5, 25);
873       ta_log.setEditable(false);
874       gb_panel.setConstraints(ta_log, pc);
875       p_log.add(ta_log);
876       
877
878       //--------------------------------------------------
879
// INPUTS
880

881
882       // Panel containing the buttons
883
Panel JavaDoc p_input = new Panel JavaDoc();
884
885       GridBagLayout JavaDoc gb_input = new GridBagLayout JavaDoc();
886       p_input.setLayout(gb_input);
887
888       c.gridx = 0;
889       c.gridy = 1;
890       c.gridwidth = 1;
891
892       gridbag.setConstraints(p_input, c);
893       add(p_input);
894       
895       
896       //---------------- Buttons
897
// unclock
898
pc.gridy = 0;
899       pc.gridx = 0;
900       pc.gridwidth = 1;
901       pc.fill = GridBagConstraints.NONE;
902
903       gb_input.setConstraints(b_unclock, pc);
904       b_unclock.addActionListener(this);
905       p_input.add(b_unclock);
906
907
908       // Clock
909
pc.gridx = 2;
910       gb_input.setConstraints(b_clock, pc);
911       b_clock.addActionListener(this);
912       p_input.add(b_clock);
913       
914       // Up
915
pc.gridy = 0;
916       pc.gridx = 1;
917       gb_input.setConstraints(b_up, pc);
918       b_up.addActionListener(this);
919       p_input.add(b_up);
920
921
922       // Down
923
pc.gridx = 1;
924       pc.gridy = 2;
925       gb_input.setConstraints(b_down, pc);
926       b_down.addActionListener(this);
927       p_input.add(b_down);
928       
929       // Left
930
pc.gridx = 0;
931       pc.gridy = 1;
932       b_left.addActionListener(this);
933       gb_input.setConstraints(b_left, pc);
934       p_input.add(b_left);
935
936       // Right
937
pc.gridx = 2;
938       gb_input.setConstraints(b_right, pc);
939       b_right.addActionListener(this);
940       p_input.add(b_right);
941
942
943       // Add
944
// This one is added in the main frame
945
//c.gridwidth = c.REMAINDER;
946
c.gridheight = 1;
947       c.gridx = 1;
948       c.fill = GridBagConstraints.NONE;
949
950       gridbag.setConstraints(b_addSphere, c);
951       b_addSphere.addActionListener(this);
952       add(b_addSphere);
953
954       // Reset
955
// This one is added in the main fram
956
c.gridx = 2;
957       c.gridy = 1;
958       c.fill = GridBagConstraints.NONE;
959
960       gridbag.setConstraints(b_reset, c);
961       b_reset.addActionListener(this);
962       add(b_reset);
963
964
965
966       //-----------------------------------------
967
// MESSAGES
968

969       Panel JavaDoc p_mess = new Panel JavaDoc();
970
971       c.insets = new Insets JavaDoc(10, 10, 10, 10);
972       c.gridx = 0;
973       c.gridy = 2;
974       c.gridwidth = GridBagConstraints.REMAINDER;
975       c.fill = GridBagConstraints.BOTH;
976       gridbag.setConstraints(p_mess, c);
977       add(p_mess);
978
979       GridBagLayout JavaDoc g_mess = new GridBagLayout JavaDoc();
980       p_mess.setLayout(g_mess);
981
982       // Users list
983
li_users = new List JavaDoc(5, false);
984       li_users.add("~BROADCAST~");
985       li_users.select(0);
986       li_users.addItemListener(this);
987
988       pc.insets = new Insets JavaDoc(5, 5, 5, 5);
989       // pc.anchor=pc.WEST;
990
pc.gridx = 0;
991       pc.gridy = 0;
992       pc.fill = GridBagConstraints.BOTH;
993       pc.gridwidth = GridBagConstraints.RELATIVE;
994       g_mess.setConstraints(li_users, pc);
995       p_mess.add(li_users);
996
997       // Output
998
ta_mess = new TextArea JavaDoc(5, 30);
999       ta_mess.setEditable(false);
1000      pc.gridwidth = GridBagConstraints.REMAINDER;
1001      pc.gridx = 1;
1002      g_mess.setConstraints(ta_mess, pc);
1003      p_mess.add(ta_mess);
1004
1005      // INPUT:::
1006

1007      Panel JavaDoc p_msg = new Panel JavaDoc();
1008      GridBagLayout JavaDoc g_msg = new GridBagLayout JavaDoc();
1009      pc = new GridBagConstraints JavaDoc();
1010      p_msg.setLayout(g_msg);
1011      c.gridy = 3;
1012      gridbag.setConstraints(p_msg, c);
1013      add(p_msg);
1014
1015      // SpyEvent
1016
pc.gridx = 0;
1017      pc.gridy = 0;
1018      pc.fill = GridBagConstraints.NONE;
1019      Label JavaDoc l_mess = new Label JavaDoc("SpyEvent:");
1020      g_msg.setConstraints(l_mess, pc);
1021      p_msg.add(l_mess);
1022  
1023      // Input
1024
tf_mess = new TextField JavaDoc();
1025      tf_mess.addActionListener(this);
1026      pc.weightx = 1.0;
1027      pc.gridwidth = GridBagConstraints.RELATIVE;
1028      pc.fill = GridBagConstraints.BOTH;
1029      pc.gridx = 1;
1030      g_msg.setConstraints(tf_mess, pc);
1031      p_msg.add(tf_mess);
1032      
1033      // Send button
1034
pc.weightx = 0.0;
1035      pc.gridwidth = GridBagConstraints.REMAINDER;
1036      b_send.addActionListener(this);
1037      pc.fill = GridBagConstraints.NONE;
1038      pc.gridx = 2;
1039      g_msg.setConstraints(b_send, pc);
1040      p_msg.add(b_send);
1041
1042      pack();
1043      setVisible(true);
1044      toFront();
1045            
1046        }
1047    public void createFinalPanel(String JavaDoc s_host, String JavaDoc s_name){
1048      createPanel(s_host,s_name);
1049
1050      try {
1051        vnDispatcher.setRuntimeInformations("LOOKUP_HOST",s_host);
1052                c3ddispatcher = (C3DDispatcher)vnDispatcher.getUniqueAO();
1053        i_user = c3ddispatcher.registerConsumer(me, createScene(), s_name);
1054        l_c3ddispatcher.setText(l_c3ddispatcher.getText() + " (" + c3ddispatcher.getOSString() + ")");
1055      } catch (Exception JavaDoc ex) {
1056        log("Exception caught: " + ex);
1057        ex.printStackTrace();
1058        log("Error: C3DDispatcher not found, try to reconnect");
1059      }
1060    }
1061    
1062    public void createPanelAfterMigration(String JavaDoc s_host, String JavaDoc s_name){
1063        createPanel(s_host,s_name);
1064      
1065      c3ddispatcher.registerMigratedUser(i_user);
1066      //System.out.println("i am here");
1067
l_c3ddispatcher.setText(l_c3ddispatcher.getText() + " (" + c3ddispatcher.getOSString() + ")");
1068      //System.out.println("i am there");
1069
}
1070
1071
1072    /**
1073     * AWT 1.1 event handling
1074     */

1075    public void actionPerformed(ActionEvent JavaDoc e) {
1076      Object JavaDoc source = e.getSource();
1077      if (source == b_left) {
1078        /* Request 'rotate left' on button click */
1079        if (c3ddispatcher != null) {
1080          //b_left.setBackground(Color.yellow);
1081
c3ddispatcher.rotateLeft(i_user);
1082        }
1083      } else if (source == b_right) {
1084        /* Request 'rotate right' on button click */
1085        if (c3ddispatcher != null) {
1086          //b_right.setBackground(Color.yellow);
1087
c3ddispatcher.rotateRight(i_user);
1088        }
1089      } else if (source == b_up) {
1090        // Go up
1091
if (c3ddispatcher != null) {
1092          //b_up.setBackground(Color.yellow);
1093
c3ddispatcher.rotateUp(i_user);
1094        }
1095      } else if (source == b_down) {
1096        // Go down
1097
if (c3ddispatcher != null) {
1098          //b_down.setBackground(Color.yellow);
1099
c3ddispatcher.rotateDown(i_user);
1100        }
1101      } else if (source == b_clock) {
1102        // Go clockwise
1103
if (c3ddispatcher != null) {
1104          //b_clock.setBackground(Color.yellow);
1105
c3ddispatcher.spinClock(i_user);
1106        }
1107      } else if (source == b_unclock) {
1108        // Go unclockwise
1109
if (c3ddispatcher != null) {
1110          //b_unclock.setBackground(Color.yellow);
1111
c3ddispatcher.spinUnclock(i_user);
1112        }
1113      } else if ((source == mi_exit) || (source == b_exit)) {
1114        /* Exit the appplication */
1115        setVisible(false);
1116        exit();
1117      } else if (source == tf_mess || source == b_send) {
1118        if (c3ddispatcher != null) {
1119          // The user wants to send a mesage
1120
String JavaDoc s_mess = tf_mess.getText();
1121          if (s_mess.length() > 0) {
1122            if (talkId == -1) {
1123              // BroadCast
1124
ta_mess.append("<to all> " + s_mess + '\n');
1125              c3ddispatcher.showUserMessageExcept(i_user, "[from " + s_username + "] " + s_mess);
1126            } else {
1127              // Private message
1128
ta_mess.append("<to " + li_users.getSelectedItem() + "> " + s_mess + '\n');
1129              c3ddispatcher.showUserMessage(talkId, "[Private from " + s_username + "] " + s_mess);
1130            }
1131            tf_mess.setText("");
1132          } else {
1133            tf_mess.setText("Enter text to send");
1134            tf_mess.selectAll();
1135          }
1136        }
1137      } else if (source == b_connect || source == tf_name || source == tf_host) {
1138        /* The Welcome panel has been validated */
1139        String JavaDoc s_name = tf_name.getText();
1140        String JavaDoc s_host = tf_host.getText();
1141        if (s_name.length() < 1)
1142          tf_name.setText("Enter your name");
1143        if (s_host.length() < 1)
1144          tf_host.setText("Enter dispatcher host");
1145        if ((s_name.length() > 0) && (s_host.length() > 0)) {
1146          s_username = s_name;
1147          dispatcher_host = s_host;
1148          
1149          createFinalPanel(s_host, s_name);
1150        }
1151      } else if (source == mi_clear) {
1152        ta_log.setText("");
1153      } else if (source == mi_list) {
1154        try {
1155          log("List of current users:");
1156          log(c3ddispatcher.getUserList());
1157        } catch (Exception JavaDoc ex) {
1158            ex.printStackTrace();
1159        }
1160      } else if (source == b_addSphere) {
1161        /* The user wants to add a sphere */
1162    
1163        // Computes randoms coords btw -10 and 10
1164
double x,y,z,r;
1165        x = (Math.random() - 0.5) * 20.0;
1166        y = (Math.random() - 0.5) * 20.0;
1167        z = (Math.random() - 0.5) * 20.0;
1168        r = (Math.random()) * 10.0;
1169
1170        Sphere sphere = new Sphere(new Vec(x, y, z), r);
1171    
1172        // Should maybe check that the sphere isn't in another one???
1173
// Color, reflection and so...
1174
// sets the color to a specific color chosen arbitrary in the color[] array (which btw is a Vec array..)
1175
// Hm.. well, I'm really sorry for the person who'll maintain this code...
1176
// I _DO_ apologize for the following line.
1177
sphere.surf.color = (color[Math.round((float)(Math.random() * (color.length - 1)))]);
1178
1179        // Voodoo stuff...
1180
sphere.surf.kd = Math.random();
1181        sphere.surf.ks = Math.random();
1182        sphere.surf.shine = Math.random() * 20.0;
1183        //sphere.surf.kt=Math.random();
1184
//sphere.surf.ior=Math.random();
1185

1186        c3ddispatcher.addSphere(sphere);
1187      } else if (source == mi_about) {
1188        new MyDialog(this);
1189      } else if (source == b_reset) {
1190        c3ddispatcher.resetScene(createScene());
1191      }
1192    }
1193
1194
1195    /**
1196     * Sets the ID of the messages' recipient
1197     * @param it The corresponding Item Event on li_user
1198     * @version ADDON Florian 17/8/98
1199     */

1200    public void itemStateChanged(ItemEvent JavaDoc it) {
1201      if (li_users.getSelectedIndex() == 0)
1202        talkId = -1;
1203      else
1204        talkId = ((Integer JavaDoc)h_users.get(li_users.getSelectedItem())).intValue();
1205    }
1206
1207
1208    public void paint(Graphics JavaDoc g) {
1209      update(g);
1210    }
1211
1212
1213    public void update(Graphics JavaDoc g) {
1214      i_top = this.getInsets().top;
1215      g.setColor(Color.gray);
1216      g.drawLine(0, i_top - 1, 2000, i_top - 1);
1217      g.setColor(Color.white);
1218      g.drawLine(0, i_top, 2000, i_top);
1219    }
1220
1221
1222    /**
1223     * AWT 1.1 event handling for window events
1224     */

1225    class MyWindowListener extends WindowAdapter JavaDoc implements java.io.Serializable JavaDoc {
1226
1227      public void windowClosing(WindowEvent JavaDoc e) {
1228        setVisible(false);
1229        exit();
1230      }
1231
1232
1233      public void windowOpened(WindowEvent JavaDoc e) {
1234        if (b_isBot) {
1235          s_username = "Benchmarking bot";
1236          createFinalPanel(botUrl, s_username);
1237        }
1238      }
1239    }
1240  }
1241
1242  /**
1243   * The about box
1244   */

1245  class MyDialog extends Dialog JavaDoc implements ActionListener JavaDoc, MouseListener JavaDoc, java.io.Serializable JavaDoc {
1246
1247    private Label JavaDoc d_title = new Label JavaDoc("ProActive PDC", Label.CENTER);
1248    private Label JavaDoc d_url = new Label JavaDoc("http://www.inria.fr/proactive/", Label.CENTER);
1249    /**
1250     * Button to close the about box
1251     */

1252    private Button JavaDoc b_ok = new Button JavaDoc("OK");
1253
1254
1255    public MyDialog(UserFrame parent) {
1256      super(parent, "About ProActive PDC", true);
1257      
1258      // Creates a modal about box
1259
GridBagLayout JavaDoc gb = new GridBagLayout JavaDoc();
1260      GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
1261
1262      setLayout(gb);
1263      
1264      // Title
1265
c.gridy = 0;
1266      c.fill = GridBagConstraints.HORIZONTAL;
1267      gb.setConstraints(d_title, c);
1268      d_title.setForeground(Color.blue);
1269      d_title.setFont(new Font JavaDoc("arial", Font.BOLD | Font.ITALIC, 16));
1270      add(d_title);
1271      
1272      // Subtitle
1273
c.gridy = 1;
1274      gb.setConstraints(d_url, c);
1275      d_url.addMouseListener(this);
1276      add(d_url);
1277      
1278
1279
1280      //Button
1281
c.gridy = 2;
1282      c.fill = GridBagConstraints.NONE;
1283      gb.setConstraints(b_ok, c);
1284      b_ok.addActionListener(this);
1285      add(b_ok);
1286
1287      pack();
1288
1289      setLocation(400, 200);
1290      setSize(200, 200);
1291      setVisible(true);
1292      toFront();
1293    }
1294
1295
1296    public void mouseClicked(MouseEvent JavaDoc e) {
1297      // TO BE REIMPLEMENTED AS SOON AS SOMEONE KNOWS HOW TO GET THE PATH WITHOUT SYSTEM.GETENV (DEPRECATED)
1298
/*
1299          Runtime rt = Runtime.getRuntime();
1300          System.out.println(rt);
1301          try{
1302          rt.exec("/usr/local/netscape/netscape/netscape -remote openURL'(http://www.inria.fr/proactive/)' &");
1303          }
1304          catch (Exception ex){}
1305        */

1306    }
1307
1308
1309    public void mouseReleased(MouseEvent JavaDoc e) {
1310    }
1311
1312
1313    public void mouseEntered(MouseEvent JavaDoc e) {
1314    }
1315
1316
1317    public void mouseExited(MouseEvent JavaDoc e) {
1318    }
1319
1320
1321    public void mousePressed(MouseEvent JavaDoc e) {
1322    }
1323
1324
1325    public void actionPerformed(ActionEvent JavaDoc e) {
1326      setVisible(false);
1327      dispose();
1328    }
1329  }
1330}
1331
1332/**
1333 * This class encapsulates the <code>Image</code> in an AWT
1334 * <code>Container</code>. It integrates easily with other AWT components
1335 * and hides the drawing and scaling issues inherent to image displaying
1336 */

1337class MyImageContainer extends Canvas JavaDoc implements java.io.Serializable JavaDoc {
1338
1339  private Image JavaDoc img, scaled_img;
1340  private int minwidth, minheight;
1341  private Insets JavaDoc insets;
1342  private Panel JavaDoc p;
1343
1344
1345  public MyImageContainer(Image JavaDoc img, int width, int height, Panel JavaDoc p) {
1346    this.img = img;
1347    this.scaled_img = img;
1348    this.minwidth = width;
1349    this.minheight = height;
1350    this.p = p;
1351    //System.out.println("----------"+p.getSize().width);
1352
insets = p.getInsets();
1353  }
1354
1355
1356  public void paint(Graphics JavaDoc g) {
1357    g.drawImage(scaled_img, insets.left, insets.top, this);
1358  }
1359
1360
1361  public Dimension JavaDoc getPreferredSize() {
1362    insets = p.getInsets();
1363    return new Dimension JavaDoc(minwidth + insets.left + insets.right, minheight + insets.left + insets.right);
1364  }
1365
1366
1367  public Dimension JavaDoc getMinimumSize() {
1368    insets = p.getInsets();
1369    return new Dimension JavaDoc(minwidth + insets.left + insets.right, minheight + insets.left + insets.right);
1370  }
1371
1372
1373  public void validate() {
1374    insets = p.getInsets();
1375        //System.out.println("----------"+p.getSize().width+"-----------"+insets.left+"-----"+insets.right);
1376
int width = p.getSize().width - insets.left - insets.right;
1377    int height = p.getSize().height - insets.top - insets.bottom;
1378// int width = 270 - insets.left - insets.right;
1379
// int height = 270 - insets.top - insets.bottom;
1380
// System.out.println("width "+width);
1381
// System.out.println("height "+height);
1382

1383    int size;
1384    if (width > height)
1385      size = height;
1386    else
1387      size = width;
1388    scaled_img = img.getScaledInstance(size, size, Image.SCALE_SMOOTH);
1389    super.validate();
1390    repaint();
1391  }
1392}
1393
Popular Tags