From 5fecee12df4a2649a21b779d48e260215a9d4efc Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Fri, 17 Feb 2012 15:01:18 +0400 Subject: [PATCH] IDEA-81400: Annoying 'Can't connect to YouTrack message' from task plugin --- .../intellij/notification/Notifications.java | 35 +++--- .../intellij/tasks/impl/TaskManagerImpl.java | 107 ++++++++++-------- .../com/intellij/tasks/TaskManagerTest.java | 45 ++++++++ .../intellij/tasks/TaskManagerTestCase.java | 5 +- 4 files changed, 130 insertions(+), 62 deletions(-) diff --git a/platform/platform-api/src/com/intellij/notification/Notifications.java b/platform/platform-api/src/com/intellij/notification/Notifications.java index 0a0f7aaf35df..b91b069aedcb 100644 --- a/platform/platform-api/src/com/intellij/notification/Notifications.java +++ b/platform/platform-api/src/com/intellij/notification/Notifications.java @@ -67,22 +67,29 @@ public interface Notifications { } public static void notify(@NotNull final Notification notification, @Nullable final Project project) { - if (ApplicationManager.getApplication().isUnitTestMode()) return; - //noinspection SSBasedInspection - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - if (project != null && !project.isDisposed()) { - project.getMessageBus().syncPublisher(TOPIC).notify(notification); - } else { - Application app = ApplicationManager.getApplication(); - if (!app.isDisposed()) { - app.getMessageBus().syncPublisher(TOPIC).notify(notification); - } + if (ApplicationManager.getApplication().isUnitTestMode()) { + doNotify(notification, project); + } + else { + //noinspection SSBasedInspection + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + doNotify(notification, project); } - } - }); + }); + } } + private static void doNotify(Notification notification, Project project) { + if (project != null && !project.isDisposed()) { + project.getMessageBus().syncPublisher(TOPIC).notify(notification); + } else { + Application app = ApplicationManager.getApplication(); + if (!app.isDisposed()) { + app.getMessageBus().syncPublisher(TOPIC).notify(notification); + } + } + } } } diff --git a/plugins/tasks/tasks-core/src/com/intellij/tasks/impl/TaskManagerImpl.java b/plugins/tasks/tasks-core/src/com/intellij/tasks/impl/TaskManagerImpl.java index 5abec03165b2..2c9edabd7362 100644 --- a/plugins/tasks/tasks-core/src/com/intellij/tasks/impl/TaskManagerImpl.java +++ b/plugins/tasks/tasks-core/src/com/intellij/tasks/impl/TaskManagerImpl.java @@ -75,7 +75,7 @@ public class TaskManagerImpl extends TaskManager implements ProjectComponent, Pe return o.getId(); } }; - private static final String TASKS_NOTIFICATION_GROUP = "Task Group"; + static final String TASKS_NOTIFICATION_GROUP = "Task Group"; private final Project myProject; @@ -647,40 +647,49 @@ public class TaskManagerImpl extends TaskManager implements ProjectComponent, Pe return; } myUpdating = true; - ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { - public void run() { - try { - List issues = getIssuesFromRepositories(null, myConfig.updateIssuesCount, 0, false); - - synchronized (myIssueCache) { - myIssueCache.clear(); - for (Task issue : issues) { - myIssueCache.put(issue.getId(), issue); - } - } - // update local tasks - synchronized (myTasks) { - for (Iterator> it = myTasks.entrySet().iterator(); it.hasNext();) { - Map.Entry entry = it.next(); - Task issue = myIssueCache.get(entry.getKey()); - if (issue != null) { - if (issue.isClosed()) { - it.remove(); - } else { - entry.getValue().updateFromIssue(issue); - } - } - } - } + if (ApplicationManager.getApplication().isUnitTestMode()) { + doUpdate(onComplete); + } + else { + ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { + public void run() { + doUpdate(onComplete); } - finally { - if (onComplete != null) { - onComplete.run(); - } - myUpdating = false; + }); + } + } + + private void doUpdate(Runnable onComplete) { + try { + List issues = getIssuesFromRepositories(null, myConfig.updateIssuesCount, 0, false); + + synchronized (myIssueCache) { + myIssueCache.clear(); + for (Task issue : issues) { + myIssueCache.put(issue.getId(), issue); } } - }); + // update local tasks + synchronized (myTasks) { + for (Iterator> it = myTasks.entrySet().iterator(); it.hasNext();) { + Map.Entry entry = it.next(); + Task issue = myIssueCache.get(entry.getKey()); + if (issue != null) { + if (issue.isClosed()) { + it.remove(); + } else { + entry.getValue().updateFromIssue(issue); + } + } + } + } + } + finally { + if (onComplete != null) { + onComplete.run(); + } + myUpdating = false; + } } private List getIssuesFromRepositories(@Nullable String request, int max, long since, boolean forceRequest) { @@ -696,25 +705,31 @@ public class TaskManagerImpl extends TaskManager implements ProjectComponent, Pe } catch (Exception e) { myBadRepositories.add(repository); - Notifications.Bus.register(TASKS_NOTIFICATION_GROUP, NotificationDisplayType.BALLOON); - Notifications.Bus.notify(new Notification(TASKS_NOTIFICATION_GROUP, "Cannot connect to " + repository.getUrl(), - "

Configure server...

", NotificationType.WARNING, - new NotificationListener() { - public void hyperlinkUpdate(@NotNull Notification notification, - @NotNull HyperlinkEvent event) { - TaskRepositoriesConfigurable configurable = - new TaskRepositoriesConfigurable(myProject); - ShowSettingsUtil.getInstance().editConfigurable(myProject, configurable); - if (!ArrayUtil.contains(repository, getAllRepositories())) { - notification.expire(); - } - } - }), myProject); + if (forceRequest) { + notifyAboutConnectionFailure(repository); + } } } return issues; } + private void notifyAboutConnectionFailure(final TaskRepository repository) { + Notifications.Bus.register(TASKS_NOTIFICATION_GROUP, NotificationDisplayType.BALLOON); + Notifications.Bus.notify(new Notification(TASKS_NOTIFICATION_GROUP, "Cannot connect to " + repository.getUrl(), + "

Configure server...

", NotificationType.WARNING, + new NotificationListener() { + public void hyperlinkUpdate(@NotNull Notification notification, + @NotNull HyperlinkEvent event) { + TaskRepositoriesConfigurable configurable = + new TaskRepositoriesConfigurable(myProject); + ShowSettingsUtil.getInstance().editConfigurable(myProject, configurable); + if (!ArrayUtil.contains(repository, getAllRepositories())) { + notification.expire(); + } + } + }), myProject); + } + @Override public boolean isVcsEnabled() { return ProjectLevelVcsManager.getInstance(myProject).getAllActiveVcss().length > 0; diff --git a/plugins/tasks/tasks-tests/test/com/intellij/tasks/TaskManagerTest.java b/plugins/tasks/tasks-tests/test/com/intellij/tasks/TaskManagerTest.java index f7f524850bc5..f75295c0a636 100644 --- a/plugins/tasks/tasks-tests/test/com/intellij/tasks/TaskManagerTest.java +++ b/plugins/tasks/tasks-tests/test/com/intellij/tasks/TaskManagerTest.java @@ -1,6 +1,13 @@ package com.intellij.tasks; +import com.intellij.notification.Notification; +import com.intellij.notification.NotificationDisplayType; +import com.intellij.notification.Notifications; import com.intellij.openapi.util.Ref; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Collections; /** * @author Dmitry Avdeev @@ -40,4 +47,42 @@ public class TaskManagerTest extends TaskManagerTestCase { myManager.removeTaskListener(listener); } + public void testNotifications() throws Exception { + + final Ref notificationRef = new Ref(); + getProject().getMessageBus().connect(getTestRootDisposable()).subscribe(Notifications.TOPIC, new Notifications() { + @Override + public void notify(@NotNull Notification notification) { + notificationRef.set(notification); + } + + @Override + public void register(@NotNull String groupDisplayName, @NotNull NotificationDisplayType defaultDisplayType) { + + } + + @Override + public void register(@NotNull String groupDisplayName, + @NotNull NotificationDisplayType defaultDisplayType, + boolean shouldLog) { + + } + }); + + TestRepository repository = new TestRepository() { + @Override + public Task[] getIssues(@Nullable String query, int max, long since) throws Exception { + throw new Exception(); + } + }; + myManager.setRepositories(Collections.singletonList(repository)); + + myManager.updateIssues(null); + + assertNull(notificationRef.get()); + + myManager.getIssues(""); + + assertNotNull(notificationRef.get()); + } } diff --git a/plugins/tasks/tasks-tests/test/com/intellij/tasks/TaskManagerTestCase.java b/plugins/tasks/tasks-tests/test/com/intellij/tasks/TaskManagerTestCase.java index eafa7877eeb5..72906308279a 100644 --- a/plugins/tasks/tasks-tests/test/com/intellij/tasks/TaskManagerTestCase.java +++ b/plugins/tasks/tasks-tests/test/com/intellij/tasks/TaskManagerTestCase.java @@ -1,5 +1,6 @@ package com.intellij.tasks; +import com.intellij.tasks.impl.TaskManagerImpl; import com.intellij.testFramework.UsefulTestCase; import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; @@ -12,11 +13,11 @@ public abstract class TaskManagerTestCase extends LightCodeInsightFixtureTestCas super(UsefulTestCase.IDEA_MARKER_CLASS, "PlatformLangXml"); } - protected TaskManager myManager; + protected TaskManagerImpl myManager; @Override protected void setUp() throws Exception { super.setUp(); - myManager = TaskManager.getManager(getProject()); + myManager = (TaskManagerImpl)TaskManager.getManager(getProject()); } }