001 /* 002 * Main.java 003 * 004 * Developed for the "Rethinking CS101" project. See http://www.cs101.org, the 005 * CS101 homepage or email las@olin.edu. 006 * 007 * Please do not redistribute without obtaining permission. 008 */ 009 010 package nodenet; 011 012 //import com.sun.java.swing.*; 013 import javax.swing.*; 014 import java.applet.*; 015 import java.awt.event.*; 016 import java.awt.*; 017 018 import java.util.List; 019 import java.util.ArrayList; 020 021 import nodenet.registrar.UniqueNodeNamePolicy; 022 import nodenet.registrar.NodeNameRegistry; 023 import nodenet.registrar.NodeNameRegistrar; 024 025 /** 026 * Main defines the <code>public static void main(String[])</code> 027 * method. It can also be run as an applet.<p> 028 * 029 * Copyright (c) 1998 Massachusetts Institute of Technology 030 * 031 * @author Todd C. Parnell, tparnell@ai.mit.edu 032 * @author Patrick G. Heck, gus.heck@olin.edu 033 * @version $Id: Main.java,v 1.9 2004/01/14 21:43:17 gus Exp $ 034 */ 035 public class Main extends JApplet { 036 037 private NodeNetFrame me; 038 039 public static List loadedBehaviors = new ArrayList(); 040 /** 041 * Starts a new simulation. All arguments are treated as 042 * NodeBehaviors and will be available to the user. 043 */ 044 public static void main(String[] args) { 045 046 // process the args and load any nodeNameRegistar classes we 047 // find. If we find more than one we will get an illegal state 048 // exception which should not be caught. 049 050 for (int i=0; i<args.length;i++){ 051 try { 052 NodeNameRegistry.getInstance(). 053 setPolicy((NodeNameRegistrar)Class.forName(args[i]).newInstance()); 054 } catch (ClassCastException cce) { 055 //This case is for classes that were found but were not 056 // of type NodeNameRegistrar. Save them since they may 057 // be behavior classes. 058 loadedBehaviors.add(args[i]); 059 } catch (ClassNotFoundException cnfe) { 060 System.out.println("Specified class name not found:"+args[i]); 061 } catch (InstantiationException ie) { 062 System.out.println("Failed to instantiate:"+args[i]); 063 } catch (IllegalAccessException iae) { 064 iae.printStackTrace(); 065 System.exit(1); 066 } 067 } 068 069 // if we didn't find a registrar on the command line use the 070 // default UniqueNodeNamePolicy 071 if (!NodeNameRegistry.getInstance().isPolicySet()) { 072 System.out.println("Defaulting to UniqueNodeNamePolicy to " + 073 "control node naming"); 074 075 NodeNameRegistry.getInstance().setPolicy(new UniqueNodeNamePolicy()); 076 } 077 078 // Now put the behaviors back in an array so we can pass it to 079 // NodeNetFrame 080 if (loadedBehaviors.size() > 0) { 081 args = new String[loadedBehaviors.size()]; 082 Object[] holder = loadedBehaviors.toArray(); 083 for(int i=0; i<loadedBehaviors.size();i++) { 084 args[i] = (String) holder[i]; 085 } 086 } else { 087 args = new String[] {}; 088 } 089 090 // set the look and feel... 091 //( preexisting code: I might can this later for simplicity sake) 092 try { 093 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 094 } catch (ClassNotFoundException cnfe) { 095 // stick with the default if we can't find it. 096 } catch (InstantiationException ie) { 097 // stick witht he defualt if it seems to be broken 098 } catch (IllegalAccessException iae) { 099 // stick with the default if we arn't allowed to load it. 100 } catch (UnsupportedLookAndFeelException ulafe) { 101 // stick with the default if the jvm won't support the system LaF 102 } 103 104 // and finally build our frame passing in the remaining 105 // command line arguments. 106 NodeNetFrame nnf = new NodeNetFrame(args); 107 } 108 109 /** 110 * Initilization as an applet. 111 */ 112 public void init() { 113 NodeNameRegistry.getInstance().setPolicy(new UniqueNodeNamePolicy()); 114 Button b = new Button("Show/Hide"); 115 String[] foo = new String[1]; 116 foo[0] = "nodenet.IntermediateNodeBehavior"; 117 this.me = new NodeNetFrame(foo); 118 b.addActionListener(new ActionListener() { 119 public void actionPerformed(ActionEvent evt) { 120 if (Main.this.me.isVisible()) 121 Main.this.me.setVisible(false); 122 else Main.this.me.show(); 123 } 124 }); 125 this.getContentPane().add(b); 126 } 127 } 128 129 /* 130 * $Log: Main.java,v $ 131 * Revision 1.9 2004/01/14 21:43:17 gus 132 * more javadoc, plus reformat 133 * 134 * Revision 1.8 2004/01/14 20:23:21 gus 135 * Javadoc and comment cleanup 136 * 137 * Revision 1.7 2004/01/07 18:32:47 gus 138 * Ripped out ControlPanel and NodeNetRadioButton. 139 * Replaced them with NodeTypeSelector, which provides 140 * The loaded User NodeBehavior types as an indexed 141 * property. Simulation panel now implements PropertyChangeListener 142 * to listen for changes in the selected node behavior. The 143 * buttons in the display are now JToggleButtons, and will 144 * use the public field BEHAVIOR_NAME to lable the buttons 145 * if it is declared for any NodeBehavior. 146 * 147 * Revision 1.6 2004/01/06 18:44:25 gus 148 * Make the names of the loaded behaviors available 149 * 150 * Revision 1.5 2003/02/25 15:23:34 gus 151 * fix class cast exception with casting of arrays. 152 * Add comments to illucidate the main method. 153 * 154 * Revision 1.4 2003/02/21 18:07:49 gus 155 * Search the commandline for an implementation of NodeNameRegistrar, and use it 156 * implement our naming policy for NodeNameRegistry, then pass any arguments 157 * that were loadable classes but not NodeNameRegistrars to the constructor of 158 * NodeNetFrame. 159 * 160 * Also, cleaned up the horribly ugly catch(Exception e) 161 * 162 * Revision 1.3 2002/08/20 15:39:18 gus 163 * corrected package name 164 * 165 * Revision 1.2 2002/06/13 19:32:43 gus 166 * Commented out com.sun.java.swing package references, and updated obsolete 167 * imorts and references into cs101 package. 168 * 169 * Revision 1.1 2002/06/13 17:33:21 gus 170 * Moved all java files into the nodenet directory (who let them out anyway?) 171 * 172 * Revision 1.1.1.1 2002/06/05 21:56:35 root 173 * CS101 comes to Olin finally. 174 * 175 * Revision 1.4 2000/05/09 06:03:54 mharder 176 * Changed packagename from nodeNet to nodenet. 177 * 178 * Revision 1.3 1999/08/04 09:08:52 jsmthng 179 * Added javadoc comments to InputChannelVector and OutputChannelVector; 180 * finished updating the rest of the nodeNet package to reflect new 181 * changes in name and code. 182 * 183 * Modified index.html to reflect the new nodeNet code, as well as to 184 * clarify some parts of the problem set. 185 * 186 * Revision 1.1 1999/07/30 01:09:22 jsmthng 187 * Renaming BinSort package to nodeNet; moving directories and files as 188 * necessary. 189 * 190 * Revision 1.6 1999/01/21 20:57:06 tparnell 191 * it worked 192 * 193 * Revision 1.5 1998/08/14 16:17:43 tparnell 194 * now appearing in applet version 195 * 196 * Revision 1.4 1998/08/12 19:29:36 tparnell 197 * Another pass after comments from las & natashao. Added support to 198 * dynamically add NodeBehaviors. Add keyboard shortcuts to menus. Added 199 * workaround to jdk bug relating to lighweight components. Misc other 200 * bugfixes. 201 * 202 * Revision 1.3 1998/08/10 17:45:50 tparnell 203 * Revision to use JDK1.2 and Swing. Redesign of GUI. Removed old kludge 204 * for file I/O and replaced with object serialization. Channel no longer 205 * requires animacy. Removed unnessary dependencies between classes. 206 * Added ability to configure channel's latency and capacity. Added 207 * javadoc to all files. General code cleanup. 208 * 209 */