KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.*;
32 import javax.swing.table.AbstractTableModel JavaDoc;
33 import javax.swing.event.TableModelEvent JavaDoc;
34 import java.awt.*;
35 import java.awt.event.*;
36 import java.io.*;
37 import java.util.Observer JavaDoc;
38 import java.util.Observable JavaDoc;
39 import java.util.StringTokenizer JavaDoc;
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 JavaDoc;
44 import org.omg.CORBA.Any JavaDoc;
45 import org.omg.CfMAF.AgentProfile;
46 import org.omg.CfMAF.AgentSystemInfo;
47
48
49 /**
50  * MobiliTools $Name: $, $Id: FinderGUI.java,v 1.1.1.1 2003/03/28 14:48:06 dillense Exp $
51  * <P>
52  * GUI for SMI Finder. May be run as a standalone programme which launches a new finder
53  * and its GUI, or may be used to create a GUI for an existing finder.
54 */

55 public class FinderGUI
56     extends JFrame
57     implements Observer JavaDoc
58 {
59     static boolean standalone = false;
60     static String JavaDoc[] columnNames = { "Name", "Location" };
61
62
63     /**
64         Creates a new finder with a GUI.
65         @param args the last argument must specify the region name.
66     */

67     static public void main(String JavaDoc[] args)
68     {
69         ORB JavaDoc 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 JavaDoc[][] data;
78     TableSorter tableSorter;
79     MyTableModel tableModel;
80     JTable table;
81     RegionManager region;
82
83
84     /**
85         Opens a GUI for the specified Finder.
86         @param finder the SMI finder to browse.
87     */

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 JavaDoc[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         // table.setPreferredScrollableViewportSize(new Dimension(500, 70));
111

112         //Create the scroll pane and add the table to it.
113
JScrollPane scrollPane = new JScrollPane(table);
114
115         //Add the scroll pane to this window.
116
getContentPane().add(scrollPane, BorderLayout.CENTER);
117
118         pack();
119         setVisible(true);
120     }
121
122
123     /**
124         Terminates the GUI.
125         If FinderGUI was launched as a standalone programme, the attached MAFFinder is terminated,
126         and the programme exits.
127     */

128     public void quit()
129     {
130         dispose();
131         if (standalone)
132         {
133             System.exit(my_finder.exit());
134         }
135     }
136
137
138     //////////////////////////////////////////
139
// Implementation of interface Observer //
140
//////////////////////////////////////////
141

142
143     public void update(Observable JavaDoc o, java.lang.Object JavaDoc arg)
144     {
145         FinderEntry[] entries = (FinderEntry[])arg;
146         data = new Object JavaDoc[entries.length][];
147         for (int i=0 ; i<data.length ; ++i)
148         {
149             data[i] = new Object JavaDoc[] { entries[i], entries[i].getLocation() };
150         }
151         tableSorter.tableChanged(new TableModelEvent JavaDoc(tableModel));
152         table.clearSelection();
153     }
154
155
156     ///////////////////////////////////////
157
// refinement of class WindowAdapter //
158
///////////////////////////////////////
159

160
161     class OnWindowClosing extends WindowAdapter
162     {
163         public void windowClosing(WindowEvent e)
164         {
165             FinderGUI.this.quit();
166         }
167     }
168
169
170     /////////////////////////////////////////////////////////
171
// inner class for managing double-clicks on the table //
172
/////////////////////////////////////////////////////////
173

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     ////////////////////////////////////////////////////
196
// inner class for giving the table a table model //
197
////////////////////////////////////////////////////
198

199
200     class MyTableModel extends AbstractTableModel JavaDoc {
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 JavaDoc getColumnName(int col) {
210             return FinderGUI.columnNames[col];
211         }
212
213         public Object JavaDoc getValueAt(int row, int col) {
214             return FinderGUI.this.data[row][col];
215         }
216     }
217
218
219     ///////////////////////////////////////////////////
220
// inner class for viewing and managing an agent //
221
///////////////////////////////////////////////////
222

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 JavaDoc 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 JavaDoc 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 JavaDoc[] { "region", "agency", "place" },
291                     new String JavaDoc[] { "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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc[] 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 JavaDoc 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     //////////////////////////////////////////////////////////
356
// inner class for viewing and managing an agent system //
357
//////////////////////////////////////////////////////////
358

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 JavaDoc[] {
402                         "class name",
403                         "agent name",
404                         "place name",
405                         "code base",
406                         "arguments" },
407                     new String JavaDoc[] { "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 JavaDoc 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 JavaDoc[] result)
426         {
427             if (result[result.length-1].equals("create"))
428             {
429                 StringTokenizer JavaDoc parser = new StringTokenizer JavaDoc(result[4], " ");
430                 String JavaDoc[] args = new String JavaDoc[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 JavaDoc[0]),
457                         null,
458                         args);
459                 }
460                 catch (Exception JavaDoc 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