
import java.awt.*;
import java.io.*;
//import gjt.Border;

//------------------------------------------------------------------------------
// HelpDialog.java:
//		Creates a dialog box for displaying a help string
//------------------------------------------------------------------------------

public class HelpDialog extends Dialog
{
	//private sdaLabel canvas;
	//private OurFrame parent;
	private Button ok_button;
	private Button set_button;
	private Panel bodyPanel;
	private String help_string;
	private String topic;
	private TextArea text_area;
	private TextField field1;
	private TextField field3;

	// Constructors
	//--------------------------------------------------------------------------
    public HelpDialog (String s1,String s2,boolean modal)
	{
		super(null,"Critique",modal);
		//canvas = null;
		help_string = s2;
		//parent = f;
		topic = s1;
		ConstructHelp();
	}

	public HelpDialog (Frame f,String s1,String s2,boolean modal)
	{
		super(f,"Critique",modal);
		//canvas = null;
		help_string = s2;
		//parent = f;
		topic = s1;
		ConstructHelp();
	}

	private void ConstructHelp()
	{
	    setFont(new Font("Helvetica",Font.PLAIN,12));

		// Title
		Label label = new Label(topic);
		if(System.getProperty("java.vendor").compareTo("Netscape Communications Corporation") == 0)  {
    		Panel label_panel = new Panel();
    		label_panel.setLayout(new BorderLayout());
    		label_panel.add("West",new Label("   "));
    		label_panel.add("Center",label);
    		add("North",label_panel);
        }  else
		    add("North",label);

		// Text area
		bodyPanel = new Panel();
		bodyPanel.setLayout(new BorderLayout());
		text_area = new TextArea("",4,36);
		text_area.setFont(new Font("Helvetica",Font.PLAIN,12));
		bodyPanel.add("Center",text_area);
		bodyPanel.add("West",new Label("   "));
		bodyPanel.add("East",new Label("   "));

		/* if(canvas == null)  {
			add("Center",bodyPanel);

			// Buttons
			Panel buttonPanel = new Panel();
			ok_button = new Button("OK");
			Border blackBorder = new Border(ok_button,1);
			//blackBorder.setLineColor(parent.sdacolor.buttonborder);
			buttonPanel.setLayout(new FlowLayout());
			buttonPanel.add(blackBorder);
			add("South",buttonPanel);

			//setBackground(parent.sdacolor.dialogbackground);
			resize(400,350);
			return;
		}  */

		// Status information
		Panel statusPanel = new Panel();
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		statusPanel.setLayout(gbl);
		gbc.insets = new Insets(0,0,0,10);

		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gbc.fill = GridBagConstraints.NONE;
		Label field_name = new Label("");
		gbl.setConstraints(field_name,gbc);
		statusPanel.add(field_name);

		field1 = new TextField(15);
		Label field_name1 = new Label("Current Data's Source");
		gbc.anchor = GridBagConstraints.WEST;
		gbc.gridwidth = 1;
		gbc.fill = GridBagConstraints.NONE;
		gbl.setConstraints(field_name1,gbc);
		statusPanel.add(field_name1);
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gbc.fill = GridBagConstraints.NONE;
		gbl.setConstraints(field1,gbc);
		statusPanel.add(field1);

		field3 = new TextField(15);
		Label field_name3 = new Label("Current Value");
		gbc.anchor = GridBagConstraints.WEST;
		gbc.gridwidth = 1;
		gbc.fill = GridBagConstraints.NONE;
		gbl.setConstraints(field_name3,gbc);
		statusPanel.add(field_name3);
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gbc.fill = GridBagConstraints.NONE;
		gbl.setConstraints(field3,gbc);
		statusPanel.add(field3);

		TextField field2 = new TextField(15);
		Label field_name2 = new Label("Default Value");
/*		if((canvas.wrap.data_member.type() != 3 && canvas.wrap.defaultValue() >= 0.0)
			|| (canvas.wrap.data_member.type() == 3 && canvas.wrap.sdefaultValue().length() > 0))  {
			gbc.gridwidth = 1;
			gbc.fill = GridBagConstraints.NONE;
			gbl.setConstraints(field_name2,gbc);
			statusPanel.add(field_name2);
			gbc.gridwidth = GridBagConstraints.REMAINDER;
			gbc.fill = GridBagConstraints.NONE;
			gbl.setConstraints(field2,gbc);
			statusPanel.add(field2);
		}  */

		//if(parent.m_masterform.form_title.getText().compareTo("Results Form") != 0)
			//bodyPanel.add("South",statusPanel);
		add("Center",bodyPanel);

		// Buttons
		Panel buttonPanel = new Panel();
		ok_button = new Button("OK");
		//Border blackBorder = new Border(ok_button,1);
		//blackBorder.setLineColor(parent.sdacolor.buttonborder);
		set_button = new Button("afds");
		buttonPanel.setLayout(new FlowLayout());
		//buttonPanel.add(blackBorder);
/*		if(parent.m_masterform.form_title.getText().compareTo("Results Form") != 0)
			if((canvas.wrap.data_member.type() != 3 && canvas.wrap.defaultValue() >= 0.0)
			|| (canvas.wrap.data_member.type() == 3 && canvas.wrap.sdefaultValue().length() > 0))
				buttonPanel.add(set_button);  */
		add("South",buttonPanel);

		//setBackground(parent.sdacolor.dialogbackground);
		resize(500,250);

	/*	// Enter information
		field1.setEditable(false);
		field2.setEditable(false);
		field3.setEditable(false);
		field1.setText(canvas.wrap.data_source());
		switch(canvas.wrap.data_member.type())  {
		case 1:
			int x = (int) canvas.wrap.defaultValue();
			Integer z1 = new Integer(x);
			field2.setText(z1.toString());
			break;
		case 2:
			double y = canvas.wrap.defaultValue();
			Double z2 = new Double(y);
			if(canvas.wrap.unit != null)
				field2.setText(z2.toString()+" "+canvas.wrap.unit.getSelectedItem());
			else
				field2.setText(z2.toString());
			break;
		case 3:
			String z = canvas.wrap.sdefaultValue();
			field2.setText(z);
			break;
	    case 4:
	        break;
		}

		switch(canvas.wrap.data_member.type())  {
		case 1:
			int x = canvas.wrap.igetnumber();
			if(x >= 0)  {
				Integer z1 = new Integer(x);
				field3.setText(z1.toString());
			}
			break;
		case 2:
			double y = canvas.wrap.dgetnumber();
			if(canvas.wrap.unit != null)  {
				UnitConversion uc = new UnitConversion();
				y = uc.convertFromMKS(y,canvas.wrap.unit.getSelectedItem());
			}
			Double z2 = new Double(y);
			if(y >= 0.0)  {
				if(canvas.wrap.unit != null)
					field3.setText(z2.toString()+" "+canvas.wrap.unit.getSelectedItem());
				else
					field3.setText(z2.toString());
			}
			break;
		case 3:
			String z = canvas.wrap.getchoice();
			field3.setText(z);
			break;
		case 4:
	        if (canvas.wrap.combobox != null)
	            field3.setText(canvas.wrap.combobox.getString());
	        break;
		}  */
	}

    // Insert a string into the Help TextArea
    // An optional line length (text_area width) can be provided to this function.
    // If x is <= 0 it is ignored.
    private void helpSetText(int x)
    {
        String modified_s = "";
        String ss;
        int line_length;

        // Get help string
		String line;
		//if(canvas != null)
			//line = HelpMessage(topic);
		//else
			line = help_string;

        // Get current length of text_area in number of characters
        text_area.setFont(new Font("Helvetica",Font.PLAIN,12));
        FontMetrics fm = getFontMetrics(text_area.getFont());
        int char_width = fm.stringWidth(line)/line.length();
        if(x <= 0)
            line_length = (15*text_area.size().width/20)/char_width;
        else
            line_length = (15*x/20)/char_width;

        // Insert return characters into the string s
        StringBufferInputStream s;
		StreamTokenizer st;
	    try {
			s = new StringBufferInputStream(line);
			st = new StreamTokenizer(s);
			st.wordChars('"','"');  // treat \" as part of a word
			st.wordChars('^', '^'); // using ^^ to indicate a carriage return
			st.wordChars(')', ')');
			st.wordChars('(', '(');
			st.wordChars('/', '/');
			st.wordChars(':', ':');
			st.wordChars('-', '-');
			st.wordChars(',', ',');
			st.wordChars('\'', '\'');
			st.wordChars('=', '=');
			st.wordChars('>', '>');
			st.wordChars('%', '%');
			st.wordChars(';', ';');
			st.wordChars('$', '$');
			st.wordChars('~', '~');
			st.wordChars('@', '@');
			int state = 0;
			int current_line_length = 0;
			String value = "";
			while(state == 0)  {
				st.nextToken();
				if(st.ttype == st.TT_NUMBER)  {
				    Double z = new Double(st.nval);
				    value = z.toString();
				}
				if(st.ttype == st.TT_WORD)  {
				    value = st.sval;
				}
				if(st.ttype == st.TT_EOF || st.ttype == st.TT_EOL)  {
					state = 1;
				}  else  {
                    ss = modified_s + value;
                    if(value.compareTo("^^") == 0)  {
                        modified_s = modified_s + "\n";
                        current_line_length = 0;
                    }  else  {
                        current_line_length += value.length();
                        if(current_line_length <= line_length)  {
                            modified_s = ss + " ";
                            current_line_length += 1;
                        }  else  {
                            modified_s = modified_s + "\n" + value + " ";
                            current_line_length = value.length() + 1;
                        }
                    }
                }
			}
		}
        catch (IOException e) {  }
        finally {}

        // Insert the modified string into text_area
        text_area.setText(modified_s);

    }

    public boolean handleEvent(Event e) {
        if (e.id == Event.WINDOW_DESTROY && e.target == this) {
            dispose();
    		return true;
        }
        return super.handleEvent(e);
    }

	// Catch events in the dialog box
	//--------------------------------------------------------------------------
	public boolean action(Event event,Object obj)
	{
		Object target = event.target;

		if(target instanceof Button)
		{
			if(target == ok_button)
			{
				dispose();
			}
			if(target == set_button)
			{
			}
			return true;
		}  else
			ok_button.requestFocus();

		return false;

	}

	public boolean keyDown (Event event,int key)
	{
		// Regular return (enter) key
		if(event.id == event.KEY_PRESS && key == '\n')  {
			dispose();

		}
		return true;
	}

	public void popup()
	{
	    //if(System.getProperty("java.vendor").compareTo("Netscape Communications Corporation") == 0)  {
		    helpSetText(325);  // in Netscape, show() does not call paint()
		    bodyPanel.requestFocus();
		    show();
        //}  else  {
			//show();
		//}
	}

	public void paint(Graphics g)
	{
	    super.paint(g);

	    // Retrieve proper message, this must be in paint so that
	    // the message resizes if the user resizes that dialog box.
		helpSetText(0);

		bodyPanel.requestFocus();
	}

	private String HelpMessage(String topic)
	{
		return("");
	}

}
