<?xml version="1.0" encoding="UTF-8"?>
<!--
 DO NOT ALTER OR REMOVE COPYRIGHT NOTICE OR THIS HEADER.

 Copyright 2006, 2007 Sun Microsystems, Inc. All rights reserved.
 Use is subject to license terms.

 The contents of this file are subject to the terms of the Common Development
 and Distribution License("CDDL") (the "License"). You may not use this file
 except in compliance with the License.

 You can obtain a copy of the License at https://openinstaller.dev.java.net/license.html
 or http://openinstaller.dev.java.net/license.txt . See the License for the
 specific language governing permissions and limitations under the License.

 When distributing the Covered Code, include this CDDL Header Notice in each
 file and include the License file at http://openinstaller.dev.java.net/license.txt .
 If applicable, add the following below this CDDL Header, with the fields
 enclosed by brackets [] replaced by your own identifying information:
 "Portions Copyrighted [year] [name of copyright owner]"

 ident "@(#)%Name% %Revision% %Date% SMI"

-->
<apt:template apt:name="JDKSelection" xmlns:apt="http://www.sun.com/jds/apoc/2004/template"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:oor="http://openoffice.org/2001/registry"
  xsi:schemaLocation="http://www.sun.com/jds/apoc/2004/template ../../../consumables/schemas/config/apoc/policytemplate.xsd">
 <apt:category apt:name="JDKSelection" apt:label="JDKSelection" >

 <apt:page apt:name="Install_page" apt:label="InstallHome" >

  <apt:section apt:name="directory" apt:label="DirectorySelection" >
      <apt:property apt:name="JDK_HOME_TEXT"
         apt:dataPath=""
         apt:type="xs:string">
         <apt:prop-value>$JDK_HOME_TEXT</apt:prop-value>
         <apt:visual>
            <apt:htmlPanel/>
         </apt:visual>
        </apt:property>
        <apt:property apt:name="JDK_FROM_MEDIA_CHOICE" apt:label=""
         apt:dataPath="directory.JDK_FROM_MEDIA_CHOICE"
         apt:type="xs:boolean">
         <apt:prop-value>false</apt:prop-value>
         <apt:visual>
            <apt:checkBox apt:labelPost="$JDK_FROM_MEDIA_CHOICE_TEXT"/>
         </apt:visual>
         <apt:scriptHandler apt:scriptLanguage="beanshell" >
           <apt:scriptCode><![CDATA[
             theAL = new ActionListener() {
               actionPerformed(e) {
                 option = wizard.getComponentByName(content, "JDK_FROM_MEDIA_CHOICE");
                 theCombo = wizard.getComponentByName(content, "JDK_LIST");
                 theJDKTypeIn = wizard.getComponentByName(content, "JDK_TYPE_IN");
                 theHiddenJDK = wizard.getComponentByName(content, "HIDDEN_JDK");
                 theJDKTypeIn.setText("");
                 theHiddenJDK.setText("");
                 if(option.isSelected()) {
                   theCombo.setEnabled(false);
                   theJDKTypeIn.setEnabled(false);
                 }
               }
             };
             return theAL;
           ]]></apt:scriptCode>
         </apt:scriptHandler>
        </apt:property>

        <apt:property apt:name="JDK_FROM_MEDIA_VERSION" apt:label=""
         apt:dataPath=""
         apt:type="xs:string">
         <apt:prop-value></apt:prop-value>
         <apt:visual>
            <apt:htmlPanel/>
         </apt:visual>
    </apt:property>
    <apt:property apt:name="JDK_FROM_LIST_CHOICE" apt:label=""
         apt:dataPath="directory.JDK_FROM_LIST_CHOICE"
         apt:type="xs:boolean">
         <apt:prop-value>true</apt:prop-value>
         <apt:visual>
            <apt:checkBox apt:labelPost="$JDK_FROM_LIST_CHOICE_TEXT"/>
         </apt:visual>
         <apt:scriptHandler apt:scriptLanguage="beanshell" >
           <apt:scriptCode><![CDATA[
             theAL = new ActionListener() {
               actionPerformed(e) {
                 option = wizard.getComponentByName(content, "JDK_FROM_LIST_CHOICE");
                 theCombo = wizard.getComponentByName(content, "JDK_LIST");
                 theJDKTypeIn = wizard.getComponentByName(content, "JDK_TYPE_IN");
                 theHiddenJDK = wizard.getComponentByName(content, "HIDDEN_JDK");
                 theJDKTypeIn.setText("");
                 theHiddenJDK.setText("");
                 if(option.isSelected()) {
                   theCombo.setEnabled(true);
                   theJDKTypeIn.setEnabled(false);
                   setValueForCombo(0);
                 }
               }
             };
             return theAL;
           ]]></apt:scriptCode>
         </apt:scriptHandler>
    </apt:property>
    <apt:property apt:name="JDK_LIST" apt:label=""
         apt:dataPath="directory.JDK_LIST"
         apt:type="xs:string">
         <apt:prop-value></apt:prop-value>
         <apt:visual>
            <apt:comboBox apt:toolTip="$JDK_LIST_TOOLTIP"/>
         </apt:visual>
    </apt:property>
    <apt:property apt:name="JDK_TYPED_IN_CHOICE" apt:label=""
         apt:dataPath="directory.JDK_TYPED_IN_CHOICE"
         apt:type="xs:boolean">
       <apt:prop-value>false</apt:prop-value>
       <apt:visual>
          <apt:checkBox apt:labelPost="$JDK_TYPED_IN_CHOICE_TEXT"/>
       </apt:visual>
       <apt:scriptHandler apt:scriptLanguage="beanshell" >
          <apt:scriptCode><![CDATA[
             theAL = new ActionListener() {
               actionPerformed(e) {
                 option = wizard.getComponentByName(content, "JDK_TYPED_IN_CHOICE");
                 theCombo = wizard.getComponentByName(content, "JDK_LIST");
                 theJDKTypeIn = wizard.getComponentByName(content, "JDK_TYPE_IN");
                 theHiddenJDK = wizard.getComponentByName(content, "HIDDEN_JDK");
                 theJDKTypeIn.setText("");
                 theHiddenJDK.setText("");
                 if(option.isSelected()) {
                   theCombo.setEnabled(false);
                   theJDKTypeIn.setEnabled(true);
                 }
               }
             };
             return theAL;
           ]]></apt:scriptCode>
         </apt:scriptHandler>
        </apt:property>
        <apt:property apt:name="JDK_TYPE_IN" apt:label=""
         apt:dataPath="directory.JDK_TYPE_IN"
         apt:type="xs:string">
         <apt:prop-value></apt:prop-value>
         <apt:visual>
           <apt:directorySelector apt:toolTip="$JDK_TYPE_IN_TOOLTIP"/>
         </apt:visual>
        </apt:property>
        <apt:property apt:name="HIDDEN_JDK" apt:label=""
         apt:dataPath="directory.HIDDEN_JDK"
         apt:type="xs:string">
         <apt:prop-value></apt:prop-value>
         <apt:visual>
           <apt:textField/>
         </apt:visual>
        </apt:property>
  </apt:section>
  <apt:scriptHandler apt:scriptLanguage="beanshell" >
  <apt:scriptCode><![CDATA[
  import java.io.File;
  import java.util.List;
  import java.util.ArrayList;
  import java.util.StringTokenizer;
  import org.openinstaller.util.*;
  import org.openinstaller.util.Platform.OSType;
  import org.openinstaller.provider.conf.ConfigHelper;
  import org.openinstaller.provider.dependency.ComponentDetails;
  import org.openinstaller.provider.dependency.DependencyControl;


  initComponents() {
      option1 = wizard.getComponentByName(content, "JDK_FROM_MEDIA_CHOICE");
      option2 = wizard.getComponentByName(content, "JDK_FROM_LIST_CHOICE");
      option3 = wizard.getComponentByName(content, "JDK_TYPED_IN_CHOICE");
      theCombo = wizard.getComponentByName(content, "JDK_LIST");
      theJDKTypeIn = wizard.getComponentByName(content, "JDK_TYPE_IN");
      theHiddenJDK = wizard.getComponentByName(content, "HIDDEN_JDK");
      theHiddenJDK.setVisible(false);
    }

    setValueForCombo(int index) {
      theHiddenJDK = wizard.getComponentByName(content, "HIDDEN_JDK");
      theJDKComps = (List) Scripting.getInterpreter().get("gJDKComponents");
      ComponentDetails theJDKComp = theJDKComps.get(index);
      File theFile = theJDKComp.getLocation();
      theHiddenJDK.setText(theFile.getAbsolutePath());
    }

    boolean isProductIncluded(String prodName) {
     final BusRequest theRequest = new BusRequest("PROD_CHECK",
       new DependencyControl(DependencyControl.COMMAND.GET_PRODUCT_NODE, prodName));
     final BusResponse theResponse = ControlBus.getInstance().publishRequest(
       theRequest, "svc://general/Dependency");
     return (theResponse.getResult() != null);
    }

    String getInfo(String prodName) {
      final BusRequest theRequest = new BusRequest("GET_INFO",
                                             new DependencyControl(
                                                 DependencyControl.COMMAND.GET_COMPONENT_INFO,
                                                 prodName));
      final BusResponse theResponse = ControlBus.getInstance().publishRequest(
                                          theRequest, "svc://general/Dependency");
      return theResponse.getResult();
    }

    run() {
      option1 = wizard.getComponentByName(content, "JDK_FROM_MEDIA_CHOICE");
      option2 = wizard.getComponentByName(content, "JDK_FROM_LIST_CHOICE");
      option3 = wizard.getComponentByName(content, "JDK_TYPED_IN_CHOICE");
      theCombo = wizard.getComponentByName(content, "JDK_LIST");
      theJDKTypeIn = wizard.getComponentByName(content, "JDK_TYPE_IN");
      theHiddenJDK = wizard.getComponentByName(content, "HIDDEN_JDK");

      ButtonGroup theGroup = new ButtonGroup();
      theGroup.add(option1);
      theGroup.add(option2);
      theGroup.add(option3);

      theProdName = ConfigHelper.getValue("JDK.Installation.JDK_PRODUCT_NAME");

      initComponents();

      if (isProductIncluded(theProdName)) {
        theInfo = getInfo(theProdName);
        theVersionHolder = wizard.getComponentByName(content, "JDK_FROM_MEDIA_VERSION");
        theVersionContent = new StringBuilder("<html><body><font face='Sans-Serif' size='3'><center>");
        theVersionContent.append(theInfo);
        theVersionContent.append("</center></font></body></html>");
        theVersionHolder.setText(theVersionContent.toString());
      } else {
        option1.setEnabled(false);
	option1.setSelected(false);
	option2.setSelected(true);
      }

      theJDKComps = (List) Scripting.getInterpreter().get("gJDKComponents");

      /*
       * See if we need to exclude any JDKs in 'theJDKComps':
       *  - get value of 'Exclude-JVMs' property
       *  - remove entries in 'theJDKComps' if needed
       */

      /*
       * Get value of 'Exclude-JVMs' property if it was set
       * Example value of this property:
       *	/foo/bar:/thispath/thatpath (Unix)
       *	C:\foo\bar;D:\thispath\thatpath
       */
       BusRequest opsReq = new BusRequest("ConfigRequest",
           new EngineConfigCommand(EngineConfigCommand.COMMAND.GET_CONFIG, 
						EngineConfigItem.EXCLUDE_JVMS));
       BusResponse opsResp = bus.publishRequest(opsReq, "svc://general/EngineConfig");
       String excludeJVMs = (String)opsResp.getResult();
       List pathAList = null;

      /*
       * Store list of paths in ArrayList
       */
       if (excludeJVMs != null)  {
         StringTokenizer st = new StringTokenizer(excludeJVMs, File.pathSeparator);
         pathAList = new ArrayList();
         excludeList = new ArrayList();
    
         while (st.hasMoreTokens()) {
            String theNextToken = st.nextToken();
            pathAList.add(theNextToken);
         }

         /*
	  * Remove entries in theJDKComps as needed
	  *  - build an exclude list i.e. a list of JDKs to remove from theJDKComps
	  *  - use the exclude list to perform the actual removal
	  *
	  * The above need to be separate because removing an item from a list while
	  * iterating through the list will give you a ConcurrentModificationException
	  */
         if (pathAList.size() > 0)  {
           for (ComponentDetails theJDK : theJDKComps) {
	      /*
	       * Check if theJDK is in the exclude list
	       */
	      for (String oneExclude : pathAList)  {
	          if (theJDK.getLabel().startsWith(oneExclude))  {
		      /*
		       * Add it to the exclude list
		       */
		      excludeList.add(theJDK);
		      break;
	          }
	      }

           }

	   /*
	    * If the exclude list was non empty, iterate through it, removing
	    * entries from theJDKComps.
	    */
	   if (excludeList.size() > 0)  {
	     for (ComponentDetails oneComp : excludeList)  {
	       theJDKComps.remove(oneComp);
	     }
	   }
         }
       }

      if (theJDKComps == null || theJDKComps.size() == 0) {
        option2.setEnabled(false);
        option2.setSelected(false);
        if (!isProductIncluded(theProdName)) {
          option3.setSelected(true);
          theJDKTypeIn.setEnabled(true);
          theMsg = StringUtils.wrap(Msg.get("NO_JDKS_DETECTED_OR_BUNDLED", null), 70);
        } else {
          option1.setSelected(true);
          theMsg = StringUtils.wrap(Msg.get("NO_JDKS_DETECTED", null), 70);
        }
        JOptionPane.showMessageDialog(frame, theMsg, Msg.get("INFO", null), JOptionPane.INFORMATION_MESSAGE);
      }
      else {
        for (ComponentDetails theJDK : theJDKComps) {
          theCombo.addItem(theJDK.getLabel());
        }
        theCombo.setSelectedIndex(0);

        setValueForCombo(0);

        theListAL = new ActionListener() {
          actionPerformed(e) {
            theIndex = theCombo.getSelectedIndex();
            setValueForCombo(theIndex);
          }
        };
        theCombo.addActionListener(theListAL);
      }
      theTextListener = new ActionListener() {
        actionPerformed(e) {
          theHiddenJDK.setText(theJDKTypeIn.getText());
        }
      };
      theJDKTypeIn.getTextField().addActionListener(theTextListener);

      wizard.enableCancel(true);
      wizard.enableHelp(true);
      wizard.enableBack(true);
      wizard.enableNext(true);
    }

    return (Runnable)this;
  ]]></apt:scriptCode>
  </apt:scriptHandler>
 </apt:page>
 </apt:category>
</apt:template>

