package com.intellij.ide.util.newProjectWizard.impl;

import com.intellij.ide.util.newProjectWizard.AddSupportForFrameworksPanel;
import com.intellij.ide.util.newProjectWizard.FrameworkSupportProvider;
import com.intellij.ide.util.newProjectWizard.FrameworkSupportModel;
import com.intellij.ide.util.newProjectWizard.FrameworkSupportModelListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.UserDataHolderBase;
import com.intellij.util.EventDispatcher;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

/**
 * @author nik
 */
public class FrameworkSupportModelImpl extends UserDataHolderBase implements FrameworkSupportModel {
  private final Project myProject;
  private EventDispatcher<FrameworkSupportModelListener> myDispatcher = EventDispatcher.create(FrameworkSupportModelListener.class);
  private Map<String, AddSupportForFrameworksPanel.FrameworkSupportSettings> mySettingsMap = new HashMap<String, AddSupportForFrameworksPanel.FrameworkSupportSettings>();

  public FrameworkSupportModelImpl(final @Nullable Project project) {
    myProject = project;
  }

  public void registerComponent(@NotNull final FrameworkSupportProvider provider, @NotNull final AddSupportForFrameworksPanel.FrameworkSupportSettings settings) {
    mySettingsMap.put(provider.getId(), settings);
    final JCheckBox checkBox = settings.getCheckBox();
    checkBox.addActionListener(new ActionListener() {
      public void actionPerformed(final ActionEvent e) {
        if (checkBox.isSelected()) {
          myDispatcher.getMulticaster().frameworkSelected(provider);
        }
        else {
          myDispatcher.getMulticaster().frameworkUnselected(provider);
        }
      }
    });
  }

  public Project getProject() {
    return myProject;
  }

  public boolean isFrameworkSelected(@NotNull @NonNls final String providerId) {
    final AddSupportForFrameworksPanel.FrameworkSupportSettings settings = mySettingsMap.get(providerId);
    if (settings == null) {
      throw new IllegalArgumentException("provider '" + providerId + "' not found");
    }
    return settings.getCheckBox().isSelected();
  }

  public void addFrameworkListener(@NotNull final FrameworkSupportModelListener listener) {
    myDispatcher.addListener(listener);
  }

  public void removeFrameworkListener(@NotNull final FrameworkSupportModelListener listener) {
    myDispatcher.removeListener(listener);
  }

  public void setFrameworkComponentEnabled(@NotNull @NonNls final String providerId, final boolean enable) {
    final AddSupportForFrameworksPanel.FrameworkSupportSettings settings = mySettingsMap.get(providerId);
    if (settings == null) {
      throw new IllegalArgumentException("provider '" + providerId + " not found");
    }
    if (enable != settings.getCheckBox().isEnabled()) {
      settings.setEnabled(enable);
    }
  }
}

@@ -2171,36 +2171,39 @@
 roviderId);%0A    
-if (
+return 
 settings == null
@@ -2199,120 +2199,18 @@
 ngs 
-=
+!
 = null
-) %7B%0A      throw new IllegalArgumentException(%22provider '%22 + providerId + %22' not found%22);%0A    %7D%0A    return
+ &&
  set

make things easier [nik]