package com.example.calculator.gui;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathExpressionException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import com.example.calculator.CityListModel;
import com.example.calculator.PlatformDependence;
import com.example.calculator.Settings;
import com.example.calculator.XmlFileWriter;
import org.w3c.dom.DOMException;
import org.xml.sax.SAXException;
import static java.awt.BorderLayout.CENTER;
import static java.awt.BorderLayout.SOUTH;
/**
* Created by Wambui Mustafa
* Date: 29.01.15
*/
public class SettingsFrame extends JFrame {
/** Declaration of variables **/
private JPanel dialogPane;
private JPanel contentPane;
private JLabel defaultCityLabel;
private JComboBox defaultCityComboBox;
private JTextField defaultCityTextField;
private JPanel buttonBar;
private JButton okButton;
private JButton changeButton;
private JButton closeButton;
private JTextField workingHoursTextField;
private JTextField minDayFeeTextField;
private JTextField maxDistanceTextField;
private JLabel drivingTimeLabel;
private JTextField drivingTimeTextField;
private JTextField consumptionTextField;
private ButtonGroup buttonGroup;
private JRadioButton railBonus25RadioButton;
private JRadioButton railBonus50RadioButton;
private JRadioButton railBonus100RadioButton;
private JRadioButton railBonusNoRadioButton;
private Double railBonus;
private JTextField publicDistanceTextField;
private JLabel workingHoursLabel;
private JLabel minDayFeeLabel;
private JLabel consumptionLabel;
private JLabel maxDistanceLabel;
private JLabel publicTransportLabel;
private JLabel unit1Label;
private JLabel unit2Label;
private JLabel unit3Label;
private JLabel unit4Label;
private JLabel unit5Label;
private JLabel unit6Label;
private JLabel unit7Label;
private JLabel averageTaxBurdenLabel;
private JTextField averageTaxBurdenTextField;
private CityListModel myCityModel;
private static String directory = PlatformDependence.offerValue("directory");
/** End of declaration of variables **/
/**
* Create the form of SettingsFrame
*/
public SettingsFrame() {
initComponents();
}
/**
* Components of the form
*/
private void initComponents() {
/* Initialization of the variables */
dialogPane = new JPanel();
dialogPane.setBackground(new Color(250, 219, 108));
contentPane = new JPanel();
contentPane.setBackground(new Color(250, 219, 108));
defaultCityLabel = new JLabel();
defaultCityLabel.setText("Standortstadt");
defaultCityLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
myCityModel = new CityListModel();
myCityModel.reload();
defaultCityComboBox = new JComboBox();
defaultCityComboBox.setFont(new Font("Dialog", Font.PLAIN, 11));
defaultCityComboBox.setVisible(false);
defaultCityComboBox.setEditable(true);
defaultCityComboBox.setToolTipText("Stadt des Reisebeginns. Neueingabe legt eine neue Stadt an.");
defaultCityComboBox.setModel(myCityModel);
defaultCityTextField = new JTextField();
defaultCityTextField.setFont(new Font("Dialog", Font.PLAIN, 11));
defaultCityTextField.setEnabled(false);
workingHoursLabel = new JLabel();
workingHoursLabel.setText("Arbeitsstunden pro Tag");
workingHoursLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
workingHoursTextField = new JTextField();
workingHoursTextField.setFont(new Font("Dialog", Font.PLAIN, 11));
workingHoursTextField.setToolTipText("Wieviele Stunden hat Ihr Arbeitstag?");
workingHoursTextField.setEnabled(false);
workingHoursTextField.setHorizontalAlignment(JTextField.RIGHT);
minDayFeeLabel = new JLabel();
minDayFeeLabel.setText("Min.Tageshonorar");
minDayFeeLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
minDayFeeTextField = new JTextField();
minDayFeeTextField.setFont(new Font("Dialog", Font.PLAIN, 11));
minDayFeeTextField.setToolTipText("Geben Sie hier Ihren niedrigsten Tagessatz ein.");
minDayFeeTextField.setEnabled(false);
minDayFeeTextField.setHorizontalAlignment(JTextField.RIGHT);
maxDistanceLabel = new JLabel();
maxDistanceLabel.setText("Maximalstrecke");
maxDistanceLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
maxDistanceTextField = new JTextField();
maxDistanceTextField.setFont(new Font("Dialog", Font.PLAIN, 11));
maxDistanceTextField.setToolTipText("Maximalstrecke für Zeitkarten der DB AG");
maxDistanceTextField.setEnabled(false);
maxDistanceTextField.setHorizontalAlignment(JTextField.RIGHT);
drivingTimeLabel = new JLabel();
drivingTimeLabel.setText("Fahrzeit pro Tag");
drivingTimeLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
drivingTimeTextField = new JTextField();
drivingTimeTextField.setFont(new Font("Dialog", Font.PLAIN, 11));
drivingTimeTextField.setToolTipText("Welche maximale Reisedauer pro Tag ist akzeptabel?");
drivingTimeTextField.setEnabled(false);
drivingTimeTextField.setHorizontalAlignment(JTextField.RIGHT);
consumptionLabel = new JLabel();
consumptionLabel.setText("Fahrtkosten");
consumptionLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
consumptionTextField = new JTextField();
consumptionTextField.setFont(new Font("Dialog", Font.PLAIN, 11));
consumptionTextField.setToolTipText("Berechnet Betriebskosten mit aktuellen Treibstoffpreisen und 15.000 km Laufleistung pro Jahr, Quelle www.adac.de");
consumptionTextField.setEnabled(false);
consumptionTextField.setHorizontalAlignment(JTextField.RIGHT);
buttonGroup = new ButtonGroup();
railBonus25RadioButton = new JRadioButton();
railBonus25RadioButton.setText("BahnCard25");
railBonus25RadioButton.setFont(new Font("Dialog", Font.PLAIN, 11));
railBonus25RadioButton.setBackground(new Color(250, 219, 108));
railBonus25RadioButton.setEnabled(false);
railBonus50RadioButton = new JRadioButton();
railBonus50RadioButton.setText("BahnCard50");
railBonus50RadioButton.setFont(new Font("Dialog", Font.PLAIN, 11));
railBonus50RadioButton.setBackground(new Color(250, 219, 108));
railBonus50RadioButton.setEnabled(false);
railBonus100RadioButton = new JRadioButton();
railBonus100RadioButton.setText("BahnCard100");
railBonus100RadioButton.setFont(new Font("Dialog", Font.PLAIN, 11));
railBonus100RadioButton.setBackground(new Color(250, 219, 108));
railBonus100RadioButton.setEnabled(false);
railBonusNoRadioButton = new JRadioButton();
railBonusNoRadioButton.setText("Ohne BahnCard");
railBonusNoRadioButton.setFont(new Font("Dialog", Font.PLAIN, 11));
railBonusNoRadioButton.setBackground(new Color(250, 219, 108));
railBonusNoRadioButton.setEnabled(false);
publicTransportLabel = new JLabel();
publicTransportLabel.setText("Umkreis ÖPNV");
publicTransportLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
publicDistanceTextField = new JTextField();
publicDistanceTextField.setFont(new Font("Dialog", Font.PLAIN, 11));
publicDistanceTextField.setToolTipText("In welchem Umkreis gilt noch der Tarif des ÖPNV?");
publicDistanceTextField.setEnabled(false);
publicDistanceTextField.setHorizontalAlignment(JTextField.RIGHT);
averageTaxBurdenLabel = new JLabel();
averageTaxBurdenLabel.setText("Steuerbelastung");
averageTaxBurdenLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
averageTaxBurdenTextField = new JTextField();
averageTaxBurdenTextField.setFont(new Font("Dialog", Font.PLAIN, 11));
averageTaxBurdenTextField.setToolTipText("Durchschnittssteuerbelastung");
averageTaxBurdenTextField.setEnabled(false);
averageTaxBurdenTextField.setHorizontalAlignment(JTextField.RIGHT);
unit1Label = new JLabel();
unit1Label.setText("Stunden");
unit1Label.setFont(new Font("Dialog", Font.PLAIN, 12));
unit2Label = new JLabel();
unit2Label.setText("EUR");
unit2Label.setFont(new Font("Dialog", Font.PLAIN, 12));
unit3Label = new JLabel();
unit3Label.setText("km");
unit3Label.setFont(new Font("Dialog", Font.PLAIN, 12));
unit4Label = new JLabel();
unit4Label.setText("Stunden");
unit4Label.setFont(new Font("Dialog", Font.PLAIN, 12));
unit5Label = new JLabel();
unit5Label.setText("ct/km");
unit5Label.setFont(new Font("Dialog", Font.PLAIN, 12));
unit6Label = new JLabel();
unit6Label.setText("km");
unit6Label.setFont(new Font("Dialog", Font.PLAIN, 12));
unit7Label = new JLabel();
unit7Label.setText("%");
unit7Label.setFont(new Font("Dialog", Font.PLAIN, 12));
buttonBar = new JPanel();
buttonBar.setBackground(new Color(250, 219, 108));
okButton = new JButton();
okButton.setText("Ok");
okButton.setFont(new Font("Dialog", Font.BOLD, 11));
changeButton = new JButton();
changeButton.setText("Ändern");
changeButton.setFont(new Font("Dialog", Font.BOLD, 11));
closeButton = new JButton();
closeButton.setText("Schliessen");
closeButton.setFont(new Font("Dialog", Font.BOLD, 11));
/* End of initialization of the variables */
/**
* Loading data from the config file
*/
if (new File(System.getProperties().getProperty("user.home") + File.separator + directory + File.separator + ".settings.cfg").exists()) {
Settings getValue = new Settings();
defaultCityTextField.setText(getValue.readSettings("pointOfDeparture"));
workingHoursTextField.setText(getValue.readSettings("workinghours").replace('.', ','));
minDayFeeTextField.setText(getValue.readSettings("minFee").replace('.', ','));
maxDistanceTextField.setText(getValue.readSettings("maxdistance").replace('.', ','));
consumptionTextField.setText(getValue.readSettings("consumption").replace('.', ','));
drivingTimeTextField.setText(getValue.readSettings("drivingTime").replace('.', ','));
publicDistanceTextField.setText(getValue.readSettings("publicTransport").replace('.', ','));
averageTaxBurdenTextField.setText(getValue.readSettings("averageTaxBurden").replace('.', ','));
switch (getValue.readSettings("railCard")) {
case "0.75":
railBonus25RadioButton.setSelected(true);
break;
case "0.5":
railBonus50RadioButton.setSelected(true);
break;
case "0.0":
railBonus100RadioButton.setSelected(true);
break;
default:
railBonusNoRadioButton.setSelected(true);
break;
}
} else {
workingHoursTextField.setText("8");
minDayFeeTextField.setText("400.00");
maxDistanceTextField.setText("420.00");
drivingTimeTextField.setText("2.5");
railBonusNoRadioButton.setSelected(true);
}
/** End of loading data from the config file **/
/**
* Creates the GUI-Layout
*/
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(getOwner());
setResizable(false);
setTitle("Einstellungen");
setIconImage(new ImageIcon(getClass().getResource("/resources/images16x16/currency_euro_yellow.png")).getImage());
getContentPane().setLayout(new BorderLayout());
//======== dialogPane ========
{
dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
dialogPane.setLayout(new BorderLayout());
//======== contentPane ========
{
contentPane.setLayout(new GridBagLayout());
((GridBagLayout)contentPane.getLayout()).columnWidths = new int[] {0, 55, 0, 0, 0, 0};
((GridBagLayout)contentPane.getLayout()).rowHeights = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
((GridBagLayout)contentPane.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
((GridBagLayout)contentPane.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
contentPane.add(defaultCityLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(defaultCityTextField, new GridBagConstraints(1, 0, 5, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(defaultCityComboBox, new GridBagConstraints(1, 0, 5, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(workingHoursLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(workingHoursTextField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(unit1Label, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(minDayFeeLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(minDayFeeTextField, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(unit2Label, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(maxDistanceLabel, new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(maxDistanceTextField, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(unit3Label, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(drivingTimeLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(drivingTimeTextField, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(unit4Label, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(consumptionLabel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(consumptionTextField, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(unit5Label, new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(publicTransportLabel, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(publicDistanceTextField, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(unit6Label, new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(averageTaxBurdenLabel, new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(averageTaxBurdenTextField, new GridBagConstraints(1, 7, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPane.add(unit7Label, new GridBagConstraints(2, 7, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
buttonGroup.add(railBonusNoRadioButton);
contentPane.add(railBonusNoRadioButton, new GridBagConstraints(0, 8, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
buttonGroup.add(railBonus25RadioButton);
contentPane.add(railBonus25RadioButton, new GridBagConstraints(1, 8, 2, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
buttonGroup.add(railBonus50RadioButton);
contentPane.add(railBonus50RadioButton, new GridBagConstraints(0, 9, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
buttonGroup.add(railBonus100RadioButton);
contentPane.add(railBonus100RadioButton, new GridBagConstraints(1, 9, 2, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
}
dialogPane.add(contentPane, CENTER);
//======== buttonBar ========
{
buttonBar.setBorder(new EmptyBorder(10, 0, 0, 0));
buttonBar.setLayout(new GridBagLayout());
((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 85, 80};
((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0, 0.0};
buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 10), 0, 0));
buttonBar.add(changeButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 10), 0, 0));
buttonBar.add(closeButton, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
}
dialogPane.add(buttonBar, SOUTH);
}
getContentPane().add(dialogPane, CENTER);
pack();
/** End of Creates the GUI-Layout **/
/**
* Button Listeners
*/
/* okButton */
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (railBonus25RadioButton.isSelected()) {
railBonus = 0.75;
} else if (railBonus50RadioButton.isSelected()) {
railBonus = 0.5;
} else if (railBonus100RadioButton.isSelected()) {
railBonus = 0.0;
} else if (railBonusNoRadioButton.isSelected()) {
railBonus = 1.0;
}
Settings setValue = new Settings();
setValue.generateSettings(workingHoursTextField.getText().replace(',', '.'),
(String) defaultCityComboBox.getSelectedItem(),
maxDistanceTextField.getText().replace(',', '.'),
minDayFeeTextField.getText().replace(',', '.'),
drivingTimeTextField.getText().replace(',', '.'),
consumptionTextField.getText().replace(',', '.'),
publicDistanceTextField.getText().replace(',', '.'),
averageTaxBurdenTextField.getText().replace(',', '.'),
railBonus);
if (!new File(System.getProperties().getProperty("user.home")+ File.separator + directory + File.separator + defaultCityComboBox.getSelectedItem() + "City.xml").exists()) {
XmlFileWriter city = new XmlFileWriter();
try {
city.xmlWrite((String) defaultCityComboBox.getSelectedItem());
} catch (DOMException
| XPathExpressionException | IOException | SAXException
| TransformerException ex) {
ex.printStackTrace();
}
}
Container frame = okButton.getParent();
do
frame = frame.getParent();
while (!(frame instanceof JFrame));
((JFrame) frame).dispose();
}
});
/* changeButton */
changeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
defaultCityTextField.setVisible(false);
defaultCityComboBox.setVisible(true);
defaultCityComboBox.setEnabled(true);
workingHoursTextField.setEnabled(true);
averageTaxBurdenTextField.setEnabled(true);
minDayFeeTextField.setEnabled(true);
maxDistanceTextField.setEnabled(true);
consumptionTextField.setEnabled(true);
drivingTimeTextField.setEnabled(true);
railBonus25RadioButton.setEnabled(true);
railBonus50RadioButton.setEnabled(true);
railBonus100RadioButton.setEnabled(true);
railBonusNoRadioButton.setEnabled(true);
publicDistanceTextField.setEnabled(true);
changeButton.setEnabled(false);
}
});
/* closeButton */
closeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Container frame = closeButton.getParent();
do {
frame = frame.getParent();
} while (!(frame instanceof JFrame));
((JFrame) frame).dispose();
}
});
}
/**
* Launch the application.
*/
public static void main(String[] args) {
/* Create and display the form */
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new SettingsFrame().setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
}