001         package com.croftsoft.apps.vaft.app;
002    
003         import java.awt.*;
004         import java.awt.event.*;
005         import java.io.*;
006         import java.util.*;
007    
008         import com.croftsoft.core.gui.list.*;
009         import com.orbs.open1.mit.io.Serialize;
010         import com.croftsoft.core.role.actor.Actor;
011    
012         import com.croftsoft.apps.vaft.core.*;
013         import com.croftsoft.apps.vaft.util.broadcast.*;
014    
015         /*********************************************************************
016         * <P>
017         * @author
018         *   <A HREF="http://www.alumni.caltech.edu/~croft">David W. Croft</A>
019         * @version
020         *   1998-04-26
021         *********************************************************************/
022    
023         public class  ControlPanel extends Frame
024           implements ActionListener, BroadcastListener, ItemListener,
025           WindowListener
026         //////////////////////////////////////////////////////////////////////
027         //////////////////////////////////////////////////////////////////////
028         {
029    
030         private Config    config;
031         private VAFT      vaft;
032         private TextArea  textArea;
033    
034         private MenuItem          menuItem_VAFT_Launch;
035         private MenuItem          menuItem_VAFT_Shutdown;
036         private MenuItem          menuItem_HostList_Display;
037         private MenuItem          menuItem_HostList_Update;
038         private MenuItem          menuItem_Configure_Display;
039         private CheckboxMenuItem  menuItem_Monitor_HostList;
040    
041         private DoubleListDialog  doubleListDialog;
042    
043         //////////////////////////////////////////////////////////////////////
044         //////////////////////////////////////////////////////////////////////
045    
046         public ControlPanel ( Config  config )
047         //////////////////////////////////////////////////////////////////////
048         {
049           this.config = config;
050    
051           addWindowListener ( this );
052    
053           setTitle ( config.getRmi_object_name ( ) );
054           setResizable ( true );
055    
056           // VAFT Menu
057    
058           Menu  menu_VAFT = new Menu ( "VAFT" );
059    
060           menuItem_VAFT_Launch = new MenuItem ( "Launch" );
061           menuItem_VAFT_Launch.addActionListener ( this );
062           menu_VAFT.add ( menuItem_VAFT_Launch );
063    
064           menuItem_VAFT_Shutdown = new MenuItem ( "Shutdown" );
065           menuItem_VAFT_Shutdown.addActionListener ( this );
066           menu_VAFT.add ( menuItem_VAFT_Shutdown );
067    
068           // HostList Menu
069    
070           Menu  menu_HostList = new Menu ( "HostList" );
071    
072           menuItem_HostList_Display = new MenuItem ( "Display" );
073           menuItem_HostList_Display.addActionListener ( this );
074           menu_HostList.add ( menuItem_HostList_Display );
075    
076           menuItem_HostList_Update = new MenuItem ( "Update" );
077           menuItem_HostList_Update.addActionListener ( this );
078           menu_HostList.add ( menuItem_HostList_Update );
079    
080           // Configure Menu
081    
082           Menu  menu_Configure = new Menu ( "Configure" );
083           menuItem_Configure_Display
084             = new MenuItem ( "Display" );
085           menuItem_Configure_Display.addActionListener ( this );
086           menu_Configure.add ( menuItem_Configure_Display );
087    
088           // Monitor Menu
089    
090           Menu  menu_Monitor = new Menu ( "Monitor" );
091           menuItem_Monitor_HostList = new CheckboxMenuItem ( "HostList" );
092           menuItem_Monitor_HostList.addItemListener ( this );
093           menu_Monitor.add ( menuItem_Monitor_HostList );
094    
095           MenuBar  menuBar = new MenuBar ( );
096           setMenuBar ( menuBar );
097           menuBar.add ( menu_VAFT      );
098           menuBar.add ( menu_Configure );
099           menuBar.add ( menu_HostList  );
100           menuBar.add ( menu_Monitor   );
101    
102           textArea = new TextArea ( );
103           textArea.setEditable ( false );
104           textArea.setFont ( Font.getFont ( "monospaced" ) );
105           add ( textArea );
106    
107           Toolkit  toolkit = Toolkit.getDefaultToolkit ( );
108           Dimension  dimension = toolkit.getScreenSize ( );
109           setBounds ( dimension.width / 4, dimension.height / 4,
110             dimension.width / 2, dimension.height / 2 );
111           show ( );
112    
113           display ( Defaults.INFO + "\n" + config.display ( ) );
114    
115           try
116           {
117             display ( "Starting VAFT..." );
118             vaft = new VAFT ( config );
119             monitor_HostList ( true );
120             display ( "...VAFT created and bound." );
121           }
122           catch ( Exception  ex ) { display ( ex ); }
123         }
124    
125         public synchronized void  shutdown ( )
126         //////////////////////////////////////////////////////////////////////
127         {
128           display ( "Shutdown." );
129           if ( vaft != null ) vaft.shutdown ( );
130           vaft = null;
131         }
132    
133         public synchronized void  monitor_HostList ( boolean  b )
134         //////////////////////////////////////////////////////////////////////
135         {
136           menuItem_Monitor_HostList.setState ( b );
137           HostList  hostList = vaft.getHost ( ).getHostList ( );
138           if ( b )
139           {
140             hostList.addBroadcastListener ( this );
141             display ( "Monitoring of HostList toggled on." );
142           }
143           else
144           {
145             hostList.removeBroadcastListener ( this );
146             display ( "Monitoring of HostList toggled off." );
147           }
148         }
149    
150         public void  broadcastSent ( BroadcastEvent  be )
151         //////////////////////////////////////////////////////////////////////
152         {
153           display ( be.toString ( ) );
154         }
155    
156         public void  actionPerformed ( ActionEvent  e )
157         //////////////////////////////////////////////////////////////////////
158         {
159           try
160           {
161             Object  source = e.getSource ( );
162             if ( source == menuItem_Configure_Display )
163             {
164               display ( config.display ( ) );
165             }
166             else if ( source == menuItem_HostList_Display )
167             {
168               command_HostList_Display ( );
169             }
170             else if ( source == menuItem_HostList_Update )
171             {
172               command_HostList_Update ( );
173             }
174             else if ( source == menuItem_VAFT_Launch )
175             {
176               command_Launch ( );
177             }
178             else if ( source == menuItem_VAFT_Shutdown )
179             {
180               shutdown ( );
181             }
182           }
183           catch ( Exception  ex ) { display ( ex ); }
184         }
185    
186         public void  itemStateChanged ( ItemEvent  e )
187         //////////////////////////////////////////////////////////////////////
188         {
189           try
190           {
191             if ( e.getSource ( ) == menuItem_Monitor_HostList )
192             {
193               monitor_HostList (
194                 menuItem_Monitor_HostList.getState ( ) );
195             }
196           }
197           catch ( Exception  ex ) { display ( ex ); }
198         }
199    
200         public void  windowClosing ( WindowEvent  e )
201         //////////////////////////////////////////////////////////////////////
202         {
203           try
204           {
205             shutdown ( );
206             dispose ( );
207             System.exit ( 0 );
208           }
209           catch ( Exception  ex ) { display ( ex ); }
210         }
211    
212         public void  display ( String  s )
213         //////////////////////////////////////////////////////////////////////
214         {
215           textArea.append ( s + "\n" );
216         }
217    
218         public void  display ( Exception  ex )
219         //////////////////////////////////////////////////////////////////////
220         {
221           StringWriter  stringWriter = new StringWriter ( );
222           PrintWriter  printWriter = new PrintWriter ( stringWriter );
223           ex.printStackTrace ( printWriter );
224           display ( "Exception:  " + stringWriter.toString ( ) );
225         }
226    
227         public synchronized void  command_Launch ( )
228         //////////////////////////////////////////////////////////////////////
229         {
230           if ( doubleListDialog != null ) doubleListDialog.dispose ( );
231    
232           String [ ]  items_left = fetchAgentList ( );
233    
234           Vector  list
235             = vaft.getHost ( ).getHostList ( ).getHostInfoList ( );
236           String [ ]  items_right = new String [ list.size ( ) ];
237           for ( int  i = 0; i < items_right.length; i++ )
238           {
239             items_right [ i ]
240               = ( ( HostInfo ) list.elementAt ( i ) ).toURL ( );
241           }
242    
243           Toolkit  toolkit = Toolkit.getDefaultToolkit ( );
244           Dimension  d = toolkit.getScreenSize ( );
245    
246           doubleListDialog = new DoubleListDialog (
247             this, "Launch Window", false,
248             true, true, items_left, items_right, "Launch", "Abort",
249             d.width / 4, 0, d.width / 2, d.height / 4,
250             new DoubleListDialogListener ( )
251             {
252               public synchronized void  doubleListDialogAccept (
253                 String [ ]  items_left, String [ ]  items_right )
254               {
255                 if ( ( items_left == null ) || ( items_right == null ) )
256                   return;
257                 launch ( items_left, items_right );
258               }
259             } );
260         }
261    
262         public String [ ]  fetchAgentList ( )
263         //////////////////////////////////////////////////////////////////////
264         {
265           String  agent_dir = config.getAgent_dir ( );
266           String [ ]  files = new File ( agent_dir, "." ).list ( );
267           Vector  v = new Vector ( );
268           for ( int  i = 0; i < files.length; i++ )
269           {
270             if ( files [ i ].endsWith ( ".actor" ) )
271             {
272               v.addElement ( files [ i ] );
273             }
274           }
275           String [ ]  agents = new String [ v.size ( ) ];
276           for ( int  i = 0; i < agents.length; i++ )
277           {
278             agents [ i ] = ( String ) v.elementAt ( i );
279           }
280           return agents;
281         }
282    
283         public void  launch ( String [ ]  agents, String [ ]  hosts )
284         //////////////////////////////////////////////////////////////////////
285         {
286           String  agent_dir = config.getAgent_dir ( );
287           try
288           {
289             for ( int  i = 0; i < agents.length; i++ )
290             {
291               try
292               {
293                 Actor  actor = ( Actor ) Serialize.load (
294                   agent_dir + File.separator + agents [ i ] );
295                 for ( int  j = 0; j < hosts.length; j++ )
296                 {
297                   try
298                   {
299                     display ( "Launching " + actor + " to "
300                       + hosts [ j ] + "..." );
301                     HostInfo.getHostRemote ( hosts [ j ] ).host ( actor );
302                     display ( "..." + actor + " launched to "
303                       + hosts [ j ] + "." );
304                   }
305                   catch ( Exception  ex ) { display ( ex ); }
306                 }
307               }
308               catch ( Exception  ex ) { display ( ex ); }
309             }
310           }
311           catch ( Exception  ex ) { display ( ex ); }
312         }
313    
314         public void  command_HostList_Display ( )
315         //////////////////////////////////////////////////////////////////////
316         {
317           HostList  hostList = vaft.getHost ( ).getHostList ( );
318    
319           HostInfo  self = hostList.getHostInfoSelf ( );
320           HostInfo  seed = hostList.getHostInfoSeed ( );
321           Vector    list = hostList.getHostInfoList ( );
322    
323           display ( "Self:  " + self );
324           display ( "Seed:  " + seed );
325           display ( "List:  " + list.size ( ) );
326           Enumeration  e = list.elements ( );
327           while ( e.hasMoreElements ( ) )
328           {
329             display ( "Host:  " + ( HostInfo ) e.nextElement ( ) );
330           }
331         }
332    
333         public void  command_HostList_Update ( )
334         //////////////////////////////////////////////////////////////////////
335         {
336           HostList  hostList = vaft.getHost ( ).getHostList ( );
337           hostList.swap ( );
338           command_HostList_Display ( );
339         }
340    
341         public void  windowActivated   ( WindowEvent  e ) { }
342         public void  windowClosed      ( WindowEvent  e ) { }
343         public void  windowDeactivated ( WindowEvent  e ) { }
344         public void  windowDeiconified ( WindowEvent  e ) { }
345         public void  windowIconified   ( WindowEvent  e ) { }
346         public void  windowOpened      ( WindowEvent  e ) { }
347    
348         //////////////////////////////////////////////////////////////////////
349         //////////////////////////////////////////////////////////////////////
350         }