/** * Demonstrate the use of sockets with NetScape 2.0b2. This applet * Queries a background server process witten in Java for a stock price. * The price is used to calculate a position value. * A configuration dialog box is also provided for connection to * Other servers supporting a stock proce service on a known port. * The syntax for a request is very simple. Open the known port * Send the ticker symbol and then read the returned price (End of file is * returned by the server) and display it or perofrm your calculation * with it. */ import java.awt.*; import java.applet.Applet; import GetAStock; class StockPanels extends Panel { String ServerName = new String("localhost"); int PortNumber = 7000; Label lserver,lport,lsymbol,lprice,lshares,ltotal; TextField server,port,symbol,price,shares,total; Button ask; Panel main,configure,getaquote; GridBagLayout configGrid,quoteGrid; GridBagConstraints configGridc,quoteGridc; StockPanels() { configGrid = new GridBagLayout(); configGridc = new GridBagConstraints(); quoteGrid = new GridBagLayout(); quoteGridc = new GridBagConstraints(); setFont(new Font("Courier", Font.PLAIN, 14)); setLayout(new CardLayout()); main = new Panel(); add("Main",main); configure = new Panel(); add("Configure",configure); getaquote = new Panel(); add("GetAQuote",getaquote); // Main Panel has two buttons on it main.add("North", new Button("Configure")); main.add("West", new Button("Get A Quote")); // configure.setLayout(new GridLayout(2, 2,10,3)); configure.setLayout(configGrid); // configGridc.gridwidth = 2; // configGridc.gridheight = 2; // Add a Server label and field value lserver = new Label("Server",Label.LEFT); configGrid.setConstraints(lserver, configGridc); server = new TextField("localhost",10); configGridc.gridwidth = GridBagConstraints.REMAINDER; configGrid.setConstraints(server, configGridc); configure.add( lserver ) ; configure.add(server); // Add a port label and port value lport = new Label("Port",Label.LEFT); configGridc.gridwidth = 1; configGrid.setConstraints(lport, configGridc); port = new TextField("7000",10); configGridc.gridwidth = GridBagConstraints.REMAINDER; configGrid.setConstraints(port, configGridc); configure.add(lport); configure.add(port); //getaquote.setLayout(new GridLayout(4, 2,10,3)); // Add a Symbol label and start Symbol value getaquote.setLayout(quoteGrid); lsymbol = new Label("Symbol",Label.LEFT); quoteGrid.setConstraints(lsymbol, quoteGridc); symbol = new TextField("SUNW",10); quoteGridc.gridwidth = GridBagConstraints.REMAINDER; quoteGrid.setConstraints(symbol, quoteGridc); getaquote.add(lsymbol); getaquote.add(symbol); // Add a shares label lshares = new Label("Shares",Label.LEFT); quoteGridc.gridwidth = 1; quoteGrid.setConstraints(lshares, quoteGridc); shares = new TextField("191",10); quoteGridc.gridwidth = GridBagConstraints.REMAINDER; quoteGrid.setConstraints(shares, quoteGridc); getaquote.add(lshares); getaquote.add(shares); // Add a price lprice = new Label("Price",Label.LEFT); quoteGridc.gridwidth = 1; quoteGrid.setConstraints(lprice, quoteGridc); price = new TextField("0",10); quoteGridc.gridwidth = GridBagConstraints.REMAINDER; quoteGrid.setConstraints(price, quoteGridc); getaquote.add(lprice); getaquote.add(price); // Add a total ltotal = new Label("Total",Label.LEFT); quoteGridc.gridwidth = 1; quoteGrid.setConstraints(ltotal, quoteGridc); total = new TextField("0",10); quoteGridc.gridwidth = GridBagConstraints.REMAINDER; quoteGrid.setConstraints(total, quoteGridc); getaquote.add(ltotal); getaquote.add(total); ask = new Button(" Get price for stock "); quoteGridc.weightx = 1; quoteGridc.fill = GridBagConstraints.HORIZONTAL; quoteGridc.gridwidth = GridBagConstraints.REMAINDER; quoteGrid.setConstraints(ask, quoteGridc); getaquote.add(ask); } public Dimension preferredSize() { return new Dimension(50, 50); } } public class ShowCommunicationToSocket extends Applet implements Runnable { StockPanels cards; GetAStock client; Thread myThread = null; boolean request_quote = false; public void init() { String param; param = getParameter("PortNumber"); if (param != null) cards.PortNumber = Integer.parseInt(param); param = getParameter("ServerName"); if (param != null) cards.ServerName = param; setLayout(new BorderLayout()); add("Center", cards = new StockPanels()); Panel p = new Panel(); p.setLayout(new FlowLayout()); add("South", p); p.add(new Button("first")); p.add(new Button("next")); ((CardLayout)cards.getLayout()).first(cards); } public void start () { if ( myThread == null ) { myThread = new Thread(this); myThread.start(); } } public void run () { while ( myThread != null ) { if ( request_quote ) { cards.PortNumber = Integer.parseInt(cards.port.getText()); cards.ServerName = cards.server.getText(); client = new GetAStock(this,cards.PortNumber, cards.ServerName); String newprice = client.getquote(cards.symbol.getText()); if ( newprice == null ) { cards.price.setText("No Data"); } else { cards.price.setText(newprice); int value = Integer.parseInt(cards.shares.getText()); Double p = new Double(Double.valueOf(newprice).doubleValue()); value *= p.doubleValue(); Double total_value = new Double(value); cards.total.setText(Integer.toString(total_value.intValue())); cards.total.setEditable(false); } request_quote = false; } try { Thread.sleep(1000); } catch ( InterruptedException e ) { } repaint(0); } } public boolean action(Event evt, Object arg) { if (evt.target instanceof Choice) { ((CardLayout)cards.getLayout()).show(cards,(String)arg); } else { if ("first".equals(arg)) { ((CardLayout)cards.getLayout()).first(cards); } else if ("next".equals(arg)) { ((CardLayout)cards.getLayout()).next(cards); } else if ("previous".equals(arg)) { ((CardLayout)cards.getLayout()).previous(cards); } else if ("last".equals(arg)) { ((CardLayout)cards.getLayout()).last(cards); } else if (" Get price for stock ".equals(arg)) { request_quote = true; } else if ("Configure".equals(arg)) { ((CardLayout)cards.getLayout()).show(cards,"Configure"); } else if ("Get A Quote".equals(arg)) { ((CardLayout)cards.getLayout()).show(cards,"GetAQuote"); } else { ((CardLayout)cards.getLayout()).show(cards,(String)arg); } } return true; } public void stop () { myThread = null; } }