KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > philosophers > DinnerLayout


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.philosophers;
32
33 import java.awt.GridBagConstraints JavaDoc;
34 import org.objectweb.proactive.*;
35
36 /**
37  * DinnerLayout
38  * This class acts as a wrapper for the ui.
39  * It is there also that the active objects are initialized.
40  */

41 public class DinnerLayout {
42
43   /**
44    * Reference to the philosopher's stub in order to dispatch messages from the UI
45    */

46   transient private Philosopher phils[];
47   /**
48    * Reference to the actual LOCAL awt frame
49    */

50   transient private UserFrame display;
51   /**
52    * The images needed to construct the PhilPanel objects
53    */

54   transient private javax.swing.Icon JavaDoc images[];
55   /**
56    * Reference to the Table manager, for bootstrap use.
57    * In fact, the applet creates the layout
58    * then the layout creates the active objects and
59    * sends them a reference to its own stub
60    */

61   transient private Table manager;
62   transient private String JavaDoc url;
63
64
65   /**
66    * The empty no args constructor commanded by papdc
67    * it performs <B>ABSOLUTELY NOTHING</B> because it is called by papdc
68    * before an explicit call. Any operation in this could cause memory leaks..
69    */

70   public DinnerLayout() {
71   }
72
73   /**
74    * The real constructor
75    * @param the array holding the Philosopher/Forks's images
76    */

77   public DinnerLayout(javax.swing.Icon JavaDoc images[]) {
78     this.images = images;
79     display = new UserFrame(images);
80   }
81
82   public void setNode(String JavaDoc url) {
83     this.url = url;
84   }
85
86   public javax.swing.JPanel JavaDoc getDisplay () {
87     return display;
88   }
89
90   /**
91    * init
92    * this method instanciates the remote objects,
93    * as this <b>MUST NOT<b> be done in the constructor
94    * [the getProxyOnThis method would fail, and we don't want that]
95    */

96   public ObjectForSynchronousCall init() {
97
98     /**
99      * This method instanciates the remote Table manager
100      * The parameter passed to the new Table is the reference to the layout's stub
101      */

102     Object JavaDoc[] params; // the papdc arg holder
103
params = new Object JavaDoc[1];
104     params[0] = org.objectweb.proactive.ProActive.getStubOnThis();
105     
106     // Creates the Table
107
try {
108       manager = (Table)org.objectweb.proactive.ProActive.newActive(Table.class.getName(), params, url);
109     } catch (Exception JavaDoc e) {
110       e.printStackTrace();
111     }
112    
113     // and the philosophers:
114

115     phils = new Philosopher[5];
116     
117     // Creates the philosophers
118
params = new Object JavaDoc[3];
119     params[1] = manager;
120     params[2] = ProActive.getStubOnThis();
121
122     for (int n = 0; n < 5; n++) {
123       params[0] = new Integer JavaDoc(n);
124       try {
125         phils[n] = (Philosopher)org.objectweb.proactive.ProActive.newActive(Philosopher.class.getName(), params);
126       } catch (Exception JavaDoc e) {
127         e.printStackTrace();
128       }
129     }
130     return new ObjectForSynchronousCall();
131
132   }
133
134   public void exit() {
135     try {
136       display.setVisible(false);
137       display = null;
138       System.exit(0);
139     } catch (Throwable JavaDoc t) {
140     }
141   }
142
143
144   /**
145    * This method is called by the manager
146    * after it has created the active Philosophers.
147    */

148   public void activateButtons() {
149     display.activate();
150   }
151
152
153   /**
154    * update
155    * Updates the state's image of a philosopher [ie: when he eats]
156    * @param index the philosopher's ID
157    * @param the new state [0=think, 1=wait 2=eating]
158    */

159   public void update(int index, int state) {
160     this.display.philButtons[index].changeState(state);
161     this.display.philButtons[index].setEnabled(true);
162   }
163
164   /* update fork state:
165    * @param index the fork's ID
166    * @param the new state [3=ontable, 4=inhand]
167    */

168   public void updateFork(int index, int state) {
169     this.display.forkButtons[index].changeState(state);
170   }
171
172   /**
173    * UserFrame
174    * This is the <b>real</b> AWT Frame
175    */

176   protected class UserFrame extends javax.swing.JPanel JavaDoc implements java.awt.event.ActionListener JavaDoc, java.awt.event.MouseListener JavaDoc {
177
178     protected javax.swing.JButton JavaDoc bQuit;
179     protected javax.swing.JButton JavaDoc bAuto;
180     /**
181      * This array links a philosopher to its button
182      */

183     protected PhilPanel philButtons[];
184     protected ForkPanel forkButtons[];
185     protected boolean autopilot;
186
187
188     public UserFrame(javax.swing.Icon JavaDoc images[]) {
189       // Frame size and position
190

191       // Autopilot initialization
192
autopilot = false;
193
194 /* setBackground(java.awt.Color.lightGray);
195       setFont(new java.awt.Font("SansSerif", java.awt.Font.PLAIN, 12));
196 */

197       setSize(350, 300);
198       java.awt.GridBagLayout JavaDoc grid = new java.awt.GridBagLayout JavaDoc();
199       java.awt.GridBagConstraints JavaDoc cs = new java.awt.GridBagConstraints JavaDoc();
200       this.setLayout(grid);
201       cs.gridy = 0;
202
203       // Philosophers' panels
204
javax.swing.JPanel JavaDoc pPhil = new javax.swing.JPanel JavaDoc();
205       java.awt.GridBagLayout JavaDoc gridbag = new java.awt.GridBagLayout JavaDoc();
206       java.awt.GridBagConstraints JavaDoc c = new java.awt.GridBagConstraints JavaDoc();
207
208       pPhil.setLayout(gridbag);
209       
210       // Philosophers
211
philButtons = new PhilPanel[5];
212       for (int i = 0; i < 5; i++) {
213         philButtons[i] = new PhilPanel(images);
214       }
215       // Forks
216
forkButtons = new ForkPanel[5];
217       for (int i = 0; i < 5; i++) {
218         forkButtons[i] = new ForkPanel(images);
219       }
220
221       c.gridx = 2;
222       c.gridy = 1;
223       c.fill = GridBagConstraints.BOTH;
224       c.weightx = c.weighty = 1.0;
225       gridbag.setConstraints(philButtons[0], c);
226       pPhil.add(philButtons[0]);
227
228       c.gridx = 3;
229       c.gridy = 1;
230       gridbag.setConstraints(forkButtons[1], c);
231       pPhil.add(forkButtons[1]);
232
233       c.gridx = 4;
234       c.gridy = 2;
235       gridbag.setConstraints(philButtons[1], c);
236       pPhil.add(philButtons[1]);
237
238       c.gridx = 4;
239       c.gridy = 3;
240       gridbag.setConstraints(forkButtons[2], c);
241       pPhil.add(forkButtons[2]);
242
243       c.gridx = 3;
244       c.gridy = 4;
245       gridbag.setConstraints(philButtons[2], c);
246       pPhil.add(philButtons[2]);
247
248       c.gridx = 2;
249       c.gridy = 4;
250       gridbag.setConstraints(forkButtons[3], c);
251       pPhil.add(forkButtons[3]);
252
253       c.gridx = 1;
254       c.gridy = 4;
255       gridbag.setConstraints(philButtons[3], c);
256       pPhil.add(philButtons[3]);
257
258       c.gridx = 0;
259       c.gridy = 3;
260       gridbag.setConstraints(forkButtons[4], c);
261       pPhil.add(forkButtons[4]);
262
263       c.gridx = 0;
264       c.gridy = 2;
265       gridbag.setConstraints(philButtons[4], c);
266       pPhil.add(philButtons[4]);
267
268       c.gridx = 1;
269       c.gridy = 1;
270       gridbag.setConstraints(forkButtons[0], c);
271       pPhil.add(forkButtons[0]);
272
273       grid.setConstraints(pPhil, cs);
274       this.add(pPhil);
275
276       cs.gridy = 1;
277       // Panel de commandes
278
javax.swing.JPanel JavaDoc pCmd = new javax.swing.JPanel JavaDoc();
279       // Quit
280
bQuit = new javax.swing.JButton JavaDoc("Quit");
281       bQuit.addActionListener(this);
282       pCmd.add(bQuit);
283       
284       // Autopilot
285
bAuto = new javax.swing.JButton JavaDoc("Autopilot");
286       pCmd.add(bAuto);
287
288       grid.setConstraints(pCmd, cs);
289       this.add(pCmd);
290
291       setVisible(true);
292     }
293
294     /* Don't activate before the Philosophers are built
295      */

296     void activate () {
297       bAuto.addActionListener(this);
298       for (int i = 0; i < 5; i++) {
299         philButtons[i].addMouseListener(this);
300       }
301     }
302
303
304     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
305       Object JavaDoc source = e.getSource();
306       if (source == bAuto) {
307         if (!autopilot) {
308           bAuto.setText("Manual");
309           autopilot = true;
310         } else {
311           bAuto.setText("Autopilot");
312           autopilot = false;
313         }
314         // Toggle the philosophers' state
315
for (int i = 0; i < 5; i++)
316           phils[i].toggle();
317       } else if (source == bQuit) {
318         autopilot = false;
319         exit();
320       }
321     }
322
323
324     public void mousePressed(java.awt.event.MouseEvent JavaDoc e) {
325     };
326     public void mouseReleased(java.awt.event.MouseEvent JavaDoc e) {
327     };
328     public void mouseEntered(java.awt.event.MouseEvent JavaDoc e) {
329     };
330     public void mouseExited(java.awt.event.MouseEvent JavaDoc e) {
331     };
332
333     /**
334      * mouseClicked
335      * Called when the user clicks on the panel
336      * used to toggle the state of the philosopher
337      * @param e the event
338      */

339     public void mouseClicked(java.awt.event.MouseEvent JavaDoc e) {
340       PhilPanel source = (PhilPanel)e.getSource();
341
342       if (autopilot) // We don't want no interferences when in autopilot mode
343
return;
344
345       // Find the philosopher who has just been clicked upon
346
int index = 0;
347       while (index < 5 && philButtons[index] != source) {
348         index++;
349       }
350           
351       // checks if the philosopher is eating
352
switch (philButtons[index].state) {
353         case 0:
354           // He's not eating
355
// (le philosophe fait un appel synchrone sur Table.getForks)
356
phils[index].getForks();
357           philButtons[index].changeState(1);
358           philButtons[index].setEnabled(false);
359           break;
360
361         case 2:
362           phils[index].putForks();
363           philButtons[index].changeState(0);
364           philButtons[index].setEnabled(true);
365           break;
366       }
367     }
368   }
369
370   private class PhilPanel extends javax.swing.JLabel JavaDoc {
371
372     /**
373      * The array holding the images
374      */

375     private javax.swing.Icon JavaDoc imgPhil[];
376     /**
377      * Current state
378      */

379     public int state;
380
381
382     /**
383      * PhilPanel
384      * the constructor
385      * @param imgPhil The array holding the images
386      */

387     public PhilPanel(javax.swing.Icon JavaDoc imgPhil[]) {
388       super(imgPhil[0]);
389       setPreferredSize(new java.awt.Dimension JavaDoc(70, 70));
390       this.imgPhil = imgPhil;
391       state = 0;
392     }
393
394
395     /**
396      * changeState
397      * Call this function when changing the picture diplayed
398      * @param state the new state
399      */

400     /* Assert: state in [0-2] */
401
402     public void changeState(int state) {
403       this.state = state;
404       setIcon(imgPhil[state]);
405     }
406   }
407   
408   
409   private class ForkPanel extends javax.swing.JLabel JavaDoc {
410
411     /**
412      * The array holding the images (shared with the phils)
413      * Forks images are indexed 3 (on the table) and 4 (in hand)
414      */

415     private javax.swing.Icon JavaDoc imgPhil[];
416     /**
417      * Current state
418      */

419     public int state;
420
421
422     /**
423      * ForkPanel
424      * the constructor
425      * @param imgPhil The array holding the images
426      */

427
428     public ForkPanel(javax.swing.Icon JavaDoc imgPhil[]) {
429       super(imgPhil[3]);
430       setPreferredSize(new java.awt.Dimension JavaDoc(40, 40));
431       this.imgPhil = imgPhil;
432       state = 3;
433     }
434
435
436     /**
437      * changeState
438      * Call this function when changing the picture diplayed
439      * @param state the new state
440      */

441     /* Assert: state in [3-4] */
442
443     public void changeState(int state) {
444       this.state = state;
445       setIcon(imgPhil[state]);
446     }
447   }
448 }
449
Popular Tags