/*
 * 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.
 */

/*
 * User: anna
 * Date: 23-Jan-2008
 */
package com.intellij.ide.projectView.impl;

import com.intellij.ide.projectView.ViewSettings;
import com.intellij.ide.projectView.impl.nodes.PackageNodeUtil;
import com.intellij.ide.projectView.impl.nodes.PackageElement;
import com.intellij.ide.projectView.impl.nodes.BaseProjectViewDirectoryHelper;
import com.intellij.ide.util.treeView.TreeViewUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.impl.DirectoryIndex;
import com.intellij.psi.PsiDirectory;
import org.consulo.psi.PsiPackage;
import org.consulo.psi.PsiPackageManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;

public class DefaultProjectViewDirectoryHelper extends BaseProjectViewDirectoryHelper {
  public DefaultProjectViewDirectoryHelper(Project project, DirectoryIndex index) {
    super(project, index);
  }

  @Override
  public String getLocationString(@NotNull final PsiDirectory directory) {
    PsiPackage aPackage = PsiPackageManager.getInstance(getProject()).findAnyPackage(directory);
    if (ProjectRootsUtil.isSourceRoot(directory) && aPackage != null) {   //package prefix
      return aPackage.getQualifiedName();
    }
    return super.getLocationString(directory);
  }

  @Override
  public boolean isShowFQName(final ViewSettings settings, final Object parentValue, final PsiDirectory value) {
    PsiPackage aPackage;
        return value != null
               && !(parentValue instanceof Project)
               && settings.isFlattenPackages()
               && (aPackage = PsiPackageManager.getInstance(getProject()).findAnyPackage(value)) != null
               && !aPackage.getQualifiedName().isEmpty();

  }

  @Nullable
  @Override
  public String getNodeName(final ViewSettings settings, final Object parentValue, final PsiDirectory directory) {
    PsiPackage aPackage = PsiPackageManager.getInstance(getProject()).findAnyPackage(directory);

    PsiPackage parentPackage;
    if (!ProjectRootsUtil.isSourceRoot(directory) && aPackage != null && !aPackage.getQualifiedName().isEmpty() &&
                              parentValue instanceof PsiDirectory) {

      parentPackage = PsiPackageManager.getInstance(getProject()).findAnyPackage(((PsiDirectory)parentValue));
    }
    else if (ProjectRootsUtil.isSourceRoot(directory) && aPackage != null) {   //package prefix
      aPackage = null;
      parentPackage = null;
    }
    else {
      parentPackage = null;
    }

    return TreeViewUtil.getNodeName(settings, aPackage, parentPackage, directory.getName(), isShowFQName(settings, parentValue, directory));
  }

  @Override
  public boolean skipDirectory(final PsiDirectory directory) {
    return PsiPackageManager.getInstance(getProject()).findAnyPackage(directory) == null;
  }

  @Override
  public boolean isEmptyMiddleDirectory(final PsiDirectory directory, final boolean strictlyEmpty) {
    return PsiPackageManager.getInstance(getProject()).findAnyPackage(directory) != null && PackageNodeUtil
      .isEmptyMiddlePackage(directory, null, strictlyEmpty);
  }

  @Override
  public boolean supportsFlattenPackages() {
    return true;
  }

  @Override
  public boolean supportsHideEmptyMiddlePackages() {
    return true;
  }

  @Override
  public boolean canRepresent(final Object element, final PsiDirectory directory) {
    if (super.canRepresent(element, directory)) return true;
    if (element instanceof PackageElement) {
      final PackageElement packageElement = (PackageElement)element;
      return Arrays.asList(packageElement.getPackage().getDirectories()).contains(directory);
    }
    return false;
  }
}

@@ -778,31 +778,46 @@
 l.nodes.
-PackageNodeUtil
+BaseProjectViewDirectoryHelper
 ;%0Aimport
@@ -924,38 +924,23 @@
 des.
-BaseProjectViewDirectoryHelper
+PackageNodeUtil
 ;%0Aim
@@ -1034,16 +1034,16 @@
 roject;%0A
-
 import c
@@ -1089,16 +1089,165 @@
 yIndex;%0A
+import com.intellij.openapi.vfs.ArchiveFileSystem;%0Aimport com.intellij.openapi.vfs.VirtualFile;%0Aimport com.intellij.openapi.vfs.util.ArchiveVfsUtil;%0A
 import c
@@ -2728,16 +2728,400 @@
 tory);%0A%0A
+    String name = directory.getName();%0A    VirtualFile dirFile = directory.getVirtualFile();%0A    if(dirFile.getFileSystem() instanceof ArchiveFileSystem && dirFile.getParent() == null) %7B%0A      VirtualFile virtualFileForArchive = ArchiveVfsUtil.getVirtualFileForArchive(dirFile);%0A      if(virtualFileForArchive != null) %7B%0A        name = virtualFileForArchive.getName();%0A      %7D%0A    %7D%0A%0A
     PsiP
@@ -3714,35 +3714,20 @@
 ackage, 
-directory.getN
+n
 ame
-()
 , isShow

fix bug with .net assemblies presentation of libraries(it show url with :/)