//package EDU.syr.phy.mra; // from package dev.metin.OSC36 on 28Jan96 // further develpoment, see javadoc comment below. //import EDU.syr.phy.mra.Style; import java.applet.Applet; import java.awt.*; import java.util.*; /** This is the window that pops up when we click on the help button. * * An example use for the HelpWindow, without an image displayed above the help text, * would be, in the main code: *
*     helpButton = new Button();
*     helpButton.setLabel("Help");
*     rightControls.add(helpButton); 
*   
* and int the action method: *
*    public boolean action(Event e, Object arg) {
*      ...
*      if (target == helpButton) {
*        Dimension dim = new Dimension(55,5);
*        help = new HelpWindow((Image)null,"Window name","Message string",dim);
*        help.repaint();
*        help.show();
*        return true;
*      }
*      ...
*    }
*   
*
Source: *
http://ice.syr.edu/javasrc/EDU/syr/phy/mra/HelpWindow.java *
*@author Metin Sezgin *@author Simeon Warner *@version 17 February 1997 */ public class HelpWindow extends Frame { boolean inAnApplet = true; HelpPanel help; // Panel part of the window which contains the HelpCanvas. Image helpImage; Panel parentPanel; // The panel containing this object. TextArea textArea; // The area where we will display the help text. String org_str; // Store the original unprocessed string passes to the constructor. String new_str; // Current parsed string. (For the textArea.) /** * Constructor for help window with no image. *

* Equivalent to HelpWindow((Image)null, name, text, dim) */ public HelpWindow(String name, String text, Dimension dim) { setTitle(name); // Name is passed by the user. setResizable(true); // By default this window is resizable. // // Create layout for frame GridBagLayout gridBag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gridBag); c.gridwidth = 1; c.gridheight = 1; c.fill = GridBagConstraints.BOTH; // // Create text area with size given by input parameter textArea = makeText(text,dim); gridBag.setConstraints(textArea, c); add(textArea); // // Why did we remove validate? pack(); show(); } /** * Full constructor for help window with image and text portions. *

* The constructor takes four arguments:

* @param image is an image to be displayed before text in the help window. * If no image is required this can be set to (Image)null. * @param name is the name of the help window. * @param text is the text to be displayed. * @param dim is the requested initial dimension of the text area. */ public HelpWindow(Image image, String name, String text, Dimension dim) { setTitle(name); // Name is passed by the user. setResizable(true); // By default this window is resizable. // // Create layout for frame GridBagLayout gridBag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gridBag); c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 1.0; c.weighty = 1.0; // // Create image panel (if necessary) helpImage=image; if (helpImage!=(Image)null) { // Do this if we want a canvas area for images. this.setResizable(false); // If we have a cavas area, then make this help = new HelpPanel(helpImage); // window not resizable. gridBag.setConstraints(help,c); add(help); } // // Create text area with size given by input parameter textArea = makeText(text,dim); // c.gridheight = GridBagConstraints.REMAINDER; // last component in column gridBag.setConstraints(textArea, c); add(textArea); // // Why did we remove validate? pack(); show(); } private TextArea makeText(String text, Dimension dim) { String tmp_str; TextArea ta; // setFont(Style.helpFont); org_str=text; tmp_str = formatText(text,dim.width); // Process this string properly. new_str = tmp_str; // // Note constructor for TextArea uses y then x in text columns ta = new TextArea(dim.height, dim.width); ta.appendText(new_str); ta.setBackground(Style.bgDiagramColor); ta.setEditable(false); // textArea is not editable. // return ta; } public void paint(Graphics g) { pack(); int size = getFont().getSize(); Dimension d = new Dimension((int)((this.size().width-35)/(size*7.0/12.0)),this.size().height/(2*10)); // // Process the sting again for the current window size String tmp_str = formatText(org_str,d.width); textArea.replaceText(tmp_str,0,new_str.length()); new_str = tmp_str; } /** * Function to format text for display in a window or given width. It replaces * spaces with newline ('\n') characters. If unbroken strings are longer than * the width of the window they will be left unbroken and the user will have * to use the scrollbar to view the text. The line will also be broken at * newline characters already int the text. *

* Rewrite of Metin Sezgin's version to cope with existing newline characters * by Simeon Warner, 17Feb97. *

* @param arg The string to process. * @param width Width (in characters) that the text should be formatted for. * @returns ssb Formatted string. */ String formatText(String arg, int width) { //int i=0, j=0, tmp=0; // Some helpful variables. //boolean ex=true; // If this is false break out of the second for loop. String s; StringBuffer sb = new StringBuffer(arg); int col=0, stcol, spcol, endcol=width-1; while (colEvent.WINDOW_ICONIFY and Event.WINDOW_DESTROY * events and closes the window. Hence, when the user tries to close or minimize the * HelpWindow, it is destroyed. */ public boolean handleEvent(Event event) { if ((event.id == Event.WINDOW_ICONIFY) || (event.id == Event.WINDOW_DESTROY) ) { if (inAnApplet) { dispose(); } else { System.exit(0); } } return super.handleEvent(event); } } /***** end of class HelpWindow *****/ ///////////////////////////////////////////////////////////////////////////////////// /** * HelpPanel objects are used as the top, image containing, part of the a * HelpWindow * * @author Metin Sezgin * @author Simeon Warner (modifications) * @version 13 February 1997 */ class HelpPanel extends Panel { HelpCanvas canv; // Canvas holding the image. /** * Creates panel with HelpCanvas inside and border around. Image specified will * be displayed inside the HelpCanvas. * @param helpImage Image to be displayed. */ public HelpPanel(Image helpImage) { super(); //Set layout to one that makes its contents as big as possible. setLayout(new GridLayout(1,1)); canv = new HelpCanvas(helpImage); add(canv); show(); } /** * Sets the border around the layout to the correct size for the frame. */ public Insets insets() { return new Insets(4,4,5,5); } /** * Draw a frame around the Canvas. */ public void paint(Graphics g) { Dimension d = size(); Color bg = getBackground(); g.setColor(bg); g.draw3DRect(0, 0, d.width - 1, d.height - 1, true); g.draw3DRect(3, 3, d.width - 7, d.height - 7, false); } } /***** end of class HelpPanel *****/ ////////////////////////////////////////////////////////////////////////////////////// /** * Canvas that we add inside the HelpPanel. * * @author Metin Sezgin * @author Simeon Warner (modifications) * @version 14 February 1997 */ class HelpCanvas extends Canvas { Image helpImage; Dimension imageDim; // Dimension of the image. boolean gotImage = false; String noImageStr = "Error - failed to load image."; /** * Constructor simply creates canvas and stores reference to the images. * @param i Help image to be displayed in canvas. */ HelpCanvas(Image i) { super(); helpImage=i; } /** * Find the size of canvas required to fit in the string noImageStr * with a suitable border. */ private Dimension errorStrDimension() { int ih, iw; Graphics g; FontMetrics fm; g = getGraphics(); fm = g.getFontMetrics(); iw = (fm.stringWidth(noImageStr)*3)/2; ih = (fm.getHeight()*3)/2; return new Dimension(iw,ih); } /** * Overload minimumSize() in Canvas to give real size of image. */ public Dimension minimumSize() { return imageSize(); } /** * Overload maximumSize() in Canvas to give real size of image. */ public Dimension maximumSize() { return imageSize(); } /** * Overload preferredSize() in Canvas to give real size of image. */ public Dimension preferredSize() { return imageSize(); } /** * This method returns the dimension of the image in pixels. * We leave the while() loop as soon as the dimension of the * becomes avaliable. */ public Dimension imageSize() { int iw,ih=-1,j=0; if (helpImage == (Image)null) { imageDim = new Dimension(0,0); } else { while (( ((iw=helpImage.getWidth(this)) == -1) || ((ih=helpImage.getHeight(this)) == -1) ) && (j++<100) ) { // Why is this loop in here, is it the case that the routines // getWidth and getHeight sometimes give -1 first before // returning the coreect values. // --- ask Metin! } if ((iw<0) || (ih<0)) { imageDim = errorStrDimension(); gotImage=false; } else { imageDim = new Dimension(iw,ih); gotImage=true; } } return imageDim; } /** * Paints canvas with either the image given to the constructor or an error * message if the image wasn't loaded. The color Style.errorColor * is used for the error message. */ public void paint(Graphics g) { setBackground(Style.bgDiagramColor); if (helpImage != (Image)null) { imageDim = imageSize(); if (gotImage) { g.drawImage(helpImage, 0, 0, this); // Draw the image. } else { g.setColor(Style.errorColor); g.drawString(noImageStr,imageDim.width/6,(imageDim.height*3)/4); } } } } /***** end of HelpCanvas *****/