//
// Mini Java Editor dialogs
//

import iss.awt.*;
import java.io.*;
import java.awt.*;

//
// Project options dialog
//

class OptionsDialog extends VDialog {
  MJE editor;
  HPanel p;
  TextField main, html, cpath, odir;
  Checkbox optimize, debug; 
  Choice fonts, styles, sizes;
  Button close;
  public String result;

  public OptionsDialog(Frame parent) {
    super(parent, "Project Options", false);
    editor = (MJE) parent;
    setBackground(Color.lightGray);

  // Main class

    main = new TextField(editor.main, 10);
    p = addPanel();
    p.add(50, new Label("Main Class:"));
    p.add(50, main);

  // HTML file

    html = new TextField(editor.html, 10);
    p = addPanel();
    p.add(50, new Label("HTML File:"));
    p.add(50, html);

  // Class path

    cpath = new TextField(editor.cpath, 10);
    p = addPanel();
    p.add(50, new Label("Class Path:"));
    p.add(50, cpath);

  // Output directory

    odir = new TextField(editor.odir, 10);
    p = addPanel();
    p.add(50, new Label("Output Directory:"));
    p.add(50, odir);

  // Compiler options

    p = addPanel();
    optimize = new Checkbox("Optimize");
    debug = new Checkbox("Debug Information");
    optimize.setState(editor.optimize);
    debug.setState(editor.debug);
    p.add(0, new Label("Compiler:"));
    p.add(0, optimize);
    p.add(0, debug);

  // Close button

    p = addPanel(5, 0, 80);
    close = new Button("Close");
    p.add(0, close);
  }

  public boolean handleEvent(Event evt) {
    if (evt.id == Event.WINDOW_DESTROY) 
      dispose();

    return super.handleEvent(evt);
  }

  public boolean action(Event evt, Object obj) {
    Object tgt = evt.target;

    if (tgt == close) {
      String str = (String) obj;
      editor.main = main.getText();
      editor.html = html.getText();
      editor.cpath = cpath.getText();
      editor.odir = odir.getText(); 

      dispose();
      return true;
    }
    else if (tgt == optimize) {
      editor.optimize = optimize.getState();
      return true;
    }
    else if (tgt == debug) {
      editor.debug = debug.getState();
      return true;
    }

    return false;
  }
}

//
// Preferences dialog

class PreferencesDialog extends VDialog {
  MJE editor;
  HPanel p;
  Choice fonts, styles, sizes;
  TextField width, height, browser;
  Button browse, close;
  public String result;

  public PreferencesDialog(Frame parent) {
    super(parent, "Preferences", false);
    editor = (MJE) parent;
    setBackground(Color.lightGray);

  // Font selection
   
    fonts = new Choice();
    styles = new Choice();
    sizes = new Choice();

    for (int i = 0; i < editor.fonts.length; i++)
      fonts.addItem(editor.fonts[i]);

    fonts.select(editor.font.getName());

    for (int i = 0; i < editor.styles.length; i++)
      styles.addItem(editor.styles[i]);
   
    styles.select(editor.font.getStyle());

    for (int i = 8; i < 21; i++)
      sizes.addItem(String.valueOf(i));

    sizes.select(String.valueOf(editor.font.getSize()));

    p = addPanel();
    p.add(0, new Label("Font:"));
    p.add(0, fonts);
    p.add(0, styles);
    p.add(0, sizes);

  // Window size

    p = addPanel();
    width = new TextField(String.valueOf(editor.width), 3);
    height = new TextField(String.valueOf(editor.height), 3);
    p.add(0, new Label("Window Size (Chars)   Width:"));
    p.add(0, width);
    p.add(0, new Label("Height:"));
    p.add(0, height);

  // Web browser

    p = addPanel();
    browser = new TextField(editor.browser, 10);
    browse = new Button("Browse...");
    p.add(30, new Label("Web Browser:"));
    p.add(50, browser);
    p.add(20, browse);

  // Close button

    p = addPanel(5, 0, 80);
    close = new Button("Close");
    p.add(0, close);
  }

  public boolean handleEvent(Event evt) {
    if (evt.id == Event.WINDOW_DESTROY) 
      dispose();

    return super.handleEvent(evt);
  }

  public boolean action(Event evt, Object obj) {
    Object tgt = evt.target;

    if (tgt == browse) {
      FileDialog dialog = new FileDialog(editor, "Locate Web Browser", 
	FileDialog.LOAD);

      dialog.show();
      String file = dialog.getFile();

      if (file != null)
	browser.setText(dialog.getDirectory() + file);

      return true;
    }
    else if (tgt == close) {
      String font = fonts.getSelectedItem();
      int style = 0;

      for (int i = 0; i < editor.styles.length; i++)
	if (editor.styles[i].equals(styles.getSelectedItem()))
	  style = i;

      int size = new Integer(sizes.getSelectedItem()).intValue();
 
      editor.changeFont(new Font(font, style, size));
      editor.width = Integer.parseInt(width.getText());
      editor.height = Integer.parseInt(height.getText());
      editor.browser = browser.getText();

      try {
        String prefPath = System.getProperty("user.home") + 
	  System.getProperty("file.separator") + ".mje";

        FileOutputStream fs = new FileOutputStream(prefPath);
        PrintStream ps = new PrintStream(fs);

        ps.println(font);
        ps.println(style);
        ps.println(size);
	ps.println(editor.width);
	ps.println(editor.height);
	ps.println(editor.browser);

        fs.close();
        dispose();
      }
      catch(Exception err) {
	editor.console.set("Unable to save preference.");
      }

      return true;
    }

    return false;
  }
}

//
// Go to line dialog
//

class GotoDialog extends VDialog {
  MJEWindow win;
  HPanel p;
  TextField lineText;
  Button go, close;

  public GotoDialog(Frame parent) {
    super(parent, "Go to Line...", false);
    win = (MJEWindow) parent;
    setBackground(Color.lightGray);

    lineText = new TextField("", 7);
    go = new Button("Go");
    close = new Button("Close");

    p = addPanel();
    p.add(0, new Label("Go to Line:"));
    p.add(0, lineText);

    p = addPanel();
    p.add(0, go);
    p.add(0, close);
  }

  public boolean handleEvent(Event evt) {
    if (evt.id == Event.WINDOW_DESTROY) 
      dispose();

    return super.handleEvent(evt);
  }

  public boolean action(Event evt, Object obj) {
    Object tgt = evt.target;

    if (tgt == lineText || tgt == go) {
      try {
      	win.gotoLine(new Integer(lineText.getText()).intValue());
      }
      catch (Exception err) { }

      return true;
    }
    else if (tgt == close) {
      dispose();
      return true;
    }

    return false;
  }
}

//
// Find dialog
//
  
class FindDialog extends VDialog {
  MJEWindow win;
  HPanel p;
  TextField findText, replaceText;
  Checkbox match;
  Button find, replace, close;

  public FindDialog(Frame parent) {
    super(parent, "Find...", false);
    win = (MJEWindow) parent;
    setBackground(Color.lightGray);

    findText = new TextField("", 10);
    replaceText = new TextField("", 10);    
    match = new Checkbox("Match Case");
    find = new Button("Find");
    replace = new Button("Replace");
    close = new Button("Close");

    p = addPanel();
    p.add(0, new Label("Find:"));
    p.add(0, findText);
    p.add(0, new Label("Replace:"));
    p.add(0, replaceText);

    p = addPanel();
    p.add(0, match);
    p.add(0, find);
    p.add(0, replace);
    p.add(0, close);

  }

  public boolean handleEvent(Event evt) {
    if (evt.id == Event.WINDOW_DESTROY) 
      dispose();

    return super.handleEvent(evt);
  }

  public boolean action(Event evt, Object obj) {
    Object tgt = evt.target;
    TextArea area = win.getTextArea();

    if (tgt == match)
      return true;

    if (tgt == close) {
      dispose();
      return true;
    }
    else if (tgt == replace) {

    // Replace text

      int start = area.getSelectionStart();
      int end = area.getSelectionEnd();

      if (end > start) 
        area.replaceText(replaceText.getText(), start, end);
    }

  // Find text

    String txt, str;
	
    if (match.getState()) {
      txt = area.getText();
      str = findText.getText();
    }
    else {
      txt = area.getText().toLowerCase();
      str = findText.getText().toLowerCase();
    }

    area.requestFocus();
    int pos = txt.indexOf(str, area.getSelectionEnd());
	
    if (pos > 0)
    area.select(pos, pos + str.length());
	
    return true;
  }
}

//
// Global Search dialog
//
  
class GlobalSearchDialog extends VDialog {
  MJE editor;
  HPanel p;
  TextField findText;
  Checkbox match;
  String result;

  public GlobalSearchDialog(Frame parent) {
    super(parent, "Global Search", true);
    editor = (MJE) parent;
    setBackground(Color.lightGray);

    findText = new TextField("", 10);
    match = new Checkbox("Match Case");

    p = addPanel();
    p.add(0, new Label("Find:"));
    p.add(0, findText);
    p.add(0, match);

    p = addPanel();
    p.add(0, new Button("OK"));
    p.add(0, new Button("Cancel"));
  }

  public boolean handleEvent(Event evt) {
    if (evt.id == Event.WINDOW_DESTROY) 
      dispose();

    return super.handleEvent(evt);
  }

  public boolean action(Event evt, Object obj) {
    if (evt.target instanceof Button) {
      dispose();

      if (((String) obj).equals("OK")) {
	String str = findText.getText();
	editor.console.set("Searching for " + str + "...");

        for (int i = 0; i < editor.project.size(); i++) {
	  String file = ((String[]) editor.project.get(i))[0];

	  try {
      	    FileInputStream fs = new FileInputStream(file);
            DataInputStream ds = new DataInputStream(fs);
	    int count = 0;
	    String txt = ds.readLine();

	    if (!match.getState())
	      str = str.toLowerCase();

	    while (txt != null) {
	      if (!match.getState())
	        txt = txt.toLowerCase();

              if (txt.indexOf(str) > 0)
	        count++;

  	      txt = ds.readLine();
	    }	    

	    if (count > 0)	  
	      editor.console.add("Found " + count + " in " + file + ".");

  	    fs.close();
	  }
	  catch (Exception err) {
      	    editor.console.add("Cannot open file " + file + ".");
          }
        }

        editor.console.add("Done.");
      }

      return true;
    }

    return false;
  }
}

//
// Info dialog
//

class InfoDialog extends VDialog {
  HPanel p;

  public InfoDialog(Frame parent, String title, String msg) {
    super(parent, title, true);
    setBackground(Color.lightGray);

    add(0, new Label(msg));

    p = addPanel(5, 0, 80);
    p.add(0, new Button("Close"));
  }

  public boolean handleEvent(Event evt) {
    if (evt.id == Event.WINDOW_DESTROY) 
      dispose();

    return super.handleEvent(evt);
  }

  public boolean action(Event evt, Object obj) {
    dispose();
    return true;
  }
}
