Skip to content

Instantly share code, notes, and snippets.

Created April 3, 2014 17:49
Show Gist options
  • Save anonymous/9959263 to your computer and use it in GitHub Desktop.
Save anonymous/9959263 to your computer and use it in GitHub Desktop.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package userInterface;
import airportmain.Airline;
import airportmain.Flight;
import airportmain.Reservation;
import inputOutput.ReadDataFiles;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
/**
*
* @author kwhiting
*/
public class SelectFlight extends JPanel
{
private JTabbedPane parentPane;
private JLabel departLabel;
private JTable departFlightTable;
private JLabel returnLabel;
private JTable returnFlightTable;
private BorderLayout borderLayout;
private FlowLayout flowLayout;
private JPanel dataPane;
private JPanel buttonPane;
private JButton select;
private JButton goBack;
private JButton cancel;
private ArrayList<Airline> airlines = new ArrayList<Airline>();
private String columnNames[] = {"Airline", "Flight", "From", "Departure", "To", "Arrival", "Airplane"};
// The data
Reservation reservation;
public SelectFlight(JTabbedPane parent, Reservation inReservation)
{
parentPane = parent;
reservation = inReservation;
initComponents();
}
private void initComponents()
{
borderLayout = new BorderLayout();
this.setLayout(borderLayout);
dataPane = new JPanel();
departFlightTable = new JTable();
returnFlightTable = new JTable();
// Initialize buttons
select = new JButton("Select Flight");
// TODO register select button with ActionListener
goBack = new JButton("Go Back");
// TODO register goBack button with ActionListener
cancel = new JButton("Cancel");
// TODO register cancel button with ActionListener
flowLayout = new FlowLayout();
buttonPane = new JPanel();
buttonPane.setLayout(flowLayout);
buttonPane.add(goBack, FlowLayout.LEFT);
buttonPane.add(select, FlowLayout.CENTER);
buttonPane.add(cancel, FlowLayout.RIGHT);
this.add(dataPane, BorderLayout.CENTER);
this.add(buttonPane, BorderLayout.SOUTH);
}
public void populateDataArrays()
{
try
{
// Populating data in JTables
URL url = getClass().getResource("Flights.csv");
File flightsFile = new File(url.toURI());
ReadDataFiles reader = new ReadDataFiles();
airlines = (ArrayList<Airline>)reader.processFile(flightsFile, new Airline());
String departCode = reservation.getDepartAirport().getAirportCode();
// TODO get the airport code from the selected return airport
DefaultTableModel departTableModel = new DefaultTableModel(columnNames, 0);
departFlightTable = new JTable(departTableModel);
// TODO create a DefaultTableModel for the return flight JTable
// TODO update object returnFlightTable setting it equal to a JTable
// instance that receives data type DefaultTableModel
for (int a = 0; a < airlines.size(); a++)
{
ArrayList<Flight> flights = new ArrayList<Flight>();
flights = airlines.get(a).getFlights();
for( int f = 0; f < flights.size(); f++)
{
if(flights.get(f).getOriginatingLocation().equals(departCode))
{
Object[] departData = new Object[columnNames.length];
departData[0] = airlines.get(a).getAirlineName();
departData[1] = flights.get(f).getflightnumber();
departData[2] = flights.get(f).getOriginatingLocation();
departData[3] = flights.get(f).getDeparture();
departData[4] = flights.get(f).getDestinationLocation();
departData[5] = flights.get(f).getArrival();
departData[6] = flights.get(f).getAirplaneType();
departTableModel.addRow(departData);
}
// TODO if the flight object originating location is equal to the
// value of the airport code from the selected return airport
// add this flight's data to the DefaultTableModel for
// return flight JTable
}
}
dataPane.add(departFlightTable.getTableHeader());
dataPane.add(departFlightTable);
// TODO add the return flight JTable's headers to the data panel
// TODO add the return flight JTable to the data panel
this.revalidate();
this.repaint();
}
catch(Exception ex)
{
ex.printStackTrace();
JOptionPane.showMessageDialog(parentPane, "An error occurred reading file Flights.csv");
}
}
private class SelectAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int departRowIndex = departFlightTable.getSelectedRow();
Airline departAirline = new Airline();
Flight departFlight = new Flight();
departAirline.setAirlineName(departFlightTable.getModel().getValueAt(departRowIndex, 0).toString());
departFlight.setFlightNumber(departFlightTable.getModel().getValueAt(departRowIndex, 1).toString());
departFlight.setOriginatingLocation(departFlightTable.getModel().getValueAt(departRowIndex, 2).toString());
departFlight.setDeparture(departFlightTable.getModel().getValueAt(departRowIndex, 3).toString());
departFlight.setDestinationLocation(departFlightTable.getModel().getValueAt(departRowIndex, 4).toString());
departFlight.setArrival(departFlightTable.getModel().getValueAt(departRowIndex, 4).toString());
departFlight.setAirplaneType(departFlightTable.getModel().getValueAt(departRowIndex, 5).toString());
departAirline.getFlights().add(departFlight);
reservation.getAirlines().add(departAirline);
// TODO get the selected row index of the selected row from the return
// flight JTable
// TODO create an instance of Airline and Flight for the return flight data
// TODO set the airline name of object Airline based on the data in the
// selected row index, column "Airline"
// TODO set the flight number of object Flight based on the data in the
// selected row index, column "Flight"
// TODO set the orignating location of object Flight based on the data in the
// selected row index, column "From"
// TODO set the departure date/time of object Flight based on the data in the
// selected row index, column "Departure"
// TODO set the destination location of object Flight based on the data in the
// selected row index, column "To"
// TODO set the arrival date/time of object Flight based on the data in the
// selected row index, column "Arrival"
// TODO set the airplane type of object Flight based on the data in the
// selected row index, column "Airplane"
// TODO add the instance of Flight to the ArrayList of Flight to the
// instance of Airline
// TODO add the instance of Airline to the ArrayList of Airline to the
// instance of Reservation
// Make sure we don't go past the tab count
// Move to next panel
if(parentPane.getSelectedIndex() < parentPane.getTabCount())
parentPane.setSelectedIndex(parentPane.getSelectedIndex() + 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment