import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.imageio.ImageIO;
public class LHCoinCatalogue extends JFrame {
private JFrame helpFrame;
private JPanel contentPane, panel_1;
private JPanel JTop, JBottom;
private JTabbedPane JCenter;
private JButton btnAdd, btnEdit, btnDelete;
private JTextField coinName, coinWeight, coinYear, coinCountry, coinQuantity;
private JTextArea coinDescription;
private JComboBox coinGrade, metalType, mintingCompany;
private JTextField eCoinName, eCoinWeight, eCoinYear, eCountry, eQuantity;
private JTextArea eDescription;
private JComboBox eCoinGrade, eMetalType, eCompany;
private JList eCoinList;
private DefaultListModel listModel;
private JScrollPane listScrollPane;
private Controller c;
private boolean bCoin;
private int pos;
private int size;
private Vector coinVec = null;
private String sName, sWeight, sYear, sCountry = null, sQuantity, sDescription = null;
private int sGrade, sType,sCompany;
String cbGrade[] ={"Not Graded", "Basal State [Poor (PO)]", "continuing Fair (Fr) ", "About or Almost Good (AG)", "Good (G)", "Very Good (VG)", "Fine (F)", "Very Fine (VF)", "Extremely Fine (EF)", "Almost or About Uncirculated (AU) ", "Uncirculated (Unc)", "Brilliant Uncirculated (BU)"};
String cbType[] = {"Aluminium", "Antimony ", "Carbon", "Chromium ", "Copper", "Gold", "Iron", "Lead", "Manganese", "Magnesium ", "Nickel", "Palladium", "Platinum", "Silver", "Tin", "Zinc"};
String cbCompany[] = {"CatCoin", "Other"};
String TestList[] = cbGrade;
private String basePath = "C:\\Documents and Settings\\Administrator\\Desktop\\Images";
File targetFile;
BufferedImage targetImg;
private static final int baseSize = 128;
//panel_1.add(new JLabel(new ImageIcon(targetImg)));
/**
* Launch the application.
*/
public static void main(String[] args){
LHCoinCatalogue frame = new LHCoinCatalogue();
frame.setVisible(true);
}
/**
* Create the frame.
*/
private LHCoinCatalogue() {
c = new Controller( );
// Main Frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
// Help Frame
helpFrame = new JFrame();
helpFrame.setLayout(new FlowLayout());
helpFrame.setBounds(100, 100, 200, 200);
// Title Panel
JTop = new JPanel();
JTop.setBackground(Color.GRAY);
contentPane.add(JTop, BorderLayout.NORTH);
JLabel lblLighthouseCoinCatalogue = new JLabel("Lighthouse Coin Catalogue");
lblLighthouseCoinCatalogue.setForeground(Color.BLACK);
lblLighthouseCoinCatalogue.setFont(new Font("Arial", Font.BOLD, 18));
JTop.add(lblLighthouseCoinCatalogue);
// Bottom Panel (button area)
JBottom = new JPanel();
JBottom.setBackground(Color.DARK_GRAY);
contentPane.add(JBottom, BorderLayout.SOUTH);
btnAdd = new JButton("Add");
btnAdd.addActionListener(new AddHandler());
JBottom.add(btnAdd);
btnEdit = new JButton("Edit");
btnEdit.addActionListener(new EditHandler());
JBottom.add(btnEdit);
btnDelete = new JButton("Delete");
btnDelete.addActionListener(new DeleteHandler());
JBottom.add(btnDelete);
// Center Panel
JCenter = new JTabbedPane(JTabbedPane.TOP);
contentPane.add(JCenter, BorderLayout.CENTER);
// first Tab: NewCoin
JPanel NewCoin = new JPanel();
JCenter.addTab("New Coin", null, NewCoin, null);
NewCoin.setLayout(null);
// first Tab: NewCoin - Page Name
JLabel lblAddNewCoin = new JLabel("Add New Coin");
lblAddNewCoin.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblAddNewCoin.setBounds(285, 11, 100, 14);
NewCoin.add(lblAddNewCoin);
// first Tab: NewCoin - Name
JLabel lblName = new JLabel("Name");
lblName.setBounds(25, 35, 50, 14);
NewCoin.add(lblName);
coinName = new JTextField();
coinName.setBounds(25, 60, 125, 20);
coinName.setToolTipText("Name of the Coin. Example: 150th Anniversary of the Founding of Singapore, $150 Dollar");
NewCoin.add(coinName);
coinName.setColumns(10);
// first Tab: NewCoin - Weight
JLabel lblWeight = new JLabel("Weight");
lblWeight.setBounds(180, 35, 50, 14);
NewCoin.add(lblWeight);
coinWeight = new JTextField();
coinWeight.setBounds(180, 60, 100, 20);
coinWeight.setToolTipText("Weight of the Coin");
NewCoin.add(coinWeight);
coinWeight.setColumns(10);
// first Tab: NewCoin - Coin Year
JLabel lblYearMinted = new JLabel("Year Minted");
lblYearMinted.setBounds(25, 90, 75, 14);
NewCoin.add(lblYearMinted);
coinYear = new JTextField();
coinYear.setBounds(25, 115, 100, 20);
coinYear.setToolTipText("The year of the Coin was created");
NewCoin.add(coinYear);
coinYear.setColumns(10);
// first Tab: NewCoin - Coin Country
JLabel lblCountryOfMinting = new JLabel("Country of Minting");
lblCountryOfMinting.setBounds(25, 150, 125, 14);
NewCoin.add(lblCountryOfMinting);
coinCountry = new JTextField();
coinCountry.setBounds(25, 175, 100, 20);
coinCountry.setToolTipText("In what coountry was the counted minted in ?");
NewCoin.add(coinCountry);
coinCountry.setColumns(10);
// first Tab: NewCoin - Coin Quantity
JLabel lblMintagequantityMinted = new JLabel("Mintage (Quantity Minted)");
lblMintagequantityMinted.setBounds(25, 205, 150, 14);
NewCoin.add(lblMintagequantityMinted);
coinQuantity = new JTextField();
coinQuantity.setBounds(29, 230, 100, 20);
coinQuantity.setToolTipText("Total Amount of your coin made");
NewCoin.add(coinQuantity);
coinQuantity.setColumns(10);
// first Tab: NewCoin - Description
JLabel lblDescription = new JLabel("Description");
lblDescription.setBounds(25, 260, 75, 14);
NewCoin.add(lblDescription);
coinDescription = new JTextArea();
coinDescription.setBounds(25, 285, 275, 100);
NewCoin.add(coinDescription);
coinDescription.setColumns(10);
// first Tab: NewCoin - Coin Grading
JLabel lblCoinsGrading = new JLabel("Coin's Grading");
lblCoinsGrading.setBounds(456, 35, 100, 14);
NewCoin.add(lblCoinsGrading);
coinGrade = new JComboBox(cbGrade);
coinGrade.setBounds(456, 60, 250, 20);
coinGrade.setToolTipText("Missing Text");
NewCoin.add(coinGrade);
// first Tab: NewCoin - Coin Metal Type
JLabel lblMetalType = new JLabel("Metal Type");
lblMetalType.setBounds(456, 125, 75, 14);
NewCoin.add(lblMetalType);
metalType = new JComboBox(cbType);
metalType.setBounds(456, 150, 150, 20);
metalType.setToolTipText("Material used for creating the coin");
NewCoin.add(metalType);
// first Tab: NewCoin - Minting Company
JLabel lblMintingCompany = new JLabel("Minting Company");
lblMintingCompany.setBounds(456, 205, 125, 14);
NewCoin.add(lblMintingCompany);
mintingCompany = new JComboBox(cbCompany);
mintingCompany.setBounds(456, 230, 250, 20);
mintingCompany.setToolTipText("Which company or organization manufacture the coin");
NewCoin.add(mintingCompany);
// first Tab: NewCoin - Add image
JButton btnAddImage = new JButton("Add image");
btnAddImage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
browseButtonActionPerformed(e);
}
});
btnAddImage.setBounds(456, 326, 100, 30);
NewCoin.add(btnAddImage);
// first tab: NewCoin image panel
JLabel panel_1 = new JLabel();
panel_1.add(new JLabel());
//panel_1.setLayout(new BorderLayout(0, 0));
//panel_1.add(new JLabel(new ImageIcon(targetImg)));
NewCoin.add(panel_1);
setVisible(true);
// first Tab: NewCoin - Help button
JButton btnHelp = new JButton("Help");
btnHelp.addActionListener(new HelpHandler());
btnHelp.setBounds(675, 400, 75, 25);
NewCoin.add(btnHelp);
// Second Tab: View/Edit
JPanel ViewEditCoin = new JPanel();
JCenter.addTab("View/Edit Coin", null, ViewEditCoin, null);
ViewEditCoin.setLayout(null);
// Second Tab: View/Edit - Page Name
JLabel lblVieweditCoin = new JLabel("View/Edit Coin");
lblVieweditCoin.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblVieweditCoin.setHorizontalAlignment(SwingConstants.RIGHT);
lblVieweditCoin.setBounds(430, 13, 125, 20);
ViewEditCoin.add(lblVieweditCoin);
// Second Tab: View/Edit - Name
JLabel lblName_1 = new JLabel("Name");
lblName_1.setBounds(150, 50, 33, 16);
ViewEditCoin.add(lblName_1);
eCoinName = new JTextField();
eCoinName.setBounds(150, 75, 150, 20);
ViewEditCoin.add(eCoinName);
eCoinName.setColumns(10);
// Second Tab: View/Edit - Weight
JLabel lblWeight_1 = new JLabel("Weight (grams)");
lblWeight_1.setBounds(325, 50, 50, 16);
ViewEditCoin.add(lblWeight_1);
eCoinWeight = new JTextField();
eCoinWeight.setBounds(325, 75, 100, 20);
ViewEditCoin.add(eCoinWeight);
eCoinWeight.setColumns(10);
// Second Tab: View/Edit - Coin Year
JLabel lblNewLabel = new JLabel("Year Minted");
lblNewLabel.setBounds(150, 110, 68, 16);
ViewEditCoin.add(lblNewLabel);
eCoinYear = new JTextField();
eCoinYear.setBounds(150, 135, 116, 22);
ViewEditCoin.add(eCoinYear);
eCoinYear.setColumns(10);
// Second Tab: View/Edit - Country
JLabel lblCountryOfMinting_1 = new JLabel("Country of Minting");
lblCountryOfMinting_1.setBounds(150, 170, 104, 16);
ViewEditCoin.add(lblCountryOfMinting_1);
eCountry = new JTextField();
eCountry.setBounds(150, 195, 116, 22);
ViewEditCoin.add(eCountry);
eCountry.setColumns(10);
// Second Tab: View/Edit - Quantity
JLabel lblMintagequantityMinted_1 = new JLabel("Mintage (Quantity Minted)");
lblMintagequantityMinted_1.setBounds(150, 230, 148, 16);
ViewEditCoin.add(lblMintagequantityMinted_1);
eQuantity = new JTextField();
eQuantity.setBounds(150, 255, 116, 22);
ViewEditCoin.add(eQuantity);
eQuantity.setColumns(10);
// Second Tab: View/Edit - Description
JLabel lblDescription_1 = new JLabel("Description");
lblDescription_1.setBounds(150, 290, 75, 16);
ViewEditCoin.add(lblDescription_1);
eDescription = new JTextArea();
eDescription.setBounds(150, 315, 200, 75);
ViewEditCoin.add(eDescription);
eDescription.setColumns(10);
// Second Tab: View/Edit - Coin Grading
JLabel lblCoinsGrading_1 = new JLabel("Coin's Grading");
lblCoinsGrading_1.setBounds(500, 50, 82, 16);
ViewEditCoin.add(lblCoinsGrading_1);
eCoinGrade = new JComboBox(cbGrade);
eCoinGrade.setBounds(500, 75, 250, 20);
ViewEditCoin.add(eCoinGrade);
// Second Tab: View/Edit - Coin Metal Type
JLabel lblEMetalType = new JLabel("Metal Type");
lblEMetalType.setBounds(500, 140, 75, 16);
ViewEditCoin.add(lblEMetalType);
eMetalType = new JComboBox(cbType);
eMetalType.setBounds(500, 165, 150, 20);
ViewEditCoin.add(eMetalType);
// Second Tab: View/Edit - Company
JLabel lblMintingCompany_1 = new JLabel("Minting Company");
lblMintingCompany_1.setBounds(500, 230, 100, 16);
ViewEditCoin.add(lblMintingCompany_1);
eCompany = new JComboBox(cbCompany);
eCompany.setBounds(500, 255, 250, 20);
ViewEditCoin.add(eCompany);
// Second Tab: View/Edit - Coin List
JLabel lblCoinlist = new JLabel("Coin List");
lblCoinlist.setBounds(15, 50, 50, 14);
ViewEditCoin.add(lblCoinlist);
coinVec = c.getAllDetail();
eCoinList = new JList();
DefaultListModel listModel = new DefaultListModel();
//for (int i=0; i<(c.reTurnSize()); i++)
// listModel.addElement(((Coin)coinVec.get(i)).getCoinName());
pos = eCoinList.getModel().getSize();
listModel.add(pos, coinName.getText());
eCoinList.setModel(listModel);
eCoinList.setSelectedIndex(0);
eCoinList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
eCoinList.addListSelectionListener(new ListentityHandler());
listScrollPane = new JScrollPane();
listScrollPane.setBounds(15, 75, 125, 350);
listScrollPane.setViewportView(eCoinList);
ViewEditCoin.add(listScrollPane);
// Second Tab: View/Edit - Help Button
JButton button = new JButton("Help");
button.addActionListener(new HelpHandler());
button.setBounds(675, 400, 75, 25);
ViewEditCoin.add(button);
JButton btnViewImage = new JButton("View Image");
btnViewImage.setBounds(500, 332, 100, 25);
ViewEditCoin.add(btnViewImage);
btnViewImage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//add actionlistener and add Jlabel to display image
}
});
JButton btnChangeImage = new JButton("Change Image");
btnChangeImage.setBounds(612, 332, 125, 25);
ViewEditCoin.add(btnChangeImage);
}
public BufferedImage rescale(BufferedImage originalImage)
{
BufferedImage resizedImage = new BufferedImage(baseSize, baseSize, BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, baseSize, baseSize, null);
g.dispose();
return resizedImage;
}
public void setTarget(File reference)
{
try {
targetFile = reference;
targetImg = rescale(ImageIO.read(reference));
} catch (IOException e) {
Logger.getLogger(LHCoinCatalogue.class.getName()).log(Level.SEVERE, null, e);
}
JLabel panel_1= new JLabel (); //Converted panel_1 into JLabel to support next line
panel_1.setIcon(new ImageIcon(targetImg)); // *Caution*
}
protected void ButtonViewActionPerformed( ActionEvent e) {
}
protected void browseButtonActionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser(basePath);
fc.setFileFilter(new JPEGImageFileFilter());
int res = fc.showOpenDialog(null);
// We have an image!
try {
if (res == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
setTarget(file);
} // Oops!
else {
JOptionPane.showMessageDialog(null,
"You must select one image to be the reference.", "Aborting...",
JOptionPane.WARNING_MESSAGE);
}
} catch (Exception iOException) {
}
}
// Listener
class AddHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("[test output] Add button OK! [delete when needed.]");
int newCG = (int)coinGrade.getSelectedIndex();
int newCMT = (int)metalType.getSelectedIndex();
int newCom = (int)mintingCompany.getSelectedIndex();
String strWeight = coinWeight.getText();
String strYear = coinYear.getText();
String strcoinQuantity = coinQuantity.getText();
float iWeight = 0;
int iYear = 0;
int iQuantity = 0;
try {
iWeight = Float.parseFloat(strWeight);
iYear = Integer.parseInt(strYear);
iQuantity = Integer.parseInt(strcoinQuantity);
c.newCoinDetail(coinName.getText(), coinWeight.getText(), coinYear.getText(), coinCountry.getText(), coinQuantity.getText(), coinDescription.getText(), newCG, newCMT, newCom);
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog( null,
"Please make sure the Weight, Year and Quantity must be fill with number.",
"Incorrect Input", JOptionPane.PLAIN_MESSAGE);
}
coinName.setText("");
coinWeight.setText("");
coinYear.setText("");
coinCountry.setText("");
coinQuantity.setText("");
coinDescription.setText("");
}
}
class EditHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int newCG = (int)eCoinGrade.getSelectedIndex();
int newCMT = (int)eMetalType.getSelectedIndex();
int newCom = (int)eCompany.getSelectedIndex();
c.newCoinDetail(eCoinName.getText(), eCoinWeight.getText(), eCoinYear.getText(), eCountry.getText(), eQuantity.getText(), eDescription.getText(), newCG, newCMT, newCom);
System.out.println("[test output] Edit button OK! [delete when needed.]");
}
}
class DeleteHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("[test output] Delete button OK! [delete when needed.]");
}
}
class HelpHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(helpFrame,"Test output");
}
}
class AddImageHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("[test output] Add Image button OK! [delete when needed.]");
}
}
class ListentityHandler implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent arg0) {
bCoin = c.retrieveDetail(eCoinName.getText( ));
if (bCoin == false)
{ eCoinName.setText("Error: Testing output"); }
else
{ int selected = eCoinList.getSelectedIndex();
System.out.println("\n[test]Index Selected: " + (selected+1)+"[test]");
sName = c.reTurnName(eCoinName.getText());
eCoinName.setText(sName);
sWeight = c.reTurnWeight(eCoinWeight.getText( ));
eCoinWeight.setText(sWeight);
sYear = c.reTurnYear(eCoinYear.getText());
eCoinYear.setText(sYear);
sCountry = c.reTurnCountry(eCountry.getText());
eCountry.setText(sCountry);
sQuantity = c.reTurnQuantity(eQuantity.getText());
eQuantity.setText(sQuantity);
sDescription = c.reTurnDescription(eDescription.getText());
eDescription.setText(sDescription);
sGrade = c.reTurnCoinGrade(eCoinGrade.getSelectedIndex());
eCoinGrade.setSelectedIndex(sGrade);
sType = c.reTurnType(eMetalType.getSelectedIndex());
eMetalType.setSelectedIndex(sType);
sCompany = c.reTurnCompany(eCompany.getSelectedIndex());
eCompany.setSelectedIndex(sCompany);
System.out.println("[test]" + sName + "\t" + sWeight + "\t" + sYear + "\t" + sCountry + "\t" + sQuantity + "\n" + sDescription + "\t" + sGrade + "\t" + sType + "\t" + sCompany +"[test]");
}
}
}
}