package com.haskforce.settings;


import com.intellij.openapi.externalSystem.util.ExternalSystemUiUtil;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

/**
 * The "Haskell Tools" option in Preferences->Project Settings.
 */
public class HaskellToolsConfigurable implements SearchableConfigurable {
    public static final String HASKELL_TOOLS_ID = "Haskell Tools";

    private Project project;

    private JPanel settings;
    private TextFieldWithBrowseButton cabalPath;

    public HaskellToolsConfigurable(@NotNull Project inProject) {
        project = inProject;
    }

    @NotNull
    @Override
    public String getId() {
        return HASKELL_TOOLS_ID;
    }

    @Nullable
    @Override
    public Runnable enableSearch(String s) {
        return null;
    }

    @Nls
    @Override
    public String getDisplayName() {
        return HASKELL_TOOLS_ID;
    }

    @Nullable
    @Override
    public String getHelpTopic() {
        return null;
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        settings = new JPanel(new GridBagLayout());
        cabalPath = createExecutableOption("Cabal");
        return settings;
    }

    @Override
    public boolean isModified() {
        return false;
    }

    /**
     * Triggered when the user pushes the apply button.
     */
    @Override
    public void apply() throws ConfigurationException {

    }

    /**
     * Triggered when the user pushes the cancel button.
     */
    @Override
    public void reset() {

    }

    @Override
    public void disposeUIResources() {

    }

    /**
     * Creates a label and path selector and adds them to the configuration
     * window.
     *
     * @param tool Which tool to configure.
     * @return The TextFieldWithBrowseButton created.
     */
    private TextFieldWithBrowseButton createExecutableOption(String tool) {
        // Create UI elements.
        TextFieldWithBrowseButton tf = new TextFieldWithBrowseButton();
        tf.addBrowseFolderListener("Select " + tool + " path", "", null,
                FileChooserDescriptorFactory.createSingleLocalFileDescriptor());

        // Add elements to Panel.
        settings.add(new JLabel(tool + " executable path"));
        settings.add(tf, ExternalSystemUiUtil.getFillLineConstraints(0));

        return tf;
    }
}

@@ -27,16 +27,53 @@
 ings;%0A%0A%0A
+import com.haskforce.utils.ExecUtil;%0A
 import c
@@ -914,16 +914,49 @@
 balPath;
+%0A    private JLabel cabalVersion;
 %0A%0A    pu
@@ -1625,31 +1625,134 @@
     
-return settings;%0A    %7D%0A
+cabalVersion = createDisplayVersion(%22Cabal%22);%0A        return settings;%0A    %7D%0A%0A    /**%0A     * Enables the apply button.%0A     */
 %0A   
@@ -1815,12 +1815,11 @@
 urn 
-fals
+tru
 e;%0A 
@@ -1966,16 +1966,99 @@
 ption %7B%0A
+        cabalVersion.setText(getVersion(cabalPath.getText(), %22--numeric-version%22));
 %0A    %7D%0A%0A
@@ -2875,16 +2875,17 @@
 ble path
+:
 %22));%0A   
@@ -2980,11 +2980,755 @@
 f;%0A    %7D
+%0A%0A    /**%0A     * Creates two labels adds them to the configuration window.%0A     *%0A     * @param tool Which tool to display version for.%0A     * @return The label with dynamic content.%0A     */%0A    private JLabel createDisplayVersion(String tool) %7B%0A        JLabel tf = new JLabel(%22%22);%0A%0A        // Add elements to Panel.%0A        settings.add(new JLabel(tool + %22 version:%22));%0A        settings.add(tf, ExternalSystemUiUtil.getFillLineConstraints(0));%0A%0A        return tf;%0A    %7D%0A%0A    /**%0A     * Heuristically finds the version number. Current implementation is the%0A     * identity function since cabal plays nice.%0A     */%0A    private static String getVersion(String cmd, String versionflag) %7B%0A        return ExecUtil.run(cmd + ' ' + versionflag);%0A    %7D
 %0A%7D%0A

Add a version detection box in the Haskell tools dialogue.