// Sample code file: FilesystemPage.java

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

/*
   Copyright (c) 1997-1999 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.sample.snapins.filesystem;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.File;

import com.novell.application.console.snapin.*;
import com.novell.application.console.snapin.scope.*;
import com.novell.application.console.snapin.context.*;
import com.novell.application.console.util.objectentryselector.*;
import com.novell.utility.mpec.*;




/**
  This class extends the AbstractPageSnapin class, which simplifies creation
  of property book page snap-ins. This sample page snap-in provides a property
  book page snap-in that allows the user to rename a file in the sample
  Filesystem namespace. The focusListener is used to detect a change in the
  filename text field.
*/
public class FilesystemPage extends AbstractPageSnapin
   implements FocusListener
{
   private File         theFile;
   private String       originalFileName;
   private TextField    fileName;

   private Shell        theShell;
   
   static Image browse_fs_image = null;
   static int browse_fs[] = { 
      -4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-16777216,-16777216,-16777216,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-16777216,-1,-16777216,-4144960,-16777216,-16777216,
      -16777216,-16777216,-16777216,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-16777216,-16777216,-16777216,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-16777216,-4144960,-4144960,-4144960,-16777216,
      -16777216,-16777216,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-16777216,-16777216,-16777216,-4144960,-16777216,
      -1,-16777216,-4144960,-16777216,-16777216,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-16777216,-4144960,-4144960,-4144960,-16777216,
      -16777216,-16777216,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-16777216,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-16777216,-4144960,-4144960,-4144960,-16777216,
      -16777216,-16777216,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-16777216,-16777216,-16777216,-4144960,-16777216,
      -1,-16777216,-4144960,-16777216,-16777216,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-16777216,-4144960,-4144960,-4144960,-16777216,
      -16777216,-16777216,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
      -4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,-4144960,
   };
   public static int browse_fsWidth=16;
   public static int browse_fsHeight=15;
   
   static
   {
      Component c = new Button();
      MemoryImageSource mis = new MemoryImageSource(browse_fsWidth, 
         browse_fsHeight, 
         browse_fs, 
         0, 
         browse_fsWidth);
      browse_fs_image = c.createImage(mis);
   }

   /*
     Implementation of the property book page snap-in interface.
   */

   /**
    The getSnapinName() method (derived from Snapin interface) is called by
    configurators to display the localized name of this snap-in. It returns
    the translated name for this snap-in. The shell displays the name you
    provide together with all the snap-ins of a given type. The user is then
    allowed to select which snap-ins should be set as active in the current
    configuration. The getSnapinName() method may be called before initSnapin().
    */
   public String getSnapinName()
   {
      return "Novell's Sample Filesystem Namespace PropertyBook Page";
   }

   /**
    The getSnapinDescription() method (derived from Snapin interface) is called
    by the configurators to display the localized description information
    about this snap-in. It returns the snap-in description as a String.
    */
   public String getSnapinDescription()
   {
      return "Novell's Sample Filesystem Namespace PropertyBook Page for displaying File Information";
   }

   /**
    The initAbstractSnapin() method initializes this class. You should do any
    onetime initialization here, such as adding event listeners. The method
    returns a boolean set to <i>true</i> if the snap-in is able to successfully
    complete initialization, or <i>false</i> if initialization fails. The 'info'
    parameter contains data the snap-in may use for initialization, such as
    references to the shell and the snap-in type, and may contain a reference
    to snap-in context data.
   */
   public boolean initAbstractSnapin( InitSnapinInfo info )
   {
      ObjectEntryCollection theObjectCollection = getObjectCollection();
      theShell = info.getShell();

      Label fileNameLabel = new java.awt.Label( "Filename:" );
      Label directoryLabel = new java.awt.Label( "Directory:" );
      Label fileSizeLabel = new java.awt.Label( "File length:" );

      TextField directoryName = new java.awt.TextField( 40 );
      TextField fileSize = null;

      Canvas canvas1 = new java.awt.Canvas();
      Canvas canvas2 = new java.awt.Canvas();
      Canvas canvas3 = new java.awt.Canvas();

      GridBagConstraints gbc = null;
      GridBagLayout gridBagLayout = new GridBagLayout();

      theFile = new File( theObjectCollection.getFirstElement().getName() );
      originalFileName = new String( theFile.getName() );
      fileName = new java.awt.TextField( 40 );

      setLayout( gridBagLayout );

      gbc = new GridBagConstraints();
      gbc.gridx = 1;
      gbc.gridy = 1;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.fill = GridBagConstraints.NONE;
      gbc.insets = new Insets( 0, 0, 5, 2 );
      ( (GridBagLayout)getLayout() ).setConstraints( fileNameLabel, gbc );
      add( fileNameLabel );

      gbc = new GridBagConstraints();
      gbc.gridx = 1;
      gbc.gridy = 2;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.fill = GridBagConstraints.NONE;
      gbc.insets = new Insets( 0, 0, 5, 0 );
      ( (GridBagLayout)getLayout() ).setConstraints( directoryLabel, gbc );
      add( directoryLabel );

      gbc = new GridBagConstraints();
      gbc.gridx = 1;
      gbc.gridy = 3;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.fill = GridBagConstraints.NONE;
      gbc.insets = new Insets( 0, 0, 0, 6 );
      ( (GridBagLayout)getLayout() ).setConstraints( fileSizeLabel, gbc );
      add( fileSizeLabel );

      fileName.setText( theFile.getName() );
      gbc = new GridBagConstraints();
      gbc.gridx = 3;
      gbc.gridy = 1;
      gbc.weightx = 1.0;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.insets = new Insets( 0, 0, 0, 0 );
      ( (GridBagLayout)getLayout() ).setConstraints( fileName, gbc );
      add( fileName );
      fileName.addFocusListener( this );

      directoryName.setText( theFile.getParent() );
      directoryName.setEditable(false);
      gbc = new GridBagConstraints();
      gbc.gridx = 3;
      gbc.gridy = 2;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.insets = new Insets( 0, 0, 0, 0 );
      ( (GridBagLayout)getLayout() ).setConstraints( directoryName, gbc );
      add( directoryName );

      fileSize = new java.awt.TextField(10);
      fileSize.setText( new String( theFile.length() + " bytes" ));
      fileSize.setEditable(false);
      gbc = new GridBagConstraints();
      gbc.gridx = 3;
      gbc.gridy = 3;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.insets = new Insets( 0, 0, 0, 0 );
      ( (GridBagLayout) getLayout() ).setConstraints( fileSize, gbc );
      add( fileSize );
      
      JButton b1 = new JButton(new ImageIcon(browse_fs_image));
      b1.setPreferredSize(new Dimension(19, 18));
      b1.setFocusPainted(false);
      b1.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent evt)
         {
            Shell shell = MainShell.getInstance();
            ObjectEntry oe = null;
            ObjectEntryCollection oee = shell.getCurrentSelections();
            if (oee.hasSomeElements())
               oe=oee.getFirstElement();
            
            ObjectEntrySelector selector = new ObjectEntrySelector(shell.getShellFrame(), oe);
            selector.setFederationMode(ObjectEntrySelector.CHILD_FEDERATION);
            selector.setSelectionMode(ObjectEntrySelector.MULTIPLE_SELECTION);
            selector.show();
            ObjectEntry[] selectedEntries = selector.getSelectedObjectEntries();
            if (null != selectedEntries)
            {
                System.out.println("number of selected entries = "+selectedEntries.length);
                for(int i = 0; i < selectedEntries.length; i++)
                {
                    System.out.println("selectedEntries["+i+"]= "+selectedEntries[i].getName());
                }
            }
         }
      });
      gbc = new GridBagConstraints();
      gbc.gridx = 4;
      gbc.gridy = 1;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.insets = new Insets( 0, 3, 0, 0 );
      ( (GridBagLayout) getLayout() ).setConstraints( b1, gbc );
      add (b1);
      

      canvas1.setBounds( 0, 0, 0, 0 );
      gbc = new GridBagConstraints();
      gbc.gridx = 0;
      gbc.gridy = 100;
      gbc.weightx = 0.2;
      gbc.weighty = 1.0;
      gbc.fill = GridBagConstraints.VERTICAL;
      gbc.insets = new Insets( 0, 0, 0, 0 );
      ( (GridBagLayout) getLayout() ).setConstraints( canvas1, gbc );
      add( canvas1 );

      canvas2.setBounds( 0, 0, 0, 0 );
      gbc = new GridBagConstraints();
      gbc.gridx = 0;
      gbc.gridy = 0;
      gbc.weighty = 0.2;
      gbc.fill = GridBagConstraints.NONE;
      gbc.insets = new Insets( 0, 0, 0, 0 );
      ( (GridBagLayout) getLayout() ).setConstraints( canvas2, gbc );
      add( canvas2 );

      canvas3.setBounds( 315, 16, 34, 27 );
      gbc = new GridBagConstraints();
      gbc.gridx = 100;
      gbc.gridy = 0;
      gbc.weightx = 0.15;
      gbc.fill = GridBagConstraints.NONE;
      gbc.insets = new Insets( 0, 0, 0, 0 );
      ( (GridBagLayout) getLayout() ).setConstraints( canvas3, gbc );
      add( canvas3 );

      setBackground( Color.lightGray );
      return true;
   }

   /**
     The saveData() method (defined by PageSnapin Interface) saves page
     data to a persistent store. it is called when the user selects either
     the OK button or the Apply Now button, and the canSave() method has
     returned true.
   */
   public boolean saveData()
   {
      if( (fileName.getText()).compareTo( originalFileName ) != 0 )
      {
         /*
            Make the new file.
         */
         File  newFile = new File( theFile.getParent(), fileName.getText() );

         if(theFile.renameTo( newFile ) == true )
         {
            /*
               Update the originalFileName.
            */
            originalFileName = new String( fileName.getText() );
            setModified( false, this );

            /*
                The property book will update the status of its buttons
                automatically, so you don't need to call updateButtons();
            */
            theFile = newFile;
            theShell.refreshCurrentTreeSelection();

            return true;
         }
         else
         {
            System.out.println( "Call to rename failed - " + fileName.getText());
            return false;
         }
      }

      return true;
   }

   /**
     This method (defined by the PageSnapin Interface) returns the string
     containing the language dependent translated display name for the tab.
     If this tab does not exist, the Ppoperty book will use the string name
     for the tab name, otherwise it is ignored.
   */
   public String getTabName()
   {
      return "File Info";
   }

   /**
     This method (defined by the PageSnapin Interface) returns the globally
     unique language independent object ID (identifier) of the tab where the
     current page is to be displayed.
   */
   public String getTabID()
   {
      return "com.novell.sample.snapins.filesystem";
   }

   /**
     This method (defined by the PageSnapin Interface) returns a string
     containing the language dependent translated menu display name for
     this page, if the tab is a multi-page tab. You can return null if
     you are not going to make the tab ID public and thus you do not want
     the tab to be extended.
   */
   public String getMenuName()
   {
      return "Rename";
   }

   /**
     The getPreferredSize() method returns the preferred size of the dialog.
     You can override the AWT.Component Dimension returned to make sure
     your page is shown properly, otherwise the page will be displayed in the
     default standard size. To make sure controls fit on a page, you may have
     to set the preferred size of the page.
   */
   public Dimension getPreferredSize()
   {
      return new Dimension( 100, 300 );
   }


   /**
      FocusListener Interface
   */
   public void focusGained( FocusEvent e )
   {
   }

   /**
      FocusListener Interface
   */
   public void focusLost( FocusEvent e )
   {
      String theNewFileName = ((TextField)e.getSource()).getText();

      if( theNewFileName.compareTo( originalFileName ) != 0 )
      {
         setModified( true, this );
      }
   }
}