package aufgabe3;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import aufgabe3.Complex;
import aufgabe3.CoordinateSystem;
public class MainMenu implements ActionListener, ListSelectionListener {
//Parameter
private static final int MAIN_WIDTH = 800;
private static final int MAIN_HEIGHT = 600;
//Dateimenu
private JFileChooser fileMenu = new JFileChooser();
//Listen
private DefaultListModel listModel = new DefaultListModel();
private JList listOfNumbers = new JList(listModel);
//Textfields
private JTextField realTextField = new JTextField(10);
private JTextField imagTextField = new JTextField(10);
//Paintit
private CoordinateSystem cs = new CoordinateSystem(listModel);
//Label
private JLabel labelSumme = new JLabel("Summe: Kein Wert selectiert");
private JLabel labelProdukt = new JLabel("Produkt: Kein Wert selectiert");
public static void main(final String... rArgs) {
new MainMenu().startup();
}
private JPanel buildMainPanel() {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(buildContentPane(), BorderLayout.CENTER);
mainPanel.add(buildSidebarPane(), BorderLayout.EAST);
return mainPanel;
}
private JComponent buildContentPane() {
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(buildGraphicPanel(), BorderLayout.CENTER);
contentPane.add(buildResultPanel(), BorderLayout.SOUTH);
return contentPane;
}
private JComponent buildSidebarPane() {
JPanel sidebarPane = new JPanel();
sidebarPane.setLayout(new BorderLayout());
sidebarPane.add(buildListPanel(), BorderLayout.NORTH);
sidebarPane.add(buildControlPanel(), BorderLayout.SOUTH);
return sidebarPane;
}
/** Build listPanel
*
* @return listPanel
*/
private JPanel buildListPanel() {
//Erstellen des Panels
JPanel listPanel = new JPanel();
//Festlegen des Design Aufbaus
JScrollPane scrollPane = new JScrollPane(listOfNumbers);
scrollPane.setPreferredSize(new Dimension(240, ((2 * MAIN_HEIGHT / 3) - 10)));
listPanel.add(scrollPane);
listPanel.setPreferredSize(new Dimension(250, (2 * MAIN_HEIGHT / 3)));
listOfNumbers.addListSelectionListener(this);
return listPanel;
}
/**Eingabe und Steuerfeld
*
* @return ControlPanel
*/
private JPanel buildControlPanel() {
JPanel controlPanel = new JPanel(new GridBagLayout());
GridBagConstraints gridBagConstraints = new GridBagConstraints();
// Erstellen der Buttons und ihrer Funktion
//Load Button
JButton loadButton = new JButton("Load"); // Erstellen der Button Variablen und initialisieren der Instanz des Buttons
loadButton.setToolTipText("Liste Laden"); // Erzeugen des Tooltips
loadButton.addActionListener(this); // Anmelden dieser Instanz des Button beim für diese Instanz gültigem ActionListener
loadButton.setActionCommand("Load"); // Erzeugen der ActionListener Variablen
loadButton.setMnemonic('o'); // Erzeugen und Definieren des Hotkey
//Save Button
JButton saveButton = new JButton("Save");
saveButton.setToolTipText("Liste Speichern");
saveButton.addActionListener(this);
saveButton.setActionCommand("Save");
saveButton.setMnemonic('a');
//Eingabe Felder
controlPanel.add(realTextField);
JLabel label = new JLabel("+ j *");
controlPanel.add(imagTextField);
//Add Button
JButton addButton = new JButton("Add");
addButton.setToolTipText("Eingegebene Zahl hinzufügen");
addButton.addActionListener(this);
addButton.setActionCommand("Add");
addButton.setMnemonic('d');
//AddRandom Button
JButton addRandomButton = new JButton("AddRandom");
addRandomButton.setToolTipText("Eine Zufällige Zahl hinzufügen");
addRandomButton.addActionListener(this);
addRandomButton.setActionCommand("AddRandom");
addRandomButton.setMnemonic('R');
//Delete Button
JButton deleteButton = new JButton("Delete");
deleteButton.setToolTipText("Delete selected");
deleteButton.addActionListener(this);
deleteButton.setActionCommand("Del");
//Delete All Button
JButton deleteAllButton = new JButton("Delete All");
deleteAllButton.setToolTipText("Clear List");
deleteAllButton.addActionListener(this);
deleteAllButton.setActionCommand("DelAll");
// Textfeld für die Eingabe des Realanteils
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 1;
gridBagConstraints.gridheight = 1;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
controlPanel.add(realTextField, gridBagConstraints);
// Label für J *
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 1;
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
controlPanel.add(label, gridBagConstraints);
// Textfeld für die Eingabe des Imaginäranteils
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 1;
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 0;
controlPanel.add(imagTextField, gridBagConstraints);
// Der Addbutton
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
controlPanel.add(addButton, gridBagConstraints);
// Addrandom Button
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 1;
controlPanel.add(addRandomButton, gridBagConstraints);
// Delete selected Button
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
controlPanel.add(deleteButton, gridBagConstraints);
// Clear List Button
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 2;
controlPanel.add(deleteAllButton, gridBagConstraints);
// Load
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
controlPanel.add(loadButton, gridBagConstraints);
// Save
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 3;
controlPanel.add(saveButton, gridBagConstraints);
controlPanel.setPreferredSize(new Dimension(250, 100));
return controlPanel;
}
/**Anzeige Bereich der Ergebnisse
*
* @return graphicPanel
*/
private JComponent buildGraphicPanel() {
JComponent graphicPanel = new JPanel();
graphicPanel.setPreferredSize(new Dimension(MAIN_HEIGHT, MAIN_WIDTH));
graphicPanel.add(cs);
// graphicPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
return graphicPanel;
// return cs;
}
/**Anzeige Bereich der Ergebnisse
*
* @return resultPanel
*/
private JComponent buildResultPanel() {
JComponent resultPanel = new JPanel(new GridLayout(1, 1, 5, 0));
resultPanel.add(labelSumme);
resultPanel.add(labelProdukt);
resultPanel.setPreferredSize(new Dimension(MAIN_WIDTH, 50));
return resultPanel;
}
/**Erstellen der Menueleiste
*
* @return menuBar
*/
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
JMenuItem load = new JMenuItem("Load");
JMenuItem save = new JMenuItem("Save");
JMenuItem exit = new JMenuItem("Exit");
menuBar.add(fileMenu);
fileMenu.add(load);
load.addActionListener(this);
load.setActionCommand("Load");
load.setMnemonic('L');
fileMenu.add(save);
save.addActionListener(this);
save.setActionCommand("Save");
save.setMnemonic('S');
fileMenu.addSeparator();
fileMenu.add(exit);
exit.addActionListener(this);
exit.setActionCommand("Exit");
exit.setMnemonic('E');
return menuBar;
}
private void startup() {
JFrame frame = new JFrame("Complex Solutions");
frame.setResizable(false);
frame.setJMenuBar(createMenuBar());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(MAIN_WIDTH, MAIN_HEIGHT);
frame.add(buildMainPanel());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
fileMenu.setFileSelectionMode(JFileChooser.FILES_ONLY);
cs.setSelectedIndicesArray(listOfNumbers.getSelectedIndices());
}
@Override
public void valueChanged(ListSelectionEvent e) {
calculator();
cs.setSelectedIndicesArray(listOfNumbers.getSelectedIndices());
cs.repaint();
}
@Override
public void actionPerformed(ActionEvent arg) {
if ("Load".equals(arg.getActionCommand()))
deserialize();
else if ("Save".equals(arg.getActionCommand()))
serialize();
else if ("Add".equals(arg.getActionCommand()))
addNumber();
else if ("AddRandom".equals(arg.getActionCommand()))
addRandomNumber();
else if ("Del".equals(arg.getActionCommand()))
deleteNumbers();
else if ("DelAll".equals(arg.getActionCommand()))
deleteAllNumbers();
else if ("Exit".equals(arg.getActionCommand()))
System.exit(0);
}
private void addNumber() {
try {
double real = Double.valueOf(realTextField.getText());
double imag = Double.valueOf(imagTextField.getText());
Complex newComplexNumber = new Complex(real, imag);
if (listModel.contains(newComplexNumber) == false) {
listModel.addElement(newComplexNumber);
cs.setSelectedIndicesArray(listOfNumbers.getSelectedIndices());
cs.repaint();
} else {
JOptionPane.showMessageDialog(null,
"You can't enter the same number twice", "ERROR!",
JOptionPane.INFORMATION_MESSAGE);
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,
"You entered an invalid number", "ERROR!",
JOptionPane.INFORMATION_MESSAGE);
}
}
private void addRandomNumber(){
Complex newComplexNumber;
do{
newComplexNumber= new Complex(Math.random() * 100 - 50, Math.random() * 100 - 50);
}while (listModel.contains(newComplexNumber) == true);
listModel.addElement(newComplexNumber);
cs.setSelectedIndicesArray(listOfNumbers.getSelectedIndices());
cs.repaint();
}
private void deleteNumbers(){
int[] toRemove = listOfNumbers.getSelectedIndices();
for(int index = toRemove.length - 1; index >= 0; index--){
listModel.remove(toRemove[index]);
}
}
private void deleteAllNumbers(){
listModel.removeAllElements();
}
private void calculator(){
int[] toCalc = listOfNumbers.getSelectedIndices();
if (toCalc.length > 0){
Complex sum = new Complex(0,0);
Complex prod = new Complex(1,0);
for (int index : toCalc){
sum.add((Complex) listModel.get(index));
prod.mul((Complex) listModel.get(index));
}
labelSumme.setText("Summe:" + sum);
labelProdukt.setText("Product: " + prod);
} else {
labelSumme.setText("Summe: Kein Wert selectiert");
labelProdukt.setText("Product: Kein Wert selectiert");
}
}
private void serialize(){
FileOutputStream fs;
int dialogReturn = fileMenu.showSaveDialog(null);
if(dialogReturn == JFileChooser.APPROVE_OPTION){
try {
fs = new FileOutputStream(fileMenu.getSelectedFile());
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(listModel);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "An I/O-Exception occured", "ERROR!", JOptionPane.INFORMATION_MESSAGE);
}
}
}
private void deserialize(){
FileInputStream fs;
int dialogReturn = fileMenu.showOpenDialog(null);
if(dialogReturn == JFileChooser.APPROVE_OPTION){
try {
fs = new FileInputStream(fileMenu.getSelectedFile());
ObjectInputStream os = new ObjectInputStream(fs);
listModel = (DefaultListModel) os.readObject();
listOfNumbers.setModel(listModel);
cs.setListModel(listModel);
// cs.setSelectedIndicesArray(listOfNumbers.getSelectedIndices());
cs.repaint();
}catch (IOException e) {
JOptionPane.showMessageDialog(null, "An I/O-Exception occured\nThe File you selected is invalid, doesn't exist or you lack permisson to read it", "Error", JOptionPane.INFORMATION_MESSAGE);
}catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "ERROR", "Error", JOptionPane.INFORMATION_MESSAGE);
}
}
}
//Ende
}