PageRenderTime 29ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/CommonControls/common/gui/JMouseComboBox.java

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