@Override public void actionPerformed(ActionEvent e) { if ((initStationCombo == e.getSource() || destStationCombo == e.getSource()) && e.getActionCommand().equals("comboBoxChanged")) { setPriceLabel(); } else if (okButton == e.getSource()) { TrainScheduleController controller = gui.getController(); int initStation = controller.getStationId((String) initStationCombo.getSelectedItem()); int destStation = controller.getStationId((String) destStationCombo.getSelectedItem()); if (initStation == destStation) { JOptionPane.showMessageDialog( gui.getWindow(), Labels.same_stations_error, Labels.error, JOptionPane.ERROR_MESSAGE); return; } int routeId = controller.getRouteId(initStation, destStation); if (routeId < 0) { JOptionPane.showMessageDialog( gui.getWindow(), Labels.route_not_exists, Labels.error, JOptionPane.ERROR_MESSAGE); return; } if (priceLabel.getText().isEmpty() || priceLabel.getText().equals(Labels.unknown)) { JOptionPane.showMessageDialog( gui.getWindow(), Labels.price_unknown, Labels.error, JOptionPane.ERROR_MESSAGE); return; } Purchase purchase = new Purchase(); purchase.setRoute( gui.getController() .getRoute( (String) initStationCombo.getSelectedItem(), (String) destStationCombo.getSelectedItem()) .getId()); java.util.Date now = new java.util.Date(); java.sql.Date date = new java.sql.Date(now.getTime()); purchase.setDate(date); purchase.setTime(new Time(Calendar.getInstance().getTimeInMillis())); if (!controller.addPurchase(purchase)) { } else { setVisible(false); } } else if (cancelButton == e.getSource()) { setVisible(false); } }
public AddPurchaseDialog(TrainScheduleGUI gui, boolean modal) { super(gui.getWindow(), modal); this.gui = gui; setTitle(Labels.add_purchase); JPanel settingPanel = createSettingPanel(); JPanel buttonPanel = createButtonPanel(); JPanel dialogPanel = createDialogPanel(settingPanel, buttonPanel); getContentPane().add(dialogPanel); pack(); setLocationRelativeTo(gui.getWindow()); setVisible(true); }
private void setPriceLabel() { int price = gui.getController() .getPriceOfRoute( (String) initStationCombo.getSelectedItem(), (String) destStationCombo.getSelectedItem()); priceLabel.setText(price <= 0 ? Labels.unknown : (price + " Ft")); }
private JPanel createSettingPanel() { // Fill ComboBox Items List<Station> stations = gui.getController().getStations(); for (Station station : stations) { initStationCombo.addItem(station.getName()); destStationCombo.addItem(station.getName()); } destStationCombo.setSelectedIndex(1); initStationCombo.addActionListener(this); destStationCombo.addActionListener(this); JPanel settingPanel = new JPanel(); settingPanel.setLayout(new GridLayout(0, 2, 3, 10)); settingPanel.add(new JLabel(Labels.initial_station_label, SwingConstants.RIGHT)); settingPanel.add(initStationCombo); settingPanel.add(new JLabel(Labels.destination_station_label, SwingConstants.RIGHT)); settingPanel.add(destStationCombo); Calendar calendar = Calendar.getInstance(); dateLabel.setText( calendar.get(Calendar.YEAR) + ". " + (calendar.get(Calendar.MONTH) + 1) + ". " + calendar.get(Calendar.DAY_OF_MONTH) + "."); settingPanel.add(new JLabel(Labels.date_label, SwingConstants.RIGHT)); settingPanel.add(dateLabel); int min = calendar.get(Calendar.MINUTE); timeLabel.setText(calendar.get(Calendar.HOUR_OF_DAY) + ":" + (min < 9 ? "0" : "") + min); settingPanel.add(new JLabel(Labels.time_label, SwingConstants.RIGHT)); settingPanel.add(timeLabel); settingPanel.add(new JLabel(Labels.price_label, SwingConstants.RIGHT)); settingPanel.add(priceLabel); setPriceLabel(); return settingPanel; }