// Sample code file: FilesystemNamespaceDisplay.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.util.*;
import java.io.*;
import java.awt.*;
import java.net.*;
import javax.swing.*;
import com.novell.application.console.snapin.*;
import com.novell.application.console.snapin.scope.*;
import com.novell.application.console.snapin.context.*;

  /**
    This is an example of registering an implementation of the
    DisplayIconSnapin and DisplayNameSnapin interfaces against
    the Filesystem namespace.
  */
public class FilesystemNamespaceDisplay
      implements DisplayIconSnapin, DisplayNameSnapinRev2
{
   /** Shell instance */
   private Shell shell = null;
   private static Hashtable icons = new Hashtable();

   /*
    Implementation of the display icon and display name snap-in
    interfaces.
   */

   /**
     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 Display Snapin";
   }

   /**
     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 "Provides Display information for Novell's Sample File System namespace";
   }

   /**
     The initSnapin() 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 initSnapin(InitSnapinInfo info)
   {
      shell = info.getShell();
      return true;
   }

   /**
     Implement shutdownSnapin() in your extending snap-in if there is
     anything that needs to be cleaned up, such as removing shell event listeners.
    */
   public void shutdownSnapin()
   {
   }

   /**
     This getDisplayIcon() method returns the snap-in icon associated with the
     specified ObjectEntry parameter (entry). A null can be returned when there
     is no icon available, in which case the default icon may be used if another
     snap-in doesn't have it.
    */
   public Icon getDisplayIcon(ObjectEntry entry)
   {
      return getDisplayIcon(entry.getObjectType().getNamespace().getUniqueID(),
         entry.getObjectType().getName());
   }

   /**
     This getDisplayIcon() method returns the icon associated with the ObjectType
     that has the same name as the passed in String parameter (type). A null can
     be returned when there is no icon available, in which case the default icon
     may be used if another snap-in doesn't have it.
    */
   public Icon getDisplayIcon(String namespace, String type)
   {
      String hash_name = null;
      if(type.equals("Directory"))
      {
         hash_name = "Folder";
      }
      else
      {
         hash_name = "File";
      }
      if(type.equals("jar") || type.equals("zip"))
      {
         hash_name = "ZipFile";
      }
      Icon icon = (Icon) icons.get(hash_name);

      if(icon == null)
      {
         Image i = null;
         String path = "/com/novell/sample/snapins/filesystem/images/";
         path += hash_name + ".gif";
         URL url = getClass().getResource(path);
         if(url != null)
         {
            i = Toolkit.getDefaultToolkit().getImage(url);
            icon = new ImageIcon(i);
            icons.put(hash_name, icon);
         }
         else
         {
            System.err.println("Image error in FilesystemNamespaceDisplay");
         }
      }
      return icon;
   }

   /**
     The doesIconChangePerEntry() method is used to determine if the icon can
     change depending on the ObjectEntry of the given type, or if the snap-in
     will always return the same icon for a particular ObjectType. Return <i>true</i>
     if the icon can change depending on the ObjectEntry of the given type. If the
     snap-in always returns the same icon for a particular ObjectType, this method
     should return <i>false</i>. This way ConsoleOne can cache the image, when
     possible, and use the same image for all objects of the given type.
    */
   public boolean doesIconChangePerEntry(ObjectType type)
   {
      return false;
   }

   /**
     When using getDisplayName() with the two String parameters you must return the
     localized display name of the passed in object type. A null can be returned
     when there is no name available, in which case the default name may be used
     if another snap-in doesn't have it.
    */
   public String getDisplayName(String namespace, String type)
   {
      return type;
   }

   /**
     When using getDisplayName() with the ObjectEntry parameter (entry), you
     must return the display name that the shell will use to display the specified
     ObjectEntry in the tree or view. The name of the object is displayed next to
     an icon (display image) representing the object. A null can be returned when
     there is no name available, in which case the default name may be used if
     another snap-in doesn't have it.
    */
   public String getDisplayName(ObjectEntry entry)
   {
      String name = entry.getName();
      if(name.equals(""))
      {
         return "Invalid Entry";
      }
      int slash = name.lastIndexOf(File.separator);
      if (slash > -1)
      {
        name = name.substring(slash + 1);
      }
      if(name.equals(""))
      {
         return "File System";
      }
      return name;
   }
   
   /**
     When using getDisplayName() with the ObjectEntry parameter (entry, display), you
     must return the display name that the shell will use to display the specified
     ObjectEntry in the tree or view. The name of the object is displayed next to
     an icon (display image) representing the object. A null can be returned when
     there is no name available, in which case the default name may be used if
     another snap-in doesn't have it.
     If displayStyle = DisplayNameSnapin.SHORT, don't include directory path info. 
    */
   public String getDisplayName(ObjectEntry entry, int displayStyle)
   {
      String name = entry.getName();
      if(name.equals(""))
      {
         return "Invalid Entry";
      }
      
      if (displayStyle == DisplayNameSnapin.SHORT)
      {
         // remove the path information
         int slash = name.lastIndexOf(File.separator);
         if (slash > -1)
         {
           name = name.substring(slash + 1);
         }
      }
      
      if(name.equals(""))
      {
         return "File System";
      }
      return name;
   }
}