Inspections - pass onTheFly into ProblemDescriptors & use it to create LAZY refs...
[fedora-idea.git] / plugins / java-i18n / src / com / intellij / codeInspection / i18n / I18nizeConcatenationQuickFix.java
blob506352fb84ef0916aa82a048dd04bf93a91b4304
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.codeInspection.i18n;
18 import com.intellij.codeInsight.CodeInsightBundle;
19 import com.intellij.codeInsight.intention.impl.ConcatenationToMessageFormatAction;
20 import com.intellij.lang.properties.psi.I18nizedTextGenerator;
21 import com.intellij.lang.properties.psi.PropertiesFile;
22 import com.intellij.openapi.diagnostic.Logger;
23 import com.intellij.openapi.editor.Editor;
24 import com.intellij.openapi.project.Project;
25 import com.intellij.psi.*;
26 import com.intellij.util.IncorrectOperationException;
27 import org.jetbrains.annotations.NonNls;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.Map;
36 public class I18nizeConcatenationQuickFix extends I18nizeQuickFix{
37 private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.i18n.I18nizeConcatenationQuickFix");
38 @NonNls private static final String PARAMETERS_OPTION_KEY = "PARAMETERS";
40 public void checkApplicability(final PsiFile psiFile, final Editor editor) throws IncorrectOperationException {
41 PsiBinaryExpression concatenation = ConcatenationToMessageFormatAction.getEnclosingLiteralConcatenation(psiFile,editor);
42 if (concatenation != null) return;
43 String message = CodeInsightBundle.message("quickfix.i18n.concatentation.error");
44 throw new IncorrectOperationException(message);
47 public JavaI18nizeQuickFixDialog createDialog(Project project, Editor editor, PsiFile psiFile) {
48 PsiBinaryExpression concatenation = ConcatenationToMessageFormatAction.getEnclosingLiteralConcatenation(psiFile,editor);
49 PsiLiteralExpression literalExpression = ConcatenationToMessageFormatAction.getContainingLiteral(concatenation);
50 if (literalExpression == null) return null;
51 return createDialog(project, psiFile, literalExpression);
54 @NotNull
55 public String getName() {
56 return CodeInsightBundle.message("quickfix.i18n.concatentation");
59 protected PsiElement doReplacementInJava(@NotNull final PsiFile psiFile,
60 @NotNull final Editor editor,
61 @Nullable PsiLiteralExpression literalExpression,
62 String i18nizedText) throws IncorrectOperationException {
63 PsiBinaryExpression concatenation = ConcatenationToMessageFormatAction.getEnclosingLiteralConcatenation(psiFile, editor);
64 PsiExpression expression = JavaPsiFacade.getInstance(psiFile.getProject()).getElementFactory().createExpressionFromText(i18nizedText, concatenation);
65 return concatenation.replace(expression);
68 private static String composeParametersText(final List<PsiExpression> args) {
69 final StringBuilder result = new StringBuilder();
70 for (Iterator<PsiExpression> iterator = args.iterator(); iterator.hasNext();) {
71 PsiExpression psiExpression = iterator.next();
72 result.append(psiExpression.getText());
73 if (iterator.hasNext()) {
74 result.append(",");
77 return result.toString();
80 protected JavaI18nizeQuickFixDialog createDialog(final Project project, final PsiFile context, final PsiLiteralExpression literalExpression) {
81 PsiBinaryExpression concatenation = ConcatenationToMessageFormatAction.getEnclosingLiteralConcatenation(literalExpression);
82 StringBuffer formatString = new StringBuffer();
83 final List<PsiExpression> args = new ArrayList<PsiExpression>();
84 try {
85 ArrayList<PsiExpression> argsToCombine = new ArrayList<PsiExpression>();
86 ConcatenationToMessageFormatAction.calculateFormatAndArguments(concatenation, formatString, args, argsToCombine, false);
88 catch (IncorrectOperationException e) {
89 LOG.error(e);
92 String value = ConcatenationToMessageFormatAction.prepareString(formatString.toString());
94 return new JavaI18nizeQuickFixDialog(project, context, literalExpression, value, null, true, true) {
95 @Nullable
96 protected String getTemplateName() {
97 return myResourceBundleManager.getConcatenationTemplateName();
100 protected String generateText(final I18nizedTextGenerator textGenerator, final String propertyKey, final PropertiesFile propertiesFile,
101 final PsiLiteralExpression literalExpression) {
102 return textGenerator.getI18nizedConcatenationText(propertyKey, composeParametersText(args), propertiesFile, literalExpression);
105 public PsiExpression[] getParameters() {
106 return args.toArray(new PsiExpression[args.size()]);
109 protected void addAdditionalAttributes(final Map<String, String> attributes) {
110 attributes.put(PARAMETERS_OPTION_KEY, composeParametersText(args));