IDEA-127739 Navigation Tab
[idea/community.git] / platform / platform-impl / src / com / intellij / openapi / actionSystem / impl / Utils.java
1 /*
2  * Copyright 2000-2013 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.openapi.actionSystem.impl;
17
18 import com.intellij.ide.DataManager;
19 import com.intellij.openapi.actionSystem.*;
20 import com.intellij.openapi.actionSystem.ex.ActionUtil;
21 import com.intellij.openapi.application.ApplicationManager;
22 import com.intellij.openapi.diagnostic.Logger;
23 import com.intellij.openapi.progress.ProcessCanceledException;
24 import com.intellij.openapi.project.DumbService;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.util.ActionCallback;
27 import com.intellij.openapi.util.SystemInfo;
28 import com.intellij.openapi.util.registry.Registry;
29 import com.intellij.openapi.util.text.StringUtil;
30 import com.intellij.openapi.wm.IdeFocusManager;
31 import com.intellij.util.ui.UIUtil;
32 import org.jetbrains.annotations.NonNls;
33 import org.jetbrains.annotations.NotNull;
34
35 import javax.swing.*;
36 import java.awt.*;
37 import java.util.ArrayList;
38 import java.util.List;
39
40 /**
41  * @author Anton Katilin
42  * @author Vladimir Kondratyev
43  */
44 public class Utils{
45   private static final Logger LOG=Logger.getInstance("#com.intellij.openapi.actionSystem.impl.Utils");
46   @NonNls public static final String NOTHING_HERE = "Nothing here";
47   public static final AnAction EMPTY_MENU_FILLER = new AnAction(NOTHING_HERE) {
48
49     {
50       getTemplatePresentation().setEnabled(false);
51     }
52
53     @Override
54     public void actionPerformed(AnActionEvent e) {
55     }
56
57     @Override
58     public void update(AnActionEvent e) {
59       e.getPresentation().setEnabled(false);
60       super.update(e);
61     }
62   };
63
64   private Utils() {}
65
66   private static void handleUpdateException(AnAction action, Presentation presentation, Throwable exc) {
67     String id = ActionManager.getInstance().getId(action);
68     if (id != null) {
69       LOG.error("update failed for AnAction with ID=" + id, exc);
70     }
71     else {
72       LOG.error("update failed for ActionGroup: " + action + "[" + presentation.getText() + "]", exc);
73     }
74   }
75
76   /**
77    * @param list this list contains expanded actions.
78    * @param actionManager manager
79    */
80   public static void expandActionGroup(@NotNull ActionGroup group,
81                                        List<AnAction> list,
82                                        PresentationFactory presentationFactory,
83                                        @NotNull DataContext context,
84                                        String place,
85                                        ActionManager actionManager){
86     expandActionGroup(group, list, presentationFactory, context, place, actionManager, false, group instanceof CompactActionGroup);
87   }
88
89   public static void expandActionGroup(@NotNull ActionGroup group,
90                                        List<AnAction> list,
91                                        PresentationFactory presentationFactory,
92                                        DataContext context,
93                                        @NotNull String place,
94                                        ActionManager actionManager,
95                                        boolean transparentOnly) {
96     expandActionGroup(group, list, presentationFactory, context, place, actionManager, transparentOnly, false);
97   }
98
99   /**
100    * @param list this list contains expanded actions.
101    * @param actionManager manager
102    */
103   public static void expandActionGroup(@NotNull ActionGroup group,
104                                        List<AnAction> list,
105                                        PresentationFactory presentationFactory,
106                                        DataContext context,
107                                        @NotNull String place,
108                                        ActionManager actionManager,
109                                        boolean transparentOnly,
110                                        boolean hideDisabled) {
111     Presentation presentation = presentationFactory.getPresentation(group);
112     AnActionEvent e = new AnActionEvent(
113       null,
114       context,
115       place,
116       presentation,
117       actionManager,
118       0
119     );
120     if (!doUpdate(group, e, presentation)) return;
121
122     if (!presentation.isVisible()) { // don't process invisible groups
123       return;
124     }
125     AnAction[] children = group.getChildren(e);
126     for (int i = 0; i < children.length; i++) {
127       AnAction child = children[i];
128       if (child == null) {
129         String groupId = ActionManager.getInstance().getId(group);
130         LOG.error("action is null: i=" + i + " group=" + group + " group id=" + groupId);
131         continue;
132       }
133
134       presentation = presentationFactory.getPresentation(child);
135       AnActionEvent e1 = new AnActionEvent(null, context, place, presentation, actionManager, 0);
136       e1.setInjectedContext(child.isInInjectedContext());
137
138       if (transparentOnly && child.isTransparentUpdate() || !transparentOnly) {
139         if (!doUpdate(child, e1, presentation)) continue;
140       }
141
142       if (!presentation.isVisible() || (!presentation.isEnabled() && hideDisabled)) { // don't create invisible items in the menu
143         continue;
144       }
145       if (child instanceof ActionGroup) {
146         ActionGroup actionGroup = (ActionGroup)child;
147         boolean skip = hideDisabled && !hasEnabledChildren(actionGroup, presentationFactory, context, place);
148         if (skip) {
149           continue;
150         }
151         if (actionGroup.isPopup()) { // popup menu has its own presentation
152           if (actionGroup.disableIfNoVisibleChildren()) {
153             final boolean visibleChildren = hasVisibleChildren(actionGroup, presentationFactory, context, place);
154             if (actionGroup.hideIfNoVisibleChildren() && !visibleChildren) {
155               continue;
156             }
157             presentation.setEnabled(actionGroup.canBePerformed(context) || visibleChildren);
158           }
159
160
161           list.add(child);
162         }
163         else {
164           expandActionGroup((ActionGroup)child, list, presentationFactory, context, place, actionManager, false, hideDisabled);
165         }
166       }
167       else if (child instanceof Separator) {
168         if (!StringUtil.isEmpty(((Separator)child).getText()) || (!list.isEmpty() && !(list.get(list.size() - 1) instanceof Separator))) {
169           list.add(child);
170         }
171       }
172       else {
173         if (hideDisabled && !hasEnabledChildren(new DefaultActionGroup(child), presentationFactory, context, place)) {
174           continue;
175         }
176         list.add(child);
177       }
178     }
179   }
180
181   // returns false if exception was thrown and handled
182   private static boolean doUpdate(final AnAction action, final AnActionEvent e, final Presentation presentation) throws ProcessCanceledException {
183     if (ApplicationManager.getApplication().isDisposed()) return false;
184
185     long startTime = System.currentTimeMillis();
186     final boolean result;
187     try {
188       result = !ActionUtil.performDumbAwareUpdate(action, e, false);
189     }
190     catch (ProcessCanceledException ex) {
191       throw ex;
192     }
193     catch (Throwable exc) {
194       handleUpdateException(action, presentation, exc);
195       return false;
196     }
197     long endTime = System.currentTimeMillis();
198     if (endTime - startTime > 10 && LOG.isDebugEnabled()) {
199       LOG.debug("Action " + action + ": updated in " + (endTime-startTime) + " ms");
200     }
201     return result;
202   }
203
204   private static boolean hasVisibleChildren(ActionGroup group, PresentationFactory factory, DataContext context, String place) {
205     return hasChildrenWithState(group, factory, context, place, true, false);
206   }
207
208   private static boolean hasEnabledChildren(ActionGroup group, PresentationFactory factory, DataContext context, String place) {
209     return hasChildrenWithState(group, factory, context, place, false, true);
210   }
211
212   private static boolean hasChildrenWithState(ActionGroup group,
213                                               PresentationFactory factory,
214                                               DataContext context,
215                                               String place,
216                                               boolean checkVisible,
217                                               boolean checkEnabled) {
218     //noinspection InstanceofIncompatibleInterface
219     if (group instanceof AlwaysVisibleActionGroup) {
220       return true;
221     }
222
223     AnActionEvent event = new AnActionEvent(null, context, place, factory.getPresentation(group), ActionManager.getInstance(), 0);
224     event.setInjectedContext(group.isInInjectedContext());
225     for (AnAction anAction : group.getChildren(event)) {
226       if (anAction == null) {
227         LOG.error("Null action found in group " + group + ", " + factory.getPresentation(group));
228         continue;
229       }
230       if (anAction instanceof Separator) {
231         continue;
232       }
233       final Project project = CommonDataKeys.PROJECT.getData(context);
234       if (project != null && DumbService.getInstance(project).isDumb() && !anAction.isDumbAware()) {
235         continue;
236       }
237
238       final Presentation presentation = factory.getPresentation(anAction);
239       updateGroupChild(context, place, anAction, presentation);
240       if (anAction instanceof ActionGroup) {
241         ActionGroup childGroup = (ActionGroup)anAction;
242
243         // popup menu must be visible itself
244         if (childGroup.isPopup()) {
245           if ((checkVisible && !presentation.isVisible()) || (checkEnabled && !presentation.isEnabled())) {
246             continue;
247           }
248         }
249
250         if (hasChildrenWithState(childGroup, factory, context, place, checkVisible, checkEnabled)) {
251           return true;
252         }
253       }
254       else if ((checkVisible && presentation.isVisible()) || (checkEnabled && presentation.isEnabled())) {
255         return true;
256       }
257     }
258
259     return false;
260   }
261
262   public static void updateGroupChild(DataContext context, String place, AnAction anAction, final Presentation presentation) {
263     AnActionEvent event1 = new AnActionEvent(null, context, place, presentation, ActionManager.getInstance(), 0);
264     event1.setInjectedContext(anAction.isInInjectedContext());
265     doUpdate(anAction, event1, presentation);
266   }
267
268   public static void fillMenu(@NotNull final ActionGroup group,
269                               final JComponent component,
270                               final boolean enableMnemonics,
271                               final PresentationFactory presentationFactory,
272                               @NotNull DataContext context,
273                               final String place,
274                               final boolean isWindowMenu,
275                               final boolean mayDataContextBeInvalid){
276     final ActionCallback menuBuilt = new ActionCallback();
277     final boolean checked = group instanceof CheckedActionGroup;
278
279     final ArrayList<AnAction> list = new ArrayList<AnAction>();
280     expandActionGroup(group, list, presentationFactory, context, place, ActionManager.getInstance());
281
282     final boolean fixMacScreenMenu = SystemInfo.isMacSystemMenu && isWindowMenu && Registry.is("actionSystem.mac.screenMenuNotUpdatedFix");
283     final ArrayList<Component> children = new ArrayList<Component>();
284
285     for (int i = 0, size = list.size(); i < size; i++) {
286       final AnAction action = list.get(i);
287       if (action instanceof Separator) {
288         final String text = ((Separator)action).getText();
289         if (!StringUtil.isEmpty(text) || (i > 0 && i < size - 1)) {
290           component.add(new JPopupMenu.Separator() {
291             private final JMenuItem myMenu = !StringUtil.isEmpty(text) ? new JMenuItem(text) : null;
292             @Override
293             public Insets getInsets() {
294               final Insets insets = super.getInsets();
295               final boolean fix = UIUtil.isUnderGTKLookAndFeel() &&
296                                   getBorder() != null &&
297                                   insets.top + insets.bottom == 0;
298               return fix ? new Insets(2, insets.left, 3, insets.right) : insets;  // workaround for Sun bug #6636964
299             }
300
301             @Override
302             public void doLayout() {
303               super.doLayout();
304               if (myMenu != null) {
305                 myMenu.setBounds(getBounds());
306               }
307             }
308
309             @Override
310             protected void paintComponent(Graphics g) {
311               if (UIUtil.isUnderWindowsClassicLookAndFeel() || UIUtil.isUnderDarcula() || UIUtil.isUnderWindowsLookAndFeel()) {
312                 g.setColor(component.getBackground());
313                 g.fillRect(0, 0, getWidth(), getHeight());
314               }
315               if (myMenu != null) {
316                 myMenu.paint(g);
317               } else {
318                 super.paintComponent(g);
319               }
320             }
321
322             @Override
323             public Dimension getPreferredSize() {
324               return myMenu != null ? myMenu.getPreferredSize() : super.getPreferredSize();
325             }
326           });
327         }
328       }
329       else if (action instanceof ActionGroup &&
330                !(((ActionGroup)action).canBePerformed(context) &&
331                  !hasVisibleChildren((ActionGroup)action, presentationFactory, context, place))) {
332         ActionMenu menu = new ActionMenu(context, place, (ActionGroup)action, presentationFactory, enableMnemonics, false);
333         component.add(menu);
334         children.add(menu);
335       }
336       else {
337         final ActionMenuItem each =
338           new ActionMenuItem(action, presentationFactory.getPresentation(action), place, context, enableMnemonics, !fixMacScreenMenu, checked);
339         component.add(each);
340         children.add(each);
341       }
342     }
343
344     if (list.isEmpty()) {
345       final ActionMenuItem each =
346         new ActionMenuItem(EMPTY_MENU_FILLER, presentationFactory.getPresentation(EMPTY_MENU_FILLER), place, context, enableMnemonics,
347                            !fixMacScreenMenu, checked);
348       component.add(each);
349       children.add(each);
350     }
351
352     if (fixMacScreenMenu) {
353       //noinspection SSBasedInspection
354       SwingUtilities.invokeLater(new Runnable() {
355         public void run() {
356           for (Component each : children) {
357             if (each.getParent() != null && each instanceof ActionMenuItem) {
358               ((ActionMenuItem)each).prepare();
359             }
360           }
361           menuBuilt.setDone();
362         }
363       });
364     }
365     else {
366       menuBuilt.setDone();
367     }
368
369     menuBuilt.doWhenDone(new Runnable() {
370       public void run() {
371         if (!mayDataContextBeInvalid) return;
372
373         if (IdeFocusManager.getInstance(null).isFocusBeingTransferred()) {
374           IdeFocusManager.getInstance(null).doWhenFocusSettlesDown(new Runnable() {
375             public void run() {
376               if (!component.isShowing()) return;
377
378               DataContext context = DataManager.getInstance().getDataContext();
379               expandActionGroup(group, new ArrayList<AnAction>(), presentationFactory, context, place, ActionManager.getInstance());
380
381               for (Component each : children) {
382                 if (each instanceof ActionMenuItem) {
383                   ((ActionMenuItem)each).updateContext(context);
384                 } else if (each instanceof ActionMenu) {
385                   ((ActionMenu)each).updateContext(context);
386                 }
387               }
388             }
389           });
390         }
391       }
392     });
393
394   }
395 }