1 /***
2 * RepaintingMouseHandler.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2004 Denis Bregeon
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * contact information: dbregeon@sourceforge.net
25 */
26 package org.jcreme.swing;
27
28 import java.awt.Component;
29 import java.awt.event.MouseEvent;
30 import java.awt.event.MouseListener;
31 import java.awt.event.MouseMotionListener;
32 import java.util.Iterator;
33 import java.util.Vector;
34
35 /***
36 * This Listener provokes a repaint of the registered components when a
37 * MouseEvent is received.
38 *
39 * @author $Author: dbregeon $
40 * @version $Revision: 1.1 $
41 */
42 public class RepaintingMouseHandler implements MouseMotionListener,
43 MouseListener {
44
45 /***
46 * The Components that will be repainted.
47 */
48 private final Vector repaintedComponents = new Vector();
49
50 /***
51 * Enables to register a Component for repainting.
52 *
53 * @param c
54 * the Component to register.
55 */
56 public void addRepaintedComponent(Component c) {
57 if (c != null) {
58 this.repaintedComponents.add(c);
59 }
60 }
61
62 /***
63 * Enables to unregister a Component for repainting.
64 *
65 * @param c
66 * the Component to unregister.
67 */
68 public void removeRepaintedComponent(Component c) {
69 if (c != null) {
70 this.repaintedComponents.remove(c);
71 }
72 }
73
74 /***
75 * Gives access to the Components registered for repainting.
76 *
77 * @return the list of Components that are repainted by this listener.
78 */
79 public Component[] getRepaintedComponents() {
80 return (Component[]) this.repaintedComponents.toArray(new Component[0]);
81 }
82
83 /***
84 * Controls the repaint of the registered Components.
85 */
86 protected void repaint() {
87 Iterator iter = this.repaintedComponents.iterator();
88 while (iter.hasNext()) {
89 ((Component) iter.next()).repaint();
90 }
91 }
92
93 /***
94 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
95 */
96 public void mouseEntered(MouseEvent e) {
97 repaint();
98 }
99
100 /***
101 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
102 */
103 public void mouseExited(MouseEvent e) {
104 repaint();
105 }
106
107 /***
108 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
109 */
110 public void mouseDragged(MouseEvent e) {
111 repaint();
112 }
113
114 /***
115 * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
116 */
117 public void mouseMoved(MouseEvent e) {
118 repaint();
119 }
120
121 /***
122 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
123 */
124 public void mouseClicked(MouseEvent e) {
125 repaint();
126 }
127
128 /***
129 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
130 */
131 public void mousePressed(MouseEvent e) {
132 repaint();
133 }
134
135 /***
136 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
137 */
138 public void mouseReleased(MouseEvent e) {
139 repaint();
140 }
141 }