[hg] threading: access only from awt.
authorKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Thu, 14 Apr 2011 10:39:06 +0000 (14:39 +0400)
committerKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Mon, 18 Apr 2011 09:15:02 +0000 (13:15 +0400)
plugins/hg4idea/src/org/zmlx/hg4idea/HgCurrentBranchStatusUpdater.java
plugins/hg4idea/src/org/zmlx/hg4idea/ui/HgSwitchDialog.java

index 1c729206d11d38acc5416b958a5d886e9e09a724..75db797e0892f8dc102d2d117cc203337d9c36da 100644 (file)
@@ -19,6 +19,7 @@ import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.fileEditor.FileEditorManager;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.fileEditor.FileEditorManager;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.util.ui.UIUtil;
 import com.intellij.vcsUtil.VcsUtil;
 import org.zmlx.hg4idea.command.HgTagBranchCommand;
 import org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand;
 import com.intellij.vcsUtil.VcsUtil;
 import org.zmlx.hg4idea.command.HgTagBranchCommand;
 import org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand;
@@ -26,6 +27,7 @@ import org.zmlx.hg4idea.ui.HgCurrentBranchStatus;
 
 import java.util.Collections;
 import java.util.List;
 
 import java.util.Collections;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
 
 class HgCurrentBranchStatusUpdater implements HgUpdater {
 
 
 class HgCurrentBranchStatusUpdater implements HgUpdater {
 
@@ -36,9 +38,21 @@ class HgCurrentBranchStatusUpdater implements HgUpdater {
   }
 
   public void update(final Project project) {
   }
 
   public void update(final Project project) {
-    Editor textEditor = FileEditorManager.getInstance(project).getSelectedTextEditor();
-    if (textEditor != null) {
-      Document document = textEditor.getDocument();
+    final AtomicReference<Editor> textEditor = new AtomicReference<Editor>();
+    UIUtil.invokeAndWaitIfNeeded(new Runnable() {
+      @Override
+      public void run() {
+        ApplicationManager.getApplication().runReadAction(new Runnable() {
+          @Override
+          public void run() {
+            textEditor.set(FileEditorManager.getInstance(project).getSelectedTextEditor());
+          }
+        });
+      }
+    });
+
+    if (textEditor.get() != null) {
+      Document document = textEditor.get().getDocument();
       VirtualFile file = FileDocumentManager.getInstance().getFile(document);
 
       final VirtualFile repo = VcsUtil.getVcsRootFor(project, file);
       VirtualFile file = FileDocumentManager.getInstance().getFile(document);
 
       final VirtualFile repo = VcsUtil.getVcsRootFor(project, file);
index 6f6a1a3ba9bdd0ee807a5771b125bf4ca0d32f25..d0f2489efd617ebe149d4559a0f06a2fa6df9d47 100644 (file)
@@ -16,6 +16,7 @@ import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.DialogWrapper;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.util.Consumer;
 import com.intellij.openapi.ui.DialogWrapper;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.util.Consumer;
+import com.intellij.util.ui.UIUtil;
 import org.zmlx.hg4idea.command.HgTagBranch;
 import org.zmlx.hg4idea.command.HgTagBranchCommand;
 
 import org.zmlx.hg4idea.command.HgTagBranch;
 import org.zmlx.hg4idea.command.HgTagBranchCommand;
 
@@ -118,8 +119,13 @@ public class HgSwitchDialog extends DialogWrapper {
   private void loadBranches(VirtualFile root) {
     new HgTagBranchCommand(project, root).listBranches(new Consumer<List<HgTagBranch>>() {
       @Override
   private void loadBranches(VirtualFile root) {
     new HgTagBranchCommand(project, root).listBranches(new Consumer<List<HgTagBranch>>() {
       @Override
-      public void consume(List<HgTagBranch> branches) {
-        branchSelector.setModel(new DefaultComboBoxModel(branches.toArray()));
+      public void consume(final List<HgTagBranch> branches) {
+        UIUtil.invokeAndWaitIfNeeded(new Runnable() {
+          @Override
+          public void run() {
+            branchSelector.setModel(new DefaultComboBoxModel(branches.toArray()));
+          }
+        });
       }
     });
   }
       }
     });
   }
@@ -127,8 +133,13 @@ public class HgSwitchDialog extends DialogWrapper {
   private void loadTags(VirtualFile root) {
     new HgTagBranchCommand(project, root).listTags(new Consumer<List<HgTagBranch>>() {
       @Override
   private void loadTags(VirtualFile root) {
     new HgTagBranchCommand(project, root).listTags(new Consumer<List<HgTagBranch>>() {
       @Override
-      public void consume(List<HgTagBranch> tags) {
-        tagSelector.setModel(new DefaultComboBoxModel(tags.toArray()));
+      public void consume(final List<HgTagBranch> tags) {
+        UIUtil.invokeAndWaitIfNeeded(new Runnable() {
+          @Override
+          public void run() {
+            tagSelector.setModel(new DefaultComboBoxModel(tags.toArray()));
+          }
+        });
       }
     });
   }
       }
     });
   }