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

package com.togethersoft.modules.xmleditor;

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

import com.togethersoft.openapi.ide.*;
import com.togethersoft.openapi.ide.message.*;
import com.togethersoft.openapi.ide.window.*;

import com.togethersoft.modules.xmleditor.dtd.*;


public class XMLParamDialog implements ActionListener
{
  public boolean isOk = false;

  public String xmlVersion  = new String();
  public String xmlEncoding = new String();
  public String xmlPublicID = new String();
  public String xmlSystemID = new String();

  public String xmlRootElementName = new String();

  private JDialog m_dialog;
  private IdeWindowManager m_windowManager;

  private JTextField  m_versionField;
  private JTextField  m_encodingField;
  private JTextField  m_publicIDField;
  private JTextField  m_systemIDField;
  private JComboBox   m_rootElementCombo;

  private JButton m_okButton;
  private JButton m_cancelButton;

  //--------------------------------------------------------------------
  public XMLParamDialog()
  {
  }

  //--------------------------------------------------------------------
  public void init (Component parent, DTDDocumentType documentType)
  {
    m_windowManager = IdeWindowManagerAccess.getWindowManager();

    m_dialog = m_windowManager.createDialog (parent);
    m_dialog.setTitle ("XML Header Parameters");

    //-- main panel

    m_versionField  = new JTextField (xmlVersion,  30);
    m_encodingField = new JTextField (xmlEncoding, 30);
    m_publicIDField = new JTextField (xmlPublicID, 30);
    m_systemIDField = new JTextField (xmlSystemID, 30);

    m_rootElementCombo = new JComboBox (documentType.elementDefs());
    m_rootElementCombo.setSelectedItem (documentType.findElementDef (xmlRootElementName));

    JPanel mainPane = new JPanel();
    GridBagLayout gb = new GridBagLayout();
    GridBagConstraints cs = new GridBagConstraints();
    mainPane.setLayout(gb);
    cs.fill = GridBagConstraints.HORIZONTAL;

    JLabel lbl;
    cs.insets = new Insets(8,2,2,2);

    lbl = new JLabel("Root Element:");
    cs.gridwidth = 1;
    cs.weightx = 0;
    gb.setConstraints(lbl,cs); mainPane.add(lbl);

    cs.gridwidth = GridBagConstraints.REMAINDER;
    cs.weightx = 1;
    gb.setConstraints (m_rootElementCombo, cs); mainPane.add (m_rootElementCombo);

    cs.insets = new Insets(2,2,2,2);

    lbl = new JLabel("Version:");
    cs.gridwidth = 1;
    cs.weightx = 0;
    gb.setConstraints(lbl,cs); mainPane.add(lbl);

    cs.gridwidth = GridBagConstraints.REMAINDER;
    cs.weightx = 1;
    gb.setConstraints (m_versionField, cs); mainPane.add (m_versionField);


    lbl = new JLabel("Encoding:");
    cs.gridwidth = 1;
    cs.weightx = 0;
    gb.setConstraints(lbl,cs); mainPane.add(lbl);

    cs.gridwidth = GridBagConstraints.REMAINDER;
    cs.weightx = 1;
    gb.setConstraints (m_encodingField, cs); mainPane.add (m_encodingField);


    lbl = new JLabel("Public ID:");
    cs.gridwidth = 1;
    cs.weightx = 0;
    gb.setConstraints(lbl,cs); mainPane.add(lbl);

    cs.gridwidth = GridBagConstraints.REMAINDER;
    cs.weightx = 1;
    gb.setConstraints (m_publicIDField, cs); mainPane.add (m_publicIDField);

    cs.insets = new Insets(2,2,8,2);

    lbl = new JLabel("System ID:");
    cs.gridwidth = 1;
    cs.weightx = 0;
    gb.setConstraints(lbl,cs); mainPane.add(lbl);

    cs.gridwidth = GridBagConstraints.REMAINDER;
    cs.weightx = 1;
    gb.setConstraints (m_systemIDField, cs); mainPane.add (m_systemIDField);


    //-- OK/Cancel buttons panel

    IdeDialogButtonGroup dialogButtonGroup = m_windowManager.createButtonGroup();

    m_okButton = (JButton) m_windowManager.setupStandardButton(new JButton(), IdeButtonType.OK);
    m_okButton.addActionListener (this);
    dialogButtonGroup.add (m_okButton);

    m_cancelButton = (JButton) m_windowManager.setupStandardButton(new JButton(), IdeButtonType.CANCEL);
    m_cancelButton.addActionListener (this);
    dialogButtonGroup.add (m_cancelButton);

    JPanel buttonPane = new JPanel (new FlowLayout());
    buttonPane.add (m_okButton);
    buttonPane.add (m_cancelButton);


    Container contentPane = m_dialog.getContentPane();
    contentPane.setLayout (new BorderLayout (0,4));
    contentPane.add (mainPane, BorderLayout.CENTER);
    contentPane.add (buttonPane, BorderLayout.SOUTH);

    m_dialog.pack();
  }

  //--------------------------------------------------------------------
  public void scanControls()
  {
    xmlVersion  = m_versionField.getText().trim();
    xmlEncoding = m_encodingField.getText().trim();
    xmlPublicID = m_publicIDField.getText().trim();
    xmlSystemID = m_systemIDField.getText().trim();

    DTDElementDef elemenrDef = (DTDElementDef) m_rootElementCombo.getSelectedItem();
    xmlRootElementName = (elemenrDef != null) ? elemenrDef.getTagName() : new String();
  }

  //--------------------------------------------------------------------
  public void show()
  {
    m_dialog.show();
  }

  //--------------------------------------------------------------------
  public void actionPerformed (ActionEvent event)
  {
    Object caller = event.getSource();

    if (caller == m_okButton)
    {
      scanControls();
      isOk = true;
      m_dialog.dispose();
      return;
    }

    if (caller == m_cancelButton)
    {
      m_dialog.dispose();
      return;
    }
  }
}
