Advertisement
Guest User

Morten

a guest
Nov 24th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.22 KB | None | 0 0
  1. package gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.*;
  6. import javax.swing.*;
  7. import javax.swing.border.*;
  8. import javax.swing.event.*;
  9. import model.*;
  10. import service.Service;
  11.  
  12. public class TruckDriverArrivalFrame extends JFrame {
  13.  
  14. private static final long serialVersionUID = -7764496032756629451L;
  15.  
  16. private JPanel contentPane;
  17. private JSeparator separator, separator_1, separator_2;
  18. private JLabel lblDriverInfo, lblEnterSuborderID, lblSuborderResultList,
  19. lblErrorLabel, lblPhonenumber, lblHoursOfRest, lblGrossweight,
  20. lblCargotype, lblGrossWeightResult, lblCargotypeResult,
  21. lblEmptyWeight, lblOrderId, lblSuborderinfo, lblLicensePlate,
  22. lblCustomer, lblCustomerResult;
  23. private JTextField txtHours, txtMinutes, txtPhoneNumber, txtSuborderID,
  24. txtEmptyWeight, txtLicensePlate;
  25. private JButton btnConfirm, btnAddSuborder;
  26. private JList<Suborder> lstSuborders;
  27. private Controller controller = new Controller();
  28. private JScrollPane scpSuborders;
  29. private ArrayList<Suborder> enteredSuborders = new ArrayList<Suborder>();
  30.  
  31. public TruckDriverArrivalFrame() {
  32. setIconImage(Toolkit.getDefaultToolkit().getImage(
  33. TruckDriverArrivalFrame.class.getResource("/gui/icon.jpg")));
  34. setTitle("Arrival Terminal");
  35.  
  36. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37. setBounds(100, 100, 340, 378);
  38. contentPane = new JPanel();
  39. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  40. setContentPane(this.contentPane);
  41. contentPane.setLayout(null);
  42.  
  43. // LABELS
  44. lblEnterSuborderID = new JLabel("Enter Suborder ID(s):");
  45. lblEnterSuborderID.setFont(new Font("Verdana", Font.BOLD, 10));
  46. lblEnterSuborderID.setBounds(10, 11, 134, 14);
  47. contentPane.add(lblEnterSuborderID);
  48.  
  49. lblSuborderResultList = new JLabel("Suborders for order:");
  50. lblSuborderResultList.setFont(new Font("Verdana", Font.BOLD, 10));
  51. lblSuborderResultList.setBounds(10, 69, 167, 14);
  52. contentPane.add(lblSuborderResultList);
  53.  
  54. lblDriverInfo = new JLabel("Driver Information:");
  55. lblDriverInfo.setFont(new Font("Verdana", Font.BOLD, 10));
  56. lblDriverInfo.setBounds(10, 200, 134, 14);
  57. contentPane.add(lblDriverInfo);
  58.  
  59. lblPhonenumber = new JLabel("Phonenumber:");
  60. lblPhonenumber.setFont(new Font("Verdana", Font.PLAIN, 10));
  61. lblPhonenumber.setBounds(10, 218, 98, 14);
  62. contentPane.add(lblPhonenumber);
  63.  
  64. lblHoursOfRest = new JLabel("Hours of Rest:");
  65. lblHoursOfRest.setFont(new Font("Verdana", Font.PLAIN, 10));
  66. lblHoursOfRest.setBounds(133, 218, 98, 14);
  67. contentPane.add(lblHoursOfRest);
  68.  
  69. lblGrossweight = new JLabel("Gross Weight:");
  70. lblGrossweight.setFont(new Font("Verdana", Font.PLAIN, 10));
  71. lblGrossweight.setBounds(182, 115, 80, 14);
  72. contentPane.add(lblGrossweight);
  73.  
  74. lblCargotype = new JLabel("Cargotype:");
  75. lblCargotype.setFont(new Font("Verdana", Font.PLAIN, 10));
  76. lblCargotype.setBounds(182, 130, 80, 14);
  77. contentPane.add(lblCargotype);
  78.  
  79. lblGrossWeightResult = new JLabel("N/A");
  80. lblGrossWeightResult.setFont(new Font("Verdana", Font.ITALIC, 10));
  81. lblGrossWeightResult.setBounds(270, 115, 46, 14);
  82. contentPane.add(lblGrossWeightResult);
  83.  
  84. lblCargotypeResult = new JLabel("N/A");
  85. lblCargotypeResult.setFont(new Font("Verdana", Font.ITALIC, 10));
  86. lblCargotypeResult.setBounds(270, 130, 46, 14);
  87. contentPane.add(lblCargotypeResult);
  88.  
  89. lblErrorLabel = new JLabel("");
  90. lblErrorLabel.setFont(new Font("Verdana", Font.ITALIC, 10));
  91. lblErrorLabel.setForeground(Color.RED);
  92. lblErrorLabel.setBounds(10, 312, 219, 14);
  93. contentPane.add(lblErrorLabel);
  94.  
  95. lblEmptyWeight = new JLabel("Empty Weight:");
  96. lblEmptyWeight.setFont(new Font("Verdana", Font.PLAIN, 10));
  97. lblEmptyWeight.setBounds(232, 218, 89, 16);
  98. contentPane.add(lblEmptyWeight);
  99.  
  100. lblOrderId = new JLabel("");
  101. lblOrderId.setFont(new Font("Verdana", Font.BOLD | Font.ITALIC, 10));
  102. lblOrderId.setBounds(276, 69, 62, 16);
  103. contentPane.add(lblOrderId);
  104.  
  105. lblSuborderinfo = new JLabel("Information:");
  106. lblSuborderinfo.setFont(new Font("Verdana", Font.BOLD, 10));
  107. lblSuborderinfo.setBounds(182, 87, 98, 16);
  108. contentPane.add(lblSuborderinfo);
  109.  
  110. lblLicensePlate = new JLabel("License Plate:");
  111. lblLicensePlate.setFont(new Font("Verdana", Font.PLAIN, 10));
  112. lblLicensePlate.setBounds(10, 258, 98, 14);
  113. contentPane.add(lblLicensePlate);
  114.  
  115. lblCustomer = new JLabel("Customer:");
  116. lblCustomer.setFont(new Font("Verdana", Font.PLAIN, 10));
  117. lblCustomer.setBounds(182, 101, 80, 14);
  118. contentPane.add(lblCustomer);
  119.  
  120. lblCustomerResult = new JLabel("N/A");
  121. lblCustomerResult.setFont(new Font("Verdana", Font.ITALIC, 10));
  122. lblCustomerResult.setBounds(270, 101, 46, 14);
  123. contentPane.add(lblCustomerResult);
  124.  
  125. // TEXTFIELDS
  126. txtSuborderID = new JTextField();
  127. txtSuborderID
  128. .setToolTipText("Please enter the Suborder IDs here. You can only enter one ID at a time");
  129. txtSuborderID.setBounds(10, 30, 160, 28);
  130. contentPane.add(this.txtSuborderID);
  131. txtSuborderID.setColumns(10);
  132.  
  133. txtPhoneNumber = new JTextField();
  134. txtPhoneNumber.setToolTipText("Please enter phonenumber here");
  135. txtPhoneNumber.setBounds(10, 234, 111, 20);
  136. contentPane.add(txtPhoneNumber);
  137. txtPhoneNumber.setColumns(10);
  138.  
  139. txtHours = new JTextField();
  140. txtHours.setToolTipText("Please enter the amount of hours");
  141. txtHours.setBounds(133, 234, 27, 20);
  142. contentPane.add(txtHours);
  143. txtHours.setColumns(10);
  144.  
  145. txtEmptyWeight = new JTextField();
  146. txtEmptyWeight.setBounds(232, 234, 79, 20);
  147. contentPane.add(txtEmptyWeight);
  148. txtEmptyWeight.setColumns(10);
  149.  
  150. txtLicensePlate = new JTextField();
  151. txtLicensePlate.setBounds(10, 274, 114, 20);
  152. contentPane.add(txtLicensePlate);
  153. txtLicensePlate.setColumns(10);
  154.  
  155. txtMinutes = new JTextField();
  156. txtMinutes.setToolTipText("Please enter the amount of minutes");
  157. txtMinutes.setBounds(168, 234, 35, 20);
  158. contentPane.add(txtMinutes);
  159. txtMinutes.setColumns(10);
  160.  
  161. // SEPERATORS
  162. separator = new JSeparator();
  163. separator.setBounds(10, 83, 302, 2);
  164. contentPane.add(separator);
  165.  
  166. separator_1 = new JSeparator();
  167. separator_1.setBounds(10, 214, 302, 2);
  168. contentPane.add(separator_1);
  169.  
  170. separator_2 = new JSeparator();
  171. separator_2.setBounds(10, 298, 311, 2);
  172. contentPane.add(separator_2);
  173.  
  174. // BUTTONS
  175. btnAddSuborder = new JButton("Get Order(s)");
  176. btnAddSuborder.setFont(new Font("Verdana", Font.BOLD, 10));
  177. btnAddSuborder.addActionListener(controller);
  178. btnAddSuborder.setBounds(182, 30, 132, 28);
  179. contentPane.add(this.btnAddSuborder);
  180.  
  181. btnConfirm = new JButton("Confirm");
  182. btnConfirm.setMnemonic('C');
  183. btnConfirm.addActionListener(controller);
  184. btnConfirm.setFont(new Font("Tahoma", Font.BOLD, 11));
  185. btnConfirm.setBounds(225, 308, 89, 23);
  186. contentPane.add(btnConfirm);
  187.  
  188. // LISTS AND SCROLLPANES
  189. lstSuborders = new JList<Suborder>();
  190. lstSuborders.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  191. lstSuborders.addListSelectionListener(controller);
  192.  
  193. scpSuborders = new JScrollPane(lstSuborders);
  194. scpSuborders.setBounds(10, 90, 160, 100);
  195. contentPane.add(scpSuborders);
  196.  
  197. }
  198.  
  199. private class Controller implements ActionListener, ListSelectionListener {
  200.  
  201. public void actionPerformed(ActionEvent e) {
  202. if (e.getSource() == btnAddSuborder) {
  203. lblErrorLabel.setText("");
  204. String suborderId = txtSuborderID.getText();
  205. Suborder so = Service.getSuborder(suborderId);
  206. if (so == null) {
  207. lblErrorLabel
  208. .setText("There is no Suborder with the specific ID");
  209. } else if (so.getLoading() != null) {
  210. lblErrorLabel
  211. .setText("The loading has already been created");
  212. } else if (enteredSuborders.contains(so)) {
  213. lblErrorLabel.setText("The Suborder is already entered");
  214. } else {
  215. enteredSuborders.add(so);
  216. txtSuborderID.setText("");
  217. System.out.println(enteredSuborders);
  218. lstSuborders.setListData(enteredSuborders
  219. .toArray(new Suborder[0]));
  220. }
  221. }
  222.  
  223. if (e.getSource() == btnConfirm) {
  224. if (enteredSuborders.size() == 0) {
  225. lblErrorLabel.setText("No Suborder(s) have been selected!");
  226. } else {
  227. String phoneNumber = txtPhoneNumber.getText();
  228. Driver driver = Service
  229. .getDriver(phoneNumber);
  230. if (driver == null) {
  231. lblErrorLabel
  232. .setText("The driver does not exists!");
  233. } else {
  234. Calendar cal = Calendar.getInstance();
  235. cal.setTime(new Date());
  236. if (txtHours.getText().length() == 0) {
  237. lblErrorLabel
  238. .setText("You must inform of your hours of rest");
  239. } else {
  240. int earliestTimeOfNoticeHours = Integer
  241. .parseInt(txtHours.getText());
  242. if (txtMinutes.getText().length() == 0) {
  243. lblErrorLabel
  244. .setText("You must inform of your hours of rest");
  245. } else {
  246. int earliestTimeOfNoticeMinutes = Integer
  247. .parseInt(txtMinutes.getText());
  248. cal.add(Calendar.HOUR, earliestTimeOfNoticeHours);
  249. cal.add(Calendar.MINUTE,
  250. earliestTimeOfNoticeMinutes);
  251. Date earliestTimeOfNotice = cal.getTime();
  252. String emptyweight = txtEmptyWeight.getText();
  253. if (txtEmptyWeight.getText().length() == 0) {
  254. lblErrorLabel
  255. .setText("Empty weight is a requirement!");
  256. } else {
  257. double inWeight = Double
  258. .parseDouble(emptyweight);
  259. String licensePlate = txtLicensePlate.getText();
  260. if (licensePlate.length() == 0) {
  261. lblErrorLabel
  262. .setText("A license plate for the trailer is required");
  263. } else {
  264. Trailer trailer = Service
  265. .getTrailer(licensePlate);
  266. if (trailer != null) {
  267. lblErrorLabel
  268. .setText("This trailer is already assigned to a suborder");
  269. } else {
  270. Loading loading = Service
  271. .createLoading(
  272. inWeight,
  273. earliestTimeOfNotice,
  274. trailer, driver,
  275. enteredSuborders);
  276. int ramp = loading.getRamp()
  277. .getRampNumber();
  278. if (loading.getRamp()
  279. .getLoadingQueue().size() > 0) {
  280. JOptionPane
  281. .showMessageDialog(
  282. TruckDriverArrivalFrame.this,
  283. "Welcome to Danish Crown. Please go to the parking lot");
  284. } else {
  285. JOptionPane
  286. .showMessageDialog(
  287. TruckDriverArrivalFrame.this,
  288. "Welcome to Danish Crown. Please go to ramp "
  289. + ramp);
  290. }
  291. System.out.println(loading);
  292. TruckDriverArrivalFrame.this
  293. .dispose();
  294. }
  295. }
  296. }
  297. }
  298. }
  299. }
  300. }
  301. }
  302. }
  303.  
  304. public void valueChanged(ListSelectionEvent e) {
  305. if (e.getSource() == lstSuborders) {
  306. if (!e.getValueIsAdjusting()
  307. && lstSuborders.getSelectedIndex() != -1) {
  308. lblCustomerResult.setText(lstSuborders.getSelectedValue()
  309. .getOrder().getCustomer().getCompanyName());
  310. lblGrossWeightResult.setText(""
  311. + lstSuborders.getSelectedValue().getWeight());
  312. lblOrderId.setText(lstSuborders.getSelectedValue()
  313. .getOrder().getOrderid());
  314. lblCargotypeResult.setText(""
  315. + lstSuborders.getSelectedValue().getCargoType());
  316. }
  317. }
  318. }
  319. }
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement