/*
 * Copyright 2000-2011 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.cvsSupport2.cvsoperations.cvsCheckOut;

import com.intellij.cvsSupport2.CvsUtil;
import com.intellij.cvsSupport2.config.CvsConfiguration;
import com.intellij.cvsSupport2.connections.CvsRootProvider;
import com.intellij.cvsSupport2.cvsoperations.common.CvsExecutionEnvironment;
import com.intellij.cvsSupport2.cvsoperations.common.CvsOperationOnFiles;
import com.intellij.cvsSupport2.cvsoperations.dateOrRevision.RevisionOrDate;
import com.intellij.cvsSupport2.cvsoperations.dateOrRevision.RevisionOrDateImpl;
import com.intellij.cvsSupport2.util.CvsVfsUtil;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.vfs.VirtualFile;
import org.netbeans.lib.cvsclient.admin.Entry;
import org.netbeans.lib.cvsclient.command.Command;
import org.netbeans.lib.cvsclient.command.GlobalOptions;
import org.netbeans.lib.cvsclient.command.checkout.CheckoutCommand;

import java.io.File;

/**
 * author: lesya
 */

public class CheckoutFileOperation extends CvsOperationOnFiles {
  private final File myFile;
  private final boolean myIsDirectory;
  private final String myModuleName;
  private final RevisionOrDate myRevisionOrDate;
  private final boolean myMakeNewFilesReadOnly;

  public CheckoutFileOperation(VirtualFile parent,
                               CvsConfiguration config,
                               String fileName,
                               Entry entry,
                               boolean makeNewFilesReadOnly,
                               final boolean isDirectory) {
    this(parent, RevisionOrDateImpl.createOn(parent, entry, config.CHECKOUT_DATE_OR_REVISION_SETTINGS), fileName, makeNewFilesReadOnly,
         isDirectory);
  }

  public CheckoutFileOperation(final VirtualFile parent,
                               RevisionOrDate revisionOrDate,
                               final String fileName,
                               boolean makeNewFilesReadOnly) {
    this(parent, revisionOrDate, fileName, makeNewFilesReadOnly, null);
  }

  private CheckoutFileOperation(final VirtualFile parent,
                                RevisionOrDate revisionOrDate,
                                final String fileName,
                                boolean makeNewFilesReadOnly,
                                Boolean isDirectory/*null means detect*/) {
    super(new CheckoutAdminReader());
    myMakeNewFilesReadOnly = makeNewFilesReadOnly;
    myRevisionOrDate = revisionOrDate;
    myFile = CvsVfsUtil.getFileFor(parent, fileName);
    myIsDirectory = isDirectory == null ? myFile.isDirectory() : isDirectory;
    myModuleName = getModuleName(parent, fileName);
    addFile(myFile.getAbsolutePath());
  }

  private static String getModuleName(final VirtualFile parent, final String fileName) {
    return new WriteAction<String>() {
      protected void run(Result<String> result) throws Throwable {
        final String parentModule = CvsUtil.getModuleName(parent);
        VirtualFile file = parent.findChild(fileName);
        if (parentModule == null && file != null) {
          result.setResult(CvsUtil.getModuleName(file));
        }
        else {
          result.setResult(parentModule + "/" + fileName);
        }
      }
    }.execute().getResultObject();
  }

  protected Command createCommand(CvsRootProvider root, CvsExecutionEnvironment cvsExecutionEnvironment) {
    CheckoutCommand result = new CheckoutCommand(null);
    result.setRecursive(true);
    result.addModule(myModuleName);
    myRevisionOrDate.setForCommand(result);
    if (!isRoot()) {
      result.setAlternativeCheckoutDirectory(myIsDirectory ? myFile.getName() : ".");
    }
    return result;
  }

  private boolean isRoot() {
    return new File(myModuleName).getParentFile() == null;
  }

  protected File getAdminRootFor(CvsRootProvider root) {
    return getRoot();
  }

  private File getRoot() {
    return myFile.getParentFile();
  }

  public void modifyOptions(GlobalOptions options) {
    super.modifyOptions(options);
    options.setCheckedOutFilesReadOnly(myMakeNewFilesReadOnly);
  }

  protected File getLocalRootFor(CvsRootProvider root) {
    File result = getRoot();
    LOG.assertTrue(result != null);
    return result;
  }

  protected String getOperationName() {
    return "checkout";
  }
}


@@ -17,17 +17,17 @@
 2000-201
-1
+3
  JetBrai
@@ -1177,109 +1177,8 @@
 il;%0A
-import com.intellij.openapi.application.Result;%0Aimport com.intellij.openapi.application.WriteAction;%0A
 impo
@@ -1475,15 +1475,15 @@
 %0A * 
+@
 author
-:
  les
@@ -1489,17 +1489,16 @@
 sya%0A */%0A
-%0A
 public c
@@ -1554,16 +1554,17 @@
 Files %7B%0A
+%0A
   privat
@@ -3318,118 +3318,8 @@
 ) %7B%0A
-    return new WriteAction%3CString%3E() %7B%0A      protected void run(Result%3CString%3E result) throws Throwable %7B%0A    
     
@@ -3381,19 +3381,21 @@
 t);%0A    
-   
+final
  Virtual
@@ -3430,28 +3430,24 @@
 (fileName);%0A
-    
     if (pare
@@ -3488,37 +3488,23 @@
 %7B%0A      
-  
-  result.setResult(
+return 
 CvsUtil.
@@ -3522,33 +3522,24 @@
 me(file)
-)
 ;%0A    
-  
-  %7D%0A    
+%7D%0A
     else
@@ -3551,29 +3551,15 @@
     
- 
-   result.setResult(
+return 
 pare
@@ -3587,64 +3587,16 @@
 Name
-)
 ;%0A    
-   
- 
 %7D%0A
-      %7D%0A    %7D.execute().getResultObject();%0A
   %7D%0A
@@ -3707,16 +3707,22 @@
 ) %7B%0A    
+final 
 Checkout
@@ -4470,20 +4470,26 @@
 root) %7B%0A
-
     
+final 
 File res

Remove write action (EA-50711 - assert: WriteAction.execute)