001 package controller; 002 003 import model.ILambda; 004 import sysModel.ICmdFactory; 005 import sysModel.ISecurityAdapter; 006 import sysModel.SimDriver; 007 import sysModel.env.AEnvFactory; 008 import view.MBSView; 009 010 import java.io.*; 011 import java.awt.*; 012 013 /** 014 * Controller class. Main program entry point. 015 * 016 * @author Mathias Ricken 017 */ 018 public class MBSController { 019 /** 020 * Simulation control. 021 */ 022 private SimDriver _simDriver; 023 024 /** 025 * View. 026 */ 027 private MBSView _view; 028 029 /** 030 * Main program entry point. 031 */ 032 public void run() { 033 detectDrJava(); 034 035 // initialize list of environment and fish names 036 final String[] envClassNames = new String[]{ 037 "sysModel.env.BoundedEnv", 038 "sysModel.env.UnboundedEnv", 039 "sysModel.env.NoGridEnv" 040 }; 041 final String[] fishClassNames = new String[]{ 042 "model.fish.GenericFish", 043 "model.fish.SlowFish", 044 "model.fish.DarterFish", 045 "model.fish.CircleFish", 046 "model.fish.SimpleFish", 047 "model.fish.RockFish" 048 }; 049 050 // initialize model 051 _simDriver = new SimDriver(); 052 053 // initialize view and pass adapters to model 054 _view = new MBSView(new IDisplayAdapter() { 055 public void draw(Graphics2D g, Component comp, Point.Double p1, Point.Double p2) { 056 _simDriver.draw(g, comp, p1, p2); 057 } 058 059 public Dimension getDisplaySize() { 060 return _simDriver.getDisplaySize(); 061 } 062 063 public void returnHome(IScrollAdapter sa) { 064 } 065 066 public Point.Double getViewPosition(Point.Double pos) { 067 return _simDriver.getViewPosition(pos); 068 } 069 070 public Point.Double getPanDelta(Point.Double delta) { 071 return _simDriver.getPanDelta(delta); 072 } 073 }, new ISimAdapter() { 074 public void step() { 075 _simDriver.step(); 076 } 077 078 public void start() { 079 _simDriver.start(); 080 } 081 082 public void stop() { 083 _simDriver.stop(); 084 } 085 086 public void setSpeed(int speed) { 087 _simDriver.setSpeed(speed); 088 } 089 090 public void setStartLambda(ILambda startLambda) { 091 _simDriver.setStartLambda(startLambda); 092 } 093 094 public void setIterationLambda(ILambda itLambda) { 095 _simDriver.setIterationLambda(itLambda); 096 } 097 }, new IEnvAdapter() { 098 public boolean loadEnvironment(String filename) { 099 return _simDriver.loadEnvironment(filename); 100 } 101 102 public boolean saveEnvironment(String filename) { 103 return _simDriver.saveEnvironment(filename); 104 } 105 106 public boolean createEnvironment(AEnvFactory factory) { 107 return _simDriver.createEnvironment(factory); 108 } 109 110 public void setSeedLambda(ILambda seedLambda) { 111 _simDriver.setSeedLambda(seedLambda); 112 } 113 114 public void edit(Point.Double p, int button) { 115 _simDriver.edit(p, button); 116 } 117 118 public String getToolTipText(Point.Double p) { 119 return _simDriver.getToolTipText(p); 120 } 121 122 public String[] getEnvironmentClassNames() { 123 return envClassNames; 124 } 125 126 public String[] getFishClassNames() { 127 return fishClassNames; 128 } 129 130 public ICmdFactory getCmdFactory() { 131 return _simDriver.getCmdFactory(); 132 } 133 134 public ISecurityAdapter getSecurityAdapter() { 135 return _simDriver.getSecurityAdapter(); 136 } 137 }); 138 139 // set adapters connecting model to view 140 _simDriver.setAdapters(new IEditAdapter() { 141 public String getCurrentFish() { 142 return _view.getCurrentFish(); 143 } 144 145 public Color getCurrentColor() { 146 return _view.getCurrentColor(); 147 } 148 }, new ISimulationExceptionAdapter() { 149 public void handleException(Throwable t) { 150 _view.handleException(t); 151 } 152 }); 153 154 _view.setSize(500, 400); 155 _view.setVisible(true); 156 } 157 158 159 /** 160 * Java Virtual Machine entry point. 161 * 162 * @param args command line arguments 163 */ 164 public static void main(String[] args) { 165 (new MBSController()).run(); 166 } 167 168 /** 169 * Detect presence of DrJava n the classpath. 170 * Set "edu.rice.cs.cunit.drjava" property to "true" if found, to "false" otherwise. 171 * This is necessary since DrJava is incompatible with some of the security measures the simulator uses. 172 */ 173 private void detectDrJava() { 174 // Try to load it from our repository 175 String[] classPath = System.getProperty("java.class.path").split(File.pathSeparator); 176 System.setProperty("edu.rice.cs.cunit.drjava","false"); 177 for(String classPathEntry: classPath) { 178 String name = (File.separatorChar+classPathEntry).substring(classPathEntry.lastIndexOf(File.separatorChar)+2); 179 if (0 <= name.toLowerCase().indexOf("drjava")) { 180 System.setProperty("edu.rice.cs.cunit.drjava","true"); 181 break; 182 } 183 } 184 } 185 }