/bundles/plugins-trunk/AStylePlugin/org/gjt/sp/jedit/gui/JMouseComboBox.java

# · Java · 166 lines · 74 code · 29 blank · 63 comment · 3 complexity · 36c2e92f28da12bc7226a46df7de4099 MD5 · raw file

  1. /*
  2. * JMouseComboBox.java - bugfix class for JComboBox
  3. * Copyright (C) 2001 Dirk Moebius
  4. *
  5. * :tabSize=4:indentSize=4:noTabs=false:maxLineLen=0:
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package org.gjt.sp.jedit.gui;
  22. import javax.swing.*;
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import org.gjt.sp.util.Log;
  26. /**
  27. * This is a combo-box that allows listeners to be informed of mouse
  28. * entered and mouse exited events.
  29. *
  30. * Note that other mouse events besides MOUSE_ENTERED and MOUSE_EXITED still
  31. * do <i>not</i> get delivered. This is because sending a MOUSE_PRESSED,
  32. * MOUSE_CLICKED or MOUSE_RELEASED event would cause the combo box popup to
  33. * be hidden immediately after it has been shown, resulting in that it
  34. * would not be shown at all.
  35. *
  36. * This class was created as a fix/workaround for Swing bug #4144505.
  37. *
  38. * @author Dirk Moebius (<a href="mailto:dmoebius@gmx.net">dmoebius@gmx.net</a>)
  39. * @version JMouseComboBox 1.0
  40. */
  41. public class JMouseComboBox extends JComboBox implements MouseListener {
  42. /**
  43. * Creates a JMouseComboBox with a default data model.
  44. *
  45. * The default data model is an empty list of objects.
  46. * Use <code>addItem</code> to add items.
  47. */
  48. public JMouseComboBox()
  49. {
  50. super();
  51. initialize();
  52. }
  53. /**
  54. * Creates a JMouseComboBox that contains the elements in the specified
  55. * array.
  56. */
  57. public JMouseComboBox(Object[] items)
  58. {
  59. super(items);
  60. initialize();
  61. }
  62. /**
  63. * Creates a JMouseComboBox that takes its items from an existing
  64. * <code>ComboBoxModel</code>.
  65. *
  66. * @param aModel the ComboBoxModel that provides the displayed
  67. * list of items
  68. */
  69. public JMouseComboBox(ComboBoxModel aModel)
  70. {
  71. super(aModel);
  72. initialize();
  73. }
  74. /**
  75. * Creates a JMouseComboBox that contains the elements in the specified
  76. * Vector.
  77. */
  78. public JMouseComboBox(java.util.Vector items)
  79. {
  80. super(items);
  81. initialize();
  82. }
  83. /**
  84. * Initialize the class.
  85. */
  86. private void initialize()
  87. {
  88. setName("JMouseComboBox");
  89. for (int i = 0; i < getComponentCount(); i++)
  90. getComponent(i).addMouseListener(this);
  91. }
  92. private void createMouseEvent(MouseEvent e)
  93. {
  94. if (e.getSource() != this) {
  95. int id = e.getID();
  96. switch(id) {
  97. case MouseEvent.MOUSE_PRESSED:
  98. case MouseEvent.MOUSE_RELEASED:
  99. case MouseEvent.MOUSE_CLICKED:
  100. // cannot deliver these events, because it would cause the
  101. // popup to be hidden.
  102. break;
  103. case MouseEvent.MOUSE_ENTERED:
  104. case MouseEvent.MOUSE_EXITED:
  105. // create new event from the old one, with this as source:
  106. MouseEvent newEvt = new MouseEvent(
  107. this, e.getID(), e.getWhen(), e.getModifiers(),
  108. e.getX(), e.getY(), e.getClickCount(),
  109. e.isPopupTrigger()
  110. );
  111. // send the event to the event queue:
  112. processMouseEvent(newEvt);
  113. break;
  114. }
  115. }
  116. }
  117. public void mouseClicked(MouseEvent e)
  118. {
  119. createMouseEvent(e);
  120. }
  121. public void mouseEntered(MouseEvent e)
  122. {
  123. createMouseEvent(e);
  124. }
  125. public void mouseExited(MouseEvent e)
  126. {
  127. createMouseEvent(e);
  128. }
  129. public void mousePressed(MouseEvent e)
  130. {
  131. createMouseEvent(e);
  132. }
  133. public void mouseReleased(MouseEvent e)
  134. {
  135. createMouseEvent(e);
  136. }
  137. }