// Sample code file: Frame1.java

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

/***************************************************************************
$name: Frame1.java 
$version: 1.0 
$date_modified: 210104 
$description:  
$owner: Beans for Novell Services Team 
Copyright (c) 1998 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.awt.event.*;
import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import com.novell.beans.NWDir.*;
import com.novell.beans.util.NWBeanException;

public class Frame1 extends JFrame
   implements ChangeListener
{
   public static final int    THE_WIDTH = 400;
   public static final int    THE_HEIGHT = 300;

   //Construct the frame
   BorderLayout borderLayout1 = new BorderLayout();
   JTabbedPane jTabbedPane1 = new JTabbedPane();
   JPanel fullNamePanel = new JPanel();
   JPanel buttonPanel = new JPanel();
   GeneralPanel generalPanel = new GeneralPanel();
   PostalPanel postalPanel = new PostalPanel();
   FlowLayout flowLayout1 = new FlowLayout();
   JButton browseBtn = new JButton();
   JButton clearBtn = new JButton();
   JButton saveBtn = new JButton();
   JButton nextBtn = new JButton();
   JTextField jTextField1 = new JTextField();
   NWDir nWDir1 = new NWDir();
   NWEntries entries = null;
   NWEntry  currentEntry = null;
   String theFullName = "";

   public Frame1()
   {
      enableEvents(AWTEvent.WINDOW_EVENT_MASK);
      try 
      {
         jbInit();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }
//Component initialization
   
   private void jbInit() throws Exception 
   {
      this.getContentPane().setLayout(borderLayout1);
      this.setSize(new Dimension(THE_WIDTH, THE_HEIGHT));
      this.setTitle("User Manager");
      
      fullNamePanel.setLayout(new FlowLayout());
      
      jTextField1.setText(NWDir.PREFIX);
      jTextField1.setPreferredSize(new Dimension(THE_WIDTH - 102, 22));
      jTextField1.setSize(new Dimension(THE_WIDTH - 102, 22));
      fullNamePanel.add(jTextField1);
      
      browseBtn.setText("Browse...");
      browseBtn.setPreferredSize(new Dimension(80, 22));
      browseBtn.setSize(new Dimension(80, 22));
      browseBtn.setMargin(new Insets(2, 2, 2, 2));
      browseBtn.addActionListener(new Frame1_browseBtn_actionAdapter(this));
      fullNamePanel.add(browseBtn);
      
      buttonPanel.setPreferredSize(new Dimension(THE_WIDTH - 2, 36));
      buttonPanel.setSize(new Dimension(THE_WIDTH - 2, 36));
      buttonPanel.setLayout(flowLayout1);
            
      nextBtn.setText("Next");
      nextBtn.addActionListener(new Frame1_nextBtn_actionAdapter(this));
      buttonPanel.add(nextBtn, null);
      
      clearBtn.setText("Clear");
      clearBtn.addActionListener(new Frame1_clearBtn_actionAdapter(this));
      buttonPanel.add(clearBtn, null);
      
      saveBtn.setText("Save");
      saveBtn.addActionListener(new Frame1_saveBtn_actionAdapter(this));
      buttonPanel.add(saveBtn, null);
      
      jTabbedPane1.addChangeListener(this);
      jTabbedPane1.addTab("General Info.", generalPanel);
      jTabbedPane1.addTab("Postal Info.", postalPanel);
      
      this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
      this.getContentPane().add(fullNamePanel, BorderLayout.NORTH);
      this.getContentPane().add(jTabbedPane1, BorderLayout.CENTER);

      generalPanel.initialize();
      
      centerOnScreen();
   }
   
   protected void centerOnScreen()
   {
       Dimension  scrnSize = Toolkit.getDefaultToolkit().getScreenSize();
       setLocation((scrnSize.width - THE_WIDTH) / 2,
                     (scrnSize.height - THE_HEIGHT) / 2);
   }

//Overriden so we can exit on System Close
   
   protected void processWindowEvent(WindowEvent e)
   {
      super.processWindowEvent(e);
      if (e.getID() == WindowEvent.WINDOW_CLOSING)
      {
         System.exit(0);
      }
   }
   
   public void stateChanged(ChangeEvent evt)
   {
      if (evt.getSource() == jTabbedPane1) {
         Component   selected = jTabbedPane1.getSelectedComponent();
         if (selected instanceof FlowPanel) {
            ((FlowPanel)selected).initialize();
            ((FlowPanel)selected).load(currentEntry);
         }
      }
   }

   NWEntry getFirstEntry()
   {
     entries = nWDir1.getEntry().getEntries("User");
     return(getNextEntry());
   }
   
   NWEntry getNextEntry()
   {
     if (entries != null) {
        if (entries.hasMoreElements()) {
           return(entries.next());
        }
     }
     return(null);
   }
   
   void fillVisiblePanel(NWEntry anEntry)
   {
      Component   selectedPanel = jTabbedPane1.getSelectedComponent();
      if (selectedPanel instanceof FlowPanel) {
         ((FlowPanel)selectedPanel).load(anEntry);
      }
   }

   boolean setTheFullName()
   {
      theFullName = jTextField1.getText();
      if ((theFullName.length() == 0) ||
        (theFullName.equalsIgnoreCase(NWDir.PREFIX))) {
        System.out.println("FullName is invalid.");
        return(false);
      }

      try {
        nWDir1.setFullName(theFullName);
        entries = nWDir1.getEntry().getEntries("User");
      } catch(NWBeanException e) {
        System.out.println(e);
        return(false);
      }
      
      return(true);
   }

   void nextBtn_actionPerformed(ActionEvent evt)
   {
      if ((theFullName.length() == 0) ||
         (jTextField1.getText().equalsIgnoreCase(theFullName) == false)) {
         if (setTheFullName() == false) {
            return;
         }
      }
      
      currentEntry = getNextEntry();
      if (currentEntry == null) { // loop back to start
         currentEntry = getFirstEntry();
      }

      if (currentEntry != null) {
         fillVisiblePanel(currentEntry);
      }
   }

   void clearBtn_actionPerformed(ActionEvent evt)
   {
      jTextField1.setText(NWDir.PREFIX);
      generalPanel.clear();
      postalPanel.clear();
      entries = null;
      currentEntry = null;
      theFullName = "";
   }

   void saveBtn_actionPerformed(ActionEvent evt)
   {
      if (currentEntry != null) {
         generalPanel.saveFields(currentEntry);
         postalPanel.saveFields(currentEntry);
      }
   }

   void browseBtn_actionPerformed(ActionEvent evt)
   {
      BrowseDSDlg   bd = new BrowseDSDlg(this);
      bd.setVisible(true);
      if (bd.okWasHit) {
         String   newFullName = bd.getFullName();
         if (newFullName.length() > 0) {
            jTextField1.setText(newFullName);
         }
      }
   }
}

class Frame1_browseBtn_actionAdapter implements java.awt.event.ActionListener
{
   Frame1 adaptee;

   
   Frame1_browseBtn_actionAdapter(Frame1 adaptee)
   {
      this.adaptee = adaptee;
   }

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

class Frame1_nextBtn_actionAdapter implements java.awt.event.ActionListener
{
   Frame1 adaptee;

   
   Frame1_nextBtn_actionAdapter(Frame1 adaptee)
   {
      this.adaptee = adaptee;
   }

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

class Frame1_clearBtn_actionAdapter implements java.awt.event.ActionListener
{
   Frame1 adaptee;

   
   Frame1_clearBtn_actionAdapter(Frame1 adaptee)
   {
      this.adaptee = adaptee;
   }

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

class Frame1_saveBtn_actionAdapter implements java.awt.event.ActionListener
{
   Frame1 adaptee;

   
   Frame1_saveBtn_actionAdapter(Frame1 adaptee)
   {
      this.adaptee = adaptee;
   }

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