/*
 * 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 org.jetbrains.plugins.groovy.runner;

import com.intellij.ide.util.BrowseFilesListener;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SettingsEditor;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.FieldPanel;
import com.intellij.ui.RawCommandLineEditor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.GroovyFileType;

import javax.swing.*;
import java.awt.*;

public class GroovyRunConfigurationEditor extends SettingsEditor<GroovyScriptRunConfiguration> {
  private DefaultComboBoxModel myModulesModel;
  private JComboBox myModulesBox;
  private JPanel myMainPanel;
  private RawCommandLineEditor myVMParameters;
  private RawCommandLineEditor myParameters;
  private JPanel scriptPathPanel;
  private JPanel workDirPanel;
  private JCheckBox myDebugCB;
  private final JTextField scriptPathField;
  private final JTextField workDirField;

  public GroovyRunConfigurationEditor() {

    scriptPathField = new JTextField();
    final BrowseFilesListener scriptBrowseListener = new BrowseFilesListener(scriptPathField,
        "Script Path",
        "Specify path to script",
        new FileChooserDescriptor(true, false, false, false, false, false) {
          public boolean isFileSelectable(VirtualFile file) {
            return file.getFileType() == GroovyFileType.GROOVY_FILE_TYPE;
          }
        });
    final FieldPanel scriptFieldPanel = new FieldPanel(scriptPathField, "Script path:", null, scriptBrowseListener, null);
    scriptPathPanel.setLayout(new BorderLayout());
    scriptPathPanel.add(scriptFieldPanel, BorderLayout.CENTER);

    workDirField = new JTextField();
    final BrowseFilesListener workDirBrowseFilesListener = new BrowseFilesListener(workDirField,
        "Working directory",
        "Specify working directory",
        BrowseFilesListener.SINGLE_DIRECTORY_DESCRIPTOR);
    final FieldPanel workDirFieldPanel = new FieldPanel(workDirField, "Working directory:", null, workDirBrowseFilesListener, null);
    workDirPanel.setLayout(new BorderLayout());
    workDirPanel.add(workDirFieldPanel, BorderLayout.CENTER);
  }

  public void resetEditorFrom(GroovyScriptRunConfiguration configuration) {
    myVMParameters.setDialogCaption("VM Parameters");
    myVMParameters.setText(configuration.vmParams);

    myParameters.setDialogCaption("Script Parameters");
    myParameters.setText(configuration.scriptParams);

    scriptPathField.setText(configuration.scriptPath);
    workDirField.setText(configuration.getWorkDir());

    myDebugCB.setEnabled(true);
    myDebugCB.setSelected(configuration.isDebugEnabled);

    myModulesModel.removeAllElements();
    for (Module module : configuration.getValidModules()) {
      myModulesModel.addElement(module);
    }
    myModulesModel.setSelectedItem(configuration.getModule());
  }

  public void applyEditorTo(GroovyScriptRunConfiguration configuration) throws ConfigurationException {
    configuration.setModule((Module) myModulesBox.getSelectedItem());
    configuration.vmParams = myVMParameters.getText();
    configuration.isDebugEnabled = myDebugCB.isSelected();
    configuration.scriptParams = myParameters.getText();
    configuration.scriptPath = scriptPathField.getText();
    configuration.setWorkDir(workDirField.getText());
  }

  @NotNull
  public JComponent createEditor() {
    myModulesModel = new DefaultComboBoxModel();
    myModulesBox.setModel(myModulesModel);
    myDebugCB.setEnabled(true);
    myDebugCB.setSelected(false);

    myModulesBox.setRenderer(new DefaultListCellRenderer() {
      public Component getListCellRendererComponent(JList list, final Object value, int index, boolean isSelected, boolean cellHasFocus) {
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        final Module module = (Module) value;
        if (module != null) {
          setIcon(module.getModuleType().getNodeIcon(false));
          setText(module.getName());
        }
        return this;
      }
    });

    return myMainPanel;
  }

  public void disposeEditor() {
  }
}

@@ -641,16 +641,68 @@
 unner;%0A%0A
+import com.intellij.ide.ui.ListCellRendererWrapper;%0A
 import c
@@ -4332,23 +4332,16 @@
 rer(new 
-Default
 ListCell
@@ -4352,94 +4352,122 @@
 erer
-() %7B%0A      public Component getListCellRendererComponent(JList list, final Object valu
+Wrapper%3CModule%3E(myModulesBox.getRenderer()) %7B%0A      @Override%0A      public void customize(JList list, Module modul
 e, i
@@ -4484,19 +4484,17 @@
 boolean 
-isS
+s
 elected,
@@ -4502,21 +4502,17 @@
 boolean 
-cellH
+h
 asFocus)
@@ -4518,144 +4518,8 @@
 ) %7B%0A
-        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);%0A        final Module module = (Module) value;%0A
     
@@ -4639,24 +4639,24 @@
 getName());%0A
+
         %7D%0A  
@@ -4657,29 +4657,8 @@
   %7D%0A
-        return this;%0A
     

Fix Groovy run configuration module combo for GTK+ L&F