package com.hk.server;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Calendar;
import javax.swing.*;
import com.hk.base.FlightDAO;
import com.hk.model.*;
public class AddFlightPlan extends JDialog implements ActionListener {
JLabel[] labs;
JTextField[] texts;
JCheckBox[] checks;
JComboBox jcb;
JButton ok, cancel;
JPanel jp1, jp2, jp3;
JPanel[] jps;
String[] str1 = { "添加航班计划", "航班号:", "出发地:", "目的地:", "起飞时间:", "时", "分",
"到达时间:", "时", "分", "班期:", "开始日期:", "年", "月", "日", "结束日期:", "年",
"月", "日", "总里程", "公里", "全价", "元", "执行机型" };
String[] str2 = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
public AddFlightPlan(JFrame f) {
super(f, "添加航班计划", true);
labs = new JLabel[str1.length]; //创建标签数组
texts = new JTextField[15]; //文本框数组
checks = new JCheckBox[7]; //复选项数组
ok = new JButton("添加"); //添加按钮
cancel = new JButton("取消"); //取消按钮
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jps = new JPanel[11];
for (int i = 0; i < jps.length; i++) {
jps[i] = new JPanel();
jps[i].setLayout(new FlowLayout(FlowLayout.LEFT));
}
for (int i = 0; i < labs.length; i++) {
labs[i] = new JLabel(str1[i]);
}
for (int i = 0; i < texts.length; i++) {
texts[i] = new JTextField(10);
}
texts[3].setColumns(2);
texts[4].setColumns(2);
texts[5].setColumns(2);
texts[6].setColumns(2);
texts[7].setColumns(4);
texts[8].setColumns(2);
texts[9].setColumns(2);
texts[10].setColumns(4);
texts[11].setColumns(2);
texts[12].setColumns(2);
for (int i = 0; i < checks.length; i++) {
checks[i] = new JCheckBox(str2[i], true);
}
jcb = new JComboBox(ServerMainClass.allPlaneModels.toArray()); //获取全部机型
init();
addEventHandle();
}
private void init() {
jp1.add(labs[0]);// 标题
this.add(jp1, BorderLayout.NORTH);
jps[0].add(labs[1]);
jps[0].add(texts[0]);
jps[1].add(labs[2]);
jps[1].add(texts[1]);
jps[2].add(labs[3]);
jps[2].add(texts[2]);
jps[3].add(labs[4]);
jps[3].add(texts[3]);
jps[3].add(labs[5]);
jps[3].add(texts[4]);
jps[3].add(labs[6]);
jps[4].add(labs[7]);
jps[4].add(texts[5]);
jps[4].add(labs[8]);
jps[4].add(texts[6]);
jps[4].add(labs[9]);
jps[5].add(labs[10]);
for (int i = 0; i < checks.length; i++) {
jps[5].add(checks[i]);
}
jps[6].add(labs[11]);
jps[6].add(texts[7]);
jps[6].add(labs[12]);
jps[6].add(texts[8]);
jps[6].add(labs[13]);
jps[6].add(texts[9]);
jps[6].add(labs[14]);
jps[7].add(labs[15]);
jps[7].add(texts[10]);
jps[7].add(labs[16]);
jps[7].add(texts[11]);
jps[7].add(labs[17]);
jps[7].add(texts[12]);
jps[7].add(labs[18]);
jps[8].add(labs[19]);
jps[8].add(texts[13]);
jps[8].add(labs[20]);
jps[9].add(labs[21]);
jps[9].add(texts[14]);
jps[9].add(labs[22]);
jps[10].add(labs[23]);
jps[10].add(jcb);
jp2.setLayout(new GridLayout(11, 1));
for (int i = 0; i < jps.length; i++) {
jp2.add(jps[i]);
}
this.add(jp2, BorderLayout.CENTER);
jp3.add(ok);
jp3.add(cancel);
this.add(jp3, BorderLayout.SOUTH);
}
private void addEventHandle() { //添加事件方法
ok.addActionListener(this); //为添加按钮注册事件
cancel.addActionListener(this); //为取消按钮注册事件
}
public void showMe() {
this.setSize(600, 400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("取消")) { //如果单击取消按钮
this.dispose(); //关闭对话框
} else if (e.getActionCommand().equals("添加")) { //如果单击添加按钮
FlightSchedular fs = new FlightSchedular(texts[0].getText()); //创建航班计划对象
fs.setFromAddress(texts[1].getText()); //设置出发地
fs.setToAddress(texts[2].getText()); //设置目的地
fs.setFromTime(new MyTime(Integer.parseInt(texts[3].getText()),
Integer.parseInt(texts[4].getText()))); //设置起飞时间
fs.setToTime(new MyTime(Integer.parseInt(texts[5].getText()),
Integer.parseInt(texts[6].getText()))); //设置到达时间
fs.setSchedular(getSch()); //设置班期
Calendar startcal = Calendar.getInstance();
startcal.set(Calendar.YEAR, Integer.parseInt(texts[7].getText()));
startcal.set(Calendar.MONTH, Integer.parseInt(texts[8].getText()));
startcal.set(Calendar.DATE, Integer.parseInt(texts[9].getText()));
fs.setStartDate(startcal); //设置开始日期
Calendar endcal = Calendar.getInstance();
endcal.set(Calendar.YEAR, Integer.parseInt(texts[10].getText()));
endcal.set(Calendar.MONTH, Integer.parseInt(texts[11].getText()));
endcal.set(Calendar.DATE, Integer.parseInt(texts[12].getText()));
fs.setEndDate(endcal); //设置结束日期
fs.setLength(Integer.parseInt(texts[13].getText())); //设置航程
fs.setPrice(Double.parseDouble(texts[14].getText())); //设置全价
fs.setPlane((PlaneModel) jcb.getSelectedItem()); //设置机型
try {
FlightDAO f = ServerMainClass.flightDaoSch; //获取航班计划DAO
boolean success = f.addFlightSchedular(fs); //保存航班计划对象
if (success) {
JOptionPane.showMessageDialog(this, "添加航班计划成功!");
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "添加航班计划失败!");
this.dispose();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
//解析星期表
private byte getSch() {
String str = "";
for (int i = 0; i < checks.length; i++) {
if (checks[i].isSelected())
str = str + 1;
else
str = str + 0;
}
return (byte) Integer.parseInt(str, 2);
}
private void addFLT(FlightSchedular fs) {
FlightDAO f = ServerMainClass.flightDaoSch;
Calendar cal = fs.getStartDate();
while (cal.compareTo(fs.getEndDate()) <= 0) { //开始日期小于结束日期
byte wd = (byte) cal.get(Calendar.DAY_OF_WEEK);
byte i = 1;
i = (byte) (i << (7 - wd));
if ((((byte) i) & (fs.getSchedular())) != 0) {
Flight fl = fs.createNewFlight(cal);
boolean b = f.addFlight(fl);
if (b) {
System.out.println("OK");
}
}
cal.add(Calendar.DATE,1);
}
}
}
评论0