<%@ page import="com.novell.emframe.fw.studio.StudioPluginInfo" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.List" %>
<%@ page import="com.novell.emframe.dev.dataTable.*" %>
<%@ page import="com.novell.emframe.dev.eMFrameUtils" %>
<%@ page import="com.novell.emframe.dev.MContext" %>
<%@ page import="com.novell.emframe.dev.eMFrameFactory" %>
<%@ page import="com.novell.application.console.snapin.ObjectEntry" %>
<%@ page import="java.util.Enumeration" %>
<%@ page import="com.novell.application.console.snapin.ObjectEntryEnumeration" %>
<%@ page import="com.novell.admin.ns.nds.NDSNamespace" %>
<%@ page import="com.novell.webaccess.common.JSPConduit"%>
<%@ page pageEncoding="utf-8" contentType="text/html;charset=utf-8"  %>
<%@ page import="java.util.Collections" %>
<%@ page import="java.util.Collection" %>
<%@ page import="com.novell.emframe.fw.FwUtils" %>
<%@ page import="com.novell.emframe.fw.studio.*" %>

<%@ taglib uri="/WEB-INF/iman.tld" prefix="iman" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>

<iman:stringtable bundle="FwResources"/>
<iman:stringtable bundle="DevResources"/>


<HTML>
<HEAD>
   <SCRIPT src="<c:out value="${ContextPath}" />/portal/modules/fw/javascripts/Studio.js"></SCRIPT>
   <iman:eMFrameScripts/>
   <iman:dataTableScripts/>
   <iman:stylesheet/>

   <SCRIPT>
      function onExit()
      {
         window.location = "webacc?NPService=fw.LaunchService&NPAction=Return&returnID=<c:out value="${GI_ID}"/>";
      }

      function gatherSelections(selectedIndices, tableData)
      {
         var ids = new Array();
         var types = new Array();

         for(var i=0; i < selectedIndices.length; i++)
         {
            var index = selectedIndices[i];
            ids[ids.length] = tableData[index][1];
            types[types.length] = tableData[index][0];
         }

         document.form.pluginId.value = pack(ids);
         document.form.types.value = pack(types);
      }

      function myEventHandler(menuItemId, selectedIndices, tableData)
      {
         gatherSelections(selectedIndices, tableData);
         document.form.target = "_self";

         if(menuItemId=="Import")         <%----- IMPORT -----%>
         {
            document.form.nextState.value = "importGatherInfo";
            document.form.submit();
         }
         else if(menuItemId=="Export")    <%----- EXPORT -----%>
         {
            if(selectedIndices.length < 1)
            {
            	alert("<iman:string key="Studio.selectAnItem"/>");
            }
            else
            {
            	document.form.target = "Empty";
            	document.form.nextState.value = "exportPlugin";
            	document.form.submit();
            }
         }
         else if(menuItemId=="New")       <%----- NEW -----%>
         {
            window.location = "frameservice?NPService=fw.LaunchService&NPAction=Delegate&delegate=fw.Create iManager Task&launcher=<c:out value="${taskId}"/>";
         }
         else if(menuItemId=="Edit")      <%----- EDIT -----%>
         {
            if(selectedIndices.length != 1)
            {
               alert("<iman:string key="Studio.selectOneToModify"/>");
            }
            else
            {
               var plugin = tableData[selectedIndices[0]];
               var id = plugin[1];
               var pluginType = plugin[0];
               var url = "frameservice?NPService=fw.LaunchService&NPAction=Delegate&delegate=fw.Create iManager Task&launcher=<c:out value="${taskId}"/>&nextState=doModify&merge=fw.Studio_main&pluginType="+pluginType+"&plugin=" + urlEncode(id);
               window.location = url;
            }
         }
         else if(menuItemId=="Copy")      <%----- COPY -----%>
         {
            if(selectedIndices.length != 1)
            {
               alert("<iman:string key="Studio.selectOneToCopy"/>");
            }
            else
            {
               var plugin = tableData[selectedIndices[0]];
               var id = plugin[1];
               var pluginType = plugin[0];
               window.location = "frameservice?NPService=fw.LaunchService&NPAction=Delegate&delegate=fw.Create iManager Task&launcher=<c:out value="${taskId}"/>&nextState=doCopy&merge=fw.Studio_main&pluginType="+plugin[0]+"&plugin=" + urlEncode(id);
            }
         }
         else if(menuItemId=="Delete")    <%----- DELETE -----%>
         {
         	if(selectedIndices.length > 0)
         	{
            	var ok = confirm("<iman:string key="Studio.confirmPluginDelete"/>");
            	if(ok)
            	{
               		document.form.nextState.value = "deletePlugin";
               		document.form.submit();
            	}
            }
            else
            {
            	alert("<iman:string key="Studio.selectOneToDelete"/>");
            }
         }
      }

      function init()
      {
      }
   </SCRIPT>
</HEAD>

<BODY onLoad="init()">
<FORM name="form">
   <INPUT type="hidden" name="nextState">
   <INPUT type="hidden" name="pluginId">
   <INPUT type="hidden" name="types">
   <INPUT type="hidden" name="taskId" value="<c:out value="${taskId}"/>">
   <INPUT type="hidden" name="merge" value="fw.Studio_overview">
   <INPUT type="hidden" name="error" value="dev.GenErr">

   <iman:taskHeader titleKey="Studio.taskName" titleBundle="FwResources" iconUrl="dev/bullet_default_16.gif" helpFile="fw/studio_overview.html" /><BR>
   <iman:include page="dev/Messagebar_inc.jsp"/>


      <%
         // Data
         List customPlugins = (List)pageContext.findAttribute("customTasks");
         java.util.Collections.sort(customPlugins, new PluginDisplayNameComparator(FwUtils.getCurrentLocale(session)));
         TableCell[][] data = new TableCell[customPlugins.size()][];
         int i=0;
         for(Iterator iter = customPlugins.iterator(); iter.hasNext();)
         {
            StudioPluginInfo pluginInfo = (StudioPluginInfo) iter.next();
            
            data[i] = new TableCell[3];
            String iconUrl = pluginInfo.getType().equals("task") ? "dev/dir/rbsTask2.gif" : "dev/dir/rbsBook2.gif";
            data[i][0] = new IconTableCell(pluginInfo.getType(), iconUrl, pluginInfo.getType());
            data[i][1] = new TextTableCell(pluginInfo.getDisplayName(), pluginInfo.getId());
            data[i][2] = new TextTableCell(pluginInfo.getLocation(), pluginInfo.getLocation());
            i++;
         }
         request.setAttribute("data", data);
      %>

      <iman:dataTable id="myTable" titleKey="Studio.installedCustomPlugins" tableData="${data}" menuActionHandler="myEventHandler">

         <iman:menuItem textKey="General.New" id="New"/>
         <iman:menuItem textKey="General.Edit" id="Edit"/>
         <iman:menuItem textKey="General.Delete" id="Delete"/>

         <iman:menuItem textKey="Studio.actions" id="Actions">
            <iman:menuItem textKey="Studio.copy" id="Copy"/>
            <iman:menuItem textKey="Studio.export" id="Export"/>
            <iman:menuItem textKey="Studio.import" id="Import"/>
         </iman:menuItem>
         
         <iman:columnHeader textKey="Studio.type"/>
         <iman:columnHeader textKey="Studio.id"/>
         <iman:columnHeader textKey="Studio.location"/>

      </iman:dataTable>

   <BR><BR>
   <iman:bar/>
   <iman:button key="Done" onClick="onExit(); return false;"/>

</FORM>
</BODY>
</HTML>
