package awb; import java.awt.*; import java.net.*; import java.io.*; public class ScoreReporter extends Frame { private final static String myScript = "http://cgi.cs.duke.edu/~ecj/cps1/misc/racereport.cgi?"; private String myLogin, myScore; private TextField myLoginField, myPasswordField; private Label myStatus, myScoreLabel; private Component myParent; private Button myReportButton, myCancelButton; private boolean myFirstTime; public ScoreReporter(Component p, String whose) { super("Report Scores Dialog"); myLogin = whose; myParent = p; myFirstTime = true; myLoginField = new TextField(20); myLoginField.setText(myLogin); myPasswordField = new TextField(20); myPasswordField.setEchoCharacter('*'); myReportButton = new Button("Report"); myCancelButton = new Button("Cancel"); myStatus = new Label("Status: No scores reported yet."); myScoreLabel = new Label("Reporting 00:00:00.0 as average for xxxxx"); GridBagLayout g = new GridBagLayout(); setLayout(g); Constrainer.addComponent(g,myScoreLabel,this,0,0,1,1,0,0, GridBagConstraints.NONE,GridBagConstraints.WEST); Constrainer.addComponent(g,myLoginField,this,0,1,1,1,0,0, GridBagConstraints.NONE,GridBagConstraints.WEST); Constrainer.addComponent(g,myPasswordField,this,0,2,1,1,0,0, GridBagConstraints.NONE,GridBagConstraints.WEST); Panel buttons = new Panel(); buttons.setLayout(new FlowLayout(FlowLayout.LEFT)); buttons.add(myReportButton); buttons.add(myCancelButton); Constrainer.addComponent(g,buttons,this,0,3,1,1,0,0, GridBagConstraints.NONE,GridBagConstraints.WEST); Constrainer.addComponent(g,myStatus,this,0,4,1,1,0,0, GridBagConstraints.NONE,GridBagConstraints.WEST); } public void show(String score) { myScore = score; myScoreLabel.setText("Reporting " + score + " as average for " + myLogin); super.show(); if (myFirstTime) super.pack(); myFirstTime = false; } private void reportScore(String score) { if (! myLogin.equals(myLoginField.getText())) return; if ((myLoginField.getText().equals("")) || (myPasswordField.getText().equals(""))) return; try { URL u = new URL(myScript + "score=" + URLEncoder.encode(score) + "&name=" + myLogin + "&info=" + URLEncoder.encode(myPasswordField.getText())); URLConnection uc = u.openConnection(); DataInputStream in = new DataInputStream(uc.getInputStream()); String line = in.readLine(); boolean okay = false; while (line != null) { if (line.startsWith("OKAY")) { okay = true; break; } line = in.readLine(); } if (okay) setStatus("reported " + myScore); else setStatus("not reporteed (bad pass/loc)"); } catch (MalformedURLException ex) { setStatus("can't find script"); } catch (SecurityException ex) { setStatus("can't report from here"); } catch (IOException ex) { setStatus("can't report, script broke"); } } private void setStatus(String msg) { myStatus.setText("Status: " + msg); } public boolean action(Event e, Object o) { if (e.target == myReportButton) { reportScore(myScore); this.hide(); myPasswordField.setText(""); myParent.enable(); return true; } if (e.target == myCancelButton) { setStatus("cancelled score report"); this.hide(); myPasswordField.setText(""); myParent.enable(); return true; } return super.action(e,o); } public boolean handleEvent(Event e) { if (e.id == Event.WINDOW_DESTROY) { this.hide(); myPasswordField.setText(""); myParent.enable(); return true; } return super.handleEvent(e); } }