/*
* File: MilanasWSClientGUI.java
* Created on 9 / desembre / 2007, 18:24
*
* Copyright (C) 2008 M.Àngels Cerveró Abelló
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package client.ws.milanas.gui;
import client.ws.milanas.controller.Controller;
import client.ws.milanas.gui.thread.SearchBooksThread;
import client.ws.milanas.gui.thread.SearchMusicThread;
import client.ws.milanas.helpers.beans.CartItem;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
/**
*
* @author milana
*/
public class MilanasWSClientGUI extends JFrame implements ActionListener
{
private Controller controller;
private JMenuItem book;
private JMenuItem music;
private JMenuItem cart;
private JMenuItem exit;
private JPanel searchPane;
private JDesktopPane panel;
private JTabbedPane tabs;
private HashMap<Long, LoadingFrame> lfs;
private HashMap<Long, SearchBooksThread> sbts;
private HashMap<Long, SearchMusicThread> smts;
private ResourceBundle messages;
private MessageFormat formatter;
public MilanasWSClientGUI (Controller controller)
{
super ("MilanasWSClient");
Image img = Toolkit.getDefaultToolkit().getImage(java.net.URLClassLoader.getSystemResource("flors_petites.jpg"));
setIconImage(img);
setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
setResizable(false);
setDefaultCloseOperation (EXIT_ON_CLOSE);
//Inicialitzacio del controlador
this.controller = controller;
this.controller.createConnections();
Locale locale = new Locale("en", "GB");
messages = ResourceBundle.getBundle("MessagesBundle", locale);
formatter = new MessageFormat("");
formatter.setLocale(locale);
lfs = new HashMap<Long, LoadingFrame>();
sbts = new HashMap<Long, SearchBooksThread>();
smts = new HashMap<Long, SearchMusicThread>();
//Creacio del menu
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu(messages.getString("gui.menu"));
JMenu search = new JMenu(messages.getString("gui.menu.search"));
book = new JMenuItem(messages.getString("gui.menu.search.book"));
book.addActionListener(this);
search.add(book);
music = new JMenuItem(messages.getString("gui.menu.search.music"));
music.addActionListener(this);
search.add(music);
menu.add(search);
cart = new JMenuItem(messages.getString("gui.menu.cart"));
cart.addActionListener(this);
menu.add(cart);
exit = new JMenuItem(messages.getString("gui.menu.exit"));
exit.addActionListener(this);
menu.add(exit);
menuBar.add(menu);
setJMenuBar(menuBar);
//Creacio del panell i de la graella
Container content = getContentPane();
panel = new JDesktopPane();
content.add(panel, BorderLayout.CENTER);
searchPane = new JPanel();
Dimension searchPaneSize = Toolkit.getDefaultToolkit().getScreenSize();
searchPaneSize.setSize(300, searchPaneSize.getHeight() - 100);
searchPane.setSize(searchPaneSize);
searchPane.setVisible(true);
searchPane.setLocation(0, 0);
panel.add (searchPane);
tabs = new JTabbedPane();
tabs.setLocation(300, 0);
tabs.setVisible(true);
Dimension tabsSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
tabsSize.setSize(tabsSize.width - 310, tabsSize.height - 100);
tabs.setSize(tabsSize);
panel.add(tabs);
setContentPane (panel);
}
public void actionPerformed (ActionEvent event)
{
if (event.getSource ().getClass ().getName ().equals ("javax.swing.JMenuItem"))
{
menuItemTreatement ((JMenuItem) event.getSource ());
}
}
private void menuItemTreatement (JMenuItem auxMenuItem)
{
if (auxMenuItem == book)
{
searchPane.setVisible(false);
searchPane = new BookSearchPane(this, messages);
panel.add(searchPane);
}
else if (auxMenuItem == music)
{
searchPane.setVisible(false);
searchPane = new MusicSearchPane(this, messages);
panel.add(searchPane);
}
else if (auxMenuItem == cart)
{
seeCart();
}
else if (auxMenuItem == exit)
{
System.exit (0);
}
}
public void startSearchBook(String author, String title, String keywords, String condition, String sort)
{
SearchBooksThread sbt = new SearchBooksThread(this, controller);
LoadingFrame lf = new LoadingFrame(messages);
lfs.put(Long.valueOf(sbt.getId()), lf);
lf.setVisible(true);
sbts.put(Long.valueOf(sbt.getId()), sbt);
sbt.startThread(author, title, keywords, condition, sort);
}
public void updateSearchBookTab(long threadId)
{
tabs.addTab(messages.getString("gui.tab.books.title"), new BooksResultsInternalFrame(messages.getString("gui.tab.books.title"), sbts.get(threadId).getItems(), tabs.getWidth(), this, tabs.getTabCount(), messages));
lfs.get(threadId).setVisible(false);
lfs.remove(threadId);
sbts.remove(threadId);
}
public void startSearchMusic(String artist, String title, String keywords, String condition, String sort)
{
SearchMusicThread smt = new SearchMusicThread(this, controller);
LoadingFrame lf = new LoadingFrame(messages);
lfs.put(Long.valueOf(smt.getId()), lf);
lf.setVisible(true);
smts.put(Long.valueOf(smt.getId()), smt);
smt.startThread(artist, title, keywords, condition, sort);
}
public void updateSearchMusicTab(long threadId)
{
tabs.addTab(messages.getString("gui.tab.musics.title"), new MusicResultsInternalFrame(messages.getString("gui.tab.musics.title"), smts.get(threadId).getItems(), tabs.getWidth(), this, tabs.getTabCount(), messages));
lfs.get(threadId).setVisible(false);
lfs.remove(threadId);
smts.remove(threadId);
}
public void lookupBook(String asin)
{
tabs.addTab(messages.getString("gui.tab.book.title"), new BookResultInternalFrame(messages.getString("gui.tab.book.title"), controller.lookupBook(asin), tabs.getWidth(), this, tabs.getTabCount(), messages, formatter));
}
public void lookupMusic(String asin)
{
tabs.addTab(messages.getString("gui.tab.music.title"), new MusicResultInternalFrame(messages.getString("gui.tab.music.title"), controller.lookupMusic(asin), tabs.getWidth(), this, tabs.getTabCount(), messages, formatter));
}
public void addCart(String offerId)
{
boolean intParsed = false;
int quantity = 0;
String quant = null;
while(!intParsed)
{
quant = JOptionPane.showInputDialog (messages.getString("gui.dialog.ask.units"));
if(quant != null)
{
try
{
quantity = Integer.parseInt(quant);
intParsed = true;
}
catch(NumberFormatException nfe){}
}
else
{
quantity = 0;
intParsed = true;
}
}
if(controller.addItemToCart(offerId, quantity))
{
JOptionPane.showMessageDialog(null, messages.getString("gui.dialog.info.correct"), messages.getString("gui.dialog.info.correct.title"), JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, messages.getString("gui.dialog.info.incorrect"), messages.getString("gui.dialog.info.incorrect.title"), JOptionPane.INFORMATION_MESSAGE);
}
}
public void seeCart()
{
tabs.addTab(messages.getString("gui.tab.cart"), new CartInternalFrame(controller.getCart(), tabs.getWidth(), this, tabs.getTabCount(), messages));
}
public void emtpyCart()
{
controller.emtpyCart();
}
public void modifyCart(ArrayList<CartItem> items, int tabIndex)
{
controller.modifyCartItems(items);
tabs.setComponentAt(tabIndex, new CartInternalFrame(controller.getCart(), tabs.getWidth(), this, tabIndex, messages));
}
public void closeTab(int tabIndex)
{
int i = 0;
if(tabs.getTabCount() > 0)
{
for(i = tabIndex + 1; i < tabs.getTabCount(); i++)
{
ClosableTab ct = (ClosableTab) tabs.getComponentAt(i);
ct.recountTabIndex();
}
}
tabs.removeTabAt(tabIndex);
}
protected void processWindowEvent(WindowEvent e)
{
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
controller.closeConnections();
}
super.processWindowEvent(e);
}
}