/** * 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 SrchStrng = ""; // search string for finding a route name private JTextField FindRoute; // ref for a text field for route search private Label FindLab; // reference for the 'Find' button's label 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 private JButton FindBut; // button to go to the next waypoint in the list private Font font, font2; // refs for large & small fonts used in this applet findroute(movmap mm, Container cp, routesel rs, int L) { this.mm = mm; // context reference of calling applet this.rs = rs; // reference of route selector object String S = "Enter a route name."; if(L == 1) S = "Digite um nome de rota."; FindRoute = new JTextField(S); cp.add(FindRoute); FindRoute.setBounds(Tx,Ty,Tw,Th); FindRoute.addActionListener(new frlisten(frlisten.FINDROUTE,this)); S = "Search"; if(L == 1) S = "Procurar"; FindBut = new JButton(S); 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 different from last time's search string, update search string and reset search index to start of list */ if(!SrchStrng.equals(s)) { SrchStrng = s; SearchIndex = 0; } int I = rs.getTotalRoutes(), // number of route names in the list i = SearchIndex; // item after where we got to last time /* For as many routes as there are in the list, get next route name then if search string 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. */ for(int k = 0; k < I; k++) { s = rs.getRouteName(i); if(s.indexOf(SrchStrng) != -1) { rs.selectRouteName(i); SearchIndex = i + 1; System.out.println(""); rs.routeSelect(); return; } else if(++i >= I) // else loop back to the beginning i = 0; } System.out.println("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 public frlisten(int id, findroute ap) { // constructor for a new command 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(); break; // it was the 'find' field C/R case FINDBUT: ap.findRoute(); // it was the 'find' button } } }