Resource bundle editor: proper handling of read-only status (based on IDEA-152810)
authorDmitry Batkovich <dmitry.batkovich@jetbrains.com>
Tue, 15 Mar 2016 13:11:25 +0000 (16:11 +0300)
committerDmitry Batkovich <dmitry.batkovich@jetbrains.com>
Tue, 15 Mar 2016 13:12:09 +0000 (16:12 +0300)
plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleEditor.java

index d66f7dfdba4db7726a64a63dfc6c6d18e9beda8b..48c6451436fc3ec86d2dc5a659b71702d47cad58 100644 (file)
@@ -88,6 +88,8 @@ import javax.swing.event.TreeSelectionListener;
 import javax.swing.tree.DefaultMutableTreeNode;
 import javax.swing.tree.TreePath;
 import java.awt.*;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
 import java.beans.PropertyChangeListener;
 import java.util.*;
 import java.util.List;
@@ -100,7 +102,7 @@ public class ResourceBundleEditor extends UserDataHolderBase implements Document
   public static final Key<ResourceBundleEditor> RESOURCE_BUNDLE_EDITOR_KEY = Key.create("resourceBundleEditor");
 
   private final StructureViewComponent      myStructureViewComponent;
-  private final Map<PropertiesFile, Editor> myEditors;
+  private final Map<PropertiesFile, EditorEx> myEditors;
   private final ResourceBundle              myResourceBundle;
   private final ResourceBundlePropertiesUpdateManager myPropertiesInsertDeleteManager;
   private final Map<PropertiesFile, JPanel> myTitledPanels;
@@ -162,7 +164,7 @@ public class ResourceBundleEditor extends UserDataHolderBase implements Document
             !Comparing.equal(selectedPropertiesFile, getSelectedPropertiesFile())) {
 
           if (selectedProperty != null && e.getOldLeadSelectionPath() != null) {
-            for (Map.Entry<PropertiesFile, Editor> entry : myEditors.entrySet()) {
+            for (Map.Entry<PropertiesFile, EditorEx> entry : myEditors.entrySet()) {
               if (entry.getValue() == mySelectedEditor) {
                 writeEditorPropertyValue(selectedProperty.getName(), mySelectedEditor, entry.getKey());
                 break;
@@ -192,7 +194,7 @@ public class ResourceBundleEditor extends UserDataHolderBase implements Document
     });
     installPropertiesChangeListeners();
 
-    myEditors = new THashMap<PropertiesFile, Editor>();
+    myEditors = new THashMap<PropertiesFile, EditorEx>();
     myTitledPanels = new THashMap<PropertiesFile, JPanel>();
     recreateEditorsPanel();
 
@@ -416,7 +418,7 @@ public class ResourceBundleEditor extends UserDataHolderBase implements Document
     Editor previousEditor = null;
     Editor firstEditor = null;
     for (final PropertiesFile propertiesFile : propertiesFiles) {
-      final Editor editor = createEditor();
+      final EditorEx editor = createEditor();
       final Editor oldEditor = myEditors.put(propertiesFile, editor);
       if (firstEditor == null) {
         firstEditor = editor;
@@ -429,22 +431,26 @@ public class ResourceBundleEditor extends UserDataHolderBase implements Document
       if (oldEditor != null) {
         EditorFactory.getInstance().releaseEditor(oldEditor);
       }
-      final Boolean[] isViewer = new Boolean[1];
-      ((EditorEx) editor).addFocusListener(new FocusChangeListener() {
+
+      editor.setViewer(!propertiesFile.getVirtualFile().isWritable());
+      editor.getContentComponent().addKeyListener(new KeyAdapter() {
+        @Override
+        public void keyTyped(KeyEvent e) {
+          if (editor.isViewer()) {
+            editor.setViewer( ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(propertiesFile.getVirtualFile()).hasReadonlyFiles());
+          }
+        }
+      });
+
+      editor.addFocusListener(new FocusChangeListener() {
         @Override
         public void focusGained(final Editor editor) {
           mySelectedEditor = editor;
-          final EditorEx editorEx = (EditorEx)editor;
-          if (isViewer[0] == null) {
-            final boolean readOnly = ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(propertiesFile.getVirtualFile()).hasReadonlyFiles();
-            editorEx.setViewer(readOnly);
-            isViewer[0] = readOnly;
-          }
         }
 
         @Override
-        public void focusLost(final Editor eventEditor) {
-          if (Boolean.FALSE.equals(isViewer[0]) && propertiesFile.getContainingFile().isValid()) {
+        public void focusLost(final Editor editor) {
+          if (!editor.isViewer() && propertiesFile.getContainingFile().isValid()) {
             writeEditorPropertyValue(null, editor, propertiesFile);
           }
         }
@@ -513,7 +519,7 @@ public class ResourceBundleEditor extends UserDataHolderBase implements Document
 
     final UndoManagerImpl undoManager = (UndoManagerImpl)UndoManager.getInstance(myProject);
     for (final PropertiesFile propertiesFile : myResourceBundle.getPropertiesFiles()) {
-      final EditorEx editor = (EditorEx)myEditors.get(propertiesFile);
+      final EditorEx editor = myEditors.get(propertiesFile);
       if (editor == null) continue;
       final IProperty property = propertiesFile.findPropertyByKey(propertyName);
       final Document document = editor.getDocument();
@@ -564,12 +570,27 @@ public class ResourceBundleEditor extends UserDataHolderBase implements Document
 
       @Override
       public void propertyChanged(@NotNull VirtualFilePropertyEvent event) {
-        if (PropertiesImplUtil.isPropertiesFile(event.getFile(), myProject)) {
-          if (VirtualFile.PROP_NAME.equals(event.getPropertyName())) {
-            recreateEditorsPanel();
+        final VirtualFile eventFile = event.getFile();
+        if (PropertiesImplUtil.isPropertiesFile(eventFile, myProject)) {
+          EditorEx correspondingEditor = null;
+          for (Map.Entry<PropertiesFile, EditorEx> e : myEditors.entrySet()) {
+            if (eventFile.equals(e.getKey().getContainingFile())) {
+              correspondingEditor = e.getValue();
+              break;
+            }
           }
-          else {
-            updateEditorsFromProperties(true);
+          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);
+            }
           }
         }
       }
@@ -776,7 +797,7 @@ public class ResourceBundleEditor extends UserDataHolderBase implements Document
   private PropertiesFile getSelectedPropertiesFile() {
     if (mySelectedEditor == null) return null;
     PropertiesFile selectedFile = null;
-    for (Map.Entry<PropertiesFile, Editor> entry : myEditors.entrySet()) {
+    for (Map.Entry<PropertiesFile, EditorEx> entry : myEditors.entrySet()) {
       Editor editor = entry.getValue();
       if (editor == mySelectedEditor) {
         selectedFile = entry.getKey();
@@ -865,7 +886,7 @@ public class ResourceBundleEditor extends UserDataHolderBase implements Document
   @Override
   public void dispose() {
     if (mySelectedEditor != null) {
-      for (final Map.Entry<PropertiesFile, Editor> entry : myEditors.entrySet()) {
+      for (final Map.Entry<PropertiesFile, EditorEx> entry : myEditors.entrySet()) {
         if (mySelectedEditor.equals(entry.getValue())) {
           writeEditorPropertyValue(null, mySelectedEditor, entry.getKey());
         }
@@ -914,7 +935,7 @@ public class ResourceBundleEditor extends UserDataHolderBase implements Document
     }
   }
 
-  private Editor createEditor() {
+  private EditorEx createEditor() {
     EditorFactory editorFactory = EditorFactory.getInstance();
     Document document = editorFactory.createDocument("");
     EditorEx editor = (EditorEx)editorFactory.createEditor(document);
@@ -963,7 +984,7 @@ public class ResourceBundleEditor extends UserDataHolderBase implements Document
                         @Override
                         public void run() {
                           try {
-                            for (Map.Entry<PropertiesFile, Editor> entry : myEditors.entrySet()) {
+                            for (Map.Entry<PropertiesFile, EditorEx> entry : myEditors.entrySet()) {
                               final Editor translationEditor = entry.getValue();
                               if (translationEditor != editor) {
                                 final PropertiesFile propertiesFile = entry.getKey();