import java.applet.*; import java.awt.*; import java.awt.font.*; import javax.swing.*; import java.util.*; import javax.swing.text.*; import java.net.*; import java.awt.event.*; import isip.java.bullyse.bullydb.*; public class Transaction extends JPanel implements MouseListener { // the stock symbol. From this symbol, all data for this window should // be retreivable. // private String symbol = null; // data that is received from the database about the users stock // private double owned = 0.00; private double current = 0.00; private String gain = null; private String user = null; // this is the location of the applet. // private String path = new String("http://www.isip.msstate.edu/~langley/rking/"); // all of the components that will be on the panel // private JTextField buy = null; private JTextField sell = null; private JLabel price = null; private JLabel goButton = null; private JLabel stock = null; private JLabel bought = null; private JLabel profit = null; // reference to the applet that contains this panel // private TransactionApplet applet = null; // this is the database handle // BullyDB db; public Transaction(String s, TransactionApplet a, String user_) { // setup the database handle // db = new BullyDB(); // give the class a reference to the applet containing it // applet = a; // make a local copy of the stock symbol // symbol = s; // make a local copy of the user AccountId // user = user_; // set all the other data base on the stock symbol // loadStockData(); // layout the panel and draw it // layoutPanel(); } // this is used as a testing constructor so it can be run as an // application when necessary. Some features of the applet will not // work. // public Transaction(String s) { // make a local copy of the stock symbol // symbol = s; // set all the other data base on the stock symbol // loadStockData(); // layout the panel and draw it // layoutPanel(); } // this function will in the end query the database and set the values // for owned, current, and gain. For now though, it hard codes some // values so the GUI can be developed. The database query will be based // on the value of symbol. // private void loadStockData() { // query the database for the user's holdings info // System.out.println("Getting holdings for symbol " + symbol); Vector holdings = db.getHoldings(user, symbol); System.out.println("Holdings for user " + user + " on symbol " + symbol + " gotten"); if(symbol.equals("Ballance") || symbol.equals("Cash")) { System.out.println("using Cash"); String temp = holdings.elementAt(0).toString(); System.out.println("stripping temp: "+temp); int start = temp.indexOf("["); int end = temp.indexOf("]"); //temp.replace('[',' '); //temp.replace('\]',' '); //temp.trim(); String ttemp = temp.substring(start+1, end-1); temp = ttemp; System.out.println(temp); //setOwned(temp); owned = Double.parseDouble(temp); //setCurrent(""); //setGain(""); current = 0; gain = "0"; } else { System.out.println("Not using Cash"); //setOwned((holdings.elementAt(1)).toString()); owned = Double.parseDouble((holdings.elementAt(1)).toString()); //setCurrent((holdings.elementAt(3)).toString()); current = Double.parseDouble((holdings.elementAt(3)).toString()); double cur = Double.parseDouble((String)holdings.elementAt(3)); double last = Double.parseDouble((String)holdings.elementAt(2)); //setGain(Double.toString(cur - last)); gain = Double.toString(cur-last); } } // this function will create two text boxes, a button, and draw all // the appropriate text for creating one of tranaction boxes // private void layoutPanel() { this.setBackground(new Color(0xd0, 0xd0, 0xd0)); System.out.println("Layout Panel is run"); Font font = new Font("Smaller", Font.PLAIN, 10); this.setSize(new Dimension(390,25)); buy = new JTextField(3); buy.setHorizontalAlignment(JTextField.RIGHT); buy.setDocument(new IntDocument()); buy.setText("0"); sell = new JTextField(3); sell.setHorizontalAlignment(JTextField.RIGHT); sell.setDocument(new IntDocument()); sell.setText("0"); stock = new JLabel(symbol); stock.setForeground(Color.black); stock.setFont(font); bought = new JLabel("" + owned); bought.setForeground(Color.black); bought.setFont(font); price = new JLabel("" + current); price.setForeground(Color.red); Font font2 = font.deriveFont(TextAttribute.UNDERLINE_ON.intValue()); price.setFont(font2); price.addMouseListener(this); profit = new JLabel(gain); profit.setForeground(Color.black); profit.setFont(font); // create the icon that will be the go button // ImageIcon goImage = null; try { goImage = new ImageIcon(new URL(path + "go.gif")); } catch(MalformedURLException e) { System.out.println("The image for the go button cannot be read"); } goButton = new JLabel(goImage); goButton.addMouseListener(this); // create the grid bag layout and set it to the layout for the panel // GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); this.setLayout(gridbag); this.setSize(new Dimension (300, 20)); // Add the stock text field to the applet // c.gridx = 0; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1; c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.WEST; c.weightx = 1; c.weighty = 0; c.insets = new Insets(0, 0, 1, 1); gridbag.setConstraints(stock, c); this.add(stock); // Add the bought text field to the applet // c.gridx = GridBagConstraints.RELATIVE; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1; c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.WEST; c.weightx = 1; c.weighty = 0; c.insets = new Insets(0, 0, 1, 0); gridbag.setConstraints(bought, c); this.add(bought); // Add the price text field to the applet // c.gridx = GridBagConstraints.RELATIVE ; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1; c.fill = GridBagConstraints.NONE; c.anchor = GridBagConstraints.WEST; c.weightx = 1; c.weighty = 0; c.insets = new Insets(0, 0, 1, 0); gridbag.setConstraints(price, c); this.add(price); // Add the profit text field to the applet // c.gridx = GridBagConstraints.RELATIVE ; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1; c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.WEST; c.weightx = 2; c.weighty = 0; c.insets = new Insets(0, 0, 1, 0); gridbag.setConstraints(profit, c); this.add(profit); // Add the buy text field to the applet // c.gridx = GridBagConstraints.RELATIVE; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1; c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.WEST; c.weightx = GridBagConstraints.RELATIVE; c.weighty = 0; c.insets = new Insets(0, 0, 1, 1); gridbag.setConstraints(buy, c); this.add(buy); // Add the sell text field to the applet // c.gridx = GridBagConstraints.RELATIVE; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1; c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.WEST; c.weightx = GridBagConstraints.RELATIVE; c.weighty = 0; c.insets = new Insets(0, 3, 1, 1); gridbag.setConstraints(sell, c); this.add(sell); // Add the goButton to the layout // c.gridx = GridBagConstraints.RELATIVE; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1; c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.EAST; c.weightx = GridBagConstraints.REMAINDER; c.weighty = 0; c.insets = new Insets(0, 2, 1, 1); gridbag.setConstraints(goButton, c); this.add(goButton); /* this.setLayout(new GridLayout(1,7, 3, 10)); this.add(stock); this.add(bought); this.add(price); this.add(profit); this.add(buy); this.add(sell); this.add(goButton); */ } public void mouseClicked(MouseEvent e) { if (e.getComponent() == goButton) { // read in the number of shares to buy // String textFieldString = buy.getText(); int bought = 0; if ( (textFieldString.equals("0")) || (textFieldString.equals(""))) System.out.println("Nothing is bought."); else { bought = Integer.parseInt(textFieldString); System.out.println(textFieldString + " shares of " + symbol + " are bought."); } // read in the number of shares to sell // int sold = 0; textFieldString = sell.getText(); if ((textFieldString.equals("0")) || (textFieldString.equals(""))) System.out.println("Nothing is sold."); else { sold = Integer.parseInt(textFieldString); System.out.println(textFieldString + " shares of " + symbol + " are sold."); } } else if (e.getComponent() == price) { // this URL will be pulled from the database for each of the // different stocks // try { applet.getAppletContext(). showDocument(new URL("http://www.cnn.com")); } catch(MalformedURLException ex) { System.out.println("The URL is not valid"); } } } public void mouseReleased(MouseEvent e) { // mandatory to implement the mouse listener } public void mouseExited(MouseEvent e) { // mandatory to implement the mouse listener } public void mouseEntered(MouseEvent e) { // mandatory to implement the mouse listener } public void mousePressed(MouseEvent e) { // mandatory to implement the mouse listener } public static void main (String args[]) { Frame f = new Frame(); f.add(new Transaction("ADTN")); f.pack(); f.setVisible(true); } private void setOwned(String newValue) { //System.out.println(newValue); owned = Double.parseDouble(newValue); bought.setText(newValue); } private void setCurrent(String newValue) { current = Double.parseDouble(newValue); price.setText(newValue); } private void setGain(String newValue) { gain = newValue; profit.setText(newValue); } }