/*
 * 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.codeInsight.daemon.impl.quickfix;

import com.intellij.codeInsight.CodeInsightUtilBase;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

/**
 * @author ven
 */
public class AddNewArrayExpressionFix implements IntentionAction {
  private final PsiArrayInitializerExpression myInitializer;

  public AddNewArrayExpressionFix(PsiArrayInitializerExpression initializer) {
    myInitializer = initializer;
  }

  @NotNull
  public String getText() {
    PsiExpression expr = myInitializer.getInitializers()[0];
    PsiType type = expr.getType();
    return QuickFixBundle.message("add.new.array.text", type.getPresentableText());
  }

  @NotNull
  public String getFamilyName() {
    return QuickFixBundle.message("add.new.array.family");
  }

  public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    if (!myInitializer.isValid() || !myInitializer.getManager().isInProject(myInitializer)) return false;
    PsiExpression[] initializers = myInitializer.getInitializers();
    return initializers.length > 0 && initializers[0].getType() != null;
  }

  public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    if (!CodeInsightUtilBase.preparePsiElementsForWrite(myInitializer, file)) return;
    PsiManager manager = file.getManager();
    PsiExpression expr = myInitializer.getInitializers()[0];
    PsiType type = expr.getType();
    PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
    @NonNls String text = "new " + type.getPresentableText() + "[]{}";
    PsiNewExpression newExpr = (PsiNewExpression) factory.createExpressionFromText(text, null);
    newExpr.getArrayInitializer().replace(myInitializer);
    newExpr = (PsiNewExpression) manager.getCodeStyleManager().reformat(newExpr);
    myInitializer.replace(newExpr);
  }

  public boolean startInWriteAction() {
    return true;
  }
}

@@ -1382,69 +1382,8 @@
 ) %7B%0A
-    PsiExpression expr = myInitializer.getInitializers()%5B0%5D;%0A
     
@@ -1389,37 +1389,32 @@
  PsiType type = 
-expr.
 getType();%0A    r
@@ -1806,126 +1806,15 @@
     
-PsiExpression%5B%5D initializers = myInitializer.getInitializers();%0A    return initializers.length %3E 0 && initializers%5B0%5D.
+return 
 getT
@@ -2080,69 +2080,8 @@
 ();%0A
-    PsiExpression expr = myInitializer.getInitializers()%5B0%5D;%0A
     
@@ -2099,13 +2099,8 @@
 e = 
-expr.
 getT
@@ -2510,24 +2510,24 @@
 t(newExpr);%0A
-
     myInitia
@@ -2547,32 +2547,498 @@
 (newExpr);%0A  %7D%0A%0A
+  private PsiType getType() %7B%0A    final PsiExpression%5B%5D initializers = myInitializer.getInitializers();%0A    final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) myInitializer.getParent();%0A    final PsiType type = assignmentExpression.getType();%0A    if (!(type instanceof PsiArrayType)) %7B%0A      if (initializers.length %3C= 0) return null;%0A      return initializers%5B0%5D.getType();%0A    %7D%0A    return ((PsiArrayType)type).getComponentType();%0A  %7D%0A%0A
   public boolean

create array expression of correct type (and for empty array initializers)