You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AJButtonMenuCombo.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v 2.0
  7. * which accompanies this distribution and is available at
  8. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * Helen Hawkins Converted to new interface (bug 148190)
  13. * ******************************************************************/
  14. package org.aspectj.ajde.ui.swing;
  15. import java.awt.BorderLayout;
  16. import java.awt.Dimension;
  17. import java.awt.event.ActionEvent;
  18. import java.awt.event.ActionListener;
  19. import java.awt.event.MouseAdapter;
  20. import java.awt.event.MouseEvent;
  21. import javax.swing.BorderFactory;
  22. import javax.swing.Icon;
  23. import javax.swing.JButton;
  24. import javax.swing.JPanel;
  25. import javax.swing.JPopupMenu;
  26. import org.aspectj.ajde.Ajde;
  27. public class AJButtonMenuCombo extends JPanel {
  28. private static final long serialVersionUID = -4866207530403336160L;
  29. private JButton mainButton;
  30. private JButton popupButton;
  31. private JPopupMenu menu;
  32. // private boolean depressable = false;
  33. private boolean isPressed = false;
  34. public AJButtonMenuCombo(String name,
  35. String toolTipText,
  36. Icon icon,
  37. JPopupMenu menu,
  38. boolean depressable) {
  39. this.menu = menu;
  40. // this.depressable = depressable;
  41. mainButton = new JButton();
  42. mainButton.setIcon(icon);
  43. mainButton.setBorder(AjdeWidgetStyles.DEFAULT_BORDER);
  44. mainButton.setToolTipText(toolTipText);
  45. mainButton.setPreferredSize(new Dimension(22, 20));
  46. mainButton.setMinimumSize(new Dimension(22, 20));
  47. mainButton.setMaximumSize(new Dimension(22, 20));
  48. popupButton = new JButton();
  49. popupButton.setIcon(Ajde.getDefault().getIconRegistry().getPopupIcon());
  50. popupButton.setBorder(BorderFactory.createEmptyBorder());
  51. popupButton.setToolTipText(toolTipText);
  52. popupButton.setPreferredSize(new Dimension(13, 20));
  53. popupButton.setMinimumSize(new Dimension(13, 20));
  54. popupButton.setMaximumSize(new Dimension(13, 20));
  55. PopupListener popupListener = new PopupListener(mainButton);
  56. if (depressable) {
  57. mainButton.addActionListener(new ButtonActionListener());
  58. } else {
  59. mainButton.addMouseListener(popupListener);
  60. }
  61. popupButton.addActionListener(new java.awt.event.ActionListener() {
  62. public void actionPerformed(ActionEvent e) {
  63. popupButton.setBorder(null);
  64. }
  65. });
  66. BorderUpdateListener borderUpdateListner = new BorderUpdateListener();
  67. mainButton.addMouseListener(borderUpdateListner);
  68. popupButton.addMouseListener(borderUpdateListner);
  69. popupButton.addMouseListener(popupListener);
  70. this.setLayout(new BorderLayout());
  71. this.add(mainButton, BorderLayout.CENTER);
  72. this.add(popupButton, BorderLayout.EAST);
  73. this.setMinimumSize(new Dimension(35, 20));
  74. this.setMaximumSize(new Dimension(35, 20));
  75. }
  76. class ButtonActionListener implements ActionListener {
  77. public void actionPerformed(ActionEvent e) {
  78. if (isPressed) {
  79. mainButton.setBorder(AjdeWidgetStyles.DEFAULT_BORDER);
  80. isPressed = false;
  81. } else {
  82. mainButton.setBorder(AjdeWidgetStyles.LOWERED_BEVEL_BORDER);
  83. isPressed = true;
  84. }
  85. }
  86. }
  87. class BorderUpdateListener extends MouseAdapter {
  88. public void mouseEntered(MouseEvent e) {
  89. popupButton.setBorder(AjdeWidgetStyles.RAISED_BEVEL_BORDER);
  90. mainButton.setBorder(AjdeWidgetStyles.RAISED_BEVEL_BORDER);
  91. }
  92. public void mouseExited(MouseEvent e) {
  93. popupButton.setBorder(AjdeWidgetStyles.DEFAULT_BORDER);
  94. if (isPressed) {
  95. mainButton.setBorder(AjdeWidgetStyles.LOWERED_BEVEL_BORDER);
  96. } else {
  97. mainButton.setBorder(AjdeWidgetStyles.DEFAULT_BORDER);
  98. }
  99. }
  100. }
  101. class PopupListener extends MouseAdapter {
  102. private JButton button;
  103. public PopupListener(JButton button) {
  104. this.button = button;
  105. }
  106. public void mousePressed(MouseEvent e) {
  107. maybeShowPopup(e);
  108. }
  109. public void mouseReleased(MouseEvent e) {
  110. maybeShowPopup(e);
  111. }
  112. private void maybeShowPopup(MouseEvent e) {
  113. menu.show(e.getComponent(), button.getX(), button.getY() + popupButton.getHeight());
  114. }
  115. }
  116. public void setEnabled(boolean enabled) {
  117. mainButton.setEnabled(enabled);
  118. popupButton.setEnabled(enabled);
  119. }
  120. public void setMenu(JPopupMenu menu) {
  121. this.menu = menu;
  122. this.repaint();
  123. }
  124. }