// // AirLook.java - defines top level applet class for Airport Lookup Tool // // Copyright (c) 1996 by Philip Garnatz, Garnatz and Grovender, Inc. // All Rights Reserved // import java.awt.*; // references to Java packages used import java.net.*; import java.io.*; import java.lang.*; import java.util.*; import java.applet.Applet; // public class AirLook2 extends Applet { // member data TextField _textField; TextArea _outField; Button _button; Button cty_button; Button clr_button; // only works in appletviewer final static int _width=550, _height=350; int i = 0; int ix = 0; int ixm = 0; int lflag = 1; StringBuffer data1 = new StringBuffer(8000); StringBuffer data3 = new StringBuffer(8000); String master_i; String data_blk; String data_blk3; String file2_sav = " "; String file3_sav = " "; Color ltgreen = new Color(128, 255, 153); Color ltgold = new Color(255, 255, 204); public void init(){ resize(_width, _height); // size the app window _textField=new TextField(); // input field _textField.setText("msp"); // sample start Font f = new Font("Courier", Font.BOLD, 14); setFont(f); Color gold = new Color(255, 222, 173); Panel buttonbar = new Panel(); buttonbar.setLayout(new GridLayout(2,1)); Panel topbar = new Panel(); GridBagLayout gbl = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); topbar.setLayout(gbl); topbar.setBackground(Color.white); // text field gbc.weightx = 1; gbc.ipadx = 5; gbc.fill = GridBagConstraints.HORIZONTAL; gbl.setConstraints(_textField, gbc); topbar.add(_textField); // lookup code button _button=new Button("lookup airport code"); _button.setBackground(ltgreen); buttonbar.add(_button); // lookup city cty_button=new Button("lookup city name"); cty_button.setBackground(ltgold); buttonbar.add(cty_button); // add buttonbar to topbar gbc.weightx = 0; gbc.fill = GridBagConstraints.NONE; gbl.setConstraints(_button, gbc); gbl.setConstraints(buttonbar, gbc); topbar.add(buttonbar); // clear button clr_button = new Button("Clear"); clr_button.setBackground(gold); gbc.gridwidth = GridBagConstraints.REMAINDER; gbl.setConstraints(clr_button, gbc); topbar.add(clr_button); setLayout(new BorderLayout()); // create a layout mgr // setBackground(Color.white); setBackground(gold); add("North", topbar); _outField=new TextArea(7,80); // output lines _outField.setEditable(false); _outField.setBackground(ltgold); data1 = ReadFileURL("air/air.ix", 11); master_i = new String(data1.toString()); lookIt(); add("Center", _outField); } // end of init // called to look up a new code public void lookIt(){ String s=_textField.getText().toUpperCase(); // get user input ixm = BinarySearch(master_i, s, 11, 3, 0); String file2 = "air/air." + master_i.substring(ixm*11+5,ixm*11+7); if(!file2.equals(file2_sav)) { data1 = ReadFileURL(file2, 80); data_blk = new String(data1.toString()); file2_sav = file2; } ixm = BinarySearch(data_blk, s, 80, 3, 0); if(StrComp (data_blk.substring(ixm*80,ixm*80+3), s) == 0) { _outField.appendText (" Here is the airport for the code " + s +"\n"); _outField.appendText (data_blk.substring(ixm*80,ixm*80+80)+"\n"); } else { _outField.appendText (" No exact match found for "+ s +", here are the closest:\n"); int ixl = ixm-2; if(ixl<0) ixl = 0; int ixh = ixm+3; if(ixh > data_blk.length()/80) ixh = data_blk.length()/80; for(ix=ixl; ix 'Z' ) { _outField.appendText (" Lookup city name - first letter must be [A-Z] " + ch1 + "\n"); return; } String file3 = "air/cty." + ch1; if(!file3.equals(file3_sav)) { data3 = ReadFileURL(file3, 40); data_blk3 = new String(data3.toString().toUpperCase()); file3_sav = file3; } ixm = BinarySearch(data_blk3, s, 40, 32, 8); int ixl = ixm-2; if(ixl<0) ixl = 0; int ixh = ixm+4; if(ixh > data_blk3.length()/40) ixh = data_blk3.length()/40; if(StrComp (data_blk3.substring(ixm*40+8,ixm*40+40), s) == 0) { _outField.appendText (" Here is an airport or city code for city name: " + s +"\n"); ixl = ixm; ixh = ixm; for (ix=ixm-1; ix>0; ix-- ) { if(Math.abs(StrComp(data_blk3.substring(ix*40+8,ix*40+40), s)) == 2) break; } if( ix+1 < ixl ) ixl = ix+1; for (ix=ixm+1; ix ixh ) ixh = ix; } else { _outField.appendText (" Here are airport or city codes for the " + "closest matches to: "+ s +"\n"); } for(ix=ixl; ix nbytes ) sl = nbytes; data1.append(temp.substring(0,sl)); if( sl < nbytes ) { for( int j=sl; j ib) il = ib; ix = il * ln; int cpr = StrComp(s, blk.substring(ix+off,ix+lk+off)); if ( cpr == 0) { return il; } else { if( it == ib ) return il; if ( cpr > 0) { ib = il - 1; } else { it = il; // use it = il; don't use: it = il + 1 } } } // end while return il; } // called for string comparison public int StrComp(String r, String s){ int ll = r.length(); if( s.length() < ll ) ll = s.length(); for(int i=0; i < ll; i++) { char r1 = r.charAt(i); char s1 = s.charAt(i); if ( s1 < r1 ) return -2; if ( r1 < s1 ) return 2; } // exact match up to end of one string - check for non-trailing blanks if ( s.length() < r.length() ) { for(int i=ll; i < r.length(); i++) { char r1 = r.charAt(i); if( r1 != ' ') return -1; } } else if ( r.length() < s.length() ) { for(int i=ll; i < s.length(); i++) { char s1 = s.charAt(i); if( s1 != ' ') return 1; } } return 0; } // public boolean action(Event e_, Object arg_){ // lookIt(); // return true; // } public boolean handleEvent(Event event) { if(( event.target == _button) && (event.id == Event.ACTION_EVENT)) { _button.setBackground(ltgreen); cty_button.setBackground(ltgold); lflag = 1; lookIt(); return true; } if(( event.target == cty_button)&&(event.id == Event.ACTION_EVENT)) { _button.setBackground(ltgold); cty_button.setBackground(ltgreen); lflag = 2; lookName(); return true; } if(( event.target == clr_button) && (event.id == Event.ACTION_EVENT)) { _outField.setText(""); _textField.setText(""); return true; } if( event.target == _textField ) { if(event.id == Event.KEY_PRESS ) { if((char)event.key == '\n' ) { if(lflag == 1) { lookIt(); } else { lookName(); } return true; } } } return super.handleEvent(event); } } // end class AirLook2