maven notifier ui
[fedora-idea.git] / plugins / maven / src / main / java / org / jetbrains / idea / maven / utils / MavenImportNotifier.java
blob1227bcc6d87b0e896c61f0da174e3d02e03666cc
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 org.jetbrains.idea.maven.utils;
18 import com.intellij.openapi.fileEditor.FileEditor;
19 import com.intellij.openapi.fileEditor.FileEditorManager;
20 import com.intellij.openapi.fileEditor.FileEditorManagerAdapter;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.util.Key;
23 import com.intellij.openapi.vfs.VirtualFile;
24 import com.intellij.ui.EditorNotificationPanel;
25 import com.intellij.util.ui.update.MergingUpdateQueue;
26 import com.intellij.util.ui.update.Update;
27 import org.jetbrains.idea.maven.project.MavenProject;
28 import org.jetbrains.idea.maven.project.MavenProjectsManager;
29 import org.jetbrains.idea.maven.project.ProjectBundle;
31 import javax.swing.*;
33 // todo: migrate to EditorNotifications
34 public class MavenImportNotifier extends SimpleProjectComponent {
35 private static final Key<NotifierPanel> PANEL_KEY = new Key<NotifierPanel>(NotifierPanel.class.getName());
37 private final FileEditorManager myFileEditorManager;
38 private final MavenProjectsManager myMavenProjectsManager;
40 private final MergingUpdateQueue myUpdatesQueue;
42 public MavenImportNotifier(Project p, FileEditorManager fileEditorManager, MavenProjectsManager mavenProjectsManager) {
43 super(p);
45 myFileEditorManager = fileEditorManager;
46 myMavenProjectsManager = mavenProjectsManager;
48 myUpdatesQueue = new MergingUpdateQueue(getComponentName(), 500, false, MergingUpdateQueue.ANY_COMPONENT, myProject);
50 myMavenProjectsManager.addManagerListener(new MavenProjectsManager.Listener() {
51 public void activated() {
52 init();
53 scheduleUpdate();
56 public void scheduledImportsChanged() {
57 scheduleUpdate();
59 });
62 private void init() {
63 myFileEditorManager.addFileEditorManagerListener(new FileEditorManagerAdapter() {
64 @Override
65 public void fileOpened(FileEditorManager source, VirtualFile file) {
66 for (FileEditor each : source.getEditors(file)) {
67 updateNotification(file, each);
70 }, myProject);
71 myUpdatesQueue.activate();
74 private void scheduleUpdate() {
75 myUpdatesQueue.queue(new Update(myUpdatesQueue) {
76 public void run() {
77 updateNotifications();
79 });
82 private void updateNotifications() {
83 for (VirtualFile f : myFileEditorManager.getOpenFiles()) {
84 for (FileEditor e : myFileEditorManager.getEditors(f)) {
85 updateNotification(f, e);
90 private void updateNotification(VirtualFile file, FileEditor editor) {
91 if (myMavenProjectsManager.getImportingSettings().isImportAutomatically()
92 || !myMavenProjectsManager.hasScheduledImports()) {
93 JComponent panel = editor.getUserData(PANEL_KEY);
94 if (panel == null) return;
96 myFileEditorManager.removeTopComponent(editor, panel);
97 editor.putUserData(PANEL_KEY, null);
99 return;
102 MavenProject project = myMavenProjectsManager.findContainingProject(file);
103 if (project == null) return;
105 NotifierPanel panel = editor.getUserData(PANEL_KEY);
106 if (panel == null) {
107 panel = new NotifierPanel();
108 editor.putUserData(PANEL_KEY, panel);
109 myFileEditorManager.addTopComponent(editor, panel);
112 panel.update();
115 private class NotifierPanel extends EditorNotificationPanel {
117 private NotifierPanel() {
119 myLabel.setIcon(MavenIcons.MAVEN_ICON);
121 createActionLabel(ProjectBundle.message("maven.project.import.changed"), new Runnable() {
122 public void run() {
123 myMavenProjectsManager.performScheduledImport();
126 createActionLabel(ProjectBundle.message("maven.project.import.enable.auto"), new Runnable() {
127 public void run() {
128 myMavenProjectsManager.getImportingSettings().setImportAutomatically(true);
133 public void update() {
134 int projectsCount = myMavenProjectsManager.getScheduledProjectsCount();
135 String s;
136 if (projectsCount == 0) {
137 s = ProjectBundle.message("maven.project.something.changed");
139 else {
140 s = ProjectBundle.message("maven.project.changed", projectsCount, projectsCount == 1 ? " is" : "s are");
142 myLabel.setText(s);
143 myLabel.setToolTipText(s);