/plugins/git4idea/src/git4idea/history/wholeTree/UsersFilterAction.java

https://bitbucket.org/nbargnesi/idea · Java · 242 lines · 206 code · 16 blank · 20 comment · 19 complexity · d73359af05e9c7c5ffb9bd4e57d76f6b MD5 · raw file

  1. /*
  2. * Copyright 2000-2011 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 git4idea.history.wholeTree;
  17. import com.intellij.codeInsight.completion.CompletionResultSet;
  18. import com.intellij.codeInsight.lookup.LookupElementBuilder;
  19. import com.intellij.openapi.actionSystem.AnAction;
  20. import com.intellij.openapi.actionSystem.AnActionEvent;
  21. import com.intellij.openapi.actionSystem.CommonShortcuts;
  22. import com.intellij.openapi.components.ServiceManager;
  23. import com.intellij.openapi.fileTypes.FileTypes;
  24. import com.intellij.openapi.keymap.KeymapUtil;
  25. import com.intellij.openapi.project.DumbAwareAction;
  26. import com.intellij.openapi.project.Project;
  27. import com.intellij.openapi.ui.popup.ComponentPopupBuilder;
  28. import com.intellij.openapi.ui.popup.JBPopup;
  29. import com.intellij.openapi.ui.popup.JBPopupFactory;
  30. import com.intellij.openapi.util.Comparing;
  31. import com.intellij.openapi.util.text.StringUtil;
  32. import com.intellij.ui.EditorCustomization;
  33. import com.intellij.ui.EditorTextField;
  34. import com.intellij.ui.EditorTextFieldProvider;
  35. import com.intellij.util.Consumer;
  36. import com.intellij.util.TextFieldCompletionProvider;
  37. import com.intellij.util.TextFieldCompletionProviderDumbAware;
  38. import git4idea.history.NewGitUsersComponent;
  39. import org.jetbrains.annotations.NotNull;
  40. import javax.swing.*;
  41. import javax.swing.border.CompoundBorder;
  42. import java.awt.*;
  43. import java.util.Collections;
  44. import java.util.List;
  45. /**
  46. * @author irengrig
  47. * Date: 1/20/11
  48. * Time: 7:35 PM
  49. */
  50. public class UsersFilterAction extends BasePopupAction {
  51. public static final String ALL = "All";
  52. public static final String USER = "User:";
  53. private final UserFilterI myUserFilterI;
  54. private AnAction myAllAction;
  55. private AnAction mySelectMe;
  56. private AnAction mySelect;
  57. private String myCurrentText;
  58. private final NewGitUsersComponent myUsers;
  59. private EditorTextField myEditorField;
  60. private JBPopup myPopup;
  61. private ComponentPopupBuilder myComponentPopupBuilder;
  62. private AnAction mySelectOkAction;
  63. private TextFieldCompletionProvider myTextFieldCompletionProvider;
  64. private String myPreselectedUser;
  65. private DumbAwareAction myPresetUserAction;
  66. public UsersFilterAction(final Project project, final UserFilterI userFilterI) {
  67. super(project, USER, "User");
  68. myUserFilterI = userFilterI;
  69. myCurrentText = "";
  70. myUsers = NewGitUsersComponent.getInstance(myProject);
  71. myAllAction = new DumbAwareAction(ALL) {
  72. @Override
  73. public void actionPerformed(AnActionEvent e) {
  74. myLabel.setText(ALL);
  75. myUserFilterI.allSelected();
  76. myCurrentText = "";
  77. myPanel.setToolTipText(USER + " " + ALL);
  78. myPreselectedUser = null;
  79. }
  80. };
  81. mySelectMe = new DumbAwareAction() {
  82. @Override
  83. public void actionPerformed(AnActionEvent e) {
  84. final String meText = getMeText();
  85. myLabel.setText(meText);
  86. myPanel.setToolTipText(USER + " " + meText);
  87. myUserFilterI.meSelected();
  88. myCurrentText = "";
  89. myPreselectedUser = null;
  90. }
  91. @Override
  92. public void update(AnActionEvent e) {
  93. super.update(e);
  94. e.getPresentation().setVisible(myUserFilterI.isMeKnown());
  95. e.getPresentation().setText(getMeText());
  96. }
  97. };
  98. myPresetUserAction = new DumbAwareAction() {
  99. @Override
  100. public void actionPerformed(AnActionEvent e) {
  101. myLabel.setText(myPreselectedUser);
  102. myPanel.setToolTipText(USER + " " + myPreselectedUser);
  103. myUserFilterI.filter(myPreselectedUser);
  104. myCurrentText = myPreselectedUser;
  105. myPreselectedUser = null;
  106. }
  107. @Override
  108. public void update(AnActionEvent e) {
  109. super.update(e);
  110. e.getPresentation().setVisible((! StringUtil.isEmptyOrSpaces(myPreselectedUser)) &&
  111. (! Comparing.equal(myPreselectedUser, myUserFilterI.getMe())));
  112. e.getPresentation().setText(myPreselectedUser);
  113. }
  114. };
  115. createPopup(project);
  116. mySelect = new DumbAwareAction("Select..") {
  117. @Override
  118. public void actionPerformed(AnActionEvent e) {
  119. if (myPopup != null) {
  120. mySelectOkAction.unregisterCustomShortcutSet(myPopup.getContent());
  121. }
  122. myComponentPopupBuilder.setMayBeParent(true);
  123. myPopup = myComponentPopupBuilder.createPopup();
  124. myTextFieldCompletionProvider.apply(myEditorField);
  125. myEditorField.setText(myCurrentText);
  126. final JComponent content = myPopup.getContent();
  127. mySelectOkAction.registerCustomShortcutSet(CommonShortcuts.CTRL_ENTER, content);
  128. final Point point = new Point(0, 0);
  129. SwingUtilities.convertPointToScreen(point, myLabel);
  130. myPopup.setMinimumSize(new Dimension(200, 90));
  131. myPopup.showInScreenCoordinates(myLabel, point);
  132. myPreselectedUser = null;
  133. }
  134. };
  135. myLabel.setText(ALL);
  136. }
  137. private void createPopup(Project project) {
  138. final JPanel panel = new JPanel(new BorderLayout());
  139. final EditorTextFieldProvider service = ServiceManager.getService(project, EditorTextFieldProvider.class);
  140. myEditorField = service.getEditorField(FileTypes.PLAIN_TEXT.getLanguage(), project,
  141. Collections.singletonList(EditorCustomization.Feature.SOFT_WRAP),
  142. Collections.singletonList(EditorCustomization.Feature.SPELL_CHECK));
  143. myEditorField.setBorder(new CompoundBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2), myEditorField.getBorder()));
  144. myEditorField.setText("s");
  145. myEditorField.setText(myCurrentText);
  146. myEditorField.setOneLineMode(false);
  147. panel.add(myEditorField, BorderLayout.CENTER);
  148. myTextFieldCompletionProvider = new TextFieldCompletionProviderDumbAware(true) {
  149. @NotNull
  150. @Override
  151. protected String getPrefix(@NotNull String currentTextPrefix) {
  152. final int text = currentTextPrefix.lastIndexOf(',');
  153. return text == -1 ? currentTextPrefix : currentTextPrefix.substring(text + 1).trim();
  154. }
  155. @Override
  156. protected void addCompletionVariants(@NotNull String text,
  157. int offset,
  158. @NotNull String prefix,
  159. @NotNull CompletionResultSet result) {
  160. final List<String> list = myUsers.get();
  161. if (list != null) {
  162. for (String completionVariant : list) {
  163. final LookupElementBuilder element = LookupElementBuilder.create(completionVariant);
  164. result.addElement(element.withLookupString(completionVariant.toLowerCase()));
  165. }
  166. }
  167. }
  168. };
  169. myComponentPopupBuilder = JBPopupFactory.getInstance().createComponentPopupBuilder(panel, myEditorField)
  170. .setCancelOnClickOutside(true)
  171. .setAdText(KeymapUtil.getShortcutsText(CommonShortcuts.CTRL_ENTER.getShortcuts()) + " to finish")
  172. .setTitle("Specify user names, comma separated")
  173. .setMovable(true)
  174. .setRequestFocus(true).setResizable(true);
  175. mySelectOkAction = new AnAction() {
  176. @Override
  177. public void actionPerformed(AnActionEvent e) {
  178. myPopup.closeOk(e.getInputEvent());
  179. final String newText = myEditorField.getText();
  180. if (Comparing.equal(newText.trim(), myCurrentText.trim())) return;
  181. myCurrentText = newText;
  182. setText(myCurrentText.trim());
  183. myPanel.setToolTipText(USER + " " + myCurrentText);
  184. myUserFilterI.filter(myCurrentText);
  185. }
  186. };
  187. }
  188. private void setText(final String text) {
  189. final String[] pieces = text.split(",");
  190. if (pieces.length == 0) {
  191. myLabel.setText(ALL);
  192. } else if (pieces.length == 1) {
  193. myLabel.setText(pieces[0].trim());
  194. } else {
  195. myLabel.setText(pieces[0].trim() + "+");
  196. }
  197. }
  198. public void setSelectedPresets(final String selected, final boolean meSelected) {
  199. myCurrentText = selected == null ? "" : selected;
  200. if (selected == null) {
  201. myLabel.setText(ALL);
  202. } else if (meSelected) {
  203. myLabel.setText(getMeText(selected));
  204. myPanel.setToolTipText(USER + " " + selected);
  205. } else {
  206. setText(selected);
  207. myPanel.setToolTipText(USER + " " + selected);
  208. }
  209. }
  210. private String getMeText() {
  211. return getMeText(myUserFilterI.getMe());
  212. }
  213. private String getMeText(final String name) {
  214. return "me ( " + StringUtil.shortenTextWithEllipsis(name, 30, 0) + " )";
  215. }
  216. @Override
  217. protected void createActions(Consumer<AnAction> actionConsumer) {
  218. actionConsumer.consume(myPresetUserAction);
  219. actionConsumer.consume(myAllAction);
  220. actionConsumer.consume(mySelectMe);
  221. actionConsumer.consume(mySelect);
  222. }
  223. public void setPreselectedUser(String preselectedUser) {
  224. myPreselectedUser = preselectedUser;
  225. }
  226. }