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

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.fileTypes.FileTypes;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.PsiManagerImpl;
import com.intellij.util.IncorrectOperationException;

import java.io.IOException;

public class PsiFileImplUtil {
  private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.file.PsiFileImplUtil");

  public static PsiFile setName(final PsiFile file, String newName) throws IncorrectOperationException {
    VirtualFile vFile = file.getViewProvider().getVirtualFile();
    PsiManagerImpl manager = (PsiManagerImpl)file.getManager();

    try{
      final FileType newFileType = FileTypeManager.getInstance().getFileTypeByFileName(newName);
      if (FileTypes.UNKNOWN.equals(newFileType) || newFileType.isBinary()) {
        // before the file becomes unknown or a binary (thus, not openable in the editor), save it to prevent data loss
        final FileDocumentManager fdm = FileDocumentManager.getInstance();
        final Document doc = fdm.getCachedDocument(vFile);
        if (doc != null) {
          fdm.saveDocumentAsIs(doc);
        }
      }

      vFile.rename(manager, newName);
    }
    catch(IOException e){
      throw new IncorrectOperationException(e.toString(),e);
    }

    return file.getViewProvider().isPhysical() ? manager.findFile(vFile) : file;
  }

  public static void checkSetName(PsiFile file, String name) throws IncorrectOperationException {
    VirtualFile vFile = file.getVirtualFile();
    VirtualFile parentFile = vFile.getParent();
    if (parentFile == null) return;
    VirtualFile child = parentFile.findChild(name);
    if (child != null && !child.equals(vFile)){
      throw new IncorrectOperationException("File " + child.getPresentableUrl() + " already exists.");
    }
  }

  public static void doDelete(final PsiFile file) throws IncorrectOperationException {
    final PsiManagerImpl manager = (PsiManagerImpl)file.getManager();

    final VirtualFile vFile = file.getVirtualFile();
    try{
      vFile.delete(manager);
    }
    catch(IOException e){
      throw new IncorrectOperationException(e.toString(),e);
    }
  }
}

@@ -636,55 +636,8 @@
 e;%0A%0A
-import com.intellij.openapi.diagnostic.Logger;%0A
 impo
@@ -677,16 +677,16 @@
 cument;%0A
+
 import c
@@ -831,23 +831,24 @@
 FileType
-Manager
+Registry
 ;%0Aimport
@@ -879,24 +879,31 @@
 leTypes.
+Unknown
 FileType
 s;%0Aimpor
@@ -894,17 +894,16 @@
 FileType
-s
 ;%0Aimport
@@ -1148,82 +1148,8 @@
 ate 
-static final Logger LOG = Logger.getInstance(%22#com.intellij.psi.impl.file.
 PsiF
@@ -1159,19 +1159,24 @@
 ImplUtil
-%22);
+() %7B%0A  %7D
 %0A%0A  publ
@@ -1456,23 +1456,24 @@
 FileType
-Manager
+Registry
 .getInst
@@ -1521,24 +1521,31 @@
     if (
+Unknown
 FileType
 s.UNKNOW
@@ -1540,17 +1540,17 @@
 Type
-s.UNKNOWN
+.INSTANCE
 .equ

use FileTypeRegistry instead of FileTypeManager in PsiFileImplUtil