/*
 * 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.idea.devkit.module;

import com.intellij.execution.RunManager;
import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.ide.util.projectWizard.JavaModuleBuilder;
import com.intellij.ide.util.projectWizard.ModuleWizardStep;
import com.intellij.ide.util.projectWizard.SettingsStep;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.module.*;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.SdkTypeId;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.startup.StartupManager;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.devkit.DevKitBundle;
import org.jetbrains.idea.devkit.build.PluginBuildConfiguration;
import org.jetbrains.idea.devkit.projectRoots.IdeaJdk;
import org.jetbrains.idea.devkit.run.PluginConfigurationType;

public class PluginModuleBuilder extends JavaModuleBuilder{
  @NonNls private static final String META_INF = "META-INF";
  @NonNls private static final String PLUGIN_XML = "plugin.xml";


  public ModuleType getModuleType() {
    return PluginModuleType.getInstance();
  }

  public void setupRootModel(final ModifiableRootModel rootModel) throws ConfigurationException {
    super.setupRootModel(rootModel);
    final String defaultPluginXMLLocation = getModuleFileDirectory() + '/' + META_INF + '/' + PLUGIN_XML;
    final Module module = rootModel.getModule();
    final Project project = module.getProject();
    StartupManager.getInstance(project).runWhenProjectIsInitialized(new Runnable() {
      public void run() {
        final PluginBuildConfiguration buildConfiguration = PluginBuildConfiguration.getInstance(module);
        if (buildConfiguration != null) {
          buildConfiguration.setPluginXmlPathAndCreateDescriptorIfDoesntExist(defaultPluginXMLLocation);
        }

        VirtualFile file = LocalFileSystem.getInstance().findFileByPath(defaultPluginXMLLocation);
        if (file != null) {
          FileEditorManager.getInstance(project).openFile(file, true);
        }
      }
    });
  }

  @Nullable
  @Override
  public Module commitModule(@NotNull Project project, @Nullable ModifiableModuleModel model) {
    Module module = super.commitModule(project, model);
    if (module != null) {
      RunManager runManager = RunManager.getInstance(project);
      RunnerAndConfigurationSettings configuration =
        runManager.createRunConfiguration(DevKitBundle.message("run.configuration.title"), new PluginConfigurationType().getConfigurationFactories()[0]);
      runManager.addConfiguration(configuration, false);
      runManager.setSelectedConfiguration(configuration);
    }
    return module;
  }

  @Override
  public boolean isSuitableSdkType(SdkTypeId sdk) {
    return sdk == IdeaJdk.getInstance();
  }

  @Override
  public String getParentGroup() {
    return JavaModuleType.JAVA_GROUP;
  }

  @Override
  public int getWeight() {
    return 0;
  }

  public ModuleWizardStep modifyProjectTypeStep(@NotNull SettingsStep settingsStep) {
    return StdModuleTypes.JAVA.modifyProjectTypeStep(settingsStep, this);
  }
}

@@ -741,16 +741,80 @@
 ttings;%0A
+import com.intellij.ide.projectView.actions.MarkRootActionBase;%0A
 import c
@@ -863,24 +863,24 @@
 uleBuilder;%0A
-
 import com.i
@@ -1238,16 +1238,64 @@
 TypeId;%0A
+import com.intellij.openapi.roots.ContentEntry;%0A
 import c
@@ -1442,16 +1442,61 @@
 System;%0A
+import com.intellij.openapi.vfs.VfsUtilCore;%0A
 import c
@@ -1536,49 +1536,8 @@
 le;%0A
-import org.jetbrains.annotations.NonNls;%0A
 impo
@@ -1845,16 +1845,74 @@
 ionType;
+%0Aimport org.jetbrains.jps.model.java.JavaResourceRootType;
 %0A%0Apublic
@@ -1969,134 +1969,8 @@
 er%7B%0A
-  @NonNls private static final String META_INF = %22META-INF%22;%0A  @NonNls private static final String PLUGIN_XML = %22plugin.xml%22;%0A
 %0A%0A  
@@ -2196,108 +2196,620 @@
     
-final String defaultPluginXMLLocation = getModuleFileDirectory() + '/'
+String contentEntryPath = getContentEntryPath();%0A    if (contentEntryPath == null) return;%0A%0A    String resourceRootPath = contentEntryPath + %22/resources%22;%0A    VirtualFile contentRoot = LocalFileSystem.getInstance().findFileByPath(contentEntryPath);%0A    if (contentRoot == null) return;%0A%0A    ContentEntry contentEntry = MarkRootActionBase.findContentEntry(rootModel, contentRoot);%0A    if (contentEntry != null) %7B%0A      contentEntry.addSourceFolder(VfsUtilCore.pathToUrl(resourceRootPath), JavaResourceRootType.RESOURCE);%0A    %7D%0A%0A    final String defaultPluginXMLLocation = resourceRootPath
  + 
+%22/
 META
-_INF + '/' + PLUGIN_XML
+-INF/plugin.xml%22
 ;%0A  

put META-INF/plugin.xml for new plugin module under 'resources' directory and mark it as resources root (this will ensure that all files from this directory will be included to the plugin jar/zip, currently we automatically include config-file specified in 'depends' tags but won't include files references via xi:include for example)