/*
 * 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 com.intellij.ide.impl.dataRules;

import com.intellij.ide.DataManager;
import com.intellij.ide.impl.DataManagerImpl;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataProvider;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;

/**
 * @author Eugene Zhuravlev
 */
public class ModuleRule implements GetDataRule {
  @Override
  public Object getData(@NotNull DataProvider dataProvider) {
    Object moduleContext = LangDataKeys.MODULE_CONTEXT.getData(dataProvider);
    if (moduleContext != null) {
      return moduleContext;
    }
    Project project = CommonDataKeys.PROJECT.getData(dataProvider);
    if (project == null) {
      PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(dataProvider);
      if (element == null) {
        PsiElement[] psiElements = LangDataKeys.PSI_ELEMENT_ARRAY.getData(dataProvider);
        if (psiElements != null && psiElements.length > 0) {
          element = psiElements[0];
        }
      }
      if (element == null || !element.isValid()) return null;
      project = element.getProject();
    }

    VirtualFile[] files = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataProvider);
    if (files == null) {
      GetDataRule dataRule = ((DataManagerImpl)DataManager.getInstance()).getDataRule(CommonDataKeys.VIRTUAL_FILE_ARRAY.getName());
      if (dataRule != null) {
        files = (VirtualFile[])dataRule.getData(dataProvider);
      }
    }

    if (files == null) {
      return null;
    }

    Module singleModule = null;
    for (VirtualFile file : files) {
      Module module = ModuleUtilCore.findModuleForFile(file, project);
      if (module == null) {
        return null;
      }
      if (singleModule == null) {
        singleModule = module;
      }
      else if (module != singleModule) {
        return null;
      }
    }

    return singleModule;
  }
}

@@ -879,32 +879,75 @@
 m.LangDataKeys;%0A
+import com.intellij.openapi.editor.Editor;%0A
 import com.intel
@@ -969,24 +969,24 @@
 ule.Module;%0A
-
 import com.i
@@ -1150,16 +1150,49 @@
 lement;%0A
+import com.intellij.psi.PsiFile;%0A
 import o
@@ -1224,16 +1224,131 @@
 NotNull;
+%0Aimport org.jetbrains.annotations.Nullable;%0A%0Aimport static com.intellij.openapi.actionSystem.CommonDataKeys.EDITOR;
 %0A%0A/**%0A *
@@ -1671,142 +1671,22 @@
 t = 
-CommonDataKeys.PROJECT.getData(dataProvider);%0A    if (project == null) %7B%0A      PsiElement element = CommonDataKeys.PSI_ELEMENT.getData
+extractProject
 (dat
@@ -1705,342 +1705,41 @@
     
-  
 if (
-element == null) %7B%0A        PsiElement%5B%5D psiElements = LangDataKeys.PSI_ELEMENT_ARRAY.getData(dataProvider);%0A        if (psiElements != null && psiElements.length %3E 0) %7B%0A          element = psiElements%5B0%5D;%0A        %7D%0A      %7D%0A      if (element == null %7C%7C !element.isValid()) return null;%0A      project = element.getProject();%0A    %7D
+project == null) return null;
 %0A%0A  
@@ -2478,24 +2478,24 @@
    %7D%0A    %7D%0A%0A
-
     return s
@@ -2510,11 +2510,961 @@
 ule;%0A  %7D
+%0A%0A  @Nullable%0A  private static Project extractProject(@NotNull DataProvider dataProvider) %7B%0A    Project project = CommonDataKeys.PROJECT.getData(dataProvider);%0A    if (project != null) return project;%0A    %0A    Editor editor = EDITOR.getData(dataProvider);%0A    project = editor != null ? editor.getProject() : null;%0A    if (project != null) return project;%0A    PsiFile file = CommonDataKeys.PSI_FILE.getData(dataProvider);%0A    if (file != null && file.isValid()) return file.getProject();%0A%0A    //todo remove this part, it can be too slow for such simple rule%0A    PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(dataProvider);%0A    if (element == null) %7B%0A      PsiElement%5B%5D psiElements = LangDataKeys.PSI_ELEMENT_ARRAY.getData(dataProvider);%0A      if (psiElements != null && psiElements.length %3E 0) %7B%0A        element = psiElements%5B0%5D;%0A      %7D%0A    %7D%0A    if (element == null %7C%7C !element.isValid()) return null;%0A    return element.getProject();%0A  %7D
 %0A%7D%0A

fix performance problem if project isn't specifed EA-260567 - F: WSDLReferenceProvider.collectTags