/** @author Scott Marshall @author University of Glasgow, MSc IT Project 2001 @author Building an OnLine Course in Computing Fundamentals Multiple Choice Questions */ package ukacgla_HTMLMulti; import ukacgla_HTMLMulti.*; import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; import javax.swing.text.html.HTMLEditorKit; import javax.swing.border.*; /** Class includes hyperlink listener. This is not used in this implementation, but future expansions may require it. */ public class Viewer extends JPanel{ //instance variables private HTMLEditorKit edit; private JEditorPane html; private URL display; /** Editor pane constructed with a URL to display upon instantiation. The int size argument is used to set the size of the display area. */ public Viewer(URL display, int size){ this.display = display; html = new JTextPane(); edit = new HTMLEditorKit(); html.setEditorKit(edit); html.setEditable(false); html.addHyperlinkListener(new LinkClick());//not sure if use, but leave in anyway. JScrollPane jsp = new JScrollPane(html,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); jsp.setPreferredSize(setDimension(size)); jsp.setBorder(new BevelBorder(BevelBorder.LOWERED)); displayPage(display); add("Center",jsp); } /** Blank editor pane constructed. No URL is displayed upon instantiation */ public Viewer(int size){ html = new JTextPane(); edit = new HTMLEditorKit(); html.setEditorKit(edit); html.setEditable(false); html.addHyperlinkListener(new LinkClick());//not sure if use, but leave in anyway. JScrollPane jsp = new JScrollPane(html,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); jsp.setPreferredSize(setDimension(size)); jsp.setBorder(new BevelBorder(BevelBorder.LOWERED)); add("Center",jsp); } /** Will display specified URL on the editor pane. Displays blank if no URL is specified. Warning - displaying text that has not been formatted as HTML causes CascadingStyleSheet related exceptions if the program is not restarted. The catch (IO Exception) is for developers - it should never be used as part of normal use. */ protected void displayPage(URL display){ this.display = display; try { html.setPage(display); } catch (IOException e) { html.setText("Error in displaying URL" + display); } html.revalidate(); html.repaint(); } /** Strings must be formatted as HTML to avoid CascadingStyleSheet exceptions being thrown. Use the TestUI mkHTMLString method before feeding in a string. */ protected void displayPage(String s){ this.display = null; html.setText(s); html.revalidate(); html.repaint(); } /** Returns Dimension based on int supplied as argument. An int 1 will return a Dimension of 350 x 100, an int 2 will return a Dimension of 350 x 200 */ protected Dimension setDimension(int n) { Dimension d = new Dimension(); if (n == 1) d.setSize(350,100); if (n==2) d.setSize(350,200); return d; } //--------------------------HYPER LINK LISTENER FILE------------------------------------ /** LinkClick is not used in this implementation. It is included for completeness, and if a future expansion of the package requires it. */ protected class LinkClick implements HyperlinkListener{ public void hyperlinkUpdate(HyperlinkEvent e){ if (e.getEventType()== HyperlinkEvent.EventType.ACTIVATED) { try { html.setPage(e.getURL()); } catch(IOException x2){ System.err.println(e.getURL()); } html.repaint(); html.revalidate(); } } }//ends linkClick }//ends class