// Sample code file: UserProps.java

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

  %name: UserProps.java %
  %version: 2 %
  %date_modified: Thu Dec 20 14:24:47 2001 %

  Copyright (c) 2001 Novell, Inc.  All Rights Reserved.

  THIS WORK IS AN UNPUBLISHED WORK AND CONTAINS CONFIDENTIAL PROPRIETARY
  AND TRADE SECRET INFORMATION OF NOVELL, INC. ACCESS  TO  THIS  WORK IS
  RESTRICTED TO (I) NOVELL, INC.  EMPLOYEES WHO HAVE A NEED TO  KNOW HOW
  TO  PERFORM  TASKS WITHIN  THE SCOPE  OF  THEIR   ASSIGNMENTS AND (II)
  ENTITIES OTHER  THAN  NOVELL, INC.  WHO  HAVE ENTERED INTO APPROPRIATE
  LICENSE   AGREEMENTS.  NO  PART  OF  THIS WORK MAY BE USED, PRACTICED,
  PERFORMED COPIED, DISTRIBUTED, REVISED, MODIFIED, TRANSLATED, ABRIDGED,
  CONDENSED, EXPANDED, COLLECTED, COMPILED, LINKED,  RECAST, TRANSFORMED
  OR ADAPTED  WITHOUT THE PRIOR WRITTEN CONSENT OF NOVELL, INC.  ANY USE
  OR EXPLOITATION  OF  THIS WORK WITHOUT AUTHORIZATION COULD SUBJECT THE
  PERPETRATOR  TO CRIMINAL AND CIVIL LIABILITY.

  @author Sriramya Jambunathan
  @modifier Sanjay Gupta

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

public class UserProps extends JDialog implements KeyListener
{
    JTextField m_hdText,m_uidNoText,  m_loginShellText;//,  m_uniqIdText;
    JComboBox m_gidNoCombo;
    JTextArea m_gecosText;
    JButton m_ok, m_cancel, m_apply, m_help, m_add, m_delete;
    DefaultListModel dlModel;
    JList groupList;
    JScrollPane groupScroll;
    JTabbedPane m_tabPane;
    UAMConsole m_uam = null;
    User m_usr = null;
    String m_fullName = null;
    String m_hd,m_uidNo,m_sh,m_gecos,m_pgrp;
    boolean showNew = false;
 
    
    UserProps(UAMConsole uam, String title, String fullName)
    {
   super(uam.topFrame, title,true);
   m_uam = uam;
   m_fullName = fullName;
   try
   {
      m_usr = new User(m_uam.m_populator, m_fullName);
   }
   catch (Exception e)
   {
      e.printStackTrace();
   }
    }
    
    void setUI()
    {
     GridBagLayout gridbag;
     GridBagConstraints c;
     JLabel m_hdLabel, m_uidNoLabel, m_gidNoLabel, m_loginShellLabel, m_gecosLabel;//, m_uniqIdLabel;
     JPanel m_rootPane,m_lpane, m_buttonPane,m_listAllPane, m_listPane, m_listButtonPane;
    
        
     m_tabPane = new JTabbedPane();
     gridbag = new GridBagLayout();
     c = new GridBagConstraints();
     m_lpane = new JPanel();
     m_lpane.setLayout(gridbag);
     //create the labels and the text field/areas. Set the labelfor for each label
     m_hdLabel = new JLabel("Home Directory : ");
     m_hdLabel.setDisplayedMnemonic('h');
     m_hdLabel.setLabelFor(m_hdText);
     m_hdText = new JTextField(10);
     m_hdText.addKeyListener( this );
     
     m_uidNoLabel = new JLabel("UID Number :");
     m_uidNoLabel.setDisplayedMnemonic('u');
     m_uidNoLabel.setLabelFor(m_uidNoText);
     m_uidNoText = new JTextField(5);
     m_uidNoText.addKeyListener( this );
     
     m_gidNoLabel = new JLabel("Primary Group : ");
     m_gidNoLabel.setDisplayedMnemonic('g');
     m_gidNoLabel.setLabelFor(m_gidNoCombo);
     m_gidNoCombo = new JComboBox();     
     m_gidNoCombo.setEditable(false);
     /*m_gidNoCombo.addItemListener(new ItemListener()
               {
                  public void itemStateChanged(ItemEvent ie)
                  {
                     enableButton();
                  }
               });*/
     
     m_loginShellLabel = new JLabel("Login Shell : ");
     m_loginShellLabel.setDisplayedMnemonic('l');
     m_loginShellLabel.setLabelFor(m_loginShellText);     
     m_loginShellText = new JTextField(10);
     m_loginShellText.addKeyListener( this );     

     m_gecosLabel = new JLabel("Gecos : " );
     m_gecosLabel.setDisplayedMnemonic('c');
     m_gecosLabel.setLabelFor(m_gecosText); 
     m_gecosText = new JTextArea();
     m_gecosText.addKeyListener( this );
     m_gecosText.setBorder(new LineBorder(Color.black));
     
     c.fill = c.BOTH;//Expand horizontally and vertically
     //Remind.. Isnt Horizontal enough?
     
    // c.weightx = 1.0;
    c.ipadx = 2;
    c.ipady = 2;
    Insets thisinset = new Insets(3,2,3,2);
    c.insets = thisinset;
     c.gridx = 0;
     c.gridy = 0;
     c.weightx = 0.0;
     c.weighty = 0.0;
     gridbag.setConstraints(m_uidNoLabel, c);
     m_lpane.add(m_uidNoLabel);
     
     c.weightx = 0.0;
     c.gridx = 1;
     c.gridwidth = c.REMAINDER;
     c.weightx = 1.0;
     gridbag.setConstraints(m_uidNoText, c);
     m_lpane.add(m_uidNoText);
     
     c.gridwidth =1;
     c.weightx = 0.0;
     c.gridx=0;
     c.gridy =1;
     c.weightx = 0.0;
     gridbag.setConstraints(m_hdLabel, c);
     m_lpane.add(m_hdLabel);
     
     c.gridwidth = c.REMAINDER;
     c.weightx = 1.0;
     c.gridx = 1;
     c.weightx = 1.0;
     gridbag.setConstraints(m_hdText, c);
     m_lpane.add(m_hdText);
     
     c.gridwidth =1;
     c.weightx = 0.0;
     c.gridx =0;
     c.gridy = 2;
     c.weightx = 0.0;
     gridbag.setConstraints(m_gidNoLabel, c);
     m_lpane.add(m_gidNoLabel);
     
     c.gridwidth = c.REMAINDER;
     c.gridx =1;
     c.weightx = 1.0;
     gridbag.setConstraints(m_gidNoCombo, c);
     m_lpane.add(m_gidNoCombo);
     
     c.gridwidth =1;
     c.gridx = 0;
     c.gridy =3;
     c.weightx = 0.0;
     gridbag.setConstraints(m_loginShellLabel, c);
     m_lpane.add(m_loginShellLabel);
     
     c.gridwidth = c.REMAINDER;
     c.gridx =1;
     c.weightx = 1.0;
     gridbag.setConstraints(m_loginShellText, c);
     m_lpane.add(m_loginShellText);
     
     c.gridwidth =1;
     //c.gridheight = 3;
     c.gridx = 0;
     c.gridy = 4;
     c.weightx = 0.0;
     gridbag.setConstraints(m_gecosLabel, c);
     m_lpane.add(m_gecosLabel);
     
     //c.weighty = 2.0;
     c.gridheight = 2;
     c.gridx = 1;
     c.weighty = 0.5;
     c.gridwidth = c.REMAINDER;
     gridbag.setConstraints(m_gecosText, c);
     m_lpane.add(m_gecosText);
     
     m_tabPane.add("UnixProfile", m_lpane);
     m_rootPane = new JPanel(new BorderLayout());
     m_buttonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
     m_ok = new JButton(" Ok ");
     m_ok.setMnemonic('o');
     m_ok.setEnabled(false);
     m_ok.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e)
           {
             onOKPressed();
           }
     });
     
     m_cancel = new JButton(" Cancel ");
     m_cancel.setMnemonic('c');
     m_cancel.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            cancelClose();
        }
     });
     
     m_apply = new JButton (" Apply ");
     m_apply.setMnemonic('a');
     m_apply.setEnabled(false);
     m_apply.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
            try
            {
                onApplyPressed();
            }catch(Exception exx){}
         }
     });
     
     m_help = new JButton (" Help ");
     m_help.setMnemonic('h');
     m_help.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            onHelpPressed();
        }
     });

     dlModel = new DefaultListModel();
     groupList = new JList(dlModel);
     groupList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
     groupScroll = new JScrollPane(groupList);
     m_listPane = new JPanel(new BorderLayout());
     m_listPane.add(groupScroll, BorderLayout.CENTER);
     m_listAllPane = new JPanel(new BorderLayout());
     m_listAllPane.add(m_listPane, BorderLayout.CENTER);
     
     m_add = new JButton("Add");
     m_add.setMnemonic('d');
     m_add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
            onAddPressed();
        }
     });
        
     m_delete = new JButton("Delete");
     m_delete.setMnemonic('l');
     m_delete.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            onDeletePressed();
        }
        });
        
     m_listButtonPane = new JPanel();
     m_listButtonPane.setLayout(new VerticalFlowLayout());//BoxLayout(m_listButtonPane, BoxLayout.Y_AXIS));
     m_listButtonPane.add(m_add);
     m_listButtonPane.add(m_delete);
     
     m_listAllPane.add(m_listButtonPane, BorderLayout.EAST);
     m_tabPane.addTab("Groups", m_listAllPane);
     
     m_buttonPane.add(m_ok);
     m_buttonPane.add(m_cancel);
     m_buttonPane.add(m_apply);
     m_buttonPane.add(m_help);
     m_rootPane.add(m_tabPane,BorderLayout.CENTER);
     m_rootPane.add(m_buttonPane, BorderLayout.SOUTH);
     this.getContentPane().add(m_rootPane);
     this.addKeyListener(new KeyAdapter()
     {
        public void keyTyped(KeyEvent e)
        {
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
            {
                cancelClose();
            }
        }
        });
     this.setSize(400,300);
      Utilities.centerWindow(this);
     //this.setVisible(true);
    
    }

    void addToList(String fullName)
    {
        dlModel.addElement(fullName);
        m_gidNoCombo.addItem(fullName);
        m_gidNoCombo.repaint();
        int size = dlModel.getSize();     
             if (size > 0)
            m_delete.setEnabled(true);   
    }
    public void showNewUser(boolean show)
    {
        showNew = show;
    }
   public void showValue()
   {
      try
      {
         m_hd = m_usr.getHomeDirectory();
         m_hdText.setText(m_hd);
         Integer uidNo = new Integer(m_usr.getUserID());
         m_uidNo = uidNo.toString();
         m_uidNoText.setText(m_uidNo);
         m_sh = m_usr.getLoginShell();
         m_loginShellText.setText(m_sh);
         m_gecos = m_usr.getComment();
         m_gecosText.setText(m_gecos);
         String grp1;
         m_pgrp = m_usr.getPrimaryGroup();
         Vector grp = m_usr.getGroups();
         int j = grp.size();
         dlModel.clear();
         m_gidNoCombo.removeAllItems();
         if (j>0)
            for(int i= 0; i<j;i++)
            {
               Object elem = grp.elementAt(i);
               m_gidNoCombo.insertItemAt(elem,i);
               dlModel.addElement(elem);
               if (m_pgrp.equalsIgnoreCase(elem.toString()))
                  m_gidNoCombo.setSelectedItem(elem);
            }
         
         int size = dlModel.getSize();     
             if (size == 0)
            m_delete.setEnabled(false);
         this.repaint();
         this.show();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      
   }
   
   void onOKPressed()
      {
          try
          {
             onApplyPressed();
         }
         catch(Exception e)
         {
             return;
         }
         
      cancelClose();
   }

   void cancelClose()
       {
           if (m_apply.isEnabled())
          {
         int selOption = JOptionPane.showConfirmDialog(m_uam.topFrame, "There are unsaved changes. Do you wish to continue?", "Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
         if (selOption != 0)
            return;
          }
          this.dispose();
          if (showNew)
         {
             NewUser nwusr = new NewUser(m_uam);
                nwusr.setUI();
                nwusr.showProp(true);
                nwusr.showNewUser(true);
                nwusr.anotherUsrRB.setSelected(true);
                nwusr.addPropsRB.setSelected(true);
                 Utilities.centerWindow(nwusr);
               nwusr.show();
         }
            
      
       }

       void onApplyPressed() throws Exception
       {
      String val;
      try
      {
          val = m_hdText.getText();
          if (!m_hd.equalsIgnoreCase(val)) 
            m_usr.setHomeDirectory(val);
      
      }
      catch (Exception e)
      {
          JOptionPane.showMessageDialog(this, "Invalid Value", "Error" ,JOptionPane.ERROR_MESSAGE);
         val = "";
         m_hdText.setText("");
         m_hdText.requestFocus();
         throw e;
         
      }
      
      try
      {
          val = m_uidNoText.getText();
          if (!m_uidNo.equalsIgnoreCase(val)) 
         {
            Integer uid = new Integer(val);
            m_usr.setUserID(uid.intValue());
         }
      }
      catch (Exception e)
      {
          JOptionPane.showMessageDialog(this, "Invalid Value", "Error" ,JOptionPane.ERROR_MESSAGE);
         val = "";
         m_uidNoText.setText("");
         m_uidNoText.requestFocus();
         throw e;
      }
      
      try
      {
          val = m_loginShellText.getText();
         if (!m_sh.equalsIgnoreCase(val)) 
            m_usr.setLoginShell(val);
      
      }
      catch (Exception e)
      {
         //val = "";
         JOptionPane.showMessageDialog(this, "Invalid Value", "Error" ,JOptionPane.ERROR_MESSAGE);
         val = "";
         m_loginShellText.setText("");
         m_loginShellText.requestFocus();
         throw e;
      }
      
      try
      {
          val = m_gecosText.getText();
         if (!m_gecos.equalsIgnoreCase(val)) 
            m_usr.setComment(val);
      
      }
      catch (Exception e)
      {
         //val = "";
         JOptionPane.showMessageDialog(this, "Invalid Value", "Error" ,JOptionPane.ERROR_MESSAGE);
         val = "";
         m_gecosText.setText("");
         m_gecosText.requestFocus();
         throw e;
      }
      
      try
      {
          val = m_gidNoCombo.getSelectedItem().toString();
         if (!m_pgrp.equalsIgnoreCase(val)) 
            m_usr.setPrimaryGroup(val);
      
      }
      catch (Exception e)
      {

         JOptionPane.showMessageDialog(this, "Invalid Value", "Error" ,JOptionPane.ERROR_MESSAGE);
         val = "";
         throw e;
      }
      m_apply.setEnabled(false);   
       }
    
   void onHelpPressed()
       {
       }

       void onAddPressed()
       {
      AddDialog addDlg = new AddDialog(m_uam, "Group",this);
      addDlg.setUI();
      addDlg.showValue();      
      m_add.setEnabled(true);
       }
    

   void onDeletePressed()
       {
      if (groupList.isSelectionEmpty())
      {
         JOptionPane.showMessageDialog(m_uam.topFrame, "Please Select a group to be removed", "Error", JOptionPane.ERROR_MESSAGE);
         return;
      }
       int confirmInt = JOptionPane.showConfirmDialog(this, "Are you sure that you want to delete? ", "Delete Confirm Dialog" , JOptionPane.YES_NO_OPTION);
   
       if (confirmInt == JOptionPane.NO_OPTION)
       {
           m_uam.getFrame().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
           return;
       }
      try
      {
         int index = groupList.getSelectedIndex();
         String grpName = groupList.getSelectedValue().toString();
         if(m_usr.removeFromGroup(grpName))
         {
            dlModel.remove(index);
            m_gidNoCombo.removeItem(grpName);
         }
         int size = dlModel.getSize();     
             if (size == 0)
            m_delete.setEnabled(false);      
      }
      catch(Exception e)
      {
         JOptionPane.showMessageDialog(m_uam.topFrame, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
         e.printStackTrace();
      }
       }

   public void keyTyped(KeyEvent e)
   {
      
   }
          
   public void keyReleased(KeyEvent e) 
   {
   }
          
   public void keyPressed(KeyEvent evt)
          {
              enableButton();
   }

   void enableButton()
   {
      m_apply.setEnabled(true);
      m_ok.setEnabled(true);   
   }   
}