// // 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.awt.event.*; import java.net.*; import java.io.*; import java.applet.Applet; import FileBlock; // public class AirLook3 extends Applet implements ActionListener { // member data TextField _textField; TextArea _outField; Button apt_button; Button cty_button; Button clr_button; // only works in appletviewer final static int _width=550, _height=350; FileBlock master_i = new FileBlock(); FileBlock data_blk = new FileBlock(); FileBlock data_blk3 = new FileBlock(); int i = 0; int ix = 0; int ixm = 0; int lflag = 1; 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 _textField.addActionListener(this); 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 apt_button=new Button("lookup airport code"); apt_button.setBackground(ltgreen); buttonbar.add(apt_button); // lookup city cty_button=new Button("lookup city name"); cty_button.setBackground(ltgold); cty_button.addActionListener(this); apt_button.addActionListener(this); buttonbar.add(cty_button); // add buttonbar to topbar gbc.weightx = 0; gbc.fill = GridBagConstraints.NONE; gbl.setConstraints(apt_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); clr_button.addActionListener(this); 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); master_i = new FileBlock(this.getDocumentBase(), "air/air.ix", 11); 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 = master_i.BinarySearch(s, 3, 0); String file2 = "air/air." + master_i.substring(ixm, 5, 7); if(!file2.equals(data_blk.fname())) { data_blk = new FileBlock(this.getDocumentBase(), file2, 80); } ixm = data_blk.BinarySearch(s, 3, 0); if(FileBlock.StrComp (data_blk.substring(ixm, 0, 3), s) == 0) { _outField.append (" Here is the airport for the code " + s +"\n"); _outField.append (data_blk.substring(ixm, 0, 80)+"\n"); int newlen = _outField.getText().length(); _outField.select(newlen, newlen); } else { _outField.append (" 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()) ixh = data_blk.length(); for(ix=ixl; ix 'Z' ) { _outField.append (" Lookup city name - first letter must be [A-Z]: " + ch1 + "\n"); return; } String file3 = "air/cty." + ch1; if(!file3.equals(data_blk3.fname())) { data_blk3 = new FileBlock(this.getDocumentBase(), file3, 40); } ixm = data_blk3.BinarySearch( s, 32, 8); int ixl = ixm-2; if(ixl<0) ixl = 0; int ixh = ixm+4; if(ixh > data_blk3.length()) ixh = data_blk3.length(); if(FileBlock.StrComp (data_blk3.substring(ixm, 8, 40), s) == 0) { _outField.append (" 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( FileBlock.StrComp(data_blk3.substring(ix, 8, 40), s) ) == 2) break; } if( ix+1 < ixl ) ixl = ix+1; for (ix=ixm+1; ix ixh ) ixh = ix; } else { _outField.append (" 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 * lenrec; int cpr = StrComp(s, data_blk.substring(ix+off,ix+lk+off).toUpperCase()); 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 static 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 String substring(int line, int start, int end) { return data_blk.substring(line*lenrec+start, line*lenrec+end); } public String fname() { return file_sav; } public int length() { if(lenrec == 0) return 0; return data_blk.length()/lenrec; } } // end class FileBlock