/*
 * 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.projectView.impl;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiManager;
import org.jetbrains.annotations.NonNls;

/**
 * @author cdr
 */
public class DirectoryUrl extends AbstractUrl {
  @NonNls private static final String ELEMENT_TYPE = "directory";

  public DirectoryUrl(String url, String moduleName) {
    super(url, moduleName,ELEMENT_TYPE);
  }
  public static DirectoryUrl create(PsiDirectory directory) {
    Project project = directory.getProject();
    final VirtualFile virtualFile = directory.getVirtualFile();
    final Module module = ModuleUtil.findModuleForFile(virtualFile, project);
    return new DirectoryUrl(virtualFile.getUrl(), module != null ? module.getName() : null);
  }

  public Object[] createPath(Project project) {
    if (moduleName != null) {

      final Module module = ModuleManager.getInstance(project).findModuleByName(moduleName);
      if (module == null) return null;
    }
    final VirtualFileManager virtualFileManager = VirtualFileManager.getInstance();
    VirtualFile file = virtualFileManager.findFileByUrl(url);
    if (file == null) return null;
    final PsiDirectory directory = PsiManager.getInstance(project).findDirectory(file);
    if (directory == null) return null;
    return new Object[]{directory};
  }

  protected AbstractUrl createUrl(String moduleName, String url) {
      return new DirectoryUrl(url, moduleName);
  }

  public AbstractUrl createUrlByElement(Object element) {
    if (element instanceof PsiDirectory) {
      return create((PsiDirectory)element);
    }
    return null;
  }
}

@@ -639,16 +639,76 @@
 .impl;%0A%0A
+import com.intellij.openapi.application.ApplicationManager;%0A
 import c
@@ -884,16 +884,61 @@
 roject;%0A
+import com.intellij.openapi.util.Computable;%0A
 import c
@@ -1140,16 +1140,59 @@
 .NonNls;
+%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0A/**%0A *
@@ -1800,16 +1800,22 @@
 atePath(
+final 
 Project 
@@ -1855,17 +1855,16 @@
 null) %7B%0A
-%0A
       fi
@@ -1878,24 +1878,188 @@
 ule module =
+ ApplicationManager.getApplication().runReadAction(new Computable%3CModule%3E() %7B%0A        @Nullable%0A        @Override%0A        public Module compute() %7B%0A          return
  ModuleManag
@@ -2108,24 +2108,44 @@
 oduleName);%0A
+        %7D%0A      %7D);%0A
       if (mo
@@ -2265,16 +2265,22 @@
 ();%0A    
+final 
 VirtualF
@@ -2398,16 +2398,184 @@
 ectory =
+ ApplicationManager.getApplication().runReadAction(new Computable%3CPsiDirectory%3E() %7B%0A      @Nullable%0A      @Override%0A      public PsiDirectory compute() %7B%0A        return
  PsiMana
@@ -2616,24 +2616,40 @@
 tory(file);%0A
+      %7D%0A    %7D);%0A
     if (dire

update favorites view: read action added