/platform/platform-api/src/com/intellij/openapi/actionSystem/ex/ActionUtil.java

https://bitbucket.org/nbargnesi/idea · Java · 147 lines · 104 code · 19 blank · 24 comment · 37 complexity · 600bc71365c87b36cae652ba61b2b275 MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 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.ex;
  17. import com.intellij.openapi.actionSystem.*;
  18. import com.intellij.openapi.application.ApplicationNamesInfo;
  19. import com.intellij.openapi.project.DumbAware;
  20. import com.intellij.openapi.project.DumbService;
  21. import com.intellij.openapi.project.IndexNotReadyException;
  22. import com.intellij.openapi.project.Project;
  23. import com.intellij.openapi.util.text.StringUtil;
  24. import org.jetbrains.annotations.NonNls;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. public class ActionUtil {
  28. @NonNls private static final String WAS_ENABLED_BEFORE_DUMB = "WAS_ENABLED_BEFORE_DUMB";
  29. @NonNls public static final String WOULD_BE_ENABLED_IF_NOT_DUMB_MODE = "WOULD_BE_ENABLED_IF_NOT_DUMB_MODE";
  30. @NonNls private static final String WOULD_BE_VISIBLE_IF_NOT_DUMB_MODE = "WOULD_BE_VISIBLE_IF_NOT_DUMB_MODE";
  31. private ActionUtil() {
  32. }
  33. public static void showDumbModeWarning(AnActionEvent... events) {
  34. Project project = null;
  35. List<String> actionNames = new ArrayList<String>();
  36. for (final AnActionEvent event : events) {
  37. final String s = event.getPresentation().getText();
  38. if (StringUtil.isNotEmpty(s)) {
  39. actionNames.add(s);
  40. }
  41. final Project _project = PlatformDataKeys.PROJECT.getData(event.getDataContext());
  42. if (_project != null && project == null) {
  43. project = _project;
  44. }
  45. }
  46. if (project == null) {
  47. return;
  48. }
  49. String message;
  50. final String beAvailableUntil = " available while " + ApplicationNamesInfo.getInstance().getProductName() + " is updating indices";
  51. if (actionNames.isEmpty()) {
  52. message = "This action is not" + beAvailableUntil;
  53. } else if (actionNames.size() == 1) {
  54. message = "'" + actionNames.get(0) + "' action is not" + beAvailableUntil;
  55. } else {
  56. message = "None of the following actions are" + beAvailableUntil + ": " + StringUtil.join(actionNames, ", ");
  57. }
  58. DumbService.getInstance(project).showDumbModeNotification(message);
  59. }
  60. /**
  61. * @param action action
  62. * @param e action event
  63. * @param beforeActionPerformed whether to call
  64. * {@link com.intellij.openapi.actionSystem.AnAction#beforeActionPerformedUpdate(com.intellij.openapi.actionSystem.AnActionEvent)}
  65. * or
  66. * {@link com.intellij.openapi.actionSystem.AnAction#update(com.intellij.openapi.actionSystem.AnActionEvent)}
  67. * @return true if update tried to access indices in dumb mode
  68. */
  69. public static boolean performDumbAwareUpdate(AnAction action, AnActionEvent e, boolean beforeActionPerformed) {
  70. final Presentation presentation = e.getPresentation();
  71. final Boolean wasEnabledBefore = (Boolean)presentation.getClientProperty(WAS_ENABLED_BEFORE_DUMB);
  72. final Project project = PlatformDataKeys.PROJECT.getData(e.getDataContext());
  73. final boolean dumbMode = project != null && DumbService.getInstance(project).isDumb();
  74. if (wasEnabledBefore != null && !dumbMode) {
  75. presentation.putClientProperty(WAS_ENABLED_BEFORE_DUMB, null);
  76. presentation.setEnabled(wasEnabledBefore.booleanValue());
  77. presentation.setVisible(true);
  78. }
  79. final boolean enabledBeforeUpdate = presentation.isEnabled();
  80. final boolean notAllowed = dumbMode && !action.isDumbAware();
  81. try {
  82. if (beforeActionPerformed) {
  83. action.beforeActionPerformedUpdate(e);
  84. }
  85. else {
  86. action.update(e);
  87. }
  88. presentation.putClientProperty(WOULD_BE_ENABLED_IF_NOT_DUMB_MODE, notAllowed && presentation.isEnabled());
  89. presentation.putClientProperty(WOULD_BE_VISIBLE_IF_NOT_DUMB_MODE, notAllowed && presentation.isVisible());
  90. }
  91. catch (IndexNotReadyException e1) {
  92. if (notAllowed) {
  93. return true;
  94. }
  95. throw e1;
  96. }
  97. finally {
  98. if (notAllowed) {
  99. if (wasEnabledBefore == null) {
  100. presentation.putClientProperty(WAS_ENABLED_BEFORE_DUMB, enabledBeforeUpdate);
  101. }
  102. presentation.setEnabled(false);
  103. }
  104. }
  105. return false;
  106. }
  107. public static boolean lastUpdateAndCheckDumb(AnAction action, AnActionEvent e, boolean visibilityMatters) {
  108. performDumbAwareUpdate(action, e, true);
  109. final Project project = PlatformDataKeys.PROJECT.getData(e.getDataContext());
  110. if (project != null && DumbService.getInstance(project).isDumb() && !action.isDumbAware()) {
  111. if (Boolean.FALSE.equals(e.getPresentation().getClientProperty(WOULD_BE_ENABLED_IF_NOT_DUMB_MODE))) {
  112. return false;
  113. }
  114. if (visibilityMatters && Boolean.FALSE.equals(e.getPresentation().getClientProperty(WOULD_BE_VISIBLE_IF_NOT_DUMB_MODE))) {
  115. return false;
  116. }
  117. showDumbModeWarning(e);
  118. return false;
  119. }
  120. if (!e.getPresentation().isEnabled()) {
  121. return false;
  122. }
  123. if (visibilityMatters && !e.getPresentation().isVisible()) {
  124. return false;
  125. }
  126. return true;
  127. }
  128. }