1 31 package org.objectweb.proactive.examples.philosophers; 32 33 import java.awt.GridBagConstraints ; 34 import org.objectweb.proactive.*; 35 36 41 public class DinnerLayout { 42 43 46 transient private Philosopher phils[]; 47 50 transient private UserFrame display; 51 54 transient private javax.swing.Icon images[]; 55 61 transient private Table manager; 62 transient private String url; 63 64 65 70 public DinnerLayout() { 71 } 72 73 77 public DinnerLayout(javax.swing.Icon images[]) { 78 this.images = images; 79 display = new UserFrame(images); 80 } 81 82 public void setNode(String url) { 83 this.url = url; 84 } 85 86 public javax.swing.JPanel getDisplay () { 87 return display; 88 } 89 90 96 public ObjectForSynchronousCall init() { 97 98 102 Object [] params; params = new Object [1]; 104 params[0] = org.objectweb.proactive.ProActive.getStubOnThis(); 105 106 try { 108 manager = (Table)org.objectweb.proactive.ProActive.newActive(Table.class.getName(), params, url); 109 } catch (Exception e) { 110 e.printStackTrace(); 111 } 112 113 115 phils = new Philosopher[5]; 116 117 params = new Object [3]; 119 params[1] = manager; 120 params[2] = ProActive.getStubOnThis(); 121 122 for (int n = 0; n < 5; n++) { 123 params[0] = new Integer (n); 124 try { 125 phils[n] = (Philosopher)org.objectweb.proactive.ProActive.newActive(Philosopher.class.getName(), params); 126 } catch (Exception 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 t) { 140 } 141 } 142 143 144 148 public void activateButtons() { 149 display.activate(); 150 } 151 152 153 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 168 public void updateFork(int index, int state) { 169 this.display.forkButtons[index].changeState(state); 170 } 171 172 176 protected class UserFrame extends javax.swing.JPanel implements java.awt.event.ActionListener , java.awt.event.MouseListener { 177 178 protected javax.swing.JButton bQuit; 179 protected javax.swing.JButton bAuto; 180 183 protected PhilPanel philButtons[]; 184 protected ForkPanel forkButtons[]; 185 protected boolean autopilot; 186 187 188 public UserFrame(javax.swing.Icon images[]) { 189 191 autopilot = false; 193 194 197 setSize(350, 300); 198 java.awt.GridBagLayout grid = new java.awt.GridBagLayout (); 199 java.awt.GridBagConstraints cs = new java.awt.GridBagConstraints (); 200 this.setLayout(grid); 201 cs.gridy = 0; 202 203 javax.swing.JPanel pPhil = new javax.swing.JPanel (); 205 java.awt.GridBagLayout gridbag = new java.awt.GridBagLayout (); 206 java.awt.GridBagConstraints c = new java.awt.GridBagConstraints (); 207 208 pPhil.setLayout(gridbag); 209 210 philButtons = new PhilPanel[5]; 212 for (int i = 0; i < 5; i++) { 213 philButtons[i] = new PhilPanel(images); 214 } 215 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 javax.swing.JPanel pCmd = new javax.swing.JPanel (); 279 bQuit = new javax.swing.JButton ("Quit"); 281 bQuit.addActionListener(this); 282 pCmd.add(bQuit); 283 284 bAuto = new javax.swing.JButton ("Autopilot"); 286 pCmd.add(bAuto); 287 288 grid.setConstraints(pCmd, cs); 289 this.add(pCmd); 290 291 setVisible(true); 292 } 293 294 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 e) { 305 Object 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 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 e) { 325 }; 326 public void mouseReleased(java.awt.event.MouseEvent e) { 327 }; 328 public void mouseEntered(java.awt.event.MouseEvent e) { 329 }; 330 public void mouseExited(java.awt.event.MouseEvent e) { 331 }; 332 333 339 public void mouseClicked(java.awt.event.MouseEvent e) { 340 PhilPanel source = (PhilPanel)e.getSource(); 341 342 if (autopilot) return; 344 345 int index = 0; 347 while (index < 5 && philButtons[index] != source) { 348 index++; 349 } 350 351 switch (philButtons[index].state) { 353 case 0: 354 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 { 371 372 375 private javax.swing.Icon imgPhil[]; 376 379 public int state; 380 381 382 387 public PhilPanel(javax.swing.Icon imgPhil[]) { 388 super(imgPhil[0]); 389 setPreferredSize(new java.awt.Dimension (70, 70)); 390 this.imgPhil = imgPhil; 391 state = 0; 392 } 393 394 395 400 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 { 410 411 415 private javax.swing.Icon imgPhil[]; 416 419 public int state; 420 421 422 427 428 public ForkPanel(javax.swing.Icon imgPhil[]) { 429 super(imgPhil[3]); 430 setPreferredSize(new java.awt.Dimension (40, 40)); 431 this.imgPhil = imgPhil; 432 state = 3; 433 } 434 435 436 441 442 443 public void changeState(int state) { 444 this.state = state; 445 setIcon(imgPhil[state]); 446 } 447 } 448 } 449 | Popular Tags |