001: /*
002: * JMouseComboBox.java - bugfix class for JComboBox
003: * Copyright (C) 2001 Dirk Moebius
004: *
005: * jEdit buffer options:
006: * :tabSize=4:indentSize=4:noTabs=false:maxLineLen=0:
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022: package org.acm.seguin.ide.common.options;
023:
024: import java.awt.event.MouseEvent;
025: import java.awt.event.MouseListener;
026: import java.util.Vector;
027: import javax.swing.ComboBoxModel;
028: import javax.swing.JComboBox;
029:
030: /**
031: * This is a combo-box that allows listeners to be informed of mouse entered and
032: * mouse exited events. Note that other mouse events besides MOUSE_ENTERED and
033: * MOUSE_EXITED still do <i>not</i> get delivered. This is because sending a MOUSE_PRESSED,
034: * MOUSE_CLICKED or MOUSE_RELEASED event would cause the combo box popup to be
035: * hidden immediately after it has been shown, resulting in that it would not be
036: * shown at all. This class was created as a fix/workaround for Swing bug #4144505.
037: *
038: *@author Mike Atkinson (<a href="mailto:javastyle@ladyshot.demon.co.uk">Mike@ladyshot.demon.co.uk
039: * </a>)
040: *@author Dirk Moebius (<a href="mailto:dmoebius@gmx.net">dmoebius@gmx.net</a>
041: * )
042: *@version $Version: $
043: *@since 1.0
044: */
045: public class JMouseComboBox extends JComboBox implements MouseListener {
046:
047: /**
048: * Creates a JMouseComboBox with a default data model. The default data model
049: * is an empty list of objects. Use <code>addItem</code> to add items.
050: */
051: public JMouseComboBox() {
052: super ();
053: initialize();
054: }
055:
056: /**
057: * Creates a JMouseComboBox that contains the elements in the specified array.
058: *
059: *@param items Description of Parameter
060: */
061: public JMouseComboBox(Object[] items) {
062: super (items);
063: initialize();
064: }
065:
066: /**
067: * Creates a JMouseComboBox that takes its items from an existing <code>ComboBoxModel</code>
068: * .
069: *
070: *@param aModel the ComboBoxModel that provides the displayed list of items
071: */
072: public JMouseComboBox(ComboBoxModel aModel) {
073: super (aModel);
074: initialize();
075: }
076:
077: /**
078: * Creates a JMouseComboBox that contains the elements in the specified Vector.
079: *
080: *@param items Description of Parameter
081: */
082: public JMouseComboBox(Vector items) {
083: super (items);
084: initialize();
085: }
086:
087: public void mouseClicked(MouseEvent e) {
088: createMouseEvent(e);
089: }
090:
091: public void mouseEntered(MouseEvent e) {
092: createMouseEvent(e);
093: }
094:
095: public void mouseExited(MouseEvent e) {
096: createMouseEvent(e);
097: }
098:
099: public void mousePressed(MouseEvent e) {
100: createMouseEvent(e);
101: }
102:
103: public void mouseReleased(MouseEvent e) {
104: createMouseEvent(e);
105: }
106:
107: /**
108: * If something else (one of its children) created the mouse event
109: * then pretend it came from here.
110: *
111: *@param e The mouse event.
112: */
113: private void createMouseEvent(MouseEvent e) {
114: if (e.getSource() != this ) {
115: int id = e.getID();
116: switch (id) {
117: case MouseEvent.MOUSE_PRESSED:
118: case MouseEvent.MOUSE_RELEASED:
119: case MouseEvent.MOUSE_CLICKED:
120: // cannot deliver these events, because it would cause the
121: // popup to be hidden.
122: break;
123: case MouseEvent.MOUSE_ENTERED:
124: case MouseEvent.MOUSE_EXITED:
125: // create new event from the old one, with this as source:
126: MouseEvent newEvt = new MouseEvent(this , e.getID(), e
127: .getWhen(), e.getModifiers(), e.getX(), e
128: .getY(), e.getClickCount(), e.isPopupTrigger());
129: // send the event to the event queue:
130: processMouseEvent(newEvt);
131: break;
132: }
133: }
134: }
135:
136: /**
137: * Initialize the class.
138: */
139: private void initialize() {
140: setName("JMouseComboBox");
141: for (int i = 0; i < getComponentCount(); i++) {
142: getComponent(i).addMouseListener(this);
143: }
144: }
145:
146: }
|