/** * Route Loader Panel * @author Robert J Morton * @version 04 December 2007 * @copyright Robert J Morton (all rights reserved) */ /* This applet conforms to API 1.1 It does the following: INITIALISATION SEQUENCE 1. Loads a list of names of air routes from a file located on the server, and places them into a "routes" Choice list. 2. Selects the first one in the list and loads the list of waypoints that make up a selected route. USER-INITIATED ACTIONS 1. User can select another route from the list. The new route's list of waypoints is then loaded from the server into the waypoints Choice list. 2. User can select a waypoint from the waypoint Choice list. 3. User can enter a word or phrase and search for it within all route names in the list of routes. If found, the first route's name is selected and its waypoints loaded. Pressing the Find/Next button again proceeds with the search onwards down the list of routes. */ import java.io.*; // file input/output handling import java.net.*; // for downloading data from the remote server class loader { // TO LOAD AND DISPLAY ROUTE NAMES AND GEOGRAPHIC FEATURES private static loader ld; // reference to current instance of loader class private boolean loadingNamesFTT = true, // This is the first pass of the program loadingFeatures = false, // Loading routes, TRUE = loading features namesLoaded = false, // Route names not yet loaded featuresLoaded = false; // Selected route's waypoints not yet loaded int NR = 0, // number of available routes NF[], // number of features in each route NW[], // number of these that are waypoints rn = 0, // index number of current route during loading fn = 0, // index number of current feature within current route nf = 0; // total number of geographic features in current route private int lp = 0, // 0=idle 1=connecting 2=loading 3=connect error 4=load error L = 0, // length of the remote item being loaded l = 0, // number of bytes of the above successfully downloaded m = 0; // message index private byte B[]; // gigantic byte array to hold the downloaded index data private String loadFile = "names.txt", // name of the route names file E, // for Exception during downloading + method where it occurred cb; // URL path to data files // for reading in the route and waypoint lists from files private BufferedReader R; // input stream for downloading index of current HTML file private InputStream I; // REFERENCES TO......... private movmap mm; // the parent applet class private route ROUTE; // object of current route private msgpanel mp; // current instance of the message panel class private routesel rs; // current instance of route selector class private selwp ws; // current instance of waypoint selector class private butpanel bp; // the button panel object private aircraft ac; // rthe aircraft object private airmap am; // airmap object private navpanel np; // navigation data panel object private dandb db; // rdistance and bearing calculation object private route r; // a route object private waypnt w; // a waypoint object loader(String cb, movmap mm, msgpanel mp, routesel rs, selwp ws, aircraft ac, airmap am, navpanel np, selgeoid gs, butpanel bp) { ld = this; // reference to this instance of this class this.cb = cb; // URL (less file name) from where this applet came this.mm = mm; // context reference of calling applet this.mp = mp; // message panel reference this.bp = bp; // reference to the button panel object this.rs = rs; // reference to route selector menu this.ws = ws; // reference to waypoint selector menu this.ac = ac; // reference to the aircraft object this.am = am; // reference to airmap object this.np = np; // reference to navigation data panel object db = gs.getDandB(); // get reference to the distance and bearing object lp = 1; // start the route names loading process } void loadRoute(int sr) { // LOAD THE FEATURES OF A SELECTED ROUTE featuresLoaded = false; // selected route's waypoints not yet loaded loadingFeatures = true; // state type of file being loaded // form string version of the selected route number loadFile="route" + pad(sr) + ".txt"; waypnt.killAll(); // remove all currently installed waypoints nf = NF[sr]; // number of geographic features within route fn = 0; // index of a geographic feature within route int x = NW[sr]; // number of waypoints in the selected route /* Create a new route with the required number of waypoints and clear the previous route's features from Choice Menu. */ r = new route(x,ac,bp); ws.clearWaypoints(x); lp = 1; // start the features loading process } // CONNECT TO THE APPROPRIATE DATA FILE ON THE SERVER private void fileConnect() { /* Display the 'Connecting to server...' message. The 'true' means that this is an informative message [as opposed to an error message]. The 'false' means 'not image load'. */ mp.showMsg("Connecting to server...",true,false); try { // capture any exceptions locally /* If loading from a '.jar' file, create a stream to load file from the applet's jar file. We need to set the standard file length because it is not easy to determine the content length of a jarred data file. */ if(mm.JarredRoutes) { I = getClass().getResourceAsStream(loadFile); L = 4096; } /* Otherwise, if loading directly from the remote server, create a stream to load file from remote server, use this to form the url of the route data file, open a connection to the remote file, create an input stream object to access the file and get the length of the remote file (in bytes). */ else { URL url = new URL(cb + loadFile); URLConnection u = url.openConnection(); I = u.getInputStream(); L = u.getContentLength(); } B = new byte[L]; // Create a gigantic buffer for the data l = 0; // zero the number of bytes so far downloaded lp = 2; // advance to the index loading phase } /* If any exception at all occurs, note the type of exception and where it occurred, display the exception message on the browser's status line and reset loader to its idle state. */ catch(Exception e) { E = "fileConnect() " + e; mm.showStatus(E); lp = 4; if(loadingFeatures) mp.showMsg("Couldn't find waypoints file.", false, false); else mp.showMsg("Couldn't find routes file.", false, false); } } // DOWNLOAD THE APPROPRIATE DATA FILE private void fileLoad() { if(loadingFeatures) mp.showMsg("Loading waypoints...", true, false); else mp.showMsg("Loading route names...", true, false); int k; // current byte being read() try { // if read() hasn't hit the current end of input stream /* If loading from the applet's '.jar' file, then while the entire file has not yet been downloaded, add its new byte to the big byte array. Note that, in this case, the total content length is unknown. */ if(mm.JarredRoutes) while((k=I.read()) != -1) B[l++] = (byte)k; /* Otherwise, we are loading directly from the remote server. Since the remote content length is known, while more bytes are expected add new bytes to the big byte array. */ else while(l 998) return "999"; String s = "" + x; while(s.length() < 3) s = "0" + s; return s; } // return the status of the namesLoaded flag boolean getNamesLoaded(){ return namesLoaded; } int getlp(){return lp;} void setlp(int x){lp = x;} boolean DefaultsLoaded(){return (namesLoaded && featuresLoaded);} }