Debian Codegraph @

laf-widget/src/main/java/org/pushingpixels/lafwidget/tabbed/TabPagerWidget.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.Component;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.InputEvent;
36 import java.awt.event.KeyEvent;
37 import java.awt.event.MouseAdapter;
38 import java.awt.event.MouseEvent;
39 import java.awt.event.MouseListener;
40 import java.awt.event.MouseWheelListener;
41 import java.beans.PropertyChangeEvent;
42 import java.beans.PropertyChangeListener;
43
44 import javax.swing.AbstractAction;
45 import javax.swing.InputMap;
46 import javax.swing.JComponent;
47 import javax.swing.JTabbedPane;
48 import javax.swing.KeyStroke;
49 import javax.swing.SwingUtilities;
50
51 import org.pushingpixels.lafwidget.LafWidget;
52 import org.pushingpixels.lafwidget.LafWidgetAdapter;
53 import org.pushingpixels.lafwidget.LafWidgetUtilities2;
54 import org.pushingpixels.lafwidget.Resettable;
55
56 /**
57 * Adds tab pager to tabbed panes.
58 *
59 * @author Kirill Grouchnikov
60 */
61 public class TabPagerWidget extends LafWidgetAdapter<JTabbedPane> implements
62 Resettable {
63 /**
64 * Mouse wheel listener for Ctrl paging (from version 2.1).
65 */
66 protected MouseWheelListener mouseWheelListener;
67
68 /**
69 * Mouse listener for Ctrl paging (from version 2.1).
70 */
71 protected MouseListener mouseListener;
72
73 protected PropertyChangeListener propertyChangeListener;
74
75 /*
76 * (non-Javadoc)
77 *
78 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#installUI()
79 */
80 @Override
81 public void installUI() {
82 super.installUI();
83 if (LafWidgetUtilities2.getTabPreviewPainter(this.jcomp) != null) {
84 installMaps();
85 }
86 }
87
88 private void installMaps() {
89 InputMap currMap = SwingUtilities.getUIInputMap(this.jcomp,
90 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
91
92 InputMap newMap = new InputMap();
93 if (currMap != null) {
94 KeyStroke[] kss = currMap.allKeys();
95 for (int i = 0; i < kss.length; i++) {
96 KeyStroke stroke = kss[i];
97 Object val = currMap.get(stroke);
98 newMap.put(stroke, val);
99 }
100 }
101
102 newMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,
103 InputEvent.CTRL_MASK), "tabSwitcherForward");
104 newMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,
105 InputEvent.CTRL_MASK), "tabSwitcherBackward");
106 newMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL, 0, true),
107 "tabSwitcherClose");
108 newMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
109 "tabSwitcherHide");
110
111 this.jcomp.getActionMap().put("tabSwitcherForward",
112 new AbstractAction() {
113 @Override
114 public void actionPerformed(ActionEvent e) {
115 // fix for issue 323 in Substance - don't show pager on
116 // tabbed
117 // panes with no preview painter
118 TabPreviewPainter tpp = LafWidgetUtilities2
119 .getTabPreviewPainter(TabPagerWidget.this.jcomp);
120 if (tpp == null)
121 return;
122
123 TabPagerManager te = TabPagerManager.getPager();
124 te.page(TabPagerWidget.this.jcomp, true);
125 }
126 });
127
128 this.jcomp.getActionMap().put("tabSwitcherBackward",
129 new AbstractAction() {
130 @Override
131 public void actionPerformed(ActionEvent e) {
132 // fix for issue 323 in Substance - don't show pager on
133 // tabbed panes with no preview painter
134 TabPreviewPainter tpp = LafWidgetUtilities2
135 .getTabPreviewPainter(TabPagerWidget.this.jcomp);
136 if (tpp == null)
137 return;
138
139 TabPagerManager te = TabPagerManager.getPager();
140 te.page(TabPagerWidget.this.jcomp, false);
141 }
142 });
143
144 this.jcomp.getActionMap().put("tabSwitcherClose", new AbstractAction() {
145 @Override
146 public void actionPerformed(final ActionEvent e) {
147 SwingUtilities.invokeLater(new Runnable() {
148 @Override
149 public void run() {
150 // fix for issue 323 in Substance - don't show pager on
151 // tabbed panes with no preview painter
152 TabPreviewPainter tpp = LafWidgetUtilities2
153 .getTabPreviewPainter(TabPagerWidget.this.jcomp);
154 if (tpp == null) {
155 return;
156 }
157
158 TabPagerManager te = TabPagerManager.getPager();
159 int index = te.hide();
160 // fix for issue 177 in Substance (disallowing selection
161 // of disabled tabs).
162 if ((index >= 0)
163 && tpp.isSensitiveToEvents(
164 TabPagerWidget.this.jcomp, index)) {
165 TabPagerWidget.this.jcomp.setSelectedIndex(index);
166 }
167 }
168 });
169 }
170 });
171
172 this.jcomp.getActionMap().put("tabSwitcherHide", new AbstractAction() {
173 @Override
174 public void actionPerformed(ActionEvent e) {
175 TabPagerManager te = TabPagerManager.getPager();
176 if (te.isVisible()) {
177 te.hide();
178 } else {
179 // fix for defect 233 on Substance - the key event
180 // is not dispatched when tab pager is not showing.
181 Component comp = jcomp.getParent();
182 while (comp != null) {
183 if (comp instanceof JComponent) {
184 JComponent jc = (JComponent) comp;
185 KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(
186 KeyEvent.VK_ESCAPE, 0, false);
187 ActionListener al = jc
188 .getActionForKeyStroke(escapeKeyStroke);
189 if (al != null) {
190 al.actionPerformed(e);
191 return;
192 }
193 }
194 comp = comp.getParent();
195 }
196 }
197 }
198 });
199
200 SwingUtilities.replaceUIInputMap(this.jcomp,
201 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, newMap);
202 }
203
204 private void uninstallMaps() {
205 InputMap currMap = SwingUtilities.getUIInputMap(this.jcomp,
206 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
207
208 InputMap newMap = new InputMap();
209 if (currMap != null) {
210 KeyStroke[] kss = currMap.allKeys();
211 for (int i = 0; i < kss.length; i++) {
212 KeyStroke stroke = kss[i];
213 Object val = currMap.get(stroke);
214 if (stroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,
215 InputEvent.CTRL_MASK))) {
216 if ("tabSwitcherForward".equals(val))
217 continue;
218 }
219 if (stroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,
220 InputEvent.CTRL_MASK))) {
221 if ("tabSwitcherBackward".equals(val))
222 continue;
223 }
224 if (stroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL,
225 0, true))) {
226 if ("tabSwitcherClose".equals(val))
227 continue;
228 }
229 if (stroke
230 .equals(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0))) {
231 if ("tabSwitcherHide".equals(val))
232 continue;
233 }
234 newMap.put(stroke, val);
235 }
236 }
237
238 this.jcomp.getActionMap().remove("tabSwitcherForward");
239 this.jcomp.getActionMap().remove("tabSwitcherBackward");
240 this.jcomp.getActionMap().remove("tabSwitcherClose");
241 this.jcomp.getActionMap().remove("tabSwitcherHide");
242
243 SwingUtilities.replaceUIInputMap(this.jcomp,
244 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, newMap);
245 }
246
247 /*
248 * (non-Javadoc)
249 *
250 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#uninstallUI()
251 */
252 @Override
253 public void uninstallUI() {
254 this.uninstallMaps();
255 super.uninstallUI();
256 }
257
258 /*
259 * (non-Javadoc)
260 *
261 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#installListeners()
262 */
263 @Override
264 public void installListeners() {
265 this.mouseWheelListener = new TabPagerMouseWheelListener(this.jcomp);
266 this.jcomp.addMouseWheelListener(this.mouseWheelListener);
267 this.mouseListener = new MouseAdapter() {
268 @Override
269 public void mouseClicked(MouseEvent e) {
270 if ((e.getModifiers() & InputEvent.CTRL_MASK) != 0) {
271 SwingUtilities.invokeLater(new Runnable() {
272 @Override
273 public void run() {
274 TabPagerManager te = TabPagerManager.getPager();
275 int index = te.hide();
276 if (index >= 0)
277 TabPagerWidget.this.jcomp
278 .setSelectedIndex(index);
279 }
280 });
281 }
282 }
283 };
284 this.jcomp.addMouseListener(this.mouseListener);
285
286 this.propertyChangeListener = new PropertyChangeListener() {
287 @Override
288 public void propertyChange(PropertyChangeEvent evt) {
289 if (LafWidget.TABBED_PANE_PREVIEW_PAINTER.equals(evt
290 .getPropertyName())) {
291 TabPreviewPainter oldValue = (TabPreviewPainter) evt
292 .getOldValue();
293 TabPreviewPainter newValue = (TabPreviewPainter) evt
294 .getNewValue();
295 if ((oldValue == null) && (newValue != null)) {
296 installMaps();
297 }
298 if ((oldValue != null) && (newValue == null)) {
299 uninstallMaps();
300 }
301 }
302 }
303 };
304 this.jcomp.addPropertyChangeListener(this.propertyChangeListener);
305 }
306
307 /*
308 * (non-Javadoc)
309 *
310 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#uninstallListeners()
311 */
312 @Override
313 public void uninstallListeners() {
314 this.jcomp.removeMouseWheelListener(this.mouseWheelListener);
315 this.mouseWheelListener = null;
316
317 this.jcomp.removeMouseListener(this.mouseListener);
318 this.mouseListener = null;
319
320 this.jcomp.removePropertyChangeListener(this.propertyChangeListener);
321 this.propertyChangeListener = null;
322 }
323
324 /*
325 * (non-Javadoc)
326 *
327 * @see org.pushingpixels.lafwidget.LafWidget#requiresCustomLafSupport()
328 */
329 @Override
330 public boolean requiresCustomLafSupport() {
331 return false;
332 }
333
334 /*
335 * (non-Javadoc)
336 *
337 * @see org.pushingpixels.lafwidget.Resettable#reset()
338 */
339 @Override
340 public void reset() {
341 TabPagerManager.reset();
342 }
343 }