1 26 27 28 package org.objectweb.mobilitools.smi.goodies; 29 30 31 import javax.swing.*; 32 import javax.swing.table.AbstractTableModel ; 33 import javax.swing.event.TableModelEvent ; 34 import java.awt.*; 35 import java.awt.event.*; 36 import java.io.*; 37 import java.util.Observer ; 38 import java.util.Observable ; 39 import java.util.StringTokenizer ; 40 import org.objectweb.mobilitools.smi.*; 41 import org.objectweb.mobilitools.smi.api.*; 42 import org.objectweb.mobilitools.util.gui.*; 43 import org.omg.CORBA.ORB ; 44 import org.omg.CORBA.Any ; 45 import org.omg.CfMAF.AgentProfile; 46 import org.omg.CfMAF.AgentSystemInfo; 47 48 49 55 public class FinderGUI 56 extends JFrame 57 implements Observer 58 { 59 static boolean standalone = false; 60 static String [] columnNames = { "Name", "Location" }; 61 62 63 67 static public void main(String [] args) 68 { 69 ORB orb = ORB.init(args, System.getProperties()); 70 standalone = true; 71 new FinderGUI(new Finder(args[args.length-1], orb)); 72 orb.run(); 73 } 74 75 76 Finder my_finder; 77 Object [][] data; 78 TableSorter tableSorter; 79 MyTableModel tableModel; 80 JTable table; 81 RegionManager region; 82 83 84 88 public FinderGUI(Finder finder) 89 { 90 super("SMI Finder for region " + finder.getRegion()); 91 my_finder = finder; 92 try 93 { 94 region = new RegionManager(finder.getORB()); 95 } 96 catch (BadOperation e) 97 { 98 System.err.println(e.toString()); 99 region = null; 100 } 101 data = new String [0][2]; 102 finder.addObserver(this); 103 addWindowListener(new OnWindowClosing()); 104 105 tableModel = new MyTableModel(); 106 tableSorter = new TableSorter(tableModel); 107 table = new JTable(tableSorter); 108 table.addMouseListener(new OnMouseClick()); 109 tableSorter.addMouseListenerToHeaderInTable(table); 110 112 JScrollPane scrollPane = new JScrollPane(table); 114 115 getContentPane().add(scrollPane, BorderLayout.CENTER); 117 118 pack(); 119 setVisible(true); 120 } 121 122 123 128 public void quit() 129 { 130 dispose(); 131 if (standalone) 132 { 133 System.exit(my_finder.exit()); 134 } 135 } 136 137 138 142 143 public void update(Observable o, java.lang.Object arg) 144 { 145 FinderEntry[] entries = (FinderEntry[])arg; 146 data = new Object [entries.length][]; 147 for (int i=0 ; i<data.length ; ++i) 148 { 149 data[i] = new Object [] { entries[i], entries[i].getLocation() }; 150 } 151 tableSorter.tableChanged(new TableModelEvent (tableModel)); 152 table.clearSelection(); 153 } 154 155 156 160 161 class OnWindowClosing extends WindowAdapter 162 { 163 public void windowClosing(WindowEvent e) 164 { 165 FinderGUI.this.quit(); 166 } 167 } 168 169 170 174 175 class OnMouseClick extends MouseAdapter 176 { 177 public void mouseClicked(MouseEvent e) 178 { 179 if (e.getClickCount() == 2) 180 { 181 FinderEntry selection = (FinderEntry)tableSorter.getValueAt(FinderGUI.this.table.rowAtPoint(e.getPoint()), 0); 182 if (selection.isAgent()) 183 { 184 new AgentWindow(FinderGUI.this, selection); 185 } 186 else 187 { 188 new AgentSystemWindow(FinderGUI.this, selection); 189 } 190 } 191 } 192 } 193 194 195 199 200 class MyTableModel extends AbstractTableModel { 201 public int getColumnCount() { 202 return FinderGUI.columnNames.length; 203 } 204 205 public int getRowCount() { 206 return FinderGUI.this.data.length; 207 } 208 209 public String getColumnName(int col) { 210 return FinderGUI.columnNames[col]; 211 } 212 213 public Object getValueAt(int row, int col) { 214 return FinderGUI.this.data[row][col]; 215 } 216 } 217 218 219 223 224 class AgentWindow extends JDialog implements ActionListener, FormListener 225 { 226 FinderEntry my_agent; 227 JButton moveBtn, suspendBtn, resumeBtn, terminateBtn; 228 229 230 AgentWindow(Frame frame, FinderEntry agent) 231 { 232 super(frame, agent.toString()); 233 my_agent = agent; 234 boolean isRunning; 235 String infoStr = 236 "Name: " + agent.getName().identity() + "\n" + 237 "Authority: " + agent.getName().authority() + "\n" + 238 "Type: " + String.valueOf(agent.getName().type()) + "\n" + 239 "Location: " + agent.getLocation() + "\n" + 240 "Activity: "; 241 Container pane = this.getContentPane(); 242 JPanel buttonPnl = new JPanel(); 243 buttonPnl.setLayout(new FlowLayout()); 244 buttonPnl.add(moveBtn = new JButton("move")); 245 moveBtn.addActionListener(this); 246 try 247 { 248 isRunning = region.isRunning(new Location(agent.getLocation()), agent.getName()); 249 if (isRunning) 250 { 251 buttonPnl.add(suspendBtn = new JButton("suspend")); 252 suspendBtn.addActionListener(this); 253 resumeBtn = null; 254 infoStr += "running\n"; 255 } 256 else 257 { 258 buttonPnl.add(resumeBtn = new JButton("resume")); 259 resumeBtn.addActionListener(this); 260 suspendBtn = null; 261 infoStr += "suspended\n"; 262 } 263 } 264 catch (Exception e) 265 { 266 System.err.println("Warning: can't check " + agent + "'s activity status:\n" + e); 267 infoStr += "unknown\n"; 268 } 269 StringWriter strWriter = new StringWriter(); 270 agent.getProperties().list(new PrintWriter(strWriter)); 271 infoStr += strWriter.toString(); 272 buttonPnl.add(terminateBtn = new JButton("terminate")); 273 terminateBtn.addActionListener(this); 274 pane.add(buttonPnl, BorderLayout.SOUTH); 275 JTextArea infoTxt = new JTextArea(infoStr); 276 infoTxt.setEditable(false); 277 pane.add(new JScrollPane(infoTxt), BorderLayout.CENTER); 278 this.pack(); 279 this.setVisible(true); 280 } 281 282 283 public void actionPerformed(ActionEvent ev) 284 { 285 if (ev.getSource() == moveBtn) 286 { 287 new FormFrame( 288 "Move " + my_agent, 289 "Enter destination for " + my_agent, 290 new String [] { "region", "agency", "place" }, 291 new String [] { "move", "stay" } 292 ).run(this); 293 } 294 else if (ev.getSource() == resumeBtn) 295 { 296 try 297 { 298 region.resumeAgent(new Location(my_agent.getLocation()), my_agent.getName()); 299 } 300 catch (Exception ex) 301 { 302 new AlertWindow(FinderGUI.this, "Can't resume " + my_agent, ex.toString(), "OK"); 303 } 304 this.dispose(); 305 } 306 else if (ev.getSource() == suspendBtn) 307 { 308 try 309 { 310 region.suspendAgent(new Location(my_agent.getLocation()), my_agent.getName()); 311 } 312 catch (Exception ex) 313 { 314 new AlertWindow(FinderGUI.this, "Can't suspend " + my_agent, ex.toString(), "OK"); 315 } 316 this.dispose(); 317 } 318 else if (ev.getSource() == terminateBtn) 319 { 320 try 321 { 322 region.terminateAgent(new Location(my_agent.getLocation()), my_agent.getName()); 323 } 324 catch (Exception ex) 325 { 326 new AlertWindow(FinderGUI.this, "Can't terminate " + my_agent, ex.toString(), "OK"); 327 } 328 this.dispose(); 329 } 330 } 331 332 333 public void formResult(String [] result) 334 { 335 if (result[result.length-1].equals("move")) 336 { 337 try 338 { 339 region.moveAgent( 340 new Location(my_agent.getLocation()), 341 my_agent.getName(), 342 new Location(result[0], result[1]), 343 result[2]); 344 } 345 catch (Exception e) 346 { 347 new AlertWindow(FinderGUI.this, "Can't move " + my_agent, e.toString(), "OK"); 348 } 349 } 350 this.dispose(); 351 } 352 } 353 354 355 359 360 class AgentSystemWindow extends JDialog implements ActionListener, FormListener 361 { 362 FinderEntry my_agency; 363 JButton createBtn, terminateBtn; 364 365 366 AgentSystemWindow(Frame frame, FinderEntry agency) 367 { 368 super(frame, agency.toString()); 369 my_agency = agency; 370 Container pane = this.getContentPane(); 371 StringWriter strWriter = new StringWriter(); 372 agency.getProperties().list(new PrintWriter(strWriter)); 373 JTextArea infoTxt = new JTextArea( 374 "Name: " + agency.getName().identity() + "\n" + 375 "Authority: " + agency.getName().authority() + "\n" + 376 "Type: " + String.valueOf(agency.getName().type()) + "\n" + 377 "Location: " + agency.getLocation() + "\n" + 378 strWriter.toString()); 379 infoTxt.setEditable(false); 380 pane.add(new JScrollPane(infoTxt), BorderLayout.CENTER); 381 JPanel buttonPnl = new JPanel(); 382 buttonPnl.setLayout(new FlowLayout()); 383 buttonPnl.add(createBtn = new JButton("new agent")); 384 createBtn.addActionListener(this); 385 buttonPnl.add(terminateBtn = new JButton("terminate")); 386 terminateBtn.addActionListener(this); 387 pane.add(buttonPnl, BorderLayout.SOUTH); 388 this.pack(); 389 this.setVisible(true); 390 } 391 392 393 public void actionPerformed(ActionEvent ev) 394 { 395 396 if (ev.getSource() == createBtn) 397 { 398 new FormFrame( 399 "New agent in " + my_agency, 400 "Enter new agent parameters", 401 new String [] { 402 "class name", 403 "agent name", 404 "place name", 405 "code base", 406 "arguments" }, 407 new String [] { "create", "cancel" } 408 ).run(this); 409 } 410 else if (ev.getSource() == terminateBtn) 411 { 412 try 413 { 414 region.terminateAgency(new Location(my_agency.getLocation())); 415 } 416 catch (Exception ex) 417 { 418 new AlertWindow(FinderGUI.this, "Can't terminate " + my_agency, ex.toString(), "OK"); 419 } 420 this.dispose(); 421 } 422 } 423 424 425 public void formResult(String [] result) 426 { 427 if (result[result.length-1].equals("create")) 428 { 429 StringTokenizer parser = new StringTokenizer (result[4], " "); 430 String [] args = new String [parser.countTokens()]; 431 for (int i=0 ; i<args.length ; ++i) 432 { 433 args[i] = parser.nextToken(); 434 } 435 AgentSystemInfo agencyInfo = (AgentSystemInfo)my_agency.getProfile(); 436 org.objectweb.mobilitools.smi.api.Name name = new org.objectweb.mobilitools.smi.api.Name( 437 System.getProperty("user.name"), 438 result[1], 439 agencyInfo.agent_system_type); 440 441 try 442 { 443 region.createAgent( 444 new Location(my_agency.getLocation()), 445 result[0], 446 result[3], 447 name, 448 result[2], 449 new AgentProfile( 450 agencyInfo.language_maps[0].language_id, 451 agencyInfo.agent_system_type, 452 agencyInfo.agent_system_description, 453 agencyInfo.major_version, 454 agencyInfo.minor_version, 455 agencyInfo.language_maps[0].serializations[0], 456 new Any [0]), 457 null, 458 args); 459 } 460 catch (Exception ex) 461 { 462 new AlertWindow( 463 FinderGUI.this, 464 "Agent creation failed", 465 name + " could not be created in " + my_agency + ":\n" + ex, 466 "OK"); 467 } 468 } 469 this.dispose(); 470 } 471 } 472 } 473 | Popular Tags |