![]() |
|||
![]() ![]() |
![]() |
|
![]() |
AirportChoice. java AirportChoice.java class queries the database to build a list of airport codes such as SFO, LAX, or JFK. The list is used within the user interface to let the user choose the departure and arrival airports. Listing 10-6 shows the source code for AirPortChoice.java. Listing 10-6: AirportChoice.java. // // AirportChoice.java - user interface widget showing a choice of airports // // Copyright (C) 1996 by Connect Software. All rights reserved. // // Written by Gionata Mettifogo, Peter Ham. // package airplet; import connect.sql.*; // import jdbc and other sql libraries import java.awt.*; // java windowing toolkit /** A choice user interface widget showing a list of available airport codes. */ class AirportChoice extends Choice { /** * Initialize the choice user interface widget with a list of airports * available in the database. The method will query the airports table * of the database, listing all available airports by code. */ public AirportChoice() throws SQLException { // use sql to select all airport codes from the airports table then add them to the widget Statement s = Airplet.createStatement(); // scan all the airports in the table for(ResultSet r = s.executeQuery("select code from airports order by code") ; r.next() ;) { String name = r.getString(1); // name of this airport // System.out.println("AirportChoice - " + r.getString("code") + " is '" + name + "'"); addItem(name); // add airport to the choices } s.close(); // close statement } /** Returns the Airport corresponding to the entry with the given index. */ public Airport getAirport(int index) { return Airport.getAirport(getItem(index)); // return Airport object } /** Returns the currently selected Airport. */ public Airport getSelectedAirport() { return getAirport(getSelectedIndex()); } } ColumnLayout. java ColumnLayout.java class arranges a set of components in a single column. The source code for this class is in Listing 10-7. Listing 10-7: ColumnLayout.java. // // ColumnLayout.java - layout that arranges all components in a column // // Copyright (C) 1996 by Connect Software. All rights reserved. // // Written by Gionata Mettifogo. // package airplet; import java.awt.*; // import java windowing classes /** A layout that arranges all components in a single column. */ public class ColumnLayout implements LayoutManager // just another layout manager { public ColumnLayout() { hgap = vgap = 0; // no gap between components } public ColumnLayout(int hgap,int vgap) { this.hgap = hgap; this.vgap = vgap; // use this spacing between components } private int hgap, vgap; // horizontal and vertical spacing between components /** Arrange components contained in iParent in a single column using their preferred size. */ public void layoutContainer(Container iParent) { Insets insets = iParent.insets(); // insets (borders around the container) Dimension dimension = iParent.size(); // size of parent container dimension.width -= insets.left + insets.right; // net width of container for(int i = 0, v = vgap ; i < iParent.countComponents() ; i++) { Component component = iParent.getComponent(i); // scan each component in the container Dimension size = component.preferredSize(); // get component's preferred size the reshape it component.reshape(insets.left,v,dimension.width - insets.left - insets.right,size.height); component.repaint(); // redraw the component v += size.height + vgap; // update vertical origin for next component } } /** Returns the minimum layout size calculated using each component's preferred size. */ public Dimension minimumLayoutSize(Container iParent) { Dimension dimension = new Dimension(0,0); for(int i = 0 ; i < iParent.countComponents() ; i++) // scan components { Component component = iParent.getComponent(i); Dimension size = component.preferredSize(); // get i-th component's size dimension.width = Math.max(dimension.width,size.width); dimension.height += size.height + vgap; // update height including this component } Insets insets = iParent.insets(); // add insets (border) dimension.width += insets.left + insets.right + 2 * hgap; dimension.height += insets.top + insets.bottom + vgap; return dimension; } /** Preferred size is just like minimum size but can be as wide as the parent component. */ public Dimension preferredLayoutSize(Container iParent) { Dimension dimension = minimumLayoutSize(iParent); dimension.width = Math.max(iParent.size().width,dimension. width); return dimension; } public void addLayoutComponent(String iName,Component iComponent) { } public void removeLayoutComponent(Component iComponent) { } } Flight.java Flight.java contains a constructor that initializes the members of the flight information and passes them as a parameter. It extracts the flight number, departure and arrival, flight frequency, and plane identification. Listing 10-8 shows the source code for this class. Listing 10-8: Flight.java. // // Flight.java - holds information regarding a flight // // Copyright (C) 1996 by Connect Software. All rights reserved. // // Written by Gionata Mettifogo, Peter Ham. // package airplet; import connect.sql.*; // import jdbc and other sql libraries class Flight { /** * Initialize flight from the information contained in the current * row of this result set. The result set is a subset of rows from * the flights table in the airline database. This method will read * information on current row (it will not call next). * * @param iFlight is a result set whose current row is a flight */ public Flight(ResultSet iFlight) throws SQLException { code = iFlight.getString("code"); // get flight number from = iFlight.getString("from_city"); to = iFlight.getString("to_city"); departure = iFlight.getTime("departure"); arrival = iFlight.getTime("arrival"); frequency = iFlight.getString("frequency"); // flight frequency (eg. which days this flight operates) plane = iFlight.getString("plane"); // airplane used } String code, from, to; // the flight code/number and city of departure/arrival, e.g., 'TWA800' Time departure, arrival; // departure and arrival time String frequency; // days when the flight is available, e.g., 123 for Mon, Tue, Wed String plane; // airplane used, e.g., "Boeing 767" public String toString() { return "Flight[" + code + "," + from + " " + departure + "," + to + " " + arrival + "," + frequency + "," + plane + "]"; } }
|
![]() |
|