001: /* SwingML
002: * Copyright (C) 2002 SwingML Team
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: *
019: * Authors:
020: * Ezequiel Cuellar <ecuellar@crosslogic.com>
021: *
022: */
023:
024: package org.swingml.component;
025:
026: import java.awt.event.*;
027: import java.net.*;
028: import java.util.*;
029:
030: import javax.swing.*;
031:
032: import org.swingml.*;
033: import org.swingml.event.*;
034: import org.swingml.model.*;
035:
036: public class JButtonComponent extends JButton implements
037: ActionListener, MouseListener {
038:
039: private class ClickNotifier implements Runnable {
040:
041: private int numberOfClicks = 1;
042: private boolean run = true;
043:
044: public void addClick() {
045: this .numberOfClicks++;
046: }
047:
048: public void run() {
049: while (shouldRun()) {
050: // notify of event
051: if (numberOfClicks == 0 && isPressedDown()) {
052: // fire one event
053: eventHandler.handleEvent(model,
054: Constants.MOUSE_SINGLE_CLICKED,
055: JButtonComponent.this );
056: } else {
057: // fire off an event for each click that happened
058: while (numberOfClicks > 0) {
059: eventHandler.handleEvent(model,
060: Constants.MOUSE_SINGLE_CLICKED,
061: JButtonComponent.this );
062: numberOfClicks--;
063: }
064: }
065:
066: try {
067: // wait a bit
068: Thread.sleep(175);
069: } catch (InterruptedException e) {
070: // do nothing
071: }
072: }
073: }
074:
075: public void setShouldRun(boolean shouldRun) {
076: this .run = shouldRun;
077: }
078:
079: public boolean shouldRun() {
080: return run;
081: }
082: }
083:
084: private Thread aThread;
085: private EventHandler eventHandler = EventHandler.getInstance();
086: private JButtonModel model = null;
087: private ClickNotifier notifier;
088: private boolean pressedDown;
089:
090: public JButtonComponent(JButtonModel aModel) {
091: try {
092: this .model = aModel;
093: super .setVisible(model.isVisible());
094: super .setName(model.getName());
095:
096: if (this .model.getText() != null
097: && this .model.getText().trim().length() > 0) {
098: super .setText(model.getText());
099: }
100:
101: super .setEnabled(this .model.isEnabled());
102: super .setToolTipText(this .model.getTooltip());
103: super .addActionListener(this );
104:
105: if (this .model.getIcon() != null) {
106: URL iconURL = new URL(this .model.getIcon());
107: if (iconURL != null) {
108: super .setIcon(new ImageIcon(iconURL));
109: }
110: }
111:
112: // do we need a mouse listener?
113: if (this .model.getListeners() != null) {
114: Iterator schmiterator = this .model.getListeners()
115: .iterator();
116: while (schmiterator.hasNext()) {
117: ListenerModel listenerModel = (ListenerModel) schmiterator
118: .next();
119: if (listenerModel.getEvent().equalsIgnoreCase(
120: Constants.MOUSE_SINGLE_CLICKED)) {
121: super .addMouseListener(this );
122: break;
123: }
124: }
125: }
126:
127: } catch (Exception e) {
128: // do nothing
129: }
130: }
131:
132: public void actionPerformed(ActionEvent aEvt) {
133: this .eventHandler.handleEvent(this .model,
134: Constants.ACTION_PERFORMED, this );
135: }
136:
137: private boolean isPressedDown() {
138: return pressedDown;
139: }
140:
141: public void mouseClicked(MouseEvent e) {
142: }
143:
144: public void mouseEntered(MouseEvent e) {
145: }
146:
147: public void mouseExited(MouseEvent e) {
148: }
149:
150: public void mousePressed(MouseEvent e) {
151: setPressedDown(true);
152:
153: if (this .notifier == null) {
154: this .notifier = new ClickNotifier();
155: this .aThread = new Thread(this .notifier);
156: this .aThread.setName(this .getClass().getName()
157: + " mouse listener (" + new Date() + ")");
158: this .aThread.start();
159: } else {
160: this .notifier.addClick();
161: this .aThread.interrupt();
162: }
163: }
164:
165: public void mouseReleased(MouseEvent e) {
166: setPressedDown(false);
167:
168: if (this .notifier != null) {
169: this .notifier.setShouldRun(false);
170: this .aThread.interrupt();
171:
172: this .notifier = null;
173: this .aThread = null;
174: }
175: }
176:
177: private void setPressedDown(boolean pressedDown) {
178: this.pressedDown = pressedDown;
179: }
180: }
|