/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.execution.ui;

import com.intellij.execution.ExecutionBundle;
import com.intellij.ide.util.BrowseFilesListener;
import com.intellij.openapi.projectRoots.ProjectJdkTable;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.ui.ComponentWithBrowseButton;
import com.intellij.openapi.ui.TextComponentAccessor;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.ui.GuiUtils;
import com.intellij.ui.InsertPathAction;
import com.intellij.ui.TextFieldWithHistory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * User: anna
 * Date: Jun 21, 2005
 */
public class AlternativeJREPanel extends JPanel{
  private final ComponentWithBrowseButton<TextFieldWithHistory> myPathField;
  private final JCheckBox myCbEnabled;
  final TextFieldWithHistory myFieldWithHistory;

  public AlternativeJREPanel() {
    super(new GridBagLayout());
    myCbEnabled = new JCheckBox(ExecutionBundle.message("run.configuration.use.alternate.jre.checkbox"));
    final GridBagConstraints gc = new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0, GridBagConstraints.NORTHWEST,
                                                         GridBagConstraints.HORIZONTAL, new Insets(2, -2, 2, 2), 0, 0);
    add(myCbEnabled, gc);

    myFieldWithHistory = new TextFieldWithHistory();
    myFieldWithHistory.setBorder(BorderFactory.createEtchedBorder());
    final ArrayList<String> foundJdks = new ArrayList<String>();
    final Sdk[] allJdks = ProjectJdkTable.getInstance().getAllJdks();
    for (Sdk jdk : allJdks) {
      foundJdks.add(jdk.getHomePath());
    }
    myFieldWithHistory.setHistory(foundJdks);
    myPathField = new ComponentWithBrowseButton<TextFieldWithHistory>(myFieldWithHistory, null);
    myPathField.addBrowseFolderListener(ExecutionBundle.message("run.configuration.select.alternate.jre.label"),
                                        ExecutionBundle.message("run.configuration.select.jre.dir.label"),
                                        null, BrowseFilesListener.SINGLE_DIRECTORY_DESCRIPTOR, TextComponentAccessor.TEXT_FIELD_WITH_HISTORY_WHOLE_TEXT);
    gc.insets.left = 20;
    add(myPathField, gc);
    InsertPathAction.addTo(myFieldWithHistory.getTextEditor());

    gc.weighty = 1;
    add(Box.createVerticalBox(), gc);

    myCbEnabled.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        enabledChanged();
      }
    });
    enabledChanged();
  }

  private void enabledChanged() {
    final boolean pathEnabled = isPathEnabled();
    GuiUtils.enableChildren(myPathField, pathEnabled);
    myFieldWithHistory.invalidate(); //need to revalidate inner component
  }

  public String getPath() {
    return FileUtil.toSystemIndependentName(myPathField.getChildComponent().getText().trim());
  }

  private void setPath(final String path) {
    myPathField.getChildComponent().setText(FileUtil.toSystemDependentName(path == null ? "" : path));
  }

  public boolean isPathEnabled() {
    return myCbEnabled.isSelected();
  }

  private void setPathEnabled(boolean b) {
    myCbEnabled.setSelected(b);
    enabledChanged();
  }

  public void init(String path, boolean isEnabled){
    setPathEnabled(isEnabled);
    setPath(path);
  }
}


@@ -1343,16 +1343,17 @@
 s JPanel
+ 
 %7B%0A  priv
@@ -1573,24 +1573,25 @@
 gLayout());%0A
+%0A
     myCbEnab
@@ -2021,78 +2021,8 @@
 ();%0A
-    myFieldWithHistory.setBorder(BorderFactory.createEtchedBorder());%0A
     
@@ -2051,18 +2051,18 @@
 %3E foundJ
-dk
+DK
 s = new 
@@ -2102,18 +2102,18 @@
 k%5B%5D allJ
-dk
+DK
 s = Proj
@@ -2175,18 +2175,18 @@
 k : allJ
-dk
+DK
 s) %7B%0A   
@@ -2194,18 +2194,18 @@
   foundJ
-dk
+DK
 s.add(jd
@@ -2268,18 +2268,18 @@
 y(foundJ
-dk
+DK
 s);%0A    
@@ -2685,16 +2685,56 @@
 CRIPTOR,
+%0A                                       
  TextCom
@@ -3921,9 +3921,8 @@
 ;%0A  %7D%0A%7D%0A
-%0A

Fix alternative JRE panel UI