/* Proxy User Interface - PUI Window : Insik Shin, ishin@cs.stanford.edu Thr 14 Aug 1997 - Implemented menu frame for UI window Sat 16 Aug 1997 - modified for testing socket connection - took a URL as input Fri 22 Aug 1997 - modified for testing URL connection Sat 23 Aug 1997 - modified for client/server model for TCP socket Sat 30 Aug 1997 - switched to beta version of PUI window - disabled keyboard input */ import java.awt.*; public class PUIWindow extends Frame { private boolean inAnApplet = true; private AboutDialog dialog; private Label labelProxy; private Label labelProgress; private boolean onProxy = true; private TextField ProxyHost, ProxyPort; private TextArea taProgress; private CheckboxMenuItem ADsOption, ClassOption, SitesOption; private Choice ProxyChoice, MessageChoice; private SocketClient sc = new SocketClient(); Panel connectPanel; /* initialize menu frame and other GUI stuff */ public PUIWindow (String title, String host, String port) { /* display window title */ super (title); /* Layout for the entire menu frame - Border Layout */ setLayout(new BorderLayout()); Panel A1 = new Panel(); this.add("North", A1); Panel A3 = new Panel(); this.add("Center", A3); Panel A5 = new Panel(); this.add("South", A5); /* Panel for displaying proxy location and turning on/off proxy */ Panel a1 = new Panel(); Panel a3 = new Panel(); Panel a5 = new Panel(); /* using GridBagLayout */ GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); A1.setLayout(gridbag); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; /* HTTP Proxy Server */ Label hps = new Label("HTTP Proxy Server :"); a1.add(hps); gridbag.setConstraints (a1, c); A1.add (a1); /* Proxy Hostname */ Label h = new Label ("Host"); a3.add(h); ProxyHost = new TextField(host, 19); ProxyHost.setEditable (false); a3.add(ProxyHost); gridbag.setConstraints (a3, c); A1.add(a3); /* Proxy Port Number */ Label lp = new Label ("Port"); a5.add(lp); ProxyPort = new TextField(port, 4); ProxyPort.setEditable (false); a5.add(ProxyPort); c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints (a5, c); A1.add(a5); /* Label for displaying progress */ A3.setLayout(new BorderLayout()); labelProgress = new Label("Progress bar ..........", Label.CENTER); A3.add("North", labelProgress); /* Text area for displaying messages .. */ taProgress = new TextArea ("> Proxy User Interface", 10, 60); taProgress.setEditable(false); A3.add("Center", taProgress); /* UI status display line */ A5.setLayout(new BorderLayout(1,1)); /* Proxy Status */ /* labelProxy = new Label("HTTP Proxy Server", Label.CENTER); A5.add(labelProxy); */ /* Message Display Level */ /* Label labelMsg = new Label("Message Display Level", Label.CENTER); m.add(labelMsg); MessageChoice = new Choice(); MessageChoice.addItem("ALL"); MessageChoice.addItem("MIDDLE"); MessageChoice.addItem("NOTHING"); m.add(MessageChoice); */ // Menu Bar Setting MenuBar myMenuBar = new MenuBar(); makeOptionsMenu(myMenuBar); setMenuBar(myMenuBar); /* Initiate TCP server for a connection from proxy */ SocketServer ss = new SocketServer(); ss.setVariables (labelProgress, taProgress, 3331); ss.start(); /* let the proxy know that UI finished its initialization */ sc.clientSocket (labelProgress, taProgress, "CONNECT"); //modifyProxyStatus(); } public boolean handleEvent(Event e) { if (e.id == Event.WINDOW_DESTROY) { sc.clientSocket (labelProgress, taProgress, "DESTROY"); dispose(); return true; } else if (e.id == Event.ACTION_EVENT) { return action(e, e.target); } return super.handleEvent(e); } public boolean action (Event e, Object o) { if (e.target instanceof Button) { Button b = (Button) o; String s = b.getLabel(); if (s != null) handleButton(s); return true; } else if (e.target instanceof MenuItem) { MenuItem mi = (MenuItem) o; String s = mi.getLabel(); if (s != null) handleMenuItem(mi, s); return true; } else if (e.target instanceof Choice) { Choice c = (Choice) o; String s = c.getSelectedItem(); if (s != null) { /* instead of "handleChoice(s)", for optimization */ sc.clientSocket (labelProgress, taProgress, s); } return true; } else { return false; } } /* deal with selecting menu items */ private void handleMenuItem (MenuItem mi, String s) { /* in case of snipping ads option */ if (s.equals("Ad Filtering")) { if (((CheckboxMenuItem)mi).getState()) { SocketClient sc = new SocketClient(); sc.clientSocket (labelProgress, taProgress, "Ad Filtering"); } else { SocketClient sc = new SocketClient(); sc.clientSocket (labelProgress, taProgress, "No Ad Filtering"); } } /* in case of transforming class option */ else if (s.equals("Class Substitution")) { if (((CheckboxMenuItem)mi).getState()) { SocketClient sc = new SocketClient(); sc.clientSocket (labelProgress, taProgress, "Class Substitution"); } else { SocketClient sc = new SocketClient(); sc.clientSocket (labelProgress,taProgress,"No Class Substitution"); } } /* in case of blocking sites option */ else if (s.equals("Site Blocking")) { if (((CheckboxMenuItem)mi).getState()) { SocketClient sc = new SocketClient(); sc.clientSocket (labelProgress, taProgress, "Site Blocking"); } else { SocketClient sc = new SocketClient(); sc.clientSocket (labelProgress,taProgress,"No Site Blocking"); } } /* in case of asking a list of blocked sites */ else if (s.equals("Blocked Site List")) { SocketClient sc = new SocketClient(); sc.clientSocket (labelProgress, taProgress, "Asking Blocked Sites"); } /* in case of displaying about the proxy */ else if (s.equals("about")) { if (dialog == null) { dialog = new AboutDialog(this, "Proxy User Interface"); } dialog.move(320,220); dialog.show(); } /* modify proxy status */ //modifyProxyStatus(); } /* Deal with cases of clicking buttons on the window */ private void handleButton(String s) { } /* Deal with cases of clicking buttons on the window */ private void handleChoice(String s) { /* in case of enabling/disabling proxy - Proxy Status */ if (s.equals("Enabled") || s.equals("Disabled")) { SocketClient sc = new SocketClient(); sc.clientSocket (labelProgress, taProgress, ProxyChoice.getSelectedItem()); } /* in case of controlling message display - Message Display Level */ else if (s.equals("ALL") || s.equals("MIDDLE") || s.equals("NOTHING")) { SocketClient sc = new SocketClient(); sc.clientSocket (labelProgress, taProgress, MessageChoice.getSelectedItem()); } } private void modifyProxyStatus() { String s = new String(""); if (ADsOption.getState()) s = s.concat("Snipping Ads "); if (ClassOption.getState()) s = s.concat("Transforming Class "); if (SitesOption.getState()) s = s.concat("Blocking Sites"); labelProxy.setText(s); } private void makeOptionsMenu(MenuBar mb) { Menu optionsMenu = new Menu("Options"); Menu ProxyOptions = new Menu("Proxy Options"); optionsMenu.add(ProxyOptions); ADsOption = new CheckboxMenuItem("Ad Filtering"); ProxyOptions.add(ADsOption); ADsOption.setState(true); ClassOption = new CheckboxMenuItem("Class Substitution"); ProxyOptions.add(ClassOption); ClassOption.setState(true); SitesOption = new CheckboxMenuItem("Site Blocking"); ProxyOptions.add(SitesOption); SitesOption.setState(true); ProxyOptions.addSeparator(); ProxyOptions.add(new MenuItem("Blocked Site List")); optionsMenu.addSeparator(); optionsMenu.add(new MenuItem("about")); mb.add(optionsMenu); } /* public void setText(String text) { textArea.appendText(text + "\n"); } */ } class AboutDialog extends Dialog { Button OKButton; AboutDialog(Frame dw, String title) { super(dw, title, false); /* display information about proxy */ setLayout(new GridLayout(3,1)); Panel p1 = new Panel(); add(p1); Panel p2 = new Panel(); add(p2); Panel p3 = new Panel(); add(p3); Panel p4 = new Panel(); add(p4); Panel p5 = new Panel(); add(p5); Label label1 = new Label("HTTP Proxy Server"); p1.add(label1); Label label2 = new Label(""); p2.add(label2); Label label3 = new Label("Under Construction ..."); p3.add(label3); Label label4 = new Label(""); p4.add(label4); OKButton = new Button("OK"); p5.add(OKButton); //Initialize this dialog to its preferred size. //pack(); } public boolean action(Event event, Object arg) { if (event.target == OKButton) hide(); return true; } }