[hg] threading: access only from awt.
[idea/community.git] / plugins / hg4idea / src / org / zmlx / hg4idea / HgCurrentBranchStatusUpdater.java
1 // Copyright 2008-2010 Victor Iacoban
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software distributed under
10 // the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11 // either express or implied. See the License for the specific language governing permissions and
12 // limitations under the License.
13 package org.zmlx.hg4idea;
14
15 import com.intellij.openapi.application.ApplicationManager;
16 import com.intellij.openapi.editor.Document;
17 import com.intellij.openapi.editor.Editor;
18 import com.intellij.openapi.fileEditor.FileDocumentManager;
19 import com.intellij.openapi.fileEditor.FileEditorManager;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.vfs.VirtualFile;
22 import com.intellij.util.ui.UIUtil;
23 import com.intellij.vcsUtil.VcsUtil;
24 import org.zmlx.hg4idea.command.HgTagBranchCommand;
25 import org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand;
26 import org.zmlx.hg4idea.ui.HgCurrentBranchStatus;
27
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.concurrent.atomic.AtomicReference;
31
32 class HgCurrentBranchStatusUpdater implements HgUpdater {
33
34   private final HgCurrentBranchStatus hgCurrentBranchStatus;
35
36   public HgCurrentBranchStatusUpdater(HgCurrentBranchStatus hgCurrentBranchStatus) {
37     this.hgCurrentBranchStatus = hgCurrentBranchStatus;
38   }
39
40   public void update(final Project project) {
41     final AtomicReference<Editor> textEditor = new AtomicReference<Editor>();
42     UIUtil.invokeAndWaitIfNeeded(new Runnable() {
43       @Override
44       public void run() {
45         ApplicationManager.getApplication().runReadAction(new Runnable() {
46           @Override
47           public void run() {
48             textEditor.set(FileEditorManager.getInstance(project).getSelectedTextEditor());
49           }
50         });
51       }
52     });
53
54     if (textEditor.get() != null) {
55       Document document = textEditor.get().getDocument();
56       VirtualFile file = FileDocumentManager.getInstance().getFile(document);
57
58       final VirtualFile repo = VcsUtil.getVcsRootFor(project, file);
59       if (repo != null) {
60         ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
61           @Override
62           public void run() {
63             HgTagBranchCommand hgTagBranchCommand = new HgTagBranchCommand(project, repo);
64             final String branch = hgTagBranchCommand.getCurrentBranch();
65             final List<HgRevisionNumber> parents = new HgWorkingCopyRevisionsCommand(project).parents(repo);
66             ApplicationManager.getApplication().invokeLater(new Runnable() {
67               @Override
68               public void run() {
69                 hgCurrentBranchStatus.updateFor(branch, parents);
70               }
71             });
72           }
73         });
74         return;
75       }
76     }
77     hgCurrentBranchStatus.updateFor(null, Collections.<HgRevisionNumber>emptyList());
78   }
79
80 }