/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2015 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */
package customization;

import ilog.cpl.service.IlpContext;
import ilog.cpl.service.IlpImageRepository;

import ilog.tgo.graphic.IltLinkPort;
import ilog.tgo.model.IltLink;
import ilog.tgo.model.IltNetworkElement;
import ilog.tgo.resource.IltSettings;


/**
 * This class performs customizations for various objects and states in the sample.
 */
public class Customization {

   /**
    * This method customizes link media, bellcore states, SONET states and info icon using
    * gobal css settings.
    * 
    * This method performs the same customizations as the following methods:
    * <ul>
    * <li>{@link #customizeNetworkElement()}</li>
    * <li>{@link #customizeLinkMedia(IlpContext)}</li>
    * <li>{@link #customizeBellcoreState(IlpContext)}</li>
    * <li>{@link #customizeSONETState(IlpContext)}</li>
    * <li>{@link #customizeInfoIcon(IlpContext)}</li>
    * </ul>
    *
    */
   public static void customizeSettings(IltSettings settings) {
      try {
         settings.setStyleSheets(new String[] { "settings.css" });
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   
   /**
    * This method customizes the following predefined network element types:
    * <ul>
    *   <li><code>Desktop</code> as a fancy and colorful desktop computer
    *   <li><code>Laptop</code> as a fancy and colorful laptop computer
    *   <li><code>Server</code> as a fancy and colorful server computer
    *   <li><code>Printer</code> as a fancy and colorful desk printer
    * </ul>
    * For each network element type, it creates a set of custom linkports 
    * (see <code>object.css</code> for details on how to associate linkports 
    * with a given object).
    * 
    * <p>Besides the customization of predefined types, the method also
    * creates and customizes a new network element type named 
    * <code>Network</code>.
    * 
    * <p>This method customizes the network element types by defining new
    * base renderer for each one of them. Please refer to the <i>Styling</i>
    * chapter of the <i>User's Manual</i> for details on how to customize
    * business objects; the <i>Customizing Network Elements</i> section of
    * this chapter has details on how to customize netowrk elements.
    * 
    * <p><strong>Hint:</strong> The linkports are defined as fractions of
    * the base, for example:
    * <code><pre>
    *   new IltLinkPort("MyPort", 0.50f, 0.10f);
    * </pre></code>
    * defines a linkport at position <code>(width/2,height/10)</code>. 
    * If your type image is a square, it will be easier to find out the
    * linkports by resizing the image to, say, <code>100x100</code> pixels
    * on your image editor of choice and use its coordinate system to get
    * the proper values for the linkports.
    */
   public static void customizeNetworkElement() {
     // Customizes the 'Desktop' NE type
     IltSettings.SetValue("NetworkElement.Type.Desktop.Renderer", new NEBaseRendererFactory());
    
     // linkports for type "Desktop"
     IltLinkPort port = (IltLinkPort)IltLinkPort.metainfo.get("DesktopTop");
     if (null == port) {
       port = new IltLinkPort("DesktopTop", 0.50f, 0.10f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("DesktopRight");
     if (null == port) {
       port = new IltLinkPort("DesktopRight", 0.70f, 0.40f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("DesktopBottom");
     if (null == port) {
       port = new IltLinkPort("DesktopBottom", 0.50f, 0.80f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("DesktopLeft");
     if (null == port) {
       port = new IltLinkPort("DesktopLeft", 0.28f, 0.50f);
     }

     // Customizes the 'Laptop' NE type
     IltSettings.SetValue("NetworkElement.Type.Laptop.Renderer", new NEBaseRendererFactory());

     // linkports for type "Laptop"
     port = (IltLinkPort)IltLinkPort.metainfo.get("LaptopTop1");
     if (null == port) {
       port = new IltLinkPort("LaptopTop1", 0.10f, 0.20f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("LaptopTop2");
     if (null == port) {
       port = new IltLinkPort("LaptopTop2", 0.57f, 0.10f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("LaptopRight");
     if (null == port) {
       port = new IltLinkPort("LaptopRight", 0.91f, 0.76f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("LaptopBottom");
     if (null == port) {
       port = new IltLinkPort("LaptopBottom", 0.51f, 0.92f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("LaptopLeft");
     if (null == port) {
       port = new IltLinkPort("LaptopLeft", 0.15f, 0.77f);
     }

     // Customizes the 'Server' NE type
     IltSettings.SetValue("NetworkElement.Type.Server.Renderer", new NEBaseRendererFactory());

     // linkports for type "Server"
     port = (IltLinkPort)IltLinkPort.metainfo.get("ServerTop");
     if (null == port) {
       port = new IltLinkPort("ServerTop", 0.50f, 0.10f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("ServerTopLeft");
     if (null == port) {
       port = new IltLinkPort("ServerTopLeft", 0.15f, 0.15f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("ServerTopRight");
     if (null == port) {
       port = new IltLinkPort("ServerTopRight", 0.85f, 0.15f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("ServerRight");
     if (null == port) {
       port = new IltLinkPort("ServerRight", 0.90f, 0.50f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("ServerBottom");
     if (null == port) {
       port = new IltLinkPort("ServerBottom", 0.50f, 0.85f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("ServerBottomLeft");
     if (null == port) {
       port = new IltLinkPort("ServerBottomLeft", 0.20f, 0.90f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("ServerBottomRight");
     if (null == port) {
       port = new IltLinkPort("ServerBottomRight", 0.85f, 0.75f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("ServerLeft");
     if (null == port) {
       port = new IltLinkPort("ServerLeft", 0.13f, 0.50f);
     }

     // Customizes the 'Printer' NE type
     IltSettings.SetValue("NetworkElement.Type.Printer.Renderer", new NEBaseRendererFactory());
     // linkports for type "Printer"
     port = (IltLinkPort)IltLinkPort.metainfo.get("PrinterTop");
     if (null == port) {
       port = new IltLinkPort("PrinterTop", 0.50f, 0.15f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("PrinterRight");
     if (null == port) {
       port = new IltLinkPort("PrinterRight", 0.80f, 0.50f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("PrinterBottom");
     if (null == port) {
       port = new IltLinkPort("PrinterBottom", 0.50f, 0.80f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("PrinterLeft");
     if (null == port) {
       port = new IltLinkPort("PrinterLeft", 0.20f, 0.50f);
     }

     // Creates the 'Network' NE type
     IltNetworkElement.Type Network = (IltNetworkElement.Type) IltNetworkElement.Type.metainfo.get("Network");
     if (null == Network) 
       Network = new IltNetworkElement.Type("Network");
       
     // Customizes the 'Network' NE type
     IltSettings.SetValue("NetworkElement.Type.Network.Renderer", new NEBaseRendererFactory());
     IltSettings.SetValue("NetworkElement.Type.Network.Label", "Network");
    
     // linkports for type "Network"
     port = (IltLinkPort)IltLinkPort.metainfo.get("NetworkTop");
     if (null == port) {
       port = new IltLinkPort("NetworkTop", 0.61f, 0.10f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("NetworkTopRight");
     if (null == port) {
       port = new IltLinkPort("NetworkTopRight", 0.81f, 0.18f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("NetworkTopLeft");
     if (null == port) {
       port = new IltLinkPort("NetworkTopLeft", 0.26f, 0.07f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("NetworkRightA");
     if (null == port) {
       port = new IltLinkPort("NetworkRightA", 0.90f, 0.39f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("NetworkRightB");
     if (null == port) {
       port = new IltLinkPort("NetworkRightB", 0.93f, 0.70f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("NetworkBottom");
     if (null == port) {
       port = new IltLinkPort("NetworkBottom", 0.25f, 0.93f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("NetworkBottomRight");
     if (null == port) {
       port = new IltLinkPort("NetworkBottomRight", 0.87f, 0.85f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("NetworkBottomLeft");
     if (null == port) {
       port = new IltLinkPort("NetworkBottomLeft", 0.08f, 0.83f);
     }
     port = (IltLinkPort)IltLinkPort.metainfo.get("NetworkLeft");
     if (null == port) {
       port = new IltLinkPort("NetworkLeft", 0.06f, 0.41f);
     }
   }

   /**
    * This method creates the following new linke media:
    * <ul>
    *   <li><code>USB</code> meaning a regular USB connection
    *   <li><code>WiFi</code> for a generic 802.11 wireless connection
    *   <li><code>Twisted</code> for a generic Ethernet connection
    * </ul>
    * 
    * <p>A new link media is created by extending the 
    * <code>IltLink.Media</code> enumeration and associating it to an
    * icon. Please refer to <i>Customizing Link Media</i> section in the 
    * <i>Styling</i> chapter of the <i>User's Manual</i> for details on 
    * how to customize link medium.
    * 
    * @param context  The appliation context.
    */
   public static void customizeLinkMedia(IlpContext context) { 
     IlpImageRepository repository = context.getImageRepository();

     // Create new USB link media
     IltLink.Media media = (IltLink.Media) IltLink.Media.metainfo.get("USB");
     if (null == media)
       media = new IltLink.Media("USB");
     // Associate an image with it
     IltSettings.SetValue("Link.Media.USB.Icon",
         repository.getImage("resources/media_usb.png"));

     // Create new WiFi link media
     media = (IltLink.Media) IltLink.Media.metainfo.get("WiFi");
     if (null == media)
       media = new IltLink.Media("WiFi");
     // Associate an image with it
     IltSettings.SetValue("Link.Media.WiFi.Icon",
         repository.getImage("resources/media_wifi.png"));

     // Create new Twisted link media
     media = (IltLink.Media) IltLink.Media.metainfo.get("Twisted");
     if (null == media)
       new IltLink.Media("Twisted");
     // Associate an image with it
     IltSettings.SetValue("Link.Media.Twisted.Icon",
         repository.getImage("resources/media_twisted.png"));
   }
   
   /**
    * This method customizes the following Bellcore secondary states:
    * <ul>
    *   <li><code>Blocked</code> (Enabled/Active - CT)
    *   <li><code>Busy</code> (Enabled/Active - CT)
    *   <li><code>Disconnected</code> (Enabled/Active - CT)
    *   <li><code>LockedOut</code> (Enabled/Active - CT)
    *   <li><code>Maintenance</code> (Enabled/Active - CT)
    *   <li><code>Overflow</code> (Enabled/Active - CT)
    *   <li><code>Idle</code> (Enabled/Idle - NT)
    * </ul>
    * 
    * There are several ways to customize the Bellcore state system,
    * this method does it by associating a new image with the selected
    * secondary states. Please refer to 
    * <i>Customizing the Bellcore State System</i> section in the 
    * <i>Styling</i> chapter of the <i>User's Manual</i> for details.
    * 
    * @param context  The appliation context.
    */
   public static void customizeBellcoreState(IlpContext context) {
     IlpImageRepository repository = context.getImageRepository();

     // Set new icon for the Blocked (CT) sec state
     IltSettings.SetValue("Bellcore.SecState.Blocked.CT.Icon", 
                          repository.getImage("resources/sec_blocked.png"));
     // Set new icon for the Busy (CT) sec state
     IltSettings.SetValue("Bellcore.SecState.Busy.CT.Icon", 
                          repository.getImage("resources/sec_busy.png"));
     // Set new icon for the Disconnected (OOS) sec state
     IltSettings.SetValue("Bellcore.SecState.Disconnected.OOS.Icon", 
                          repository.getImage("resources/sec_disconnected.png"));
     // Set new icon for the LockedOut (CT) sec state
     IltSettings.SetValue("Bellcore.SecState.LockedOut.CT.Icon", 
                          repository.getImage("resources/sec_lockedout.png"));
     // Set new icon for the Maintenance (OOS) sec state
     IltSettings.SetValue("Bellcore.SecState.Maintenance.OOS.Icon", 
                          repository.getImage("resources/sec_maintenance.png"));
     // Set new icon for the Overflow (CT) sec state
     IltSettings.SetValue("Bellcore.SecState.Overflow.CT.Icon", 
                          repository.getImage("resources/sec_overflow.png"));
     // Set new icon for the Idle (NT) sec state
     IltSettings.SetValue("Bellcore.SecState.Idle.NT.Icon", 
                          repository.getImage("resources/sec_idle.png"));
   }
   
   /**
    * This method customizes all the SONET secondary states, namely:
    * <ul>
    *   <li><code>Exercisor</code>
    *   <li><code>ForcedSwitch</code>
    *   <li><code>Locked</code>
    *   <li><code>ManualSwitch</code>
    *   <li><code>Pending</code>
    *   <li><code>WaitToRestore</code>
    * </ul>
    * 
    * There are several ways to customize the SONET state system,
    * this method does it by associating a new image with all the 
    * secondary states. Please refer to 
    * <i>Customizeing the SONET State System</i> section in the 
    * <i>Styling</i> chapter of the <i>User's Manual</i> for details.
    * 
    * @param context  The appliation context.
    */
   public static void customizeSONETState(IlpContext context) {
     IlpImageRepository repository = context.getImageRepository();

     // Set new icon for the Exercisor sec state (protection)
     IltSettings.SetValue("SONET.Protection.Exercisor.Icon", 
                          repository.getImage("resources/prot_exercisor.png"));
     // Set new icon for the Forced Switch sec state (protection)
     IltSettings.SetValue("SONET.Protection.ForcedSwitch.Icon",  
                         repository.getImage("resources/prot_forced.png"));
     // Set new icon for the Locked sec state (protection)
     IltSettings.SetValue("SONET.Protection.Locked.Icon", 
                          repository.getImage("resources/prot_locked.png"));
     // Set new icon for the Manual Switch sec state (protection)
     IltSettings.SetValue("SONET.Protection.ManualSwitch.Icon", 
                          repository.getImage("resources/prot_manual.png"));
     // Set new icon for the Pending sec state (protection)
     IltSettings.SetValue("SONET.Protection.Pending.Icon", 
                          repository.getImage("resources/prot_pending.png"));
     // Set new icon for the Wait to Restore sec state (protection)
     IltSettings.SetValue("SONET.Protection.WaitToRestore.Icon", 
                          repository.getImage("resources/prot_wait.png"));
   }
   
   /**
    * This method customizes the icons for the information windows (which
    * group several secondary states for a given business object). Please 
    * refer to <i>Information Window</i> section in the 
    * <i>Business Objects and Data Sources</i> chapter of the 
    * <i>User's Manual</i> for details.
    * 
    * <p>The customization is done by associating a new icon to the two
    * default information window icons:
    * <ul>
    *   <li><code>InformationWindow.Icon</code> for when the information 
    *       window has not been viewed yet
    *   <li><code>InformationWindow.Acknowledged.Icon</code> for when the 
    *       information window has been viewed at least once 
    * </ul>  
    * 
    * @param context  The appliation context.
    */
   public static void customizeInfoIcon(IlpContext context) {
     IlpImageRepository repository = context.getImageRepository();

     // set infoicon for the non-viewed info window
     IltSettings.SetValue("InformationWindow.Icon",
                          repository.getImage("resources/infoicon.png"));
     // set infoicon for the already-viewed info window
     IltSettings.SetValue("InformationWindow.Acknowledged.Icon",
                          repository.getImage("resources/infoicon2.png"));
   }
}