/*
 * Aleph Toolkit
 *
 * Copyright 1997-1998, Brown University, Providence, RI.
 * 
 *                         All Rights Reserved
 * 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose other than its incorporation into a
 * commercial product is hereby granted without fee, provided that the
 * above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Brown University not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.
 * 
 * BROWN UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
 * PARTICULAR PURPOSE.  IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE FOR
 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

package aleph.gui;

import aleph.Config;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;
import java.util.Properties;

/**
 * Display <i>Property</i> message.
 **/

public class PropertiesDialog extends Dialog {
  Label label;
  Button okButton;
  Properties properties;
  TextArea textArea;

  static final Font font = new Font("Monospaced", 12, java.awt.Font.PLAIN);
  static final int WIDTH = 60;	// max window width
  static final int HEIGHT = 24; // max window height

  static public void  main (String[] args) {
    Frame frame = new Frame();
    new PropertiesDialog(frame, System.getProperties());
    System.exit(0);
  }

  public PropertiesDialog(Frame frame, Properties properties) {
    super(frame, "Properties", true);

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
	setVisible(false);
	dispose();
      }
    });

    Panel panel = new Panel();
    int maxKey = 0, maxVal = 0;
    // collect stuff
    int height = properties.size();
    String[] key = new String[height];
    String[] val = new String[height];
    int index = 0;
    for (Enumeration enum = properties.propertyNames();
	 enum.hasMoreElements(); index++) {
      key[index] = (String) enum.nextElement();
      val[index] = properties.getProperty(key[index]);
      maxKey = Math.max(maxKey, key[index].length());
      maxVal = Math.max(maxVal, val[index].length());
    }
    int width  = maxKey + maxVal + 1;
    int scrollbars;		// do we need scrollbars?
    if (width <= WIDTH) {	// no horizontal
      if (height <= HEIGHT)	// no horizontal, no vertical
	scrollbars = TextArea.SCROLLBARS_NONE;
      else			// no horizontal, yes vertical
	scrollbars = TextArea.SCROLLBARS_VERTICAL_ONLY;
    } else {			// yes horizontal
      if (height <= HEIGHT)	// yes horizontal, no vertical
	scrollbars = TextArea.SCROLLBARS_HORIZONTAL_ONLY;
      else			// yes horizontal, yes vertical
	scrollbars = TextArea.SCROLLBARS_BOTH;
    }
    textArea = new TextArea("", Math.min(HEIGHT, height),
			    Math.min(WIDTH, width),
			    scrollbars);
    textArea.setFont(font);
    for (int i = 0; i < height; i++) {
      if (i > 0)
	textArea.append("\n");
      textArea.append(key[i]);
      for (int j = key[i].length(); j < (maxKey + 1); j++) 
	textArea.append(" ");	// fill
      textArea.append(val[i]);
    }
    textArea.setEditable(false);
    panel.add(textArea);

    okButton = new Button("OK");
    okButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) {setVisible(false); dispose();}
    });
    panel.add(okButton);
    add(panel);
    pack();
    setVisible(true);
  }

}
