Y1S2-ROFAApp/src/rofaapp/MainWindow.java
2020-06-07 21:53:40 +01:00

565 lines
21 KiB
Java

package rofaapp;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
public class MainWindow extends JFrame implements ActionListener, MouseListener{
//Order Elements
private ItemOrder currentOrder;
private int currentSelected = -1;
//Main Panels
private JPanel menuPanel;
private JPanel gridPanel;
private JPanel summaryPanel;
private JPanel itemSummaryPanel;
//Image Panels
private JPanel[][] imagepanels;
//Labels
JLabel gridTotalPriceLabel;
JLabel summaryTitleLabel;
//Buttons
JButton menuAddItemButton;
JButton menuSaveButton;
JButton menuLoadButton;
JButton menuClearButton;
JButton menuSummaryButton;
/**
* Constructor that initialises the window and it's properties (such as title, size, and it's layout). Calls {@link #initPanels()} to initialise the panels shown in the grid. Also creates a new instance of the itemorder.
*/
MainWindow()
{
currentOrder = new ItemOrder();
setTitle("ROFA Furniture");
setResizable(false);
setSize(new Dimension(1280, 720));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
initPanels();
setVisible(true);
}
/**
* Initialises all four panels used in the main window (creates instances of them, their sizes and adds them to the master panel).
*/
private void initPanels()
{
/////////////////////////////
//Menu Panel initialisation//
/////////////////////////////
//Panel
menuPanel = new JPanel();
//Labels
JLabel menuTitleLabel = new JLabel("ROFC");
//Buttons
menuAddItemButton = new JButton("<html><center>Add<br>Furniture</center></html>"); //I hate html. (html looks different on linux? how cool!)
menuClearButton = new JButton("Clear All");
menuSaveButton = new JButton("Save");
menuLoadButton = new JButton("Load");
menuSummaryButton = new JButton("Summary");
//Apply Settings
//Panel
menuPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 2000, 40));
menuPanel.setPreferredSize(new Dimension(250, 0));
menuPanel.setBackground(Color.DARK_GRAY);
menuPanel.setVisible(true);
add(menuPanel, BorderLayout.LINE_START);
//Labels
menuTitleLabel.setFont(new Font(menuTitleLabel.getFont().getName(), Font.BOLD, 72));
menuTitleLabel.setForeground(Color.WHITE);
menuPanel.add(menuTitleLabel);
//Buttons
Font buttonFont = new Font(menuAddItemButton.getFont().getName(), Font.BOLD, 28);
Dimension largeButtonDimension = new Dimension(165, 90);
Dimension regularButtonDimension = new Dimension(165, 50);
//Add Item
menuAddItemButton.setFont(buttonFont);
menuAddItemButton.setPreferredSize(largeButtonDimension);
menuAddItemButton.addActionListener(this);
menuPanel.add(menuAddItemButton);
//Clear
menuClearButton.setFont(buttonFont);
menuClearButton.setPreferredSize(regularButtonDimension);
menuClearButton.addActionListener(this);
menuPanel.add(menuClearButton);
//Save
menuSaveButton.setFont(buttonFont);
menuSaveButton.setPreferredSize(regularButtonDimension);
menuSaveButton.addActionListener(this);
menuPanel.add(menuSaveButton);
//Load
menuLoadButton.setFont(buttonFont);
menuLoadButton.setPreferredSize(regularButtonDimension);
menuLoadButton.addActionListener(this);
menuPanel.add(menuLoadButton);
//Summary
menuSummaryButton.setFont(buttonFont);
menuSummaryButton.setPreferredSize(regularButtonDimension);
menuSummaryButton.addActionListener(this);
menuPanel.add(menuSummaryButton);
/////////////////////////////
//Grid Panel initialisation//
/////////////////////////////
//Main Panel
gridPanel = new JPanel();
//Image Panels
imagepanels = new JPanel[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
imagepanels[i][j] = new JPanel();
imagepanels[i][j].setPreferredSize(new Dimension(205, 205));
imagepanels[i][j].setBackground(Color.white);
}
}
//Label
gridTotalPriceLabel = new JLabel("Total Price: £0.00");
//Apply Settings
//Panel
gridPanel.setBackground(Color.black);
add(gridPanel, BorderLayout.CENTER);
//Image Panels
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
gridPanel.add(imagepanels[i][j]);
imagepanels[i][j].addMouseListener(this);
}
}
//Label
gridTotalPriceLabel.setFont(new Font(gridTotalPriceLabel.getFont().getName(), Font.BOLD, 30));
gridTotalPriceLabel.setForeground(Color.white);
gridPanel.add(gridTotalPriceLabel);
////////////////////////////////
//Summary Panel initialisation//
////////////////////////////////
//Panel
summaryPanel = new JPanel();
//Label
summaryTitleLabel = new JLabel("Summary");
//Apply Settings
//Panel
summaryPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 2000, 40));
summaryPanel.setBackground(Color.gray);
summaryPanel.setPreferredSize(new Dimension(350, 0));
add(summaryPanel, BorderLayout.LINE_END);
//Label
summaryTitleLabel.setForeground(Color.WHITE);
summaryTitleLabel.setFont(new Font(summaryTitleLabel.getFont().getName(), Font.BOLD, 32));
summaryPanel.add(summaryTitleLabel);
/////////////////////////////////////
//Item Summary Panel initialisation//
/////////////////////////////////////
//Panel
itemSummaryPanel = new JPanel();
//Label
//All
/*itemSummaryItemNameLabel = new JLabel();
itemSummaryItemImageLabel = new JLabel();
itemSummaryItemIDLabel = new JLabel();
itemSummaryItemWoodLabel = new JLabel();
itemSummaryItemPriceLabel = new JLabel();
//Chair
itemSummaryItemArmrestLabel = new JLabel();
//Desk
itemSummaryItemDrawsLabel = new JLabel();
itemSummaryItemHeightLabel = new JLabel();
itemSummaryItemWidthLabel = new JLabel();
itemSummaryItemDepthLabel = new JLabel();
//Table
itemSummaryItemChromeLabel = new JLabel();
itemSummartItemDiameterLabel = new JLabel();*/
//Apply Settings
itemSummaryPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 2000, 20));
itemSummaryPanel.setBackground(Color.gray);
itemSummaryPanel.setPreferredSize(new Dimension(350, 0));
}
/**
* Iterates through each of the panels within the {@link #gridPanel} and calls {@link #setItemPanel(JPanel, Furniture)} to update each panel with the correct image and data. After the iteration, calls {@link #updateSummaryPanel()} to finalise.
*/
private void updateItemOrder()
{
int count = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
setItemPanel(imagepanels[i][j], currentOrder.get(count));
gridTotalPriceLabel.setText("Total Price: £" + String.format("%.2f",currentOrder.getPrice()));
count++;
}
}
updateSummaryPanel();
}
/**
* Sets the given panel to the given furniture object, meaning the panel contains the icon of the given furniture.
* @param j JPanel to set.
* @param f Furniture to set.
*/
private void setItemPanel(JPanel j, Furniture f)
{
if (f != null)
{
j.removeAll();
j.add(new JLabel(f.getIcon()));
j.revalidate();
j.repaint();
}
else
{
j.removeAll();
j.revalidate();
j.repaint();
}
}
/**
* Changes the current selected, and highlighed, {@link #imagepanels}.
* @param x X coordinate of the {@link #imagepanels}.
* @param y Y coordinate of the {@link #imagepanels}.
* @param index The index of the item in the {@link #currentOrder}.
*/
private void changeSelectedPanel(int x, int y, int index)
{
//Reset all panel colours
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
imagepanels[i][j].setBackground(Color.white);
}
}
if (index == currentSelected)
{
itemSummaryPanel.removeAll();
remove(itemSummaryPanel);
add(summaryPanel, BorderLayout.LINE_END);
invalidate();
validate();
repaint();
currentSelected = -1;
return;
}
imagepanels[x][y].setBackground(Color.GRAY);
if (currentOrder.get(index) != null)
{
itemSummaryPanel.removeAll();
remove(summaryPanel);
updateItemSummaryPanel(index);
add(itemSummaryPanel, BorderLayout.LINE_END);
invalidate();
validate();
repaint();
}
currentSelected = index;
}
/**
* Updates and redraws the total price label at the bottom of the window.
*/
private void updateSummaryPanel()
{
JLabel[] itemLabels = new JLabel[8];
//Remove all previous labels.
summaryPanel.removeAll();
summaryPanel.add(summaryTitleLabel);
//Add label if not null.
for (int i = 0; i < 9; i++)
{
if (currentOrder.get(i) != null)
{
JLabel jtemp = new JLabel(currentOrder.get(i).getName() + " : £" + String.format("%.2f", currentOrder.get(i).getPrice()));
jtemp.setForeground(Color.white);
summaryPanel.add(jtemp);
}
}
}
/**
* Changes the elements within the {@link #itemSummaryPanel} depending on what the user has clicked.
* @param index The index of the panel/item within the order.
*/
private void updateItemSummaryPanel(int index)
{
//Fonts
Font titleFont = new Font(new JLabel().getFont().getName(), Font.BOLD, 38);
Font secondFont = new Font(new JLabel().getFont().getName(), Font.PLAIN, 28);
//Dimensions (Image sizes)
Dimension tableSize = new Dimension(300, 239);
Dimension deskSize = new Dimension(300, 196);
Dimension chairSize = new Dimension(200, 266);
//Get the instance of the object and depending on which one, add labels and other elements to panel.
if (currentOrder.get(index) instanceof ItemTable)
{
ItemTable it = (ItemTable)currentOrder.get(index);
//Labels
JLabel nameLabel = new JLabel(it.getName());
nameLabel.setFont(titleFont);
JLabel imageLabel = new JLabel(ItemIcon.resize(it.getIcon(), tableSize));
JLabel chromeLabel = new JLabel("Chrome: " + ((currentOrder.get(index) instanceof ItemTableChrome) ? "True" : "False"));
chromeLabel.setFont(secondFont);
JLabel woodLabel = new JLabel("Wood: " + it.getWood().toString());
woodLabel.setFont(secondFont);
JLabel diameterLabel = new JLabel("Diameter : " + it.getDiameter());
diameterLabel.setFont(secondFont);
//Add to panel
itemSummaryPanel.add(nameLabel);
itemSummaryPanel.add(imageLabel);
itemSummaryPanel.add(woodLabel);
itemSummaryPanel.add(chromeLabel);
itemSummaryPanel.add(diameterLabel);
}
else if (currentOrder.get(index) instanceof ItemDesk)
{
ItemDesk id = (ItemDesk)currentOrder.get(index);
//Labels
JLabel nameLabel = new JLabel(id.getName());
nameLabel.setFont(titleFont);
JLabel imageLabel = new JLabel(ItemIcon.resize(id.getIcon(), deskSize));
JLabel woodLabel = new JLabel("Wood: " + id.getWood().toString());
woodLabel.setFont(secondFont);
JLabel drawsLabel = new JLabel("Draws: " + id.getDraws());
drawsLabel.setFont(secondFont);
JLabel heightLabel = new JLabel("Height : " + id.getHeight());
heightLabel.setFont(secondFont);
JLabel widthLabel = new JLabel("Width: " + id.getWidth());
widthLabel.setFont(secondFont);
JLabel depthLabel = new JLabel("Depth: " + id.getDepth());
depthLabel.setFont(secondFont);
//Add to panel
itemSummaryPanel.add(nameLabel);
itemSummaryPanel.add(imageLabel);
itemSummaryPanel.add(woodLabel);
itemSummaryPanel.add(drawsLabel);
itemSummaryPanel.add(heightLabel);
itemSummaryPanel.add(widthLabel);
itemSummaryPanel.add(depthLabel);
}
else if (currentOrder.get(index) instanceof ItemChair)
{
ItemChair ic = (ItemChair)currentOrder.get(index);
//Labels
JLabel nameLabel = new JLabel(ic.getName());
nameLabel.setFont(titleFont);
JLabel imageLabel = new JLabel(ItemIcon.resize(ic.getIcon(), chairSize));
imageLabel.setFont(secondFont);
JLabel woodLabel = new JLabel("Wood: " + ic.getWood().toString());
woodLabel.setFont(secondFont);
JLabel armLabel = new JLabel("Armrests: " + ((currentOrder.get(index) instanceof ItemArmChair) ? "True" : "False"));
armLabel.setFont(secondFont);
//Add to panel
itemSummaryPanel.add(nameLabel);
itemSummaryPanel.add(imageLabel);
itemSummaryPanel.add(woodLabel);
itemSummaryPanel.add(armLabel);
}
}
public void actionPerformed(ActionEvent e)
{
//Category clicked: Button (Checks if the object clicked is of an instance of Button)
if (e.getSource() instanceof JButton)
{
if (e.getSource().equals(menuAddItemButton))
{
AddItemWindow aiw = new AddItemWindow(1, currentOrder, new TypeWood[] {TypeWood.WOOD_OAK, TypeWood.WOOD_WALNUT});
//DO CHECKS
currentOrder = aiw.getUpdatedOrder();
updateItemOrder();
}
else if (e.getSource().equals(menuClearButton))
{
//Show dialog to make sure we get confirmation of them clearing the object
if (JOptionPane.showConfirmDialog(this, "Would you like to clear all order details?", "Clear Order?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
{
//Delete all items in order.
currentOrder.deleteAll();
//If a panel is selected, de-select it.
if (currentSelected != -1)
{
changeSelectedPanel(0, 0, currentSelected);
}
}
//Update GUI elements.
updateItemOrder();
updateSummaryPanel();
}
else if (e.getSource().equals(menuLoadButton))
{
//Create file chooser for the user to select where to load
//File chooser opens in the executed directory
JFileChooser jfc = new JFileChooser(new File(".").getAbsolutePath());
if (jfc.showDialog(this, "Load") == JFileChooser.APPROVE_OPTION)
{
try
{
FileInputStream fileInput = new FileInputStream(jfc.getSelectedFile());
ObjectInputStream objectInput = new ObjectInputStream(fileInput);
currentOrder = (ItemOrder) objectInput.readObject();
objectInput.close();
fileInput.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
updateItemOrder();
}
else if (e.getSource().equals(menuSaveButton)) {
//Create file chooser for the user to select where to save
//File chooser opens in the executed directory
JFileChooser jfc = new JFileChooser(new File(".").getAbsolutePath());
if (jfc.showDialog(this, "Save") == JFileChooser.APPROVE_OPTION)
{
try {
//Create the file and object output
FileOutputStream file = new FileOutputStream(jfc.getSelectedFile().getAbsolutePath() + ".dat");
ObjectOutputStream fileOutput = new ObjectOutputStream(file);
//Write the object to the file
fileOutput.writeObject(currentOrder);
//Close the file
fileOutput.close();
file.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
else if (e.getSource().equals(menuSummaryButton))
{
//This deselects anything currently selected, and makes the item summary show.
changeSelectedPanel(0, 0, currentSelected);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getSource() instanceof JPanel)
{
int counter = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (e.getSource().equals(imagepanels[i][j]))
{
if (e.getButton() == 1)
{
//Left Click
if (currentOrder.get(counter) != null)
{
changeSelectedPanel(i, j, counter);
break;
}
else
{
JOptionPane.showMessageDialog(this, "There is no item in the selected box.", "No Item Found!", JOptionPane.INFORMATION_MESSAGE);
}
}
else if (e.getButton() == 2)
{
//Middle Click
//Edit item
if (currentOrder.get(counter) != null)
{
AddItemWindow aiw = new AddItemWindow(AddItemWindow.getTabValue(currentOrder.get(counter)), currentOrder, new TypeWood[] {TypeWood.WOOD_OAK, TypeWood.WOOD_WALNUT}, counter, currentOrder.get(counter));
currentOrder = aiw.getUpdatedOrder();
}
updateItemOrder();
}
else if (e.getButton() == 3)
{
//Right Click
//Delete item
if (currentOrder.get(counter) != null)
{
//Ask user if they want to delete the item from the order.
if (JOptionPane.showConfirmDialog(this, "Would you like to delete this order: " + currentOrder.get(counter).getName() + "?", "Clear Item?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
{
//If they press yes, delete the order.
currentOrder.del(counter);
//Deselect all panels.
changeSelectedPanel(i, j, currentSelected);
}
}
else
{
JOptionPane.showMessageDialog(this, "There is no item to delete here.", "No Item Found!", JOptionPane.INFORMATION_MESSAGE);
}
//Update GUI elements.
updateItemOrder();
//Do nothing if they don't accept the delete.
}
}
counter++;
}
}
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}