/* This object displays a textual help message in a scrolling text box. The message displayed is initially read from a file in the same directory from which the applet was loaded. The function getHelp takes a String as an argument, which is used to form the name of the pertinent help file. For example, if the String "Calendar" is passed in, the object looks in the home directory for a file named "Calendar.hlp" This should be a flat text file; the object formats the string to account for line breaks. Additional line- breaks may be inserted in the file, which will result in an extra blank line of output. NOTE that because of bugs in some browsers, it is advisable to put something on every line. That s, if a blank line is desired, add a couple of spaces before hitting the return key. Some browsers return non- sensical results when confronted by a line which only contains the newline character. The file is read form the home server the first time it's help request is made. The file is then placed in a hash table, and subsequent calls for this item will access it from there, rather than doing another download. This way, we only download files when the user requests it, and then only once. The object also displays a Credits button, which, when selected, uses the text area to display information about the people involved in this project, references, and other information. It also maintains a drop-down list of pertinent URL's which the user of the program may find interesting. The URL's are stored in another text file back o the server, with each URL entry consisting of two lines: the first line is the title to be displayed in the drop-down list, and the second line is the URL to connect with. Note again that some browsers treat these files strangely; the URL file, called "Sites", must NOT contain a linefeed as it's final character; the last thing in the file should be the final character of the final URL. */ // 11/23/97 Changed this from a Dialog to a Panel. import java.awt.*; import java.util.*; import java.net.*; import java.io.*; import java.applet.*; import WrapTextArea; public class textHelpPanel extends Panel { private Font textFont; private int Width; private int Height; private URL home; private Hashtable helpTable; private BorderLayout mainLayout; private WrapTextArea textDisplay; //private TextArea textDisplay; private Button OKbutton; private Button creditsButton; private Label sitesLabel; private boolean gotMessage = false; private boolean sitesValid = false; private Choice sites; private Hashtable siteTable; private AppletContext context; public textHelpPanel(int width, int height, Font tFont, URL base, AppletContext ctxt) { textFont = tFont; Width = width; Height = height; // Set the location of the directory we originated from. home = base; context = ctxt; // helpTable holds strings to be displayed in the help panel. // Initially each entry is null; in this case, when help is // requested, the program attempt to load a help file from // home. Subsequent help requests for this item, therefore, // don't have to be downloaded. helpTable = new Hashtable(10); // Another hash table to hold URL's of related sites we may // wish to connect to. siteTable = new Hashtable(10); sites = new Choice(); sites.setBackground(Color.white); String title; String location; // See if we can get a list of URL's. try { context.showStatus("Downloading Site List"); InputStream textStream = new URL(home.getProtocol(), home.getHost(), home.getFile() + "Sites").openStream(); DataInput d = new DataInputStream(textStream); while ( (title = d.readLine()) != null) { location = d.readLine(); if (title.length() != 0) siteTable.put(title, location); sites.addItem(title); } sitesValid = true; context.showStatus(""); } catch (MalformedURLException e) { sitesValid = false; } catch (IOException e) { sitesValid = false; } mainLayout = new BorderLayout(50, 50); setLayout(mainLayout); setBackground(Color.lightGray); setFont(tFont); // Silly experiment. String tempString = "X" + System.getProperty("line.separator"); for (int i = 0; i < 50; i++) tempString += "X" + System.getProperty("line.separator"); textDisplay = new WrapTextArea(tempString); //textDisplay = new TextArea(null, 12, 40, TextArea.SCROLLBARS_VERTICAL_ONLY); textDisplay.setBackground(Color.white); textDisplay.setEditable(false); OKbutton = new Button("Return"); OKbutton.setBackground(Color.white); creditsButton = new Button("Credits"); creditsButton.setBackground(Color.white); Panel t = new Panel(); t.setLayout(new BorderLayout(20,20)); t.add("Center", textDisplay); Panel b = new Panel(); b.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 10)); b.add(creditsButton); b.add(OKbutton); if (sitesValid == true) { Panel s = new Panel(); sitesLabel = new Label("Related Sites", Label.CENTER); s.setLayout(new GridLayout(2, 0, 0, 0)); s.add(sitesLabel); s.add(sites); b.add(s); } Panel top = new Panel(); Panel right = new Panel(); right.resize(20,20); Panel left = new Panel(); left.resize(20,20); add("North", top); add("East", right); add("West", left); add("South", b); add("Center", t); resize((7*Width)/10, (7*Height)/10); } public void getHelp(String category) { String text = (String)helpTable.get(category); String tempString = ""; if (text == null) { String line; // If no entry yet for this category, attempt to read in // the associated help file. If this fails, substitute // a failure string for this entry. try { context.showStatus("Downloading file..."); InputStream textStream = new URL(home.getProtocol(), home.getHost(), home.getFile() + category + ".hlp").openStream(); DataInput d = new DataInputStream(textStream); while ( (line = d.readLine()) != null) { tempString = tempString + line + "\n"; } context.showStatus(""); } catch (MalformedURLException e) { tempString = "No help available for this topic."; } catch (IOException e) { tempString = "No help available for this topic."; } helpTable.put(category, tempString); setText((String)helpTable.get(category)); } else { setText(text); } } public boolean getMessage() { boolean temp = gotMessage; gotMessage = false; return temp; } public boolean handleEvent(Event e) { boolean result = true; if ( (e.id != Event.ACTION_EVENT) && (e.id != Event.MOUSE_DOWN) ) return true; if (e.target == OKbutton) { gotMessage = true; result = false; } else if (e.target == creditsButton) { result = true; getHelp("Credits"); } else if (e.target == sites) { result = true; try { String temp = sites.getSelectedItem(); URL url = new URL((String)siteTable.get(temp)); context.showDocument(url); } catch (MalformedURLException ex) { System.out.println("Bad URL"); } } return result; } public void setText(String text) { textDisplay.setText(text); } }