// Sample code file: FilesystemAbstractToolBarMenu.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.net.*;
import java.io.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;

import com.novell.utility.nmsgbox.*;

// ConsoleOne snapin interfaces.
import com.novell.application.console.snapin.*;
import com.novell.application.console.snapin.scope.*;
import com.novell.application.console.snapin.context.*;

  /**
   Provides an example of extending the AbstractToolBarMenuSnapin class.

   <P>This example of a simple toolbar and menu snap-in places a toolbar 
   item on the toolbar and a menu item labeled "Example Tool" under the File 
   menu. When either of these are selected the current view selection is deleted.
   */
public class FilesystemAbstractToolBarMenu extends AbstractToolBarMenuSnapin
{
   /*
    Implementation of the abstract methods in the AbstractToolBarMenuSnapin
    class.
   */

   /**
    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 AbstractToolBarMenu Name";
   }

   /**
    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 AbstractToolBarMenu Description";
   }

   /**
    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)
   {
      return true;
   }

   /**
    Implement shutdownAbstractSnapin() in your extending snap-in if there is
    anything that needs to be cleaned up, such as removing shell event listeners.
    It will be called from this class's shutdownSnapin() method before it performs
    any necessary clean up.
    */
   public void shutdownAbstractSnapin()
   {
   }

   /**
    The getMenuLocation() method should return the location of the snap-in menu
    item in the main menu hierarchy. If the ancestors returned do not exist,
    they are created. If they do exist, the new menu item is inserted at the
    snap-in point of insertion. Menus in ConsoleOne have locale-independent key
    names that are referred to in the getMenuLocation() method to define what
    menu to snap into. These keys are defined as the ‘action command’ of the menu.
    When defining menus that will be snapped into, set the action command of the
    menu, using the setActionCommand() method, to be the key that other developers
    can use to register against. For ConsoleOne, the console snap-in's main menu uses
    the English menu name as the key. To place items in nested menus, use the % as
    a separator. For example, to snap into the File/New submenu, return File%New
    from the getMenuLocation() method.
   */
   public String getMenuLocation()
   {
      return "Tools";
   }

   /**
    The execute() method is called when a toolbar button is clicked or a menu
    item is selected.
   */
   public void execute()
   {
      ObjectEntryCollection oec = shell.getViewSelections();
      if(!oec.hasNoElements())
      {
         ObjectEntry selection = oec.getFirstElement();
         if (selection.getObjectType().getNamespace().getUniqueID().
            equals("com.novell.sample.snapins.filesystem"))
         {
            File file = new File(selection.getName());

            int ret = shell.launchMsgBox("Confirmation",
               "Are you sure you want to delete " + file.getName(),
               NMsgBox.CONFIRM,
               null);

            if(ret == NMsgBox.YES)
            {
               if (file.exists())
               {
                  try
                  {
                     file.delete();
                     shell.refreshCurrentTreeSelection();
                  }
                  catch (SecurityException e)
                  {
                     System.out.println("No rights to delete file: "
                        + file.getPath());
                  }
               }
               else
               {
                  System.out.println("File not found: "
                     + file.getPath());
               }
            }
         }
      }
   }

   /**
    The getIcon() method should return the icon that will be placed on the toolbar
    button.
   */
   public Icon getIcon()
   {
      Image i = null;
      String path = "/com/novell/sample/snapins/filesystem/images/Tool.gif";
      URL url = getClass().getResource(path);
      if(url != null)
      {
         i = Toolkit.getDefaultToolkit().getImage(url);
      }
      else
      {
         System.err.println(
            "Error loading image in Sample Filesystem snapin");
      }
      return new ImageIcon(i);
   }

   /**
    The getMenuName() method should return the language dependent translated
    (localized) menu display name for the menu item.
   */
   public String getMenuName()
   {
      return "&Example Tool";
   }

   /**
    The getToolTip() method should return the localized string to be placed in the
    tool tip of the toolbar item.
   */
   public String getToolTip()
   {
      return "Tool Tip";
   }
}