/*
 * Copyright 2000-2011 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.mvc;

import com.intellij.facet.impl.ui.libraries.LibraryCompositionSettings;
import com.intellij.facet.impl.ui.libraries.LibraryOptionsPanel;
import com.intellij.framework.library.FrameworkLibraryVersionFilter;
import com.intellij.ide.util.projectWizard.ModuleBuilder;
import com.intellij.ide.util.projectWizard.ModuleWizardStep;
import com.intellij.ide.util.projectWizard.WizardContext;
import com.intellij.ide.wizard.CommitStepException;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer;
import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainerFactory;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.config.GroovyLibraryDescription;

import javax.swing.*;
import java.util.ArrayList;

/**
 * @author nik
 */
public abstract class GroovySdkWizardStepBase extends ModuleWizardStep {
  private final String myBasePath;
  private LibraryOptionsPanel myPanel;
  private final LibrariesContainer myLibrariesContainer;
  private boolean myDownloaded;
  private LibraryCompositionSettings myLibraryCompositionSettings;
  @Nullable
  private final MvcFramework myFramework;

  public GroovySdkWizardStepBase(@Nullable final MvcFramework framework, WizardContext wizardContext, String basePath) {
    myBasePath = basePath;
    myLibrariesContainer = wizardContext.getModulesProvider() == null
                           ? LibrariesContainerFactory.createContainer(wizardContext.getProject())
                           : LibrariesContainerFactory.createContainer(wizardContext, wizardContext.getModulesProvider());
    myFramework = framework;
  }

  protected ModuleBuilder.ModuleConfigurationUpdater createModuleConfigurationUpdater() {
    return new ModuleBuilder.ModuleConfigurationUpdater() {
      @Override
      public void update(@NotNull Module module, @NotNull ModifiableRootModel rootModel) {
        if (myLibraryCompositionSettings != null) {
          myLibraryCompositionSettings.addLibraries(rootModel, new ArrayList<>(), myLibrariesContainer);
        }
        module.putUserData(MvcFramework.CREATE_APP_STRUCTURE, Boolean.TRUE);
      }
    };
  }

  @Override
  public void disposeUIResources() {
    if (myPanel != null) {
      Disposer.dispose(myPanel);
    }
  }

  @Override
  public JComponent getComponent() {
    return getPanel().getMainPanel();
  }

  @Override
  public String getHelpId() {
    return "reference.dialogs.new.project.fromScratch." + (myFramework == null ? "groovy" : myFramework.getFrameworkName().toLowerCase());
  }

  @Override
  public void _commit(boolean finishChosen) throws CommitStepException {
    if (finishChosen && !myDownloaded && myLibraryCompositionSettings != null) {
      if (myLibraryCompositionSettings.downloadFiles(getPanel().getMainPanel())) {
        myDownloaded = true;
      }
    }
  }

  @Override
  public void updateDataModel() {
    myLibraryCompositionSettings = getPanel().apply();
  }

  protected LibraryOptionsPanel getPanel() {
    if (myPanel == null) {
      final GroovyLibraryDescription libraryDescription = myFramework == null ? new GroovyLibraryDescription() : myFramework.createLibraryDescription();
      final String baseDirPath = myBasePath != null ? FileUtil.toSystemIndependentName(myBasePath) : "";
      myPanel = new LibraryOptionsPanel(libraryDescription, baseDirPath, FrameworkLibraryVersionFilter.ALL, myLibrariesContainer, false);
    }
    return myPanel;
  }
}

@@ -1072,16 +1072,56 @@
 eption;%0A
+import com.intellij.openapi.Disposable;%0A
 import c
@@ -1151,24 +1151,24 @@
 ule.Module;%0A
-
 import com.i
@@ -1214,61 +1214,8 @@
 el;%0A
-import com.intellij.openapi.roots.libraries.Library;%0A
 impo
@@ -2057,16 +2057,57 @@
 amework;
+%0A  private final Disposable myDisposable;
 %0A%0A  publ
@@ -2569,16 +2569,66 @@
 mework;%0A
+    myDisposable = wizardContext.getDisposable();%0A
   %7D%0A%0A  p
@@ -3149,128 +3149,8 @@
  %7D%0A%0A
-  @Override%0A  public void disposeUIResources() %7B%0A    if (myPanel != null) %7B%0A      Disposer.dispose(myPanel);%0A    %7D%0A  %7D%0A%0A
   @O
@@ -4156,16 +4156,16 @@
 ) : %22%22;%0A
-
       my
@@ -4294,16 +4294,64 @@
 false);%0A
+      Disposer.register(myDisposable, myPanel);%0A
     %7D%0A  

fix LibraryOptionsPanel not being properly disposed