importjava.awt.*;importjava.awt.event.*;importjava.net.*;importjava.util.*;importjavax.swing.*;importjavax.swing.event.*;importjavax.swing.text.html.*;// The Simple Web Browser.publicclassMiniBrowserextendsJFrameimplementsHyperlinkListener{// These are the buttons for iterating through the page list.privateJButtonbackButton,forwardButton;// Page location text field.privateJTextFieldlocationTextField;// Editor pane for displaying pages.privateJEditorPanedisplayEditorPane;// Browser's list of pages that have been visited.privateArrayListpageList=newArrayList();// Constructor for Mini Web Browser.publicMiniBrowser(){// Set application title.super("Mini Browser");// Set window size.setSize(640,480);// Handle closing events.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){actionExit();}});// Set up file menu.JMenuBarmenuBar=newJMenuBar();JMenufileMenu=newJMenu("File");fileMenu.setMnemonic(KeyEvent.VK_F);JMenuItemfileExitMenuItem=newJMenuItem("Exit",KeyEvent.VK_X);fileExitMenuItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){actionExit();}});fileMenu.add(fileExitMenuItem);menuBar.add(fileMenu);setJMenuBar(menuBar);// Set up button panel.JPanelbuttonPanel=newJPanel();backButton=newJButton("< Back");backButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){actionBack();}});backButton.setEnabled(false);buttonPanel.add(backButton);forwardButton=newJButton("Forward >");forwardButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){actionForward();}});forwardButton.setEnabled(false);buttonPanel.add(forwardButton);locationTextField=newJTextField(35);locationTextField.addKeyListener(newKeyAdapter(){publicvoidkeyReleased(KeyEvente){if(e.getKeyCode()==KeyEvent.VK_ENTER){actionGo();}}});buttonPanel.add(locationTextField);JButtongoButton=newJButton("GO");goButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){actionGo();}});buttonPanel.add(goButton);// Set up page display.displayEditorPane=newJEditorPane();displayEditorPane.setContentType("text/html");displayEditorPane.setEditable(false);displayEditorPane.addHyperlinkListener(this);getContentPane().setLayout(newBorderLayout());getContentPane().add(buttonPanel,BorderLayout.NORTH);getContentPane().add(newJScrollPane(displayEditorPane),BorderLayout.CENTER);}// Exit this program.privatevoidactionExit(){System.exit(0);}// Go back to the page viewed before the current page.privatevoidactionBack(){URLcurrentUrl=displayEditorPane.getPage();intpageIndex=pageList.indexOf(currentUrl.toString());try{showPage(newURL((String)pageList.get(pageIndex-1)),false);}catch(Exceptione){}}// Go forward to the page viewed after the current page.privatevoidactionForward(){URLcurrentUrl=displayEditorPane.getPage();intpageIndex=pageList.indexOf(currentUrl.toString());try{showPage(newURL((String)pageList.get(pageIndex+1)),false);}catch(Exceptione){}}// Load and show the page specified in the location text field.privatevoidactionGo(){URLverifiedUrl=verifyUrl(locationTextField.getText());if(verifiedUrl!=null){showPage(verifiedUrl,true);}else{showError("Invalid URL");}}// Show dialog box with error message.privatevoidshowError(StringerrorMessage){JOptionPane.showMessageDialog(this,errorMessage,"Error",JOptionPane.ERROR_MESSAGE);}// Verify URL format.privateURLverifyUrl(Stringurl){// Only allow HTTP URLs.if(!url.toLowerCase().startsWith("http://"))returnnull;// Verify format of URL.URLverifiedUrl=null;try{verifiedUrl=newURL(url);}catch(Exceptione){returnnull;}returnverifiedUrl;}/* Show the specified page and add it to
the page list if specified. */privatevoidshowPage(URLpageUrl,booleanaddToList){// Show hour glass cursor while crawling is under way.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));try{// Get URL of page currently being displayed.URLcurrentUrl=displayEditorPane.getPage();// Load and display specified page.displayEditorPane.setPage(pageUrl);// Get URL of new page being displayed.URLnewUrl=displayEditorPane.getPage();// Add page to list if specified.if(addToList){intlistSize=pageList.size();if(listSize>0){intpageIndex=pageList.indexOf(currentUrl.toString());if(pageIndex<listSize-1){for(inti=listSize-1;i>pageIndex;i--){pageList.remove(i);}}}pageList.add(newUrl.toString());}// Update location text field with URL of current page.locationTextField.setText(newUrl.toString());// Update buttons based on the page being displayed.updateButtons();}catch(Exceptione){// Show error messsage.showError("Unable to load page");}finally{// Return to default cursor.setCursor(Cursor.getDefaultCursor());}}/* Update back and forward buttons based on
the page being displayed. */privatevoidupdateButtons(){if(pageList.size()<2){backButton.setEnabled(false);forwardButton.setEnabled(false);}else{URLcurrentUrl=displayEditorPane.getPage();intpageIndex=pageList.indexOf(currentUrl.toString());backButton.setEnabled(pageIndex>0);forwardButton.setEnabled(pageIndex<(pageList.size()-1));}}// Handle hyperlink's being clicked.publicvoidhyperlinkUpdate(HyperlinkEventevent){HyperlinkEvent.EventTypeeventType=event.getEventType();if(eventType==HyperlinkEvent.EventType.ACTIVATED){if(eventinstanceofHTMLFrameHyperlinkEvent){HTMLFrameHyperlinkEventlinkEvent=(HTMLFrameHyperlinkEvent)event;HTMLDocumentdocument=(HTMLDocument)displayEditorPane.getDocument();document.processHTMLFrameHyperlinkEvent(linkEvent);}else{showPage(event.getURL(),true);}}}// Run the Mini Browser.publicstaticvoidmain(String[]args){MiniBrowserbrowser=newMiniBrowser();browser.show();}}