/**
* My Tiny Company User Interface
* @author George Eleftherakis
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import javax.swing.*;
public class CustomerUI extends Frame implements KeyListener,
ActionListener{
CustomerBase cb;
//private TextField tfRecNo;
private TextField tfAirCompany;
private TextField tfFlightNumber;
private TextField tfFlyingFrom;
private TextField tfFlyingTo;
private TextField tfDateOfFlight;
private TextField tfTimeOfFlight;
private TextField tfFClass;
private TextField tfSClass;
private TextField tfTClass;
private int RecNo = 0;
public int ok=1;
//The constructor
public CustomerUI() {
cb = new CustomerBase();
// Create and add the menu to the frame
MenuBar mb;
Menu m1, m2;
MenuItem mi1_1, mi1_2,mi1_3, mi2_1, mi2_2, mi2_3;
mb = new MenuBar();
setMenuBar(mb);
//Build first menu in the menu bar.
m1 = new Menu("File", true);
mb.add(m1);
mi1_1 = new MenuItem("Load");
mi1_1.addActionListener(this);
m1.add(mi1_1);
mi1_2 = new MenuItem("Save");
mi1_2.addActionListener(this);
m1.add(mi1_2);
//Build second menu in the menu bar.
m2 = new Menu("Help");
mb.setHelpMenu(m2);
mi2_1 = new MenuItem("Help Topics");
mi2_1.addActionListener(this);
m2.add(mi2_1);
mi2_2 = new MenuItem("-");
m2.add(mi2_2);
mi2_3 = new MenuItem("About");
mi2_3.addActionListener(this);
m2.add(mi2_3);
//Create a panel to group the buttons, put that south
Panel pButtons = new Panel();
Button insertButton = new Button("Insert new flight");
pButtons.add(insertButton);
insertButton.addActionListener(this);
insertButton.setEnabled(false);
Button nextButton = new Button("Next");
pButtons.add(nextButton);
nextButton.addActionListener(this);
nextButton.setEnabled(false);
Button previousButton = new Button("Previous");
pButtons.add(previousButton);
previousButton.addActionListener(this);
previousButton.setEnabled(false);
Button deleteButton = new Button("Delete");
pButtons.add(deleteButton);
deleteButton.addActionListener(this);
deleteButton.setEnabled(false);
Button searchButton = new Button("Search");
pButtons.add(searchButton);
searchButton.addActionListener(this);
searchButton.setEnabled(false);
Button clearButton = new Button("Clear");
pButtons.add(clearButton);
clearButton.addActionListener(this);
clearButton.setEnabled(false);
Button exitButton = new Button("Exit");
pButtons.add(exitButton);
exitButton.addActionListener(this);
exitButton.setEnabled(false);
add(pButtons, "South");
//Create a panel to group the textFields, put that north
Panel pTextFields = new Panel();
pTextFields.setLayout(new GridLayout(9,2));
pTextFields.add(new Label("Air company: "));
tfAirCompany = new TextField("",2);
//tfAirCompany.setMaxSize(2);
pTextFields.add(tfAirCompany);
//tfAirCompany.setMaxSize(2);
tfAirCompany.setEnabled(false);
pTextFields.add(new Label("Flight number: "));
tfFlightNumber = new TextField("",3);
pTextFields.add(tfFlightNumber);
tfFlightNumber.setEnabled(false);
pTextFields.add(new Label("Flying from: "));
tfFlyingFrom = new TextField("",50);
pTextFields.add(tfFlyingFrom);
tfFlyingFrom.setEnabled(false);
pTextFields.add(new Label("Flying to: "));
tfFlyingTo= new TextField("",50);
pTextFields.add(tfFlyingTo);
tfFlyingTo.setEnabled(false);
pTextFields.add(new Label("Date of flight: "));
tfDateOfFlight= new TextField("", 50);
pTextFields.add(tfDateOfFlight);
tfDateOfFlight.setEnabled(false);
pTextFields.add(new Label("Time of flight: "));
tfTimeOfFlight= new TextField("", 50);
pTextFields.add(tfTimeOfFlight);
tfTimeOfFlight.setEnabled(false);
pTextFields.add(new Label("First Class Seats: "));
tfFClass= new TextField("", 50);
pTextFields.add(tfFClass);
tfFClass.setEnabled(false);
pTextFields.add(new Label("Second Class Seats: "));
tfSClass= new TextField("", 50);
pTextFields.add(tfSClass);
tfSClass.setEnabled(false);
pTextFields.add(new Label("Third Class Seats: "));
tfTClass= new TextField("", 50);
pTextFields.add(tfTClass);
tfTClass.setEnabled(false);
tfTClass.addKeyListener(this);
//Create a combo box with choices.
//String[] Seats =
//{ "First Class",
// "Business Class",
//"Economy Class"};
add(pTextFields, "North");
}
protected void displayInfo(KeyEvent e, String s){
//JOptionPane.showMessageDialog(null,"xa");
//You should only rely on the key char if the event
//is a key typed event.
String keyString;
int id = e.getID();
if (id == KeyEvent.KEY_TYPED) {
char c = e.getKeyChar();
keyString = "key character = '" + c + "'";
} else {
int keyCode = e.getKeyCode();
// keyString = "key code = " + keyCode
// + " ("
// + KeyEvent.getKeyText(keyCode)
// + ")";
}
int modifiers = e.getModifiersEx();
// modString = "modifiers = " + modifiers;
// tmpString = KeyEvent.getModifiersExText(modifiers);
// if (tmpString.length() > 0) {
// modString += " (" + tmpString + ")";
// } else {
// modString += " (no modifiers)";
// }
// actionString = "action key? ";
// if (e.isActionKey()) {
// actionString += "YES";
// } else {
// actionString += "NO";
// }
String locationString = "key location: ";
int location = e.getKeyLocation();
if (location == KeyEvent.KEY_LOCATION_STANDARD) {
locationString += "standard";
} else if (location == KeyEvent.KEY_LOCATION_LEFT) {
locationString += "left";
} else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
locationString += "right";
} else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
locationString += "numpad";
} else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
locationString += "unknown";
}
//Display information about the KeyEvent...
}
/** Handle the key typed event from the text field. */
public void keyTyped(KeyEvent e) {
displayInfo(e, "KEY TYPED: ");
}
/** Handle the key pressed event from the text field. */
public void keyPressed(KeyEvent e) {
displayInfo(e, "KEY PRESSED: ");
}
/** Handle the key released event from the text field. */
public void keyReleased(KeyEvent e) {
displayInfo(e, "KEY RELEASED: ");
}
//This method handles the 'captured' events
public void actionPerformed (ActionEvent evt) {
String arg = evt.getActionCommand();
//JOptionPane.showMessageDialog(null,arg);
if (arg.equals("Next")) {
//JOptionPane.showMessageDialog (null,cb.getNumberOfCustomers());
if (RecNo < cb.getNumberOfCustomers()) RecNo++; //Control the Record Number to know where I am and avoid to increase if I am at the end
Customer c = (Customer) cb.selectCustomer (RecNo); //Read the customer from the file
display (c); //Display the info
}
else if (arg.equals("Previous")){
if (RecNo > 1 ) RecNo--;
Customer c = (Customer) cb.selectCustomer (RecNo);
display (c);
}
else if (arg.equals("Delete")){
//Customer c = new Customer();
cb.removeCustomer(RecNo);
tfAirCompany.setText("");
tfFlightNumber.setText("");
JOptionPane.showMessageDialog(null,"Flight Removed");
ok=0;
}
else if (arg.equals("Insert new flight")){
//cb.saveBaseToFile("flights.dat",tfAirCompany.getText(),tfFlightNumber.getText());
//tfAirCompany.setText("");
//tfFlightNumber.setText("");
// JOptionPane.showMessageDialog(null,tfAirCompany.getText());
if ((tfAirCompany.getText().equals("")) || (tfFlightNumber.getText().equals("")) || (tfFlyingFrom.getText().equals("")) || (tfFlyingTo.getText().equals("")) || (tfDateOfFlight.getText().equals("")) || (tfTimeOfFlight.getText().equals("")) || (tfFClass.getText().equals("")) || (tfSClass.getText().equals("")) || (tfTClass.getText().equals("")))
//|| (tfFlightNumber.getText()==null))
{
JOptionPane.showMessageDialog(null,"Please complete the form!");
}
else
{
ok=0;
JOptionPane.showMessageDialog(null,"Successfully inserted!");
Customer c=new Customer();
c.setAirCompany(tfAirCompany.getText());
c.setFlightNumber(tfFlightNumber.getText());
//JOptionPane.showMessageDialog(null,c.getFirstName());
cb.addCustomer(c);
}
}
else if (arg.equals("Save")){
cb.saveBaseToFile("flights.dat",tfAirCompany.getText(),tfFlightNumber.getText());
JOptionPane.showMessageDialog(null,"Flights saved!");
ok=1;
}
else if (arg.equals("Load")){
cb.loadBaseFromFile("flights.dat");
insertButton.setEnabled(true);
nextButton.setEnabled(true);
previousButton.setEnabled(true);
exitButton.setEnabled(true);
clearButton.setEnabled(true);
searchButton.setEnabled(true);
deleteButton.setEnabled(true);
tfTClass.setEnabled(true);
tfSClass.setEnabled(true);
tfFClass.setEnabled(true);
tfTimeOfFlight.setEnabled(true);
tfDateOfFlight.setEnabled(true);
tfFlyingTo.setEnabled(true);
tfFlyingFrom.setEnabled(true);
tfFlightNumber.setEnabled(true);
tfAirCompany.setEnabled(true);
ok=1;
}
else if (arg.equals("Search"))
{
String returnValue1 = JOptionPane.showInputDialog(null,"Please enter Air Company to search:","Search",JOptionPane.QUESTION_MESSAGE);
String nm1=returnValue1;
String returnValue2 = JOptionPane.showInputDialog("Please enter Flight Number to search:");
String nm2=returnValue2;
if ((nm1.equals("")) || (nm2.equals("")))
{
JOptionPane.showMessageDialog(null,"Please insert an Air company and a Flight Number");
}
else
{
Customer c = cb.findBy(nm1,nm2);
if (c==null)
{
JOptionPane.showMessageDialog(null,"INFORMATION NOT FOUND!");
}
}
}
else if (arg.equals("Exit")){
Object[] options = {"Yes",
"No"};
if (ok==0)
{
int selection = JOptionPane.showOptionDialog(null,
"Do you want to save the new flight records and any changes you have done?", // message
"Flights", // title
JOptionPane.YES_NO_OPTION, // how many buttons
JOptionPane.QUESTION_MESSAGE, // icon
null, // used for custom Icon
options, // array of button lables
options[0]); // lable of the default button
//JOptionPane.showMessageDialog(null,"helo");
if(selection==JOptionPane.OK_OPTION)
{
//JOptionPane.showMessageDialog(null,"patithike to ok");
ok=1;
cb.saveBaseToFile("flights.dat",tfAirCompany.getText(),tfFlightNumber.getText());
JOptionPane.showMessageDialog(null,"Flights saved!");
}
// JOptionPane.showMessageDialog(null,"Flights saved!");
}
System.exit(0);
}
else if (arg.equals("Clear")){
//cb.loadBaseFromFile("customers.dat");
tfAirCompany.setText("");
tfFlightNumber.setText("");
}
else if (arg.equals("Contents") ){
JOptionPane.showMessageDialog (this, "Select File->Load to start working with the flights database. \nSelect File->Save to save the new records in the database.", "Help", JOptionPane.INFORMATION_MESSAGE );
}
else if (arg.equals("About") ){
JOptionPane.showMessageDialog (this, "Airflights database. \nby Stefanos Neofytidis", "About...", JOptionPane.INFORMATION_MESSAGE );
}
}
public void display (Customer c) {
if (c != null) {
//Integer a = new Integer(c.getAmount());
Integer b = new Integer(RecNo);
//tfFirstName.setText(c.getFirstName());
//tfLastName.setText(c.getLastName());
//JOptionPane.showMessageDialog (null,"Loaded");
tfAirCompany.setText(c.getAirCompany());
tfFlightNumber.setText(c.getFlightNumber());
//tfAirCompany.setText(b.toString());
//JOptionPane.showMessageDialog (null,b.toString());
}
}
//The main function that actually creates the UI(frame) to the screen
public static void main(String [] Args){
Frame f = new CustomerUI();
f.setTitle("Flights Information");
f.setSize(440,350);
f.setVisible(true);
// insertButton.setVisible(true);
//This method handles the 'x' button that closes a frame
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}