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

https://bitbucket.org/nbargnesi/idea · Java · 77 lines · 45 code · 12 blank · 20 comment · 2 complexity · 2e4f4eb09a36632956313c79303271e7 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.ide.DataManager;
  18. import com.intellij.openapi.actionSystem.*;
  19. import com.intellij.util.ui.UIUtil;
  20. import javax.swing.*;
  21. import java.awt.event.ActionEvent;
  22. import java.awt.event.ActionListener;
  23. /**
  24. * @author max
  25. */
  26. public abstract class CheckboxAction extends ToggleAction implements CustomComponentAction {
  27. protected CheckboxAction() {}
  28. protected CheckboxAction(final String text) {
  29. super(text);
  30. }
  31. protected CheckboxAction(final String text, final String description, final Icon icon) {
  32. super(text, description, icon);
  33. }
  34. public JComponent createCustomComponent(Presentation presentation) {
  35. // this component cannot be stored right here because of action system architecture:
  36. // one action can be shown on multiple toolbars simultaneously
  37. JCheckBox checkBox = new JCheckBox(presentation.getText());
  38. checkBox.setOpaque(false);
  39. checkBox.setToolTipText(presentation.getDescription());
  40. checkBox.setMnemonic(presentation.getMnemonic());
  41. checkBox.setDisplayedMnemonicIndex(presentation.getDisplayedMnemonicIndex());
  42. checkBox.addActionListener(new ActionListener() {
  43. public void actionPerformed(ActionEvent e) {
  44. JCheckBox checkBox = (JCheckBox)e.getSource();
  45. ActionToolbar actionToolbar = UIUtil.getParentOfType(ActionToolbar.class, checkBox);
  46. DataContext dataContext =
  47. actionToolbar != null ? actionToolbar.getToolbarDataContext() : DataManager.getInstance().getDataContext(checkBox);
  48. CheckboxAction.this.actionPerformed(new AnActionEvent(null, dataContext,
  49. ActionPlaces.UNKNOWN, CheckboxAction.this.getTemplatePresentation(),
  50. ActionManager.getInstance(), 0));
  51. }
  52. });
  53. return checkBox;
  54. }
  55. public void update(final AnActionEvent e) {
  56. super.update(e);
  57. Object property = e.getPresentation().getClientProperty(CUSTOM_COMPONENT_PROPERTY);
  58. if (property instanceof JCheckBox) {
  59. JCheckBox checkBox = (JCheckBox)property;
  60. checkBox.setSelected(Boolean.TRUE.equals(e.getPresentation().getClientProperty(SELECTED_PROPERTY)));
  61. checkBox.setEnabled(e.getPresentation().isEnabled());
  62. checkBox.setVisible(e.getPresentation().isVisible());
  63. }
  64. }
  65. }