update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / daemon / impl / quickfix / AddNewArrayExpressionFix.java
blobeaafd069eef49b15d4492bca96b952be5d5e27e9
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInsight.daemon.impl.quickfix;
18 import com.intellij.codeInsight.CodeInsightUtilBase;
19 import com.intellij.codeInsight.daemon.QuickFixBundle;
20 import com.intellij.codeInsight.intention.IntentionAction;
21 import com.intellij.openapi.editor.Editor;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.psi.*;
24 import com.intellij.util.IncorrectOperationException;
25 import org.jetbrains.annotations.NonNls;
26 import org.jetbrains.annotations.NotNull;
28 /**
29 * @author ven
31 public class AddNewArrayExpressionFix implements IntentionAction {
32 private final PsiArrayInitializerExpression myInitializer;
34 public AddNewArrayExpressionFix(PsiArrayInitializerExpression initializer) {
35 myInitializer = initializer;
38 @NotNull
39 public String getText() {
40 PsiExpression expr = myInitializer.getInitializers()[0];
41 PsiType type = expr.getType();
42 return QuickFixBundle.message("add.new.array.text", type.getPresentableText());
45 @NotNull
46 public String getFamilyName() {
47 return QuickFixBundle.message("add.new.array.family");
50 public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
51 if (!myInitializer.isValid() || !myInitializer.getManager().isInProject(myInitializer)) return false;
52 PsiExpression[] initializers = myInitializer.getInitializers();
53 return initializers.length > 0 && initializers[0].getType() != null;
56 public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
57 if (!CodeInsightUtilBase.preparePsiElementsForWrite(myInitializer, file)) return;
58 PsiManager manager = file.getManager();
59 PsiExpression expr = myInitializer.getInitializers()[0];
60 PsiType type = expr.getType();
61 PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
62 @NonNls String text = "new " + type.getPresentableText() + "[]{}";
63 PsiNewExpression newExpr = (PsiNewExpression) factory.createExpressionFromText(text, null);
64 newExpr.getArrayInitializer().replace(myInitializer);
65 newExpr = (PsiNewExpression) manager.getCodeStyleManager().reformat(newExpr);
66 myInitializer.replace(newExpr);
69 public boolean startInWriteAction() {
70 return true;