// Sample code file: AddFieldToLayoutDlg.java

// Warning: This code has been marked up for HTML

/***************************************************************************
$name: AddFieldToLayoutDlg.java 
$version: 1.0 
$date_modified: 210104 
$description:  
$owner: Beans for Novell Services Team 
Copyright (c) 1997 Novell, Inc. All Rights Reserved.

THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE AGREEMENT
ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK) THAT CONTAINS THIS WORK.
PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO DEVELOPER A
ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE CODE IN ITS
PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS TO MARKET,
DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF DEVELOPER'S
PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR DEVELOPER'S
CUSTOMERS WITH RESPECT TO THIS CODE.
****************************************************************************/
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import com.novell.beans.NWDir.*;
import com.novell.beans.util.*;

public class AddFieldToLayoutDlg extends JDialog
{
  JPanel contentPanel = new JPanel();
  NWDir theDirBean = null;
  NWEntry theParentEntry = null;
  JButton addBtn = new JButton();
  JButton closeBtn = new JButton();
  JLabel nameLabel = new JLabel();
  JTextField nameTextField = new JTextField();
  NWLayoutDescriptions layouts = null;
  NWFieldTypes fldTypes = null;
  GroupPanel FieldDescGroup = new GroupPanel(494, 68);
  GroupPanel layoutsGroup = new GroupPanel(494, 59);
  JComboBox fieldTypeComboBox = new JComboBox();
  JComboBox layoutComboBox = new JComboBox();
  JCheckBox optionalChkBox = new JCheckBox();

  public AddFieldToLayoutDlg(JFrame frame,
                             NWDir aDirBean,
                             NWEntry parentEntry,
                             String title,
                             boolean modal)
  {
    super(frame, title, modal);

    theDirBean = aDirBean;
    theParentEntry = parentEntry;
    layouts = theDirBean.getLayouts();
    fldTypes = theDirBean.getFieldTypes();

    try {
      initialize();
      setContentPane(contentPanel);
      pack();
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    // center in window
    //
    Dimension  scrnSize = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation((scrnSize.width / 2) - 250, (scrnSize.height / 2) - 150);
  }

  public AddFieldToLayoutDlg(JFrame frame,
                             NWDir aDirBean,
                             NWEntry parentEntry)
  {
    this(frame, aDirBean, parentEntry, "Adding Field To NWLayoutDescription", false);
  }

  public AddFieldToLayoutDlg(JFrame frame,
                             NWDir aDirBean,
                             NWEntry parentEntry,
                             boolean modal)
  {
    this(frame, aDirBean, parentEntry, "Adding Field To NWLayoutDescription", modal);
  }

  public AddFieldToLayoutDlg(JFrame frame,
                             NWDir aDirBean,
                             NWEntry parentEntry,
                             String title)
  {
    this(frame, aDirBean, parentEntry, title, false);
  }

  public void initialize() throws Exception
  {
    setSize(500, 194);
     
    contentPanel.setLayout(null);
    contentPanel.setPreferredSize(new Dimension(500, 194));
    contentPanel.setSize(500, 194);

    JPanel   mainPanel = new JPanel();
    
    mainPanel.setLayout(null);
    mainPanel.setPreferredSize(new Dimension(500, 194));
    mainPanel.setSize(500, 194);

    nameLabel.setText("Layout Name:");
    nameLabel.setPreferredSize(new Dimension(84, 20));
    nameLabel.setBounds(4, 4, 84, 20);
    mainPanel.add(nameLabel);
     
    nameTextField.setPreferredSize(new Dimension(398, 20));
    nameTextField.setBounds(100, 4, 398, 20);
    mainPanel.add(nameTextField);

    FieldDescGroup.setLabel("Field Descriptions");
    FieldDescGroup.setPreferredSize(new Dimension(494, 60));
    FieldDescGroup.setBounds(4, 28, 494, 60);
    FieldDescGroup.setLayout(null);
     
    fieldTypeComboBox.addItemListener(new AddFieldToLayoutDlg_fieldTypeComboBox_itemAdapter(this));
    fieldTypeComboBox.setPreferredSize(new Dimension(343, 20));
    fieldTypeComboBox.setEditable(false);
    FieldDescGroup.add(fieldTypeComboBox);
    fieldTypeComboBox.setBounds(8, 20, 343, 20);
     
    optionalChkBox.setSelected(true);
    optionalChkBox.setText("Optional");
    optionalChkBox.setPreferredSize(new Dimension(77, 20));
    FieldDescGroup.add(optionalChkBox);
    optionalChkBox.setBounds(390, 20, 77, 20);
    mainPanel.add(FieldDescGroup);

    layoutsGroup.setLabel("Layouts");
    layoutsGroup.setPreferredSize(new Dimension(494, 60));
    layoutsGroup.setBounds(4, 96, 494, 60);
    layoutsGroup.setLayout(null);
     
    layoutComboBox.addItemListener(new AddFieldToLayoutDlg_layoutComboBox_itemAdapter(this));
    layoutComboBox.setPreferredSize(new Dimension(460, 20));
    layoutComboBox.setEditable(false);
    layoutsGroup.add(layoutComboBox);
    layoutComboBox.setBounds(8, 20, 460, 20);
    mainPanel.add(layoutsGroup);

    addBtn.setText("Add To Layout");
    addBtn.addActionListener(new AddFieldToLayoutDlg_addBtn_actionAdapter(this));
    addBtn.setPreferredSize(new Dimension(105, 20));
    addBtn.setBounds(142, 166, 105, 20);
    addBtn.setMargin(new Insets(2, 2, 2, 2));
    mainPanel.add(addBtn);
     
    closeBtn.setText("Close");
    closeBtn.addActionListener(new AddFieldToLayoutDlg_closeBtn_actionAdapter(this));
    closeBtn.setPreferredSize(new Dimension(105, 20));
    closeBtn.setBounds(274, 166, 105, 20);
    closeBtn.setMargin(new Insets(2, 2, 2, 2));
    mainPanel.add(closeBtn);

    contentPanel.add(mainPanel);

    addWindowListener(new AddFieldToLayoutDlgCloser(this));

    loadFieldTypes();
    loadLayouts();
  }

  protected void loadFieldTypes()
  {
       Cursor  savedCursor = this.getCursor();
       this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
       
       fieldTypeComboBox.setVisible(false);
       if (fieldTypeComboBox.getItemCount() > 0) {
          fieldTypeComboBox.removeAllItems();
       }

       if (fldTypes != null) {
           while (fldTypes.hasMoreElements()) {
               NWFieldType  fld = fldTypes.next();
               fieldTypeComboBox.addItem(fld.getName());
           }
       }

       fieldTypeComboBox.setVisible(true);
       fieldTypeComboBox.validate();
       
       this.setCursor(savedCursor);
  }

  protected void loadLayouts()
  {
       Cursor  savedCursor = this.getCursor();
       this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
       
       layoutComboBox.setVisible(false);
       if (layoutComboBox.getItemCount() > 0) {
          layoutComboBox.removeAllItems();
       }

       if (layouts != null) {
           while (layouts.hasMoreElements()) {
               NWLayoutDescription  aLayout = layouts.next();
               layoutComboBox.addItem(aLayout.getName());
           }
       }

       layoutComboBox.setVisible(true);
       layoutComboBox.validate();
       
       this.setCursor(savedCursor);
  }

  void addFieldToLayoutDlg_WindowClosing(WindowEvent e)
  {
       this.dispose();
  }

  void addBtn_actionPerformed(ActionEvent evt)
  {
       String  layoutName = nameTextField.getText();
       if ((layoutName == null) || (layoutName.length() <= 0)) {
           return;
       }

       String  fldName = (String)fieldTypeComboBox.getSelectedItem();
       if ((fldName == null) || (fldName.length() <= 0)) {
           return;
       }

       try {
           // get the layout
           //
           NWLayoutDescription theLayout = layouts.getElement(layoutName);
           if (theLayout == null) {
               return;
           }

           // get the collection of fields
           //
           NWFieldDescriptions flds = theLayout.getFields();
           if (flds == null) {
               return;
           }

           boolean     isNaming = false;
           boolean     isOptional = false;
           if (optionalChkBox.isSelected()) {
               isOptional = true;
           }

           // add the field to the layout
           //
           flds.addElement(fldName, isOptional, isNaming);
               
           ((DSAdmin)getParent()).showMessage("Success adding field to layout.");
       } catch(NWBeanException e) {
           ((DSAdmin)getParent()).showMessage(e.getMessage() +"  -  " +e.getErrorMessage());
           return;
       }

       nameTextField.setText("");
  }

  void closeBtn_actionPerformed(ActionEvent e)
  {
       this.dispose();
  }

  void fieldTypeComboBox_itemStateChanged(ItemEvent e)
  {
  }

  void layoutComboBox_itemStateChanged(ItemEvent e)
  {
       nameTextField.setText((String)layoutComboBox.getSelectedItem());
  }
}

class AddFieldToLayoutDlgCloser extends WindowAdapter
{
   AddFieldToLayoutDlg    theDlg = null;

   public AddFieldToLayoutDlgCloser(AddFieldToLayoutDlg aDlg)
   {
       theDlg = aDlg;
   }

   public void windowClosing(WindowEvent e)
   {
       theDlg.addFieldToLayoutDlg_WindowClosing(e);
   }
}

class AddFieldToLayoutDlg_addBtn_actionAdapter implements java.awt.event.ActionListener
{
  AddFieldToLayoutDlg adaptee;

  AddFieldToLayoutDlg_addBtn_actionAdapter(AddFieldToLayoutDlg adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.addBtn_actionPerformed(e);
  }
}

class AddFieldToLayoutDlg_closeBtn_actionAdapter implements java.awt.event.ActionListener
{
  AddFieldToLayoutDlg adaptee;

  AddFieldToLayoutDlg_closeBtn_actionAdapter(AddFieldToLayoutDlg adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.closeBtn_actionPerformed(e);
  }
}

class AddFieldToLayoutDlg_fieldTypeComboBox_itemAdapter implements java.awt.event.ItemListener
{
  AddFieldToLayoutDlg adaptee;

  AddFieldToLayoutDlg_fieldTypeComboBox_itemAdapter(AddFieldToLayoutDlg adaptee)
  {
    this.adaptee = adaptee;
  }

  public void itemStateChanged(ItemEvent e)
  {
    adaptee.fieldTypeComboBox_itemStateChanged(e);
  }
}

class AddFieldToLayoutDlg_layoutComboBox_itemAdapter implements java.awt.event.ItemListener
{
  AddFieldToLayoutDlg adaptee;

  AddFieldToLayoutDlg_layoutComboBox_itemAdapter(AddFieldToLayoutDlg adaptee)
  {
    this.adaptee = adaptee;
  }

  public void itemStateChanged(ItemEvent e)
  {
    adaptee.layoutComboBox_itemStateChanged(e);
  }
}