KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > mobilitools > smi > goodies > AgencyGUI


1 /*
2 * MobiliTools: an implementation of the Object Management Group's
3 * Mobile Agent Facility specification.
4 * Copyright (C) 2003 France Telecom R&D
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * MobiliTools $Name: $
21 *
22 * Contact: mobilitools-smi@lists.debian-sf.objectweb.org
23 *
24 * Authors: Bruno Dillenseger
25 */

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 JavaDoc;
35 import java.util.Observable JavaDoc;
36 import java.util.Properties JavaDoc;
37 import java.util.StringTokenizer JavaDoc;
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 /**
46  * MobiliTools $Name: $, $Id: AgencyGUI.java,v 1.1.1.1 2003/03/28 14:48:06 dillense Exp $
47  * <P>
48  * GUI for SMI agency. May be run as a standalone programme which launches a new agency
49  * and its GUI, or may be used to create a GUI for an existing agency.
50 */

51 public class AgencyGUI
52     extends Frame
53     implements FormListener, Observer JavaDoc, 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     /**
65         Creates a new agency.
66         @param args must specify in this order: the agency name, and the region name.
67     */

68     static public void main(String JavaDoc[] 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     /**
86         Opens a GUI for the specified agency.
87         @param the_agency the SMI agency to browse.
88     */

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     /**
133         Updates the list of hosted agents in agency's GUI.
134     */

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 JavaDoc(names[i].identity) +
146                 " (" +
147                 new String JavaDoc(names[i].authority) +
148                 ")");
149         }
150         bottomPnl.setEnabled(false);
151     }
152
153
154     /**
155         Terminates every agent in the agency, and terminates the agency.
156         If the agency GUI has been run as a standalone programme, the
157         process exits and returns the specified code.
158         @param exitCode the exit code returned to the environment by the exiting process.
159     */

160     public void shutdown(int exitCode)
161     {
162         try
163         {
164             agency.terminate();
165         }
166         catch (Exception JavaDoc e)
167         {
168             e.printStackTrace();
169         }
170         dispose();
171         if (standalone)
172         {
173             System.exit(exitCode);
174         }
175     }
176
177
178     //////////////////////////////////////////////
179
// implementation of interface FormListener //
180
//////////////////////////////////////////////
181

182     public void formResult(String JavaDoc[] result)
183     {
184         if (result != null)
185         {
186             // creation form
187
if (result[result.length-1].equals("create"))
188             {
189                 StringTokenizer JavaDoc parser = new StringTokenizer JavaDoc(result[4], " ");
190                 String JavaDoc[] args = new String JavaDoc[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 JavaDoc(),
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             // migration form
218
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             // shutdown confirmation form
237
else if (result[result.length-1].equals("shutdown"))
238             {
239                 shutdown(0);
240             }
241         }
242     }
243
244
245     //////////////////////////////////////////
246
// Implementation of interface Observer //
247
//////////////////////////////////////////
248

249
250     public void update(Observable JavaDoc o, java.lang.Object JavaDoc arg)
251     {
252         if (arg == Agency.NOTIFY_TERMINATE)
253         {
254             dispose();
255         }
256         else
257         {
258             updateNames();
259         }
260     }
261
262
263     ////////////////////////////////////////////////
264
// Implementation of interface ActionListener //
265
////////////////////////////////////////////////
266

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 JavaDoc[] fields = {
277                 "class name",
278                 "agent name",
279                 "place name",
280                 "code base",
281                 "arguments" };
282             String JavaDoc[] 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 JavaDoc 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 JavaDoc[] fields = { "region", "agency", "place" };
329             String JavaDoc[] 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 JavaDoc[] 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 JavaDoc[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     //////////////////////////////////////////////
397
// Implementation of interface ItemListener //
398
//////////////////////////////////////////////
399

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     /////////////////////////////////////////
433
// refinement of WindowAdapter methods //
434
/////////////////////////////////////////
435

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