diff --git a/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleEditor.java b/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleEditor.java index 9f941794c0c2..d1d35b89effd 100644 --- a/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleEditor.java +++ b/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleEditor.java @@ -394,7 +394,7 @@ public class ResourceBundleEditor extends UserDataHolderBase implements Document }); } - private void recreateEditorsPanel() { + void recreateEditorsPanel() { myValuesPanel.removeAll(); myValuesPanel.setLayout(new CardLayout()); @@ -513,7 +513,7 @@ public class ResourceBundleEditor extends UserDataHolderBase implements Document } } - private void updateEditorsFromProperties(final boolean checkIsUnderUndoRedoAction) { + void updateEditorsFromProperties(final boolean checkIsUnderUndoRedoAction) { String propertyName = getSelectedPropertyName(); ((CardLayout)myValuesPanel.getLayout()).show(myValuesPanel, propertyName == null ? NO_PROPERTY_SELECTED : VALUES); if (propertyName == null) return; @@ -551,51 +551,7 @@ public class ResourceBundleEditor extends UserDataHolderBase implements Document if (myVfsListener != null) { throw new AssertionError("Listeners can't be initialized twice"); } - myVfsListener = new VirtualFileAdapter() { - @Override - public void fileCreated(@NotNull VirtualFileEvent event) { - if (PropertiesImplUtil.isPropertiesFile(event.getFile(), myProject)) { - recreateEditorsPanel(); - } - } - - @Override - public void fileDeleted(@NotNull VirtualFileEvent event) { - for (PropertiesFile file : myEditors.keySet()) { - if (Comparing.equal(file.getVirtualFile(), event.getFile())) { - recreateEditorsPanel(); - return; - } - } - } - - @Override - public void propertyChanged(@NotNull VirtualFilePropertyEvent event) { - final VirtualFile eventFile = event.getFile(); - if (PropertiesImplUtil.isPropertiesFile(eventFile, myProject)) { - EditorEx correspondingEditor = null; - for (Map.Entry e : myEditors.entrySet()) { - if (eventFile.equals(e.getKey().getContainingFile())) { - correspondingEditor = e.getValue(); - break; - } - } - if (correspondingEditor != null) { - final String propertyName = event.getPropertyName(); - if (VirtualFile.PROP_WRITABLE.equals(propertyName)) { - correspondingEditor.setViewer(Boolean.valueOf((String)event.getNewValue())); - return; - } - if (VirtualFile.PROP_NAME.equals(propertyName)) { - recreateEditorsPanel(); - } - else { - updateEditorsFromProperties(true); - } - } - } - } - }; + myVfsListener = new ResourceBundleEditorFileListener(this, myProject); virtualFileManager.addVirtualFileListener(myVfsListener, this); PsiTreeChangeAdapter psiTreeChangeAdapter = new PsiTreeChangeAdapter() { @@ -940,6 +896,10 @@ public class ResourceBundleEditor extends UserDataHolderBase implements Document }); } + Map getTranslationEditors() { + return myEditors; + } + public static class ResourceBundleEditorState implements FileEditorState { private final String myPropertyName; diff --git a/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleEditorFileListener.java b/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleEditorFileListener.java new file mode 100644 index 000000000000..9fad6e78d216 --- /dev/null +++ b/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleEditorFileListener.java @@ -0,0 +1,115 @@ +/* + * Copyright 2000-2016 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.lang.properties.editor; + +import com.intellij.lang.properties.PropertiesImplUtil; +import com.intellij.lang.properties.psi.PropertiesFile; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.editor.ex.EditorEx; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Comparing; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.VirtualFileAdapter; +import com.intellij.openapi.vfs.VirtualFileEvent; +import com.intellij.openapi.vfs.VirtualFilePropertyEvent; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Supplier; + +/** + * @author Dmitry Batkovich + */ +class ResourceBundleEditorFileListener extends VirtualFileAdapter { + private final Project myProject; + private ResourceBundleEditor myEditor; + + public ResourceBundleEditorFileListener(ResourceBundleEditor editor, Project project) { + myEditor = editor; + myProject = project; + } + + @Override + public void fileCreated(@NotNull VirtualFileEvent event) { + final VirtualFile file = event.getFile(); + if (PropertiesImplUtil.isPropertiesFile(file, myProject)) { + asyncUpdateUiIfNeed(() -> { + if (file.isValid()) { + for (PropertiesFile f : myEditor.getResourceBundle().getPropertiesFiles()) { + if (Comparing.equal(f.getVirtualFile(), file)) { + return f; + } + } + } + return null; + }, (f) -> myEditor.recreateEditorsPanel()); + } + } + + @Override + public void fileDeleted(@NotNull VirtualFileEvent event) { + final VirtualFile eventFile = event.getFile(); + for (PropertiesFile file : myEditor.getTranslationEditors().keySet()) { + if (Comparing.equal(file.getVirtualFile(), eventFile)) { + myEditor.recreateEditorsPanel(); + return; + } + } + } + + @Override + public void propertyChanged(@NotNull VirtualFilePropertyEvent event) { + final VirtualFile eventFile = event.getFile(); + if (PropertiesImplUtil.isPropertiesFile(eventFile, myProject)) { + asyncUpdateUiIfNeed(() -> { + for (Map.Entry e : myEditor.getTranslationEditors().entrySet()) { + if (eventFile.equals(e.getKey().getVirtualFile())) { + return e.getValue(); + } + } + return null; + }, (editor) -> { + if (editor != null) { + final String propertyName = event.getPropertyName(); + if (VirtualFile.PROP_WRITABLE.equals(propertyName)) { + editor.setViewer((Boolean)event.getNewValue()); + return; + } + if (VirtualFile.PROP_NAME.equals(propertyName)) { + myEditor.recreateEditorsPanel(); + } + else { + myEditor.updateEditorsFromProperties(true); + } + } + }); + } + } + + private void asyncUpdateUiIfNeed(Supplier uiUpdateCondition, Consumer uiUpdate) { + ApplicationManager.getApplication().executeOnPooledThread(() -> { + final T obj = uiUpdateCondition.get(); + if (obj != null) { + ApplicationManager.getApplication().invokeLater(() -> { + if (myEditor.isValid()) { + uiUpdate.accept(obj); + } + }); + } + }); + } +}