package com.chrisfolger.needsmoredojo.intellij.configurable;

import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.DataKeys;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;

import javax.swing.*;

public class DojoSettingsConfigurable implements Configurable {
    private JComponent myComponent;
    private JButton autoDetect;
    private JPanel myPanel;
    private TextFieldWithBrowseButton projectSourcesText;
    private TextFieldWithBrowseButton dojoSourcesText;

    public String getDisplayName() {
        return "Needs More Dojo";
    }

    public boolean isModified() {
        return true;
    }

    public JComponent createComponent() {
        myComponent = (JComponent) myPanel;
        FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();

        projectSourcesText.addBrowseFolderListener("Project Sources", "Select the root of your project sources to support certain features of Needs More Dojo", null, descriptor);
        dojoSourcesText.addBrowseFolderListener("Dojo Sources", "Select the root of the dojo library sources to support certain features of Needs More Dojo", null, descriptor);

        // don't know how else to get the current project???
        DataContext context = DataManager.getInstance().getDataContext();
        Project project = DataKeys.PROJECT.getData(context);

        return myComponent;
    }

    public Icon getIcon() {
        return null;

    }

    public void apply() {
    }

    public void disposeUIResources() {

    }

    public String getHelpTopic() {
        return "";
    }

    public void reset() {

    }


}

@@ -512,56 +512,246 @@
 .ui.
-TextFieldWithBrowseButton;%0A%0Aimport javax.swing.*
+ComponentWithBrowseButton;%0Aimport com.intellij.openapi.ui.TextComponentAccessor;%0Aimport com.intellij.openapi.ui.TextFieldWithBrowseButton;%0Aimport org.jetbrains.annotations.Nullable;%0A%0Aimport javax.swing.*;%0Aimport java.awt.event.ActionEvent
 ;%0A%0Ap
@@ -1021,16 +1021,45 @@
 cesText;
+%0A    private Project project;
 %0A%0A    pu
@@ -1184,32 +1184,1202 @@
 rn true;%0A    %7D%0A%0A
+    private class ProjectSourcesChosen extends ComponentWithBrowseButton.BrowseFolderActionListener%3CJTextField%3E%0A    %7B%0A        public ProjectSourcesChosen(@Nullable String title, @Nullable String description, FileChooserDescriptor fileChooserDescriptor) %7B%0A            super(title, description, projectSourcesText, null, fileChooserDescriptor, TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT);%0A        %7D%0A%0A        @Override%0A        public void actionPerformed(ActionEvent e) %7B%0A            super.actionPerformed(e);%0A%0A            String projectSources = projectSourcesText.getText();%0A        %7D%0A    %7D%0A%0A    private class DojoSourcesChosen extends ComponentWithBrowseButton.BrowseFolderActionListener%3CJTextField%3E%0A    %7B%0A        public DojoSourcesChosen(@Nullable String title, @Nullable String description, FileChooserDescriptor fileChooserDescriptor) %7B%0A            super(title, description, dojoSourcesText, null, fileChooserDescriptor, TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT);%0A        %7D%0A%0A        @Override%0A        public void actionPerformed(ActionEvent e) %7B%0A            super.actionPerformed(e);%0A%0A            String dojoSources = dojoSourcesText.getText();%0A%0A        %7D%0A    %7D%0A%0A
     public JComp
@@ -2604,16 +2604,47 @@
 istener(
+null, new ProjectSourcesChosen(
 %22Project
@@ -2686,16 +2686,18 @@
  project
+'s
  sources
@@ -2737,38 +2737,32 @@
 eeds More Dojo%22,
- null,
  descriptor);%0A  
@@ -2757,16 +2757,17 @@
 criptor)
+)
 ;%0A      
@@ -2808,16 +2808,44 @@
 istener(
+null, new DojoSourcesChosen(
 %22Dojo So
@@ -2945,22 +2945,16 @@
 e Dojo%22,
- null,
  descrip
@@ -2957,16 +2957,17 @@
 criptor)
+)
 ;%0A%0A     
@@ -3156,16 +3156,48 @@
 ontext);
+%0A        this.project = project;
 %0A%0A      

add custom listeners to get chosen directories