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
.HyperlinkLabel
;
25 import com
.intellij
.ui
.LightColors
;
26 import com
.intellij
.util
.ui
.update
.MergingUpdateQueue
;
27 import com
.intellij
.util
.ui
.update
.Update
;
28 import org
.jetbrains
.idea
.maven
.project
.MavenProject
;
29 import org
.jetbrains
.idea
.maven
.project
.MavenProjectsManager
;
30 import org
.jetbrains
.idea
.maven
.project
.ProjectBundle
;
33 import javax
.swing
.border
.LineBorder
;
34 import javax
.swing
.event
.HyperlinkEvent
;
35 import javax
.swing
.event
.HyperlinkListener
;
38 public class MavenImportNotifier
extends SimpleProjectComponent
{
39 private static final Key
<NotifierPanel
> PANEL_KEY
= new Key
<NotifierPanel
>(NotifierPanel
.class.getName());
41 private final FileEditorManager myFileEditorManager
;
42 private final MavenProjectsManager myMavenProjectsManager
;
44 private final MergingUpdateQueue myUpdatesQueue
;
46 public MavenImportNotifier(Project p
, FileEditorManager fileEditorManager
, MavenProjectsManager mavenProjectsManager
) {
49 myFileEditorManager
= fileEditorManager
;
50 myMavenProjectsManager
= mavenProjectsManager
;
52 myUpdatesQueue
= new MergingUpdateQueue(getComponentName(), 500, false, MergingUpdateQueue
.ANY_COMPONENT
, myProject
);
54 myMavenProjectsManager
.addManagerListener(new MavenProjectsManager
.Listener() {
55 public void activated() {
60 public void scheduledImportsChanged() {
67 myFileEditorManager
.addFileEditorManagerListener(new FileEditorManagerAdapter() {
69 public void fileOpened(FileEditorManager source
, VirtualFile file
) {
70 for (FileEditor each
: source
.getEditors(file
)) {
71 updateNotification(file
, each
);
75 myUpdatesQueue
.activate();
78 private void scheduleUpdate() {
79 myUpdatesQueue
.queue(new Update(myUpdatesQueue
) {
81 updateNotifications();
86 private void updateNotifications() {
87 for (VirtualFile f
: myFileEditorManager
.getOpenFiles()) {
88 for (FileEditor e
: myFileEditorManager
.getEditors(f
)) {
89 updateNotification(f
, e
);
94 private void updateNotification(VirtualFile file
, FileEditor editor
) {
95 if (myMavenProjectsManager
.getImportingSettings().isImportAutomatically()
96 || !myMavenProjectsManager
.hasScheduledImports()) {
97 JComponent panel
= editor
.getUserData(PANEL_KEY
);
98 if (panel
== null) return;
100 myFileEditorManager
.removeTopComponent(editor
, panel
);
101 editor
.putUserData(PANEL_KEY
, null);
106 MavenProject project
= myMavenProjectsManager
.findContainingProject(file
);
107 if (project
== null) return;
109 NotifierPanel panel
= editor
.getUserData(PANEL_KEY
);
111 panel
= new NotifierPanel();
112 editor
.putUserData(PANEL_KEY
, panel
);
113 myFileEditorManager
.addTopComponent(editor
, panel
);
119 private class NotifierPanel
extends JPanel
{
120 private JLabel myLabel
;
122 private NotifierPanel() {
123 super(new GridBagLayout());
125 setBackground(LightColors
.YELLOW
);
126 setBorder(BorderFactory
.createEmptyBorder(7, 15, 7, 15));
127 myLabel
= new JLabel(MavenIcons
.MAVEN_ICON
, JLabel
.LEFT
);
129 JComponent importChangedButton
= createButton(ProjectBundle
.message("maven.project.import.changed"), new Runnable() {
131 myMavenProjectsManager
.performScheduledImport();
134 JComponent enableAutoImportButton
= createButton(ProjectBundle
.message("maven.project.import.enable.auto"), new Runnable() {
136 myMavenProjectsManager
.getImportingSettings().setImportAutomatically(true);
140 JPanel buttonsPanel
= new JPanel();
141 buttonsPanel
.setOpaque(false);
142 buttonsPanel
.setLayout(new BoxLayout(buttonsPanel
, BoxLayout
.X_AXIS
));
143 buttonsPanel
.add(myLabel
);
144 buttonsPanel
.add(importChangedButton
);
145 buttonsPanel
.add(Box
.createHorizontalStrut(10));
146 buttonsPanel
.add(enableAutoImportButton
);
148 GridBagConstraints c
= new GridBagConstraints();
149 c
.fill
= GridBagConstraints
.HORIZONTAL
;
152 c
.anchor
= GridBagConstraints
.WEST
;
158 c
.anchor
= GridBagConstraints
.EAST
;
159 add(buttonsPanel
, c
);
162 private JComponent
createButton(String text
, final Runnable action
) {
163 HyperlinkLabel result
= new HyperlinkLabel(text
, Color
.BLUE
, LightColors
.YELLOW
, Color
.BLUE
);
164 result
.addHyperlinkListener(new HyperlinkListener() {
165 public void hyperlinkUpdate(HyperlinkEvent e
) {
166 if (myProject
.isDisposed()) return;
167 if (e
.getEventType() == HyperlinkEvent
.EventType
.ACTIVATED
) {
175 public void update() {
176 int projectsCount
= myMavenProjectsManager
.getScheduledProjectsCount();
178 if (projectsCount
== 0) {
179 s
= ProjectBundle
.message("maven.project.something.changed");
182 s
= ProjectBundle
.message("maven.project.changed", projectsCount
, projectsCount
== 1 ?
" is" : "s are");
185 myLabel
.setToolTipText(s
);