1 // Copyright 2008-2010 Victor Iacoban
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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;
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;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.concurrent.atomic.AtomicReference;
32 class HgCurrentBranchStatusUpdater implements HgUpdater {
34 private final HgCurrentBranchStatus hgCurrentBranchStatus;
36 public HgCurrentBranchStatusUpdater(HgCurrentBranchStatus hgCurrentBranchStatus) {
37 this.hgCurrentBranchStatus = hgCurrentBranchStatus;
40 public void update(final Project project) {
41 final AtomicReference<Editor> textEditor = new AtomicReference<Editor>();
42 UIUtil.invokeAndWaitIfNeeded(new Runnable() {
45 ApplicationManager.getApplication().runReadAction(new Runnable() {
48 textEditor.set(FileEditorManager.getInstance(project).getSelectedTextEditor());
54 if (textEditor.get() != null) {
55 Document document = textEditor.get().getDocument();
56 VirtualFile file = FileDocumentManager.getInstance().getFile(document);
58 final VirtualFile repo = VcsUtil.getVcsRootFor(project, file);
60 ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
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() {
69 hgCurrentBranchStatus.updateFor(branch, parents);
77 hgCurrentBranchStatus.updateFor(null, Collections.<HgRevisionNumber>emptyList());