/** * Route Search Panel * @author Robert J Morton YE572246C * @version 04 December 2007 * @copyright Robert J Morton (all rights reserved) */ import javax.swing.*; // Java Swing GUI utilities import java.awt.*; // Java Abstract Windowing Toolkit import java.awt.event.*; // for the new-fangled 1.1 event handling class findroute { private int SearchIndex = 0, // list number of route name we have searched up to Tx = 305, Ty = 15, // top left corner of text field on applet panel Tw = 224, Th = 35, // Text field width and height Bx = 533, By = 15, // top left corner of Search button on applet panel Bw = 72, Bh = 35; // Search button width and height private String SearchString = ""; // search string for finding a route name private JTextField FindRoute; // ref for text field for route search engine private Label FindLab; // reference for the 'Find' button's label private JButton FindBut; // button to go to next waypoint in the list private Font font, font2; // refs for large and small fonts private FontMetrics fm, fm2; // typeface dimensions for the above fonts private movmap mm; // reference to the parent applet class private routesel rs; // reference to the route selector class findroute(movmap mm, Container cp, routesel rs) { this.mm = mm; // context reference of calling applet this.rs = rs; // reference of route selector object FindRoute = new JTextField("Enter a route name."); cp.add(FindRoute); FindRoute.setBounds(Tx, Ty, Tw, Th); FindRoute.addActionListener(new frlisten(frlisten.FINDROUTE, this)); FindBut = new JButton("Search"); cp.add(FindBut); FindBut.setBounds(Bx,By,Bw,Bh); FindBut.setMargin(new Insets(3,3,3,3)); FindBut.addActionListener(new frlisten(frlisten.FINDBUT,this)); } void findRoute() { //start/resume search from SearchIndex value String s = FindRoute.getText().trim().toLowerCase(); /* If new search string is different from last time's search string, update search string and reset search index to start of list. */ if(!SearchString.equals(s)) { SearchString = s; SearchIndex = 0; } /* Get the number of route names in the list and the item after where we got to last time. */ int I = rs.getTotalRoutes(), i = SearchIndex; // For as many routes as there are in the list, get the next route name for(int k = 0; k < I; k++) { s = rs.getRouteName(i); /* If the search string was found in this route name, select the route name, mark from where to start searching next time, clear possible 'not found' message from last time and select the current route and exit. */ if(s.indexOf(SearchString) != -1) { rs.selectRouteName(i); SearchIndex = i + 1; mm.showStatus(""); rs.routeSelect(); return; } else if(++i >= I) // Otherwise loop index back i = 0; // to beginning if necessary. } mm.showStatus("Search string not found in any route name."); } } //LISTENS FOR EVENTS FROM THE BUTTONS class frlisten implements ActionListener { static final int FINDROUTE = 1, //'route search text field' event FINDBUT = 2; //'route search button pressed' event int id; //one of the above findroute ap; //the application that called: always the above panel // constructor for a new command public frlisten(int id, findroute ap) { this.id = id; this.ap = ap; } // one of the buttons has been clicked public void actionPerformed(ActionEvent e) { switch(id) { // id of this instance of ActionEvent case FINDROUTE: ap.findRoute(); // it was the 'find' field C/R break; case FINDBUT: ap.findRoute(); // it was the 'find' button } } }