1 26 27 28 package org.objectweb.mobilitools.smi.goodies; 29 30 31 import java.awt.*; 32 import org.omg.CORBA.*; 33 import java.awt.event.*; 34 import java.util.Observer ; 35 import java.util.Observable ; 36 import java.util.Properties ; 37 import java.util.StringTokenizer ; 38 import java.io.*; 39 import org.objectweb.mobilitools.smi.*; 40 import org.objectweb.mobilitools.smi.api.*; 41 import org.objectweb.mobilitools.util.gui.*; 42 import org.omg.CfMAF.*; 43 44 45 51 public class AgencyGUI 52 extends Frame 53 implements FormListener, Observer , ActionListener, ItemListener 54 { 55 Button updateBtn, createBtn, terminateBtn, viewBtn, moveBtn, shutdownBtn, resumeBtn, suspendBtn; 56 Panel bottomPnl; 57 List namesLst; 58 Checkbox manualChk; 59 Agency agency; 60 org.omg.CfMAF.Name[] names; 61 static boolean standalone = false; 62 63 64 68 static public void main(String [] args) 69 { 70 ORB orb = ORB.init(args, null); 71 AgencyGUI.standalone = true; 72 try 73 { 74 new AgencyGUI(new Agency(args[0], args[1], orb)); 75 orb.run(); 76 } 77 catch (BadOperation e) 78 { 79 e.printStackTrace(); 80 System.exit(-1); 81 } 82 } 83 84 85 89 public AgencyGUI(Agency the_agency) 90 { 91 agency = the_agency; 92 agency.exitOnTerminate(true, -1); 93 setLayout(new BorderLayout()); 94 Panel topPnl = new Panel(); 95 topPnl.setLayout(new FlowLayout()); 96 topPnl.add(manualChk = new Checkbox("manual update", false)); 97 manualChk.addItemListener(this); 98 topPnl.add(updateBtn = new Button("update")); 99 updateBtn.setEnabled(false); 100 updateBtn.addActionListener(this); 101 topPnl.add(createBtn = new Button("create")); 102 createBtn.addActionListener(this); 103 topPnl.add(shutdownBtn = new Button("shutdown")); 104 shutdownBtn.addActionListener(this); 105 add("North", topPnl); 106 add("Center", namesLst = new List()); 107 namesLst.addActionListener(this); 108 namesLst.addItemListener(this); 109 bottomPnl = new Panel(); 110 bottomPnl.add(viewBtn = new Button("view")); 111 viewBtn.addActionListener(this); 112 bottomPnl.add(suspendBtn = new Button("suspend")); 113 suspendBtn.addActionListener(this); 114 bottomPnl.add(resumeBtn = new Button("resume")); 115 resumeBtn.addActionListener(this); 116 bottomPnl.add(moveBtn = new Button("move")); 117 moveBtn.addActionListener(this); 118 bottomPnl.add(terminateBtn = new Button("terminate")); 119 terminateBtn.addActionListener(this); 120 add("South", bottomPnl); 121 setTitle( 122 "SMI agency " + agency.getName() + 123 ", region " + agency.getRegion()); 124 updateNames(); 125 the_agency.addObserver(this); 126 addWindowListener(new OnWindowClosing()); 127 pack(); 128 show(); 129 } 130 131 132 135 synchronized void updateNames() 136 { 137 names = agency.listAgents((AgentProfile)null); 138 if (namesLst.getItemCount() > 0) 139 { 140 namesLst.removeAll(); 141 } 142 for (int i=0 ; i<names.length ; ++i) 143 { 144 namesLst.add( 145 new String (names[i].identity) + 146 " (" + 147 new String (names[i].authority) + 148 ")"); 149 } 150 bottomPnl.setEnabled(false); 151 } 152 153 154 160 public void shutdown(int exitCode) 161 { 162 try 163 { 164 agency.terminate(); 165 } 166 catch (Exception e) 167 { 168 e.printStackTrace(); 169 } 170 dispose(); 171 if (standalone) 172 { 173 System.exit(exitCode); 174 } 175 } 176 177 178 182 public void formResult(String [] result) 183 { 184 if (result != null) 185 { 186 if (result[result.length-1].equals("create")) 188 { 189 StringTokenizer parser = new StringTokenizer (result[4], " "); 190 String [] args = new String [parser.countTokens()]; 191 for (int i=0 ; i<args.length ; ++i) 192 { 193 args[i] = parser.nextToken(); 194 } 195 try 196 { 197 agency.createLocalAgent( 198 result[0], 199 new org.objectweb.mobilitools.smi.api.Name( 200 System.getProperty("user.name"), 201 result[1], 202 agency.getAgentSystemInfo().agent_system_type), 203 result[2], 204 result[3], 205 new Properties (), 206 args); 207 } 208 catch (BadOperation e) 209 { 210 new AlertWindow( 211 this, 212 "Can't create agent", 213 e.getMessage(), 214 "OK"); 215 } 216 } 217 else if (result[result.length-1].equals("move")) 219 { 220 try 221 { 222 agency.moveLocalAgent( 223 new org.objectweb.mobilitools.smi.api.Name(names[namesLst.getSelectedIndex()]), 224 new Location(result[0], result[1]), 225 result[2]); 226 } 227 catch (BadOperation e) 228 { 229 new AlertWindow( 230 this, 231 "Can't move agent " + namesLst.getSelectedItem(), 232 e.getMessage() + "\n(error code " + String.valueOf(e.getReason()) + ")", 233 "OK"); 234 } 235 } 236 else if (result[result.length-1].equals("shutdown")) 238 { 239 shutdown(0); 240 } 241 } 242 } 243 244 245 249 250 public void update(Observable o, java.lang.Object arg) 251 { 252 if (arg == Agency.NOTIFY_TERMINATE) 253 { 254 dispose(); 255 } 256 else 257 { 258 updateNames(); 259 } 260 } 261 262 263 267 268 public void actionPerformed(ActionEvent e) 269 { 270 if (e.getSource() == updateBtn) 271 { 272 updateNames(); 273 } 274 else if (e.getSource() == createBtn) 275 { 276 String [] fields = { 277 "class name", 278 "agent name", 279 "place name", 280 "code base", 281 "arguments" }; 282 String [] buttons = { 283 "create", 284 "cancel" }; 285 FormFrame form = new FormFrame( 286 "Create an agent", 287 "Enter the following parameters:", 288 fields, 289 buttons); 290 form.run(this); 291 } 292 else if (e.getSource() == terminateBtn) 293 { 294 try 295 { 296 agency.terminateLocalAgent(new org.objectweb.mobilitools.smi.api.Name(names[namesLst.getSelectedIndex()])); 297 } 298 catch (BadOperation ex) 299 { 300 new AlertWindow( 301 this, 302 "Warning", 303 ex.getMessage(), 304 "OK"); 305 } 306 } 307 else if (e.getSource() == viewBtn || e.getSource() == namesLst) 308 { 309 AgentInfo info = agency.getAgentInfo( 310 new org.objectweb.mobilitools.smi.api.Name(names[namesLst.getSelectedIndex()])); 311 StringWriter strWriter = new StringWriter(); 312 info.getProperties().list(new PrintWriter(strWriter)); 313 String message = 314 "Name: " + info.getName().identity() + "\n" + 315 "Authority: " + info.getName().authority() + "\n" + 316 "Codebase: " + info.getCodebase() + "\n" + 317 "Place: " + info.getPlace() + "\n" + 318 "Activity: " + (info.isRunning() ? " running\n" : " suspended\n") + 319 strWriter.toString(); 320 AlertWindow window = new AlertWindow( 321 this, 322 "Information on agent " + namesLst.getSelectedItem(), 323 message, 324 "OK"); 325 } 326 else if (e.getSource() == moveBtn) 327 { 328 String [] fields = { "region", "agency", "place" }; 329 String [] buttons = { "move", "stay" }; 330 FormFrame moveFrame = new FormFrame( 331 "Move agent " + namesLst.getSelectedItem(), 332 "destination for agent " + namesLst.getSelectedItem(), 333 fields, 334 buttons); 335 moveFrame.run(this); 336 } 337 else if (e.getSource() == shutdownBtn) 338 { 339 String [] buttons = { 340 "shutdown", 341 "cancel" }; 342 FormFrame form = new FormFrame( 343 "Shutdown agency " + agency.getName() + "?", 344 "Please confirm killing the agency and hosted agents.", 345 new String [0], 346 buttons); 347 form.run(this); 348 } 349 else if (e.getSource() == suspendBtn) 350 { 351 try 352 { 353 agency.suspendLocalAgent(new org.objectweb.mobilitools.smi.api.Name(names[namesLst.getSelectedIndex()])); 354 } 355 catch (BadOperation ex) 356 { 357 if (ex.getReason() == BadOperation.UNKNOWNAGENT) 358 { 359 updateNames(); 360 } 361 else 362 { 363 new AlertWindow( 364 this, 365 "Warning", 366 ex.getMessage(), 367 "OK"); 368 } 369 } 370 } 371 else if (e.getSource() == resumeBtn) 372 { 373 try 374 { 375 agency.resumeLocalAgent(new org.objectweb.mobilitools.smi.api.Name(names[namesLst.getSelectedIndex()])); 376 } 377 catch (BadOperation ex) 378 { 379 if (ex.getReason() == BadOperation.UNKNOWNAGENT) 380 { 381 updateNames(); 382 } 383 else 384 { 385 new AlertWindow( 386 this, 387 "Warning", 388 ex.getMessage(), 389 "OK"); 390 } 391 } 392 } 393 } 394 395 396 400 401 public void itemStateChanged(ItemEvent e) 402 { 403 switch (e.getStateChange()) 404 { 405 case ItemEvent.SELECTED: 406 if (e.getSource() == manualChk) 407 { 408 updateBtn.setEnabled(true); 409 agency.deleteObserver(this); 410 } 411 else if (e.getSource() == namesLst) 412 { 413 bottomPnl.setEnabled(true); 414 } 415 break; 416 case ItemEvent.DESELECTED: 417 if (e.getSource() == manualChk) 418 { 419 updateBtn.setEnabled(false); 420 updateNames(); 421 agency.addObserver(this); 422 } 423 else if (e.getSource() == namesLst) 424 { 425 bottomPnl.setEnabled(false); 426 } 427 break; 428 } 429 } 430 431 432 436 437 class OnWindowClosing extends WindowAdapter 438 { 439 public void windowClosing(WindowEvent e) 440 { 441 AgencyGUI.this.shutdown(0); 442 } 443 } 444 } 445 | Popular Tags |