import java.awt.*;
import java.io.*;
import javax.swing.*;

//1111111111111111111111111111111111111
public class Util {

//-------------------------------------
public static void DspMsg(String msg, JFrame mf) {
JScrollPane sp;
JTextArea   ta;
  ta = new JTextArea(10,30);
  ta.setEditable(false);
  ta.setLineWrap(true);
  ta.setWrapStyleWord(true);
  ta.setText(msg);
  ta.setCaretPosition(0);
  sp = new JScrollPane(ta);
  sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  JOptionPane.showMessageDialog(mf, sp);
}

//-------------------------------------
public static void DspMsg(String msg, Font dmf, JFrame mf) {
JScrollPane sp;
JTextArea   ta;
  ta = new JTextArea(10,30);
  ta.setFont(dmf);
  ta.setEditable(false);
  ta.setLineWrap(true);
  ta.setWrapStyleWord(true);
  ta.setText(msg);
  ta.setCaretPosition(0);
  sp = new JScrollPane(ta);
  sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  JOptionPane.showMessageDialog(mf, sp);
}

//-------------------------------------
public static int GetIntValue(String cap, int def, JFrame mf) {
int     rv;
String  val;
  rv = -1;
  val = JOptionPane.showInputDialog(cap, Integer.toString(def));
  try {
    rv = Integer.valueOf(val);
  } catch(Exception e) { DspMsg(e.toString(), mf); }
  return(rv);
}

//-------------------------------------
public static int GetListIndex(String cap, String[] lv, JFrame mf) {
boolean fnd;
int     i, ns, rvi;
String  rv;
  rv = lv[0];
  rv = (String)JOptionPane.showInputDialog(mf, "Select", cap, JOptionPane.PLAIN_MESSAGE, null, lv, rv);
  if (rv==null) return(-1);
  rvi = 0;
  i = 0;
  ns = lv.length;
  fnd = false;
  do {
    if (rv.compareToIgnoreCase(lv[i])==0) {
      fnd = true;
      rvi = i;
    }
    i++;
  } while ((!fnd) && (i<ns));
  return(rvi);
}

//-------------------------------------
public static String GetListValue(String cap, String[] lv, JFrame mf) {
String  rv;
  rv = lv[0];
  rv = (String)JOptionPane.showInputDialog(mf, "Select", cap, JOptionPane.PLAIN_MESSAGE, null, lv, rv);
  return(rv);
}

//-------------------------------------
public static String GetStringValue(String cap, String def, JFrame mf) {
String  rv;
  rv = "";
  rv = JOptionPane.showInputDialog(mf, cap, def);
  return(rv);
}

//-------------------------------------
public static void PutUIManFonts(Font uif) { 
  UIManager.put("OptionPane.buttonFont", uif);
  UIManager.put("OptionPane.font", uif);
  UIManager.put("OptionPane.messageFont", uif);
  UIManager.put("TextArea.font", uif);
  UIManager.put("TextField.font", uif);
  UIManager.put("List.font", uif);
  UIManager.put("Button.font", uif);
  UIManager.put("ComboBox.font", uif);
  UIManager.put("Tree.font", uif);
  UIManager.put("ScrollPane.font", uif);
  UIManager.put("EditorPane.font", uif);
  UIManager.put("Label.font", uif);
  UIManager.put("ToolBar.font", uif);
  UIManager.put("Spinner.font", uif);
  UIManager.put("Viewport.font", uif);
  UIManager.put("MenuBar.font", uif);
  UIManager.put("ColorChooser.font", uif);
  UIManager.put("FormattedTextField.font", uif);
  UIManager.put("RadioButton.font", uif);
  UIManager.put("PopupMenu.font", uif);
  UIManager.put("MenuItem.font", uif);
  UIManager.put("Table.font", uif);
  UIManager.put("CheckBox.font", uif);
  UIManager.put("ToggleButton.font", uif);
  UIManager.put("Slider.font", uif);
  UIManager.put("TabbedPane.font", uif);
  UIManager.put("TableHeader.font", uif);
  UIManager.put("TextPane.font", uif);
  UIManager.put("ProgressBar.font", uif);
  UIManager.put("Menu.font", uif);
  UIManager.put("ToolTip.font", uif);
  UIManager.put("Panel.font", uif);
}

//-------------------------------------
public static String ReadTextFile(File f, JFrame mf) {
BufferedReader br;
char[]         buf;
FileReader     fr;
int            read;
String         txt;
  txt = "";
  buf = new char[0x1000];
  try {
    fr = new FileReader(f);
    br = new BufferedReader(fr);
    do {
      read = br.read(buf, 0, 0x1000);
      if (read!=-1) txt += new String(buf, 0, read);
    } while(read!=-1);
    br.close();
    fr.close();
  } catch(Exception e) { DspMsg(e.toString(), mf); }
  return(txt);
}

//-------------------------------------
public static void SaveTextFile(File f, String s, JFrame mf) {
BufferedWriter  bw;
FileWriter      fw;
  try {
    fw = new FileWriter(f);
    bw = new BufferedWriter(fw);
    bw.write(s,0,s.length());
    bw.close();
    fw.close();
  } catch(Exception e) { DspMsg(e.toString(), mf); }
}

//1111111111111111111111111111111111111
}
