Files
project-unisa-for-shop-mana…/it/unisa/info13d/Gui/UserInterface.java

390 lines
13 KiB
Java

package it.unisa.info13d.Gui;
import com.sun.deploy.panel.JavaPanel;
import it.unisa.info13d.Articoli.Utilizzabile;
import it.unisa.info13d.GestioneCatalogo.Catalogo;
import it.unisa.info13d.Login.Access;
import it.unisa.info13d.Login.LoginData;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.Callable;
/**
* Created with MONSTER.
* User: xgiovio
* Date: 19/01/14
* Time: 19.19
*/
public class UserInterface extends JFrame{
public UserInterface( LoginWindow in_login_window ,Catalogo in_load_catalogo, LoginData in_login_data) throws FileNotFoundException,IOException,ClassNotFoundException{
login_window = in_login_window;
load_catalogo= in_load_catalogo;
login_data = in_login_data;
//////////////////////////////////////////////////// begin main menu and balance
JPanel mainWindow = new JPanel();
mainWindow.setLayout(new BorderLayout());
add(mainWindow);
JPanel north = new JPanel();
north.setLayout(new BorderLayout() );
JMenuBar mainmenu = new JMenuBar();
north.add(mainmenu,BorderLayout.NORTH);
JMenu filemenu = new JMenu("File");
JMenuItem logout_menu_item = new JMenuItem("Logout");
class logout_action implements ActionListener {
public logout_action (UserInterface in_location){
location= in_location;
}
@Override
public void actionPerformed(ActionEvent e) {
location.setVisible(false);
login_window.setVisible(true);
}
private UserInterface location;
}
logout_menu_item.addActionListener(new logout_action(this));
JMenuItem quit_menu_item = new JMenuItem("Quit");
class exit_action implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
quit_menu_item.addActionListener(new exit_action());
JMenu searchmenu = new JMenu("Cerca");
JMenuItem tutti_gli_articoli_attivi = new JMenuItem("Tutti gli articoli Attivi");
class tutti_gli_articoli_attivi_action implements ActionListener {
public tutti_gli_articoli_attivi_action (UserInterface in_location, JTextArea in_offers){
location= in_location;
offers = in_offers;
}
@Override
public void actionPerformed(ActionEvent e) {
try{
offers.setText("");
load_catalogo.offerteAttive(login_data.getUsername(),offers);
}
catch (FileNotFoundException err){}
catch (IOException err){}
catch (ClassNotFoundException err){}
}
private UserInterface location;
private JTextArea offers;
}
//action listner for tutti_gli_articoli_attivi added at offers declaration
JMenuItem articoli_con_scadenza_menu_item = new JMenuItem("Articoli con Scadenza");
JMenuItem articoli_senza_scadenza_menu_item = new JMenuItem("Articoli senza Scadenza");
JMenu visualizzamenu = new JMenu("Visualizza");
JMenuItem storico_menu_item = new JMenuItem("Storico Utente");
mainmenu.add(filemenu);
mainmenu.add(searchmenu);
mainmenu.add(visualizzamenu);
filemenu.add(logout_menu_item);
filemenu.add(quit_menu_item);
searchmenu.add(tutti_gli_articoli_attivi);
searchmenu.add(articoli_con_scadenza_menu_item);
searchmenu.add(articoli_senza_scadenza_menu_item);
visualizzamenu.add(storico_menu_item);
JPanel BilancioPanel = new JPanel();
JLabel bilancio = new JLabel("Bilancio" + " " + login_data.getUsername() + " = " + (Access.get_user_data(login_data.getUsername())).getBalance() );
bilancio.setPreferredSize(new Dimension(150,60));
JButton acquista_credito = new JButton("Acquista Credito");
BilancioPanel.add(bilancio);
BilancioPanel.add(acquista_credito);
class acquista_action implements ActionListener {
public acquista_action (UserInterface in_location, JLabel in_l){
l = in_l;
location= in_location;
}
@Override
public void actionPerformed(ActionEvent e) {
try{
String importo_da_ricaricare= JOptionPane.showInputDialog(location,"Inserire l'importo da ricaricare ");
if (Double.parseDouble(importo_da_ricaricare) > 0){
load_catalogo.aggiungiCredito(login_data.getUsername(), importo_da_ricaricare );
l.setText("Bilancio" + " " + login_data.getUsername() + " = " + (Access.get_user_data(login_data.getUsername())).getBalance());
}
}
catch (FileNotFoundException err){}
catch (IOException err){}
catch (ClassNotFoundException err){}
catch (NumberFormatException err){
JOptionPane.showMessageDialog(location,
"Input Non valido ",
"Error",
JOptionPane.ERROR_MESSAGE);
}
catch (NullPointerException err){
JOptionPane.showMessageDialog(location,
"Input Non valido ",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
private JLabel l;
private UserInterface location;
}
acquista_credito.addActionListener(new acquista_action(this,bilancio));
north.add(BilancioPanel,BorderLayout.CENTER);
mainWindow.add(north,BorderLayout.NORTH);
//////////////////////////////////////////////////// begin center area for all offers
JTextArea offers = new JTextArea(500,200);
offers.setText("Utilizza il menu Cerca per popolare questa lista");
mainWindow.add(offers,BorderLayout.CENTER);
tutti_gli_articoli_attivi.addActionListener(new tutti_gli_articoli_attivi_action(this,offers));
JPanel acquista_panel = new JPanel();
JLabel idlabel = new JLabel("ID");
JTextField id = new JTextField(5);
JButton aggiungi_a_carrello = new JButton("Aggiungi al Carrello");
JLabel prodotti_nel_carello = new JLabel(" Corrello: " + Carrello.size() + " Prodotti");
JButton acquista = new JButton("Completa Acquisto");
class aggiungi_al_carrello_action implements ActionListener {
public aggiungi_al_carrello_action (UserInterface in_location, JTextField in_id, JTextArea in_offers, JLabel in_count){
id = in_id;
location= in_location;
offers = in_offers;
count = in_count;
}
@Override
public void actionPerformed(ActionEvent e) {
try{
if (offers.getText().isEmpty()){
JOptionPane.showMessageDialog(location,
"Dovresti prima aggiornare l'elenco delle offerte attive ",
"Error",
JOptionPane.ERROR_MESSAGE);
} else{
if (Integer.parseInt(id.getText()) >= 0 ){
int found = load_catalogo.search_product_id(login_data.getUsername(),id.getText());
if (found > -1){
Carrello.add(load_catalogo.getCatalogo().get(found));
count.setText(" Corrello: " + Carrello.size() + " Prodotti");
}else{
JOptionPane.showMessageDialog(location,
"Impossibile aggiungere il prodotto indicato ",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}else {
JOptionPane.showMessageDialog(location,
"Input Non valido ",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
catch (FileNotFoundException err){}
catch (IOException err){}
catch (ClassNotFoundException err){}
catch (NumberFormatException err){
JOptionPane.showMessageDialog(location,
"Input Non valido ",
"Error",
JOptionPane.ERROR_MESSAGE);
}
catch (NullPointerException err){
JOptionPane.showMessageDialog(location,
"Input Non valido ",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
private JTextField id;
private UserInterface location;
private JTextArea offers;
private JLabel count;
}
aggiungi_a_carrello.addActionListener(new aggiungi_al_carrello_action(this,id,offers,prodotti_nel_carello));
class completa_acquisto implements ActionListener {
public completa_acquisto (UserInterface in_location, JLabel in_count, JLabel in_bilancio){
count = in_count;
location= in_location;
bilancio = in_bilancio;
}
@Override
public void actionPerformed(ActionEvent e) {
try{
if (Carrello.size() == 0){
JOptionPane.showMessageDialog(location,
"Carrello Vuoto ",
"Error",
JOptionPane.ERROR_MESSAGE);
} else{
boolean status;
status = load_catalogo.acquistaProdotti(login_data.getUsername(),Carrello);
if (status){
count.setText(" Corrello: 0 Prodotti");
bilancio.setText("Bilancio" + " " + login_data.getUsername() + " = " + (Access.get_user_data(login_data.getUsername())).getBalance() );
JOptionPane.showMessageDialog(location,
"Acquisto Completato",
"Completato",
JOptionPane.INFORMATION_MESSAGE);
}else{
JOptionPane.showMessageDialog(location,
"Impossibile completare Acquisto ",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
catch (FileNotFoundException err){}
catch (IOException err){}
catch (ClassNotFoundException err){}
catch (NumberFormatException err){
JOptionPane.showMessageDialog(location,
"Input Non valido ",
"Error",
JOptionPane.ERROR_MESSAGE);
}
catch (NullPointerException err){
JOptionPane.showMessageDialog(location,
"Input Non valido ",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
private JLabel count;
private UserInterface location;
private JLabel bilancio;
}
acquista.addActionListener(new completa_acquisto(this,prodotti_nel_carello,bilancio));
acquista_panel.add(idlabel);
acquista_panel.add(id);
acquista_panel.add(aggiungi_a_carrello);
acquista_panel.add(prodotti_nel_carello);
acquista_panel.add(acquista);
mainWindow.add(acquista_panel,BorderLayout.SOUTH);
setSize(600, 400);
setLocation(login_window.getLocation());
setTitle("User " + Access.get_user_data(login_data.getUsername()).getUser() + " Logged");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
ArrayList<Utilizzabile> Carrello = new ArrayList<Utilizzabile>();
LoginWindow login_window;
Catalogo load_catalogo;
LoginData login_data;
}