maven notifier ui
[fedora-idea.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / changes / committed / OutdatedVersionNotifier.java
blobeea29cadae4159e3ff2cc7483b99ac66b74ad5ff
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.vcs.changes.committed;
18 import com.intellij.openapi.application.ApplicationManager;
19 import com.intellij.openapi.components.ProjectComponent;
20 import com.intellij.openapi.diagnostic.Logger;
21 import com.intellij.openapi.fileEditor.FileEditor;
22 import com.intellij.openapi.fileEditor.FileEditorManager;
23 import com.intellij.openapi.fileEditor.FileEditorManagerEvent;
24 import com.intellij.openapi.fileEditor.FileEditorManagerListener;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.util.Key;
27 import com.intellij.openapi.util.Pair;
28 import com.intellij.openapi.vcs.VcsBundle;
29 import com.intellij.openapi.vcs.changes.Change;
30 import com.intellij.openapi.vcs.versionBrowser.CommittedChangeList;
31 import com.intellij.openapi.vfs.VirtualFile;
32 import com.intellij.ui.EditorNotificationPanel;
33 import com.intellij.util.Consumer;
34 import com.intellij.util.messages.MessageBus;
35 import org.jetbrains.annotations.NonNls;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
39 import java.text.DateFormat;
40 import java.util.List;
42 /**
43 * @author yole
44 * todo: use EditorNotifications
46 public class OutdatedVersionNotifier implements ProjectComponent {
47 private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.vcs.changes.committed.OutdatedVersionNotifier");
49 private final FileEditorManager myFileEditorManager;
50 private final CommittedChangesCache myCache;
51 private final Project myProject;
52 private static final Key<OutdatedRevisionPanel> PANEL_KEY = new Key<OutdatedRevisionPanel>("OutdatedRevisionPanel");
53 private volatile boolean myIncomingChangesRequested;
55 public OutdatedVersionNotifier(FileEditorManager fileEditorManager,
56 CommittedChangesCache cache,
57 MessageBus messageBus, Project project) {
58 myFileEditorManager = fileEditorManager;
59 myCache = cache;
60 myProject = project;
61 messageBus.connect().subscribe(CommittedChangesCache.COMMITTED_TOPIC, new CommittedChangesAdapter() {
62 public void incomingChangesUpdated(@Nullable final List<CommittedChangeList> receivedChanges) {
63 if (myCache.getCachedIncomingChanges() == null) {
64 requestLoadIncomingChanges();
66 else {
67 updateAllEditorsLater();
70 });
73 private void requestLoadIncomingChanges() {
74 debug("Requesting load of incoming changes");
75 if (!myIncomingChangesRequested) {
76 myIncomingChangesRequested = true;
77 myCache.loadIncomingChangesAsync(new Consumer<List<CommittedChangeList>>() {
78 public void consume(final List<CommittedChangeList> committedChangeLists) {
79 myIncomingChangesRequested = false;
80 updateAllEditorsLater();
82 });
86 private static void debug(@NonNls String message) {
87 LOG.debug(message);
90 public void projectOpened() {
91 final FileEditorManagerListener myFileEditorManagerListener = new MyFileEditorManagerListener();
92 myFileEditorManager.addFileEditorManagerListener(myFileEditorManagerListener, myProject);
95 public void projectClosed() {
98 @NonNls
99 @NotNull
100 public String getComponentName() {
101 return "OutdatedVersionNotifier";
104 public void initComponent() {
107 public void disposeComponent() {
110 private void updateAllEditorsLater() {
111 debug("Queueing update of editors");
112 ApplicationManager.getApplication().invokeLater(new Runnable() {
113 public void run() {
114 updateAllEditors();
119 private void updateAllEditors() {
120 if (myCache.getCachedIncomingChanges() == null) {
121 requestLoadIncomingChanges();
122 return;
124 debug("Updating editors");
125 final VirtualFile[] files = myFileEditorManager.getOpenFiles();
126 for(VirtualFile file: files) {
127 final Pair<CommittedChangeList,Change> pair = myCache.getIncomingChangeList(file);
128 final FileEditor[] fileEditors = myFileEditorManager.getEditors(file);
129 for(FileEditor editor: fileEditors) {
130 final OutdatedRevisionPanel oldPanel = editor.getUserData(PANEL_KEY);
131 if (pair != null) {
132 if (oldPanel != null) {
133 oldPanel.setChangeList(pair.first, pair.second);
135 else {
136 initPanel(pair.first, pair.second, editor);
139 else if (oldPanel != null) {
140 myFileEditorManager.removeTopComponent(editor, oldPanel);
141 editor.putUserData(PANEL_KEY, null);
147 private void initPanel(final CommittedChangeList list, final Change c, final FileEditor editor) {
148 final OutdatedRevisionPanel component = new OutdatedRevisionPanel(list, c);
149 editor.putUserData(PANEL_KEY, component);
150 myFileEditorManager.addTopComponent(editor, component);
153 private class MyFileEditorManagerListener implements FileEditorManagerListener {
154 public void fileOpened(FileEditorManager source, VirtualFile file) {
155 if (myCache.getCachedIncomingChanges() == null) {
156 requestLoadIncomingChanges();
158 else {
159 final Pair<CommittedChangeList, Change> pair = myCache.getIncomingChangeList(file);
160 if (pair != null) {
161 final FileEditor[] fileEditors = source.getEditors(file);
162 for(FileEditor editor: fileEditors) {
163 initPanel(pair.first, pair.second, editor);
169 public void fileClosed(FileEditorManager source, VirtualFile file) {
172 public void selectionChanged(FileEditorManagerEvent event) {
176 private static class OutdatedRevisionPanel extends EditorNotificationPanel {
177 private CommittedChangeList myChangeList;
179 public OutdatedRevisionPanel(CommittedChangeList changeList, final Change c) {
180 super();
181 createActionLabel(VcsBundle.message("outdated.version.show.diff.action"), "Compare.LastVersion");
182 createActionLabel(VcsBundle.message("outdated.version.update.project.action"), "Vcs.UpdateProject");
183 myChangeList = changeList;
184 updateLabelText(c);
187 private void updateLabelText(final Change c) {
188 final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
189 String comment = myChangeList.getComment();
190 int pos = comment.indexOf("\n");
191 if (pos >= 0) {
192 comment = comment.substring(0, pos).trim() + "...";
194 final String key = c.getType() == Change.Type.DELETED ? "outdated.version.text.deleted" : "outdated.version.text";
195 myLabel.setText(VcsBundle.message(key, myChangeList.getCommitterName(),
196 dateFormat.format(myChangeList.getCommitDate()), comment));
199 public void setChangeList(final CommittedChangeList changeList, final Change c) {
200 myChangeList = changeList;
201 updateLabelText(c);