/*-------------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved.
-------------------------------------------------------------------------------*/

package com.togethersoft.modules.inspector.customproperties;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import com.togethersoft.openapi.ide.*;
import com.togethersoft.openapi.ide.window.*;
import com.togethersoft.openapi.ide.message.*;
import com.togethersoft.openapi.util.ui.ToggleButtonPanel;
import com.togethersoft.openapi.util.ui.DialogHelper;

class NewCustomPropertyDialog {
  public String propertyName;
  public String propertyValue;
  public boolean isOK = false;

  public NewCustomPropertyDialog () {
    myDialog = IdeAccess.getWindowManager().createDialog (null);
    myDialog.setTitle ("Create new custom property");
    myDialog.setModal (true);
    myDialog.setResizable(false);
    myDialog.setDefaultCloseOperation (WindowConstants.DO_NOTHING_ON_CLOSE);
    myDialog.addWindowListener(new WindowListener () {
      public void windowClosing (WindowEvent e) {
        myDialog.dispose();
      }
      public void windowActivated   (WindowEvent e) { }
      public void windowDeactivated (WindowEvent e) { }
      public void windowDeiconified (WindowEvent e) { }
      public void windowIconified   (WindowEvent e) { }
      public void windowOpened      (WindowEvent e) { }
      public void windowClosed      (WindowEvent e) { }
    });

    //-- create controls
    myNameLabel = new JLabel("name");
    myValueLabel = new JLabel("value");

    myNameTf = new JTextField(null);
    myNameTf.addActionListener(new ActionListener () {
      public void actionPerformed (ActionEvent e) {
        propertyName = myNameTf.getText();
        propertyValue = myValueTf.getText();
        isOK = true;
        myDialog.dispose();
        if (myOkListener != null) {
            myOkListener.actionPerformed (e);
        }
      }
    });
    myNameTf.addKeyListener(new KeyListener () {
      public void keyPressed(KeyEvent e) {
        if (myNameTf.getText().equals(""))
          myOkButton.setEnabled(false);
        else
          myOkButton.setEnabled(true);
      }
      public void keyTyped(KeyEvent e) {
        if (myNameTf.getText().equals(""))
          myOkButton.setEnabled(false);
        else
          myOkButton.setEnabled(true);
      }
      public void keyReleased(KeyEvent e) {
        if (myNameTf.getText().equals(""))
          myOkButton.setEnabled(false);
        else
          myOkButton.setEnabled(true);
      }
    });

    myValueTf = new JTextField(null);
    myValueTf.addActionListener(new ActionListener () {
      public void actionPerformed (ActionEvent e) {
        propertyName = myNameTf.getText();
        propertyValue = myValueTf.getText();
        isOK = true;
        myDialog.dispose();
        if (myOkListener != null) {
            myOkListener.actionPerformed (e);
        }
      }
    });

    IdeDialogButtonGroup dialogButtonGroup = IdeAccess.getWindowManager().createButtonGroup();

    myOkButton = (JButton)IdeAccess.getWindowManager().setupStandardButton(null, IdeButtonType.OK);
    DialogHelper.setDefaultButton(myDialog, myOkButton);
    myOkButton.setEnabled(false);
    myOkButton.addActionListener(new ActionListener () {
      public void actionPerformed (ActionEvent e) {
        myDialog.dispose();
        myDialog = null;
        propertyName = myNameTf.getText();
        propertyValue = myValueTf.getText();
        isOK = true;
        if (myOkListener != null) {
            myOkListener.actionPerformed (e);
        }
      }
    });
    dialogButtonGroup.add(myOkButton);

    myCancelButton = (JButton)IdeAccess.getWindowManager().setupStandardButton(null, IdeButtonType.CANCEL);
    myCancelButton.addActionListener (new ActionListener () {
      public void actionPerformed (ActionEvent e) {
        isOK = false;
        myDialog.dispose();
        myDialog = null;
      }
    });
    dialogButtonGroup.add(myCancelButton);

    //-- Property name/value panel
    JPanel propertyPane = new JPanel();
    GridBagLayout layout = new GridBagLayout();
    propertyPane.setLayout (layout);

    GridBagConstraints constr = new GridBagConstraints();
    constr.fill = GridBagConstraints.HORIZONTAL;
    constr.gridwidth = GridBagConstraints.REMAINDER;
    constr.insets = new Insets (0,2,0,2);

    constr.weightx = 1;
    constr.weighty = 1;

    layout.setConstraints (myNameLabel, constr);
    propertyPane.add (myNameLabel, constr);

    layout.setConstraints (myNameTf, constr);
    propertyPane.add (myNameTf, constr);

    layout.setConstraints (myValueLabel, constr);
    propertyPane.add (myValueLabel, constr);

    layout.setConstraints (myValueTf, constr);
    propertyPane.add (myValueTf, constr);

    //-- OK/Cancel buttons panel
    JPanel buttonPane = new JPanel();
    layout = new GridBagLayout();
    buttonPane.setLayout (layout);

    constr = new GridBagConstraints();
    constr.fill = GridBagConstraints.HORIZONTAL;
    constr.insets = new Insets (2,2,2,2);

    layout.setConstraints (myOkButton, constr);
    buttonPane.add (myOkButton);

    constr.gridwidth = GridBagConstraints.REMAINDER;
    layout.setConstraints (myCancelButton, constr);
    buttonPane.add (myCancelButton);

    //-- prepare dialog
    Container contentPane = myDialog.getContentPane();

    layout = new GridBagLayout();
    contentPane.setLayout (layout);
    constr = new GridBagConstraints();
    constr.fill = GridBagConstraints.HORIZONTAL;

    constr.gridwidth = GridBagConstraints.REMAINDER;
    constr.weightx = 1;
    constr.weighty = 1;
    constr.insets = new Insets (2,2,2,2);

    layout.setConstraints (propertyPane, constr);
    contentPane.add (propertyPane, constr);

    constr.insets = new Insets (8,1,2,1);
    layout.setConstraints (buttonPane, constr);
    contentPane.add (buttonPane, constr);

    myDialog.pack();

    Dimension d = myDialog.getSize();
    if (d.width < 300) {
      d.width = 300;
      if (d.height < contentPane.getSize().height)
    	d.height = contentPane.getSize().height;
      myDialog.setSize (d);
    }

    centerMe();
  }

  public void show() {
    myDialog.show();
  }

  public void centerMe() {
    Dimension screenSize = myDialog.getToolkit().getScreenSize();
    Dimension size = myDialog.getSize();
    screenSize.height = screenSize.height/2;
    screenSize.width = screenSize.width/2;
    size.height = size.height/2;
    size.width = size.width/2;
    int y = screenSize.height - size.height;
    int x = screenSize.width - size.width;
    myDialog.setLocation (x,y);
  }

    public void addOkListener (ActionListener okListener)
    {
        myOkListener = okListener;
    }

    private ActionListener myOkListener;
  private JDialog myDialog;
  private JLabel myNameLabel;
  private JLabel myValueLabel;
  private JTextField myNameTf;
  private JTextField myValueTf;
  private JButton myOkButton;
  private JButton myCancelButton;
}

