update copyright
[fedora-idea.git] / plugins / ui-designer / src / com / intellij / uiDesigner / i18n / I18nizeFormQuickFix.java
blob9b1fdac8e2f5ffc4cb545e8bb1f6fa3260687214
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.uiDesigner.i18n;
18 import com.intellij.codeInsight.CodeInsightBundle;
19 import com.intellij.codeInsight.CodeInsightUtilBase;
20 import com.intellij.codeInspection.i18n.JavaI18nUtil;
21 import com.intellij.codeInspection.i18n.JavaI18nizeQuickFixDialog;
22 import com.intellij.lang.properties.psi.PropertiesFile;
23 import com.intellij.openapi.application.ApplicationManager;
24 import com.intellij.openapi.command.CommandProcessor;
25 import com.intellij.openapi.diagnostic.Logger;
26 import com.intellij.openapi.fileEditor.FileDocumentManager;
27 import com.intellij.openapi.project.Project;
28 import com.intellij.openapi.roots.ProjectFileIndex;
29 import com.intellij.openapi.roots.ProjectRootManager;
30 import com.intellij.openapi.util.text.StringUtil;
31 import com.intellij.psi.PsiDocumentManager;
32 import com.intellij.psi.PsiFile;
33 import com.intellij.uiDesigner.designSurface.GuiEditor;
34 import com.intellij.uiDesigner.lw.StringDescriptor;
35 import com.intellij.uiDesigner.quickFixes.QuickFix;
36 import com.intellij.uiDesigner.radComponents.RadComponent;
37 import com.intellij.util.IncorrectOperationException;
39 import java.util.Collection;
41 /**
42 * @author yole
44 public abstract class I18nizeFormQuickFix extends QuickFix {
45 private static final Logger LOG = Logger.getInstance("#com.intellij.uiDesigner.i18n.I18nizeFormQuickFix");
47 public I18nizeFormQuickFix(final GuiEditor editor, final String name, final RadComponent component) {
48 super(editor, name, component);
51 public void run() {
52 final StringDescriptor descriptor = getStringDescriptorValue();
53 final Project project = myEditor.getProject();
55 PsiFile psiFile = myEditor.getPsiFile();
57 if (!JavaI18nizeQuickFixDialog.isAvailable(myEditor.getPsiFile())) {
58 return;
60 String initialValue = StringUtil.escapeStringCharacters(descriptor.getValue());
61 final JavaI18nizeQuickFixDialog dialog = new JavaI18nizeQuickFixDialog(project, psiFile, null, initialValue, null, false, false){
62 protected String getDimensionServiceKey() {
63 return "#com.intellij.codeInsight.i18n.I18nizeQuickFixDialog_Form";
66 dialog.show();
67 if (!dialog.isOK()) {
68 return;
71 if (!myEditor.ensureEditable()) {
72 return;
74 final Collection<PropertiesFile> propertiesFiles = dialog.getAllPropertiesFiles();
75 PropertiesFile aPropertiesFile = null;
76 for (PropertiesFile file : propertiesFiles) {
77 if (!CodeInsightUtilBase.prepareFileForWrite(file)) return;
78 if (aPropertiesFile == null) {
79 aPropertiesFile = file;
83 CommandProcessor.getInstance().executeCommand(project, new Runnable(){
84 public void run() {
85 ApplicationManager.getApplication().runWriteAction(new Runnable(){
86 public void run() {
87 try {
88 JavaI18nUtil.createProperty(project, propertiesFiles, dialog.getKey(), dialog.getValue());
90 catch (IncorrectOperationException e) {
91 LOG.error(e);
94 });
96 }, CodeInsightBundle.message("quickfix.i18n.command.name"),project);
98 // saving files is necessary to ensure correct reload of properties files by UI Designer
99 for(PropertiesFile file: propertiesFiles) {
100 FileDocumentManager.getInstance().saveDocument(PsiDocumentManager.getInstance(project).getDocument(file));
103 if (aPropertiesFile != null) {
104 final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
105 String packageName = fileIndex.getPackageNameByDirectory(aPropertiesFile.getVirtualFile().getParent());
106 if (packageName != null) {
107 String bundleName;
108 if (packageName.length() > 0) {
109 bundleName = packageName + "." + aPropertiesFile.getResourceBundle().getBaseName();
111 else {
112 bundleName = aPropertiesFile.getResourceBundle().getBaseName();
114 bundleName = bundleName.replace('.', '/');
115 try {
116 setStringDescriptorValue(new StringDescriptor(bundleName, dialog.getKey()));
118 catch (Exception e) {
119 LOG.error(e);
121 myEditor.refreshAndSave(true);
126 protected abstract StringDescriptor getStringDescriptorValue();
127 protected abstract void setStringDescriptorValue(final StringDescriptor descriptor) throws Exception;