Debian Codegraph @

laf-widget/src/main/java/org/pushingpixels/lafwidget/tabbed/TabHoverPreviewWidget.java

raw
1 /*
2 * Copyright (c) 2005-2010 Laf-Widget Kirill Grouchnikov. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * o Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * o Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * o Neither the name of Laf-Widget Kirill Grouchnikov nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package org.pushingpixels.lafwidget.tabbed;
31
32 import java.awt.event.*;
33
34 import javax.swing.JTabbedPane;
35
36 import org.pushingpixels.lafwidget.*;
37
38 /**
39 * Adds tab preview thumbnail on tab mouse hover.
40 *
41 * @author Kirill Grouchnikov
42 */
43 public class TabHoverPreviewWidget extends LafWidgetAdapter<JTabbedPane> {
44 /**
45 * Mouse listener for rollover effects.
46 */
47 protected MouseRolloverHandler baseRolloverHandler;
48
49 /**
50 * Mouse listener for rollover effects.
51 *
52 * @author Kirill Grouchnikov
53 */
54 protected class MouseRolloverHandler implements MouseListener,
55 MouseMotionListener {
56 /**
57 * Index of previously rolled-over tab.
58 */
59 int prevRolledOver = -1;
60
61 /*
62 * (non-Javadoc)
63 *
64 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
65 */
66 @Override
67 public void mouseClicked(MouseEvent e) {
68 }
69
70 /*
71 * (non-Javadoc)
72 *
73 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
74 */
75 @Override
76 public void mouseDragged(MouseEvent e) {
77 }
78
79 /*
80 * (non-Javadoc)
81 *
82 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
83 */
84 @Override
85 public void mouseEntered(MouseEvent e) {
86 }
87
88 /*
89 * (non-Javadoc)
90 *
91 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
92 */
93 @Override
94 public void mousePressed(MouseEvent e) {
95 TabPreviewWindow.cancelPreviewRequest();
96 }
97
98 /*
99 * (non-Javadoc)
100 *
101 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
102 */
103 @Override
104 public void mouseReleased(MouseEvent e) {
105 }
106
107 /*
108 * (non-Javadoc)
109 *
110 * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
111 */
112 @Override
113 public void mouseMoved(MouseEvent e) {
114 if (e.getSource() != jcomp)
115 return;
116
117 LafWidgetSupport lafSupport = LafWidgetRepository.getRepository()
118 .getLafSupport();
119
120 int currRolledOver = 0;
121
122 try {
123 currRolledOver = lafSupport.getRolloverTabIndex(jcomp);
124 } catch (UnsupportedOperationException uoe) {
125 // Some problems in LAF support - may happen during the LAF
126 // switch when this event is processed after a LAF that doesn't
127 // have the laf-widget support is set. Just ignore it.
128 return;
129 }
130 if (currRolledOver >= jcomp.getTabCount())
131 return;
132
133 if (currRolledOver != jcomp.getSelectedIndex()) {
134 if (currRolledOver == this.prevRolledOver) {
135 if ((currRolledOver >= 0)
136 && (currRolledOver < jcomp.getTabCount())
137 && jcomp.isEnabledAt(currRolledOver)) {
138 TabPreviewWindow.getInstance().postPreviewRequest(
139 jcomp, currRolledOver);
140 }
141 } else {
142 if ((this.prevRolledOver >= 0)
143 && (this.prevRolledOver < jcomp.getTabCount())
144 && jcomp.isEnabledAt(this.prevRolledOver)) {
145 TabPreviewWindow.cancelPreviewRequest();
146 }
147 if ((currRolledOver >= 0)
148 && (currRolledOver < jcomp.getTabCount())
149 && jcomp.isEnabledAt(currRolledOver)) {
150 TabPreviewWindow.getInstance().postPreviewRequest(
151 jcomp, currRolledOver);
152 }
153 }
154 } else {
155 // no preview on the selected tab
156 TabPreviewWindow.cancelPreviewRequest();
157 }
158 this.prevRolledOver = currRolledOver;
159 }
160
161 /*
162 * (non-Javadoc)
163 *
164 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
165 */
166 @Override
167 public void mouseExited(MouseEvent e) {
168 if ((this.prevRolledOver >= 0)
169 && (this.prevRolledOver < jcomp.getTabCount())
170 && jcomp.isEnabledAt(this.prevRolledOver)) {
171 TabPreviewWindow.cancelPreviewRequest();
172 }
173 this.prevRolledOver = -1;
174 }
175 }
176
177 /*
178 * (non-Javadoc)
179 *
180 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#installListeners()
181 */
182 @Override
183 public void installListeners() {
184 this.baseRolloverHandler = new MouseRolloverHandler();
185 this.jcomp.addMouseMotionListener(this.baseRolloverHandler);
186 this.jcomp.addMouseListener(this.baseRolloverHandler);
187 }
188
189 /*
190 * (non-Javadoc)
191 *
192 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#uninstallListeners()
193 */
194 @Override
195 public void uninstallListeners() {
196 if (this.baseRolloverHandler != null) {
197 this.jcomp.removeMouseMotionListener(this.baseRolloverHandler);
198 this.jcomp.removeMouseListener(this.baseRolloverHandler);
199 this.baseRolloverHandler = null;
200 }
201 }
202
203 /*
204 * (non-Javadoc)
205 *
206 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#uninstallUI()
207 */
208 @Override
209 public void uninstallUI() {
210 if (TabPreviewThread.instanceRunning()) {
211 TabPreviewThread.getInstance().cancelTabPreviewRequests(this.jcomp);
212 }
213 TabPreviewWindow.cancelPreviewRequest();
214
215 super.uninstallUI();
216 }
217
218 /*
219 * (non-Javadoc)
220 *
221 * @see org.pushingpixels.lafwidget.LafWidget#requiresCustomLafSupport()
222 */
223 @Override
224 public boolean requiresCustomLafSupport() {
225 return false;
226 }
227 }