update copyright
[fedora-idea.git] / plugins / properties / src / com / intellij / lang / properties / references / CreatePropertyFix.java
blob23c08fecd88155fbd343797ef81f8fc8ffcd8163
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.lang.properties.references;
18 import com.intellij.codeInsight.CodeInsightBundle;
19 import com.intellij.codeInsight.CodeInsightUtilBase;
20 import com.intellij.codeInsight.intention.IntentionAction;
21 import com.intellij.codeInspection.LocalQuickFix;
22 import com.intellij.codeInspection.ProblemDescriptor;
23 import com.intellij.lang.properties.PropertiesBundle;
24 import com.intellij.lang.properties.psi.PropertiesFile;
25 import com.intellij.openapi.application.ApplicationManager;
26 import com.intellij.openapi.command.CommandProcessor;
27 import com.intellij.openapi.command.undo.UndoUtil;
28 import com.intellij.openapi.diagnostic.Logger;
29 import com.intellij.openapi.editor.Editor;
30 import com.intellij.openapi.project.Project;
31 import com.intellij.openapi.util.Pair;
32 import com.intellij.psi.PsiElement;
33 import com.intellij.psi.PsiFile;
34 import com.intellij.util.IncorrectOperationException;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
38 import java.util.Collection;
39 import java.util.List;
41 public class CreatePropertyFix implements IntentionAction, LocalQuickFix {
42 private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.i18n.I18nizeQuickFix");
43 private final PsiElement myElement;
44 private final String myKey;
45 private final List<PropertiesFile> myPropertiesFiles;
47 public static final String NAME = PropertiesBundle.message("create.property.quickfix.text");
49 public CreatePropertyFix() {
50 this(null, null, null);
53 public CreatePropertyFix(PsiElement element, String key, final List<PropertiesFile> propertiesFiles) {
54 myElement = element;
55 myKey = key;
56 myPropertiesFiles = propertiesFiles;
59 @NotNull
60 public String getName() {
61 return NAME;
64 @NotNull
65 public String getFamilyName() {
66 return getText();
69 public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
70 PsiElement psiElement = descriptor.getPsiElement();
71 if (isAvailable(project, null, null)) {
72 invoke(project, null, psiElement.getContainingFile());
76 @NotNull
77 public String getText() {
78 return NAME;
81 public boolean isAvailable(@NotNull Project project, @Nullable Editor editor, @Nullable PsiFile file) {
82 return myElement.isValid();
85 public void invoke(@NotNull final Project project, @Nullable Editor editor, @NotNull PsiFile file) {
86 invokeAction(project, file, myElement, myKey, myPropertiesFiles);
89 @Nullable
90 protected static Pair<String, String> invokeAction(@NotNull final Project project,
91 @NotNull PsiFile file,
92 @NotNull PsiElement psiElement,
93 @Nullable final String suggestedKey,
94 @Nullable final List<PropertiesFile> propertiesFiles) {
95 final I18nizeQuickFixModel model;
96 final I18nizeQuickFixDialog.DialogCustomization dialogCustomization = createDefaultCustomization(suggestedKey, propertiesFiles);
98 if (ApplicationManager.getApplication().isUnitTestMode()) {
99 model = new I18nizeQuickFixModel() {
100 public String getValue() {
101 return "";
104 public String getKey() {
105 return dialogCustomization.getSuggestedName();
108 public boolean hasValidData() {
109 return true;
112 public Collection<PropertiesFile> getAllPropertiesFiles() {
113 return propertiesFiles;
116 } else {
117 model = new I18nizeQuickFixDialog(
118 project,
119 file,
120 NAME, dialogCustomization
123 return doAction(project, psiElement, model);
126 protected static I18nizeQuickFixDialog.DialogCustomization createDefaultCustomization(String suggestedKey, List<PropertiesFile> propertiesFiles) {
127 return new I18nizeQuickFixDialog.DialogCustomization(NAME, false, true, propertiesFiles, suggestedKey == null ? "" : suggestedKey);
130 protected static Pair<String, String> doAction(Project project, PsiElement psiElement,
131 I18nizeQuickFixModel model) {
132 if (!model.hasValidData()) {
133 return null;
135 final String key = model.getKey();
136 final String value = model.getValue();
138 final Collection<PropertiesFile> selectedPropertiesFiles = model.getAllPropertiesFiles();
139 createProperty(project, psiElement, selectedPropertiesFiles, key, value);
141 return new Pair<String, String>(key, value);
144 public static void createProperty(@NotNull final Project project,
145 @NotNull final PsiElement psiElement,
146 @NotNull final Collection<PropertiesFile> selectedPropertiesFiles,
147 @NotNull final String key,
148 @NotNull final String value) {
149 for (PropertiesFile selectedFile : selectedPropertiesFiles) {
150 if (!CodeInsightUtilBase.prepareFileForWrite(selectedFile)) return;
152 UndoUtil.markPsiFileForUndo(psiElement.getContainingFile());
154 ApplicationManager.getApplication().runWriteAction(new Runnable() {
155 public void run() {
156 CommandProcessor.getInstance().executeCommand(project, new Runnable() {
157 public void run() {
158 try {
159 I18nUtil.createProperty(project, selectedPropertiesFiles, key, value);
161 catch (IncorrectOperationException e) {
162 LOG.error(e);
165 }, CodeInsightBundle.message("quickfix.i18n.command.name"), project);
170 public boolean startInWriteAction() {
171 return false;