//Sample code file: var/ndk/webBuildengine/tmp/viewable_samples/a4ad0b48-dd95-46b6-8289-721e99c8dc76/login_method/mgmt/consoleone/src/com/novell/admin/ndssnapins/loginMethods/CPassword/CPasswordUserCreatorSnapin.java //Warning: This code has been marked up for HTML

/*=============================================================================
  CPasswordUserCreatorSnapin.java

  Copyright (c) 1997-2002 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.
==============================================================================*/
package com.novell.admin.ndssnapins.loginMethods.CPassword;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.swing.*;

import com.novell.admin.common.ui.*;
import com.novell.admin.ns.*;
import com.novell.admin.ns.nds.*;
import com.novell.admin.snapins.creator.*;
import com.novell.application.console.snapin.*;
import com.novell.application.console.widgets.*;
import com.novell.utility.localization.*;
import com.novell.utility.nmsgbox.*;

import com.novell.security.japi.nmas.*;
import com.novell.admin.security.util.*;


public final class CPasswordUserCreatorSnapin extends CreatorSnapinPanel
      implements ItemListener
{
   private final String snapinName         = CPassword.getString(CPassword.CPassword_SNAPIN_NAME);
   private final String snapinDescription  = CPassword.getString(CPassword.CPassword_METHOD_SNAPIN_DESCRIPTION);

  // data
   private Shell  shell;
   private String treeName;
   private String userDN;
   private String coUserDN;
       
  // Translated strings
   private String cPasswordLabel = CPassword.getString(CPassword.CPassword_LABEL);
   private String cPasswordEntryLabel = CPassword.getString(CPassword.CPassword_ENTRY_LABEL);
   private String cPasswordNotSupportedLabel = CPassword.getString(CPassword.CPassword_NOT_SUPPORTED_LABEL);
   private String cPasswordConfirmLabel = CPassword.getString(CPassword.CPassword_CONFIRM_LABEL);
   private String cPasswordAllowLabel = CPassword.getString(CPassword.CPassword_ALLOW_LABEL);
   private String cPasswordInstructions = CPassword.getString(CPassword.CPassword_INSTRUCTIONS);
       
  // Attributes
   private NDSObjectAttribute hostServerAttrib = null;  // Case Ignore String
   
  // Controls
   private JCheckBox cPasswordAllowCheck = new JCheckBox(cPasswordAllowLabel, false);
   private JPasswordField cPasswordEntryField = new JPasswordField(20);
    private JLabel cPasswordEntryJLabel = new JLabel(cPasswordEntryLabel);

  // Private Booleans
   private boolean debug = false;
   private boolean cPasswordAllow = false;
   
  // Private Colors
   private Color colorOff;
   private Color colorOn;

   public boolean initSnapin(InitSnapinInfo info)
   {
      shell = info.getShell();

        if (OSUtil.getOStype() != OSUtil.NETWARE)
        {
            try
            {
                System.loadLibrary("NMASWrap");
            }
            catch(UnsatisfiedLinkError ex)
            {
                NMsgBox msg = new NMsgBox(shell.getShellFrame(), 
                                            CPassword.getString(CPassword.CPassword_LOAD_LIBRARY_FAILED_TITLE), 
                                            CPassword.getString(CPassword.CPassword_LOAD_LIBRARY_FAILED_MESSAGE), 
                                            3, null, null);
                msg.show();
                return false;
            }
        }
      return true;
   }
   public void shutdownSnapin( )
   {
   }

   public String getSnapinName()
   {
      return snapinName;
   }

   public String getSnapinDescription()
   {
      return snapinDescription;
   }

   public String[] getHandledAttributeNames()
   {
      String[] attributes = new String[1];
      attributes[0] = "";
      return attributes;
   }

   public ObjectAttribute[] getAttributes()
   {
      ObjectAttribute[] attribs = new ObjectAttribute[1];
      return attribs;
   }

   public boolean canCreate()
   {
      return checkData();
   }

   public void layoutControls()
   {
      int ccode, i;

      setLayout(new BorderLayout(5, 10));
      JPanel centerPanel = new JPanel(new BorderLayout(0, 10));
      JPanel templatePanel = new JPanel(new BorderLayout(20, 0));
        
        if (OSUtil.getOStype() == OSUtil.NETWARE)
      {
            centerPanel.add(new JLabel(cPasswordNotSupportedLabel));
      }
      else
      {
            cPasswordAllow = false;
           cPasswordAllowCheck.setSelected(false);
           colorOn = cPasswordEntryField.getBackground();
           colorOff = cPasswordEntryJLabel.getBackground();
           cPasswordEntryJLabel.setEnabled(false);
           cPasswordEntryField.setEnabled(false);
           cPasswordEntryField.setBackground(colorOff);
           cPasswordEntryField.setEchoChar('*');
            cPasswordAllowCheck.addItemListener(this);
           
          templatePanel.add(cPasswordAllowCheck, BorderLayout.NORTH);
          templatePanel.add(new Canvas(), BorderLayout.WEST);
          JPanel tbPanel = new JPanel(new BorderLayout(5, 0));
          tbPanel.add(cPasswordEntryJLabel, BorderLayout.NORTH);
          tbPanel.add(cPasswordEntryField, BorderLayout.CENTER);
          templatePanel.add(tbPanel, BorderLayout.CENTER);
          centerPanel.add(templatePanel, BorderLayout.NORTH);
        }

      add(centerPanel, BorderLayout.CENTER);
      }

   /** itemStateChanged is a public method ItemListener to process JCheckBox
    */
   public void itemStateChanged(ItemEvent event)
    {
        Object src = event.getSource();
        int i;
        String selectedClearance, tmpLabel;

        if (src == cPasswordAllowCheck) 
        {
            cPasswordAllow = cPasswordAllowCheck.isSelected();
           if (cPasswordAllow)
           {
                cPasswordEntryJLabel.setEnabled(true);
                cPasswordEntryField.setEnabled(true);
               cPasswordEntryField.setBackground(colorOn);               
           }
           else
           {
                cPasswordEntryJLabel.setEnabled(false);
                cPasswordEntryField.setEnabled(false);
               cPasswordEntryField.setBackground(colorOff);               
           }
        }
    }
   
   public void onPostCreate(ObjectEntry oe)
   {
      if (null != oe)
      {
         coUserDN = oe.getFullName();
         int slashLoc = coUserDN.indexOf('/');
         treeName    = coUserDN.substring(0, slashLoc);
            userDN = coUserDN.substring(slashLoc+1, coUserDN.length());
      }
      int ccode;
        if (cPasswordAllow)
      {   
         //  Save the cPasswordAllow flag to Login Config data on the user object
         if ((ccode = NMASWrap.NMAS_PutLoginConfig(treeName, userDN, CPassword.MethodIDLen, CPassword.MethodID, CPassword.AllowTag, CPassword.AllowDataLen, CPassword.AllowData)) != 0)
         {
               String message = CPassword.getString(CPassword.CPassword_PLC_ERR_MSG) + ccode;
               if(debug) System.out.println(message + "--> " + treeName + ", " + userDN + ", " + CPassword.MethodIDLen + ", " + CPassword.MethodID + ", " + CPassword.AllowTag + ", " + CPassword.AllowDataLen + ", " + CPassword.AllowData);
               if(debug) System.out.println("coUserDN--> " + coUserDN);
               NMsgBox msg = new NMsgBox(shell.getShellFrame(), CPassword.getString(CPassword.CPassword_ENTRY_ERR_TITLE), message, NMsgBox.ERROR, null, null);
                msg.show();
                return;
           }
           
          // Save cPassword to Login Secret data on the user object
           String CPasswordString = new String (cPasswordEntryField.getPassword());
           
           if(debug) System.out.println(CPasswordString);
            if(debug) System.out.println("Saving password...");
            String CPasswordDataString = CPassword.ClearTextID + CPasswordString + "\0";
            byte CPasswordData[] = CPasswordDataString.getBytes();
            if ((ccode = NMASWrap.NMAS_PutLoginSecret(treeName, userDN, CPassword.MethodIDLen, CPassword.MethodID, CPassword.SecretTag, CPasswordData.length, CPasswordData)) != 0)
            {
               String message = "Error" + ccode + "from NMAS_PutLoginSecret";
               if(debug) System.out.println(message);
               NMsgBox msg = new NMsgBox(shell.getShellFrame(), "Password Entry Error", message, NMsgBox.ERROR, null, null);
                msg.show();
            }
        }
   }

   public void onNameSet(String name)
   {
   }

   private boolean checkData()
   {
      return true;
   }
}