|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.swing; |
|
27 |
|
28 import java.awt.*; |
|
29 import java.awt.event.*; |
|
30 import java.beans.*; |
|
31 import java.util.*; |
|
32 import java.util.List; |
|
33 import javax.swing.*; |
|
34 import javax.swing.Timer; |
|
35 import javax.swing.event.*; |
|
36 import com.oracle.solaris.vp.util.misc.CollectionUtil; |
|
37 |
|
38 /** |
|
39 * The {@code ListCellOverlay} class provides an overlay over each cell in a |
|
40 * {@code JList}. This overlay extends the cell to its preferred size so that |
|
41 * the user can see the entire contents of a cell that is partially hidden |
|
42 * within a scroll pane or is sized smaller than its preferred size. |
|
43 * <br/> |
|
44 * This class assumes that the {@code JList} is contained within a {@code |
|
45 * JScrollPane}; the overlay is styled to complement that scroll pane. |
|
46 * <br/> |
|
47 * The {@link #getOverlayComponent overlay component} or the {@link |
|
48 * #getOverlayScrollPane scroll pane} that contains it may be changed visually |
|
49 * (custom borders, background color, etc.) to affect the look of the overlay. |
|
50 */ |
|
51 public class ListCellOverlay { |
|
52 // |
|
53 // Enums |
|
54 // |
|
55 |
|
56 public enum Style { |
|
57 SINGLE_POPUP, MULTI_POPUP |
|
58 } |
|
59 |
|
60 // |
|
61 // Inner classes |
|
62 // |
|
63 |
|
64 private static abstract class CellPanel extends JPanel { |
|
65 // |
|
66 // Instance data |
|
67 // |
|
68 |
|
69 private CellRendererPane rPane = new CellRendererPane(); |
|
70 |
|
71 // |
|
72 // Constructors |
|
73 // |
|
74 |
|
75 public CellPanel() { |
|
76 setLayout(null); |
|
77 add(rPane); |
|
78 } |
|
79 |
|
80 // |
|
81 // Component methods |
|
82 // |
|
83 |
|
84 @Override |
|
85 public Dimension getPreferredSize() { |
|
86 Dimension d = getRendererComponentPreferredSize(); |
|
87 Insets insets = getInsets(); |
|
88 d.width += insets.left + insets.right; |
|
89 d.height += insets.top + insets.bottom; |
|
90 |
|
91 return d; |
|
92 } |
|
93 |
|
94 // |
|
95 // JComponent methods |
|
96 // |
|
97 |
|
98 @Override |
|
99 public void paintComponent(Graphics g) { |
|
100 super.paintComponent(g); |
|
101 |
|
102 Component comp = getRendererComponent(); |
|
103 |
|
104 Insets insets = getInsets(); |
|
105 int x = insets.left; |
|
106 int y = insets.top; |
|
107 int width = getWidth() - insets.left - insets.right; |
|
108 int height = getHeight() - insets.top - insets.bottom; |
|
109 |
|
110 rPane.paintComponent(g, comp, this, x, y, width, height, true); |
|
111 } |
|
112 |
|
113 // |
|
114 // CellPanel methods |
|
115 // |
|
116 |
|
117 public abstract Component getRendererComponent(); |
|
118 |
|
119 public Dimension getRendererComponentPreferredSize() { |
|
120 return getRendererComponent().getPreferredSize(); |
|
121 } |
|
122 } |
|
123 |
|
124 private class SimpleCellPanel extends CellPanel { |
|
125 // |
|
126 // Instance data |
|
127 // |
|
128 |
|
129 private Component comp; |
|
130 |
|
131 // |
|
132 // Constructors |
|
133 // |
|
134 |
|
135 public SimpleCellPanel(Component comp) { |
|
136 this.comp = comp; |
|
137 |
|
138 MouseAdapter listener = |
|
139 new MouseAdapter() { |
|
140 // |
|
141 // MouseListener methods |
|
142 // |
|
143 |
|
144 @Override |
|
145 public void mouseClicked(MouseEvent e) { |
|
146 GUIUtil.propagate(e, list); |
|
147 } |
|
148 |
|
149 @Override |
|
150 public void mouseExited(MouseEvent e) { |
|
151 configurePopups(e); |
|
152 } |
|
153 |
|
154 @Override |
|
155 public void mousePressed(MouseEvent e) { |
|
156 GUIUtil.propagate(e, list); |
|
157 } |
|
158 |
|
159 @Override |
|
160 public void mouseReleased(MouseEvent e) { |
|
161 GUIUtil.propagate(e, list); |
|
162 } |
|
163 |
|
164 // |
|
165 // MouseMotionListener methods |
|
166 // |
|
167 |
|
168 @Override |
|
169 public void mouseDragged(MouseEvent e) { |
|
170 GUIUtil.propagate(e, list); |
|
171 } |
|
172 |
|
173 @Override |
|
174 public void mouseMoved(MouseEvent e) { |
|
175 GUIUtil.propagate(e, list); |
|
176 } |
|
177 |
|
178 // |
|
179 // MouseWheelListener methods |
|
180 // |
|
181 |
|
182 @Override |
|
183 public void mouseWheelMoved(MouseWheelEvent e) { |
|
184 if (listScroll != null) { |
|
185 GUIUtil.propagate(e, listScroll); |
|
186 } |
|
187 } |
|
188 }; |
|
189 |
|
190 addMouseListener(listener); |
|
191 addMouseMotionListener(listener); |
|
192 addMouseWheelListener(listener); |
|
193 } |
|
194 |
|
195 // |
|
196 // CellPanel methods |
|
197 // |
|
198 |
|
199 @Override |
|
200 public Component getRendererComponent() { |
|
201 return comp; |
|
202 } |
|
203 } |
|
204 |
|
205 // |
|
206 // Static data |
|
207 // |
|
208 |
|
209 /** |
|
210 * The initial delay after hovering over the list before an overlay appears. |
|
211 */ |
|
212 public static final int DELAY = 0; |
|
213 |
|
214 // |
|
215 // Instance data |
|
216 // |
|
217 |
|
218 // Set to true to paint each overlay rectangle with a solid color |
|
219 private boolean debug = false; |
|
220 |
|
221 private JList list; |
|
222 private JScrollPane listScroll; |
|
223 |
|
224 private CellPanel panel; |
|
225 private JScrollPane panelScroll; |
|
226 private Map<Popup, CellPanel> popups = new HashMap<Popup, CellPanel>(); |
|
227 private Timer timer; |
|
228 |
|
229 private int index = -1; |
|
230 private boolean enabled = true; |
|
231 private Style style = Style.MULTI_POPUP; |
|
232 |
|
233 private ListDataListener listModelListener = |
|
234 new ListDataListener() { |
|
235 @Override |
|
236 public void contentsChanged(ListDataEvent e) { |
|
237 repaintPopups(); |
|
238 } |
|
239 |
|
240 @Override |
|
241 public void intervalAdded(ListDataEvent e) { |
|
242 repaintPopups(); |
|
243 } |
|
244 |
|
245 @Override |
|
246 public void intervalRemoved(ListDataEvent e) { |
|
247 hidePopups(); |
|
248 } |
|
249 }; |
|
250 |
|
251 // |
|
252 // Constructors |
|
253 // |
|
254 |
|
255 public ListCellOverlay(final JList list) { |
|
256 this.list = list; |
|
257 |
|
258 panel = new CellPanel() { |
|
259 @Override |
|
260 public Component getRendererComponent() { |
|
261 Object value = list.getModel().getElementAt(index); |
|
262 boolean isSelected = list.isSelectedIndex(index); |
|
263 boolean hasFocus = list.hasFocus() && |
|
264 (index == list.getLeadSelectionIndex()); |
|
265 |
|
266 return list.getCellRenderer().getListCellRendererComponent( |
|
267 list, value, index, isSelected, hasFocus); |
|
268 } |
|
269 |
|
270 @Override |
|
271 public Dimension getRendererComponentPreferredSize() { |
|
272 Dimension d = super.getRendererComponentPreferredSize(); |
|
273 |
|
274 Rectangle r = list.getCellBounds(index, index); |
|
275 d.width = Math.max(d.width, r.width); |
|
276 d.height = Math.max(d.height, r.height); |
|
277 |
|
278 return d; |
|
279 } |
|
280 }; |
|
281 |
|
282 panel.setOpaque(true); |
|
283 panel.setBackground(list.getBackground()); |
|
284 |
|
285 panelScroll = new JScrollPane(); |
|
286 |
|
287 timer = new Timer(DELAY, |
|
288 new ActionListener() { |
|
289 @Override |
|
290 public void actionPerformed(ActionEvent e) { |
|
291 for (Popup popup : popups.keySet()) { |
|
292 popup.show(); |
|
293 } |
|
294 } |
|
295 }); |
|
296 timer.setRepeats(false); |
|
297 |
|
298 HierarchyListener listener = |
|
299 new HierarchyListener() { |
|
300 @Override |
|
301 public void hierarchyChanged(HierarchyEvent e) { |
|
302 listScroll = (JScrollPane)SwingUtilities.getAncestorOfClass( |
|
303 JScrollPane.class, list); |
|
304 } |
|
305 }; |
|
306 list.addHierarchyListener(listener); |
|
307 |
|
308 // Initialize |
|
309 listener.hierarchyChanged(null); |
|
310 |
|
311 list.addHierarchyBoundsListener( |
|
312 new HierarchyBoundsListener() { |
|
313 @Override |
|
314 public void ancestorMoved(HierarchyEvent e) { |
|
315 hidePopups(); |
|
316 } |
|
317 |
|
318 @Override |
|
319 public void ancestorResized(HierarchyEvent e) { |
|
320 hidePopups(); |
|
321 } |
|
322 }); |
|
323 |
|
324 list.addComponentListener( |
|
325 new ComponentAdapter() { |
|
326 @Override |
|
327 public void componentMoved(ComponentEvent e) { |
|
328 hidePopups(); |
|
329 } |
|
330 |
|
331 @Override |
|
332 public void componentResized(ComponentEvent e) { |
|
333 hidePopups(); |
|
334 } |
|
335 }); |
|
336 |
|
337 list.addMouseMotionListener( |
|
338 new MouseMotionAdapter() { |
|
339 @Override |
|
340 public void mouseMoved(MouseEvent e) { |
|
341 configurePopups(e); |
|
342 } |
|
343 }); |
|
344 |
|
345 list.addMouseListener( |
|
346 new MouseAdapter() { |
|
347 @Override |
|
348 public void mouseExited(MouseEvent e) { |
|
349 configurePopups(e); |
|
350 } |
|
351 }); |
|
352 |
|
353 list.addListSelectionListener( |
|
354 new ListSelectionListener() { |
|
355 @Override |
|
356 public void valueChanged(ListSelectionEvent e) { |
|
357 repaintPopups(); |
|
358 } |
|
359 }); |
|
360 |
|
361 list.addPropertyChangeListener("model", |
|
362 new PropertyChangeListener() { |
|
363 @Override |
|
364 public void propertyChange(PropertyChangeEvent e) { |
|
365 modelChanged((ListModel)e.getOldValue(), |
|
366 (ListModel)e.getNewValue()); |
|
367 } |
|
368 }); |
|
369 |
|
370 // Initialize |
|
371 modelChanged(null, list.getModel()); |
|
372 } |
|
373 |
|
374 // |
|
375 // ListCellOverlay methods |
|
376 // |
|
377 |
|
378 /** |
|
379 * Gets the initial delay after hovering over the list before an overlay of |
|
380 * a cell appears. |
|
381 */ |
|
382 public int getDelay() { |
|
383 return timer.getInitialDelay(); |
|
384 } |
|
385 |
|
386 /** |
|
387 * Gets whether this {@code ListCellOverlay} is enabled. |
|
388 */ |
|
389 public boolean getEnabled() { |
|
390 return enabled; |
|
391 } |
|
392 |
|
393 public JList getList() { |
|
394 return list; |
|
395 } |
|
396 |
|
397 /** |
|
398 * Gets the {@code JComponent} that is displays the cell renderer in the |
|
399 * overlay. This {@code JComponent} can be customized with a border, a |
|
400 * change in background color, etc. |
|
401 */ |
|
402 public JComponent getOverlayComponent() { |
|
403 return panel; |
|
404 } |
|
405 |
|
406 /** |
|
407 * Gets the {@code JScrollPane} containing the {@link #getOverlayComponent |
|
408 * overlay component}. This provides a border that (presumably) matches the |
|
409 * one surrounding the list. |
|
410 */ |
|
411 public JScrollPane getOverlayScrollPane() { |
|
412 return panelScroll; |
|
413 } |
|
414 |
|
415 /** |
|
416 * Gets the style to use when displaying the overlay. |
|
417 */ |
|
418 public Style getStyle() { |
|
419 return style; |
|
420 } |
|
421 |
|
422 /** |
|
423 * Sets the style to use when displaying the overlay. {@link |
|
424 * Style#SINGLE_POPUP} uses a single, tooltip-like popup, complete with |
|
425 * borders, as the overlay. {@link Style#MULTI_POPUP} uses multiple popups |
|
426 * to make it appear as though the scroll pane that encloses the list |
|
427 * changes shape to accommodate the overlay. The latter style may appear |
|
428 * more polished, but the former has a lesser performance hit. The default |
|
429 * value is {@link Style#MULTI_POPUP}. |
|
430 */ |
|
431 public void setStyle(Style style) { |
|
432 this.style = style; |
|
433 } |
|
434 |
|
435 /** |
|
436 * Sets the initial delay after hovering over the list before an overlay of |
|
437 * a cell appears. |
|
438 */ |
|
439 public void setDelay(int delay) { |
|
440 timer.setInitialDelay(delay); |
|
441 } |
|
442 |
|
443 /** |
|
444 * Gets whether this {@code ListCellOverlay} is enabled. |
|
445 */ |
|
446 public void setEnabled(boolean enabled) { |
|
447 if (this.enabled != enabled) { |
|
448 if (!enabled) { |
|
449 hidePopups(); |
|
450 } |
|
451 this.enabled = enabled; |
|
452 } |
|
453 } |
|
454 |
|
455 // |
|
456 // Private methods |
|
457 // |
|
458 |
|
459 private void configurePopups(MouseEvent e) { |
|
460 if (!enabled || listScroll == null) { |
|
461 return; |
|
462 } |
|
463 |
|
464 Rectangle cellRect = null; |
|
465 int index; |
|
466 |
|
467 Point listScrollPoint = SwingUtilities.convertPoint( |
|
468 e.getComponent(), e.getPoint(), listScroll); |
|
469 |
|
470 // Is the point visible? |
|
471 if (!listScroll.contains(listScrollPoint)) { |
|
472 index = -1; |
|
473 } else { |
|
474 Point listPoint = SwingUtilities.convertPoint( |
|
475 listScroll, listScrollPoint, list); |
|
476 |
|
477 index = list.locationToIndex(listPoint); |
|
478 if (index != -1) { |
|
479 cellRect = list.getCellBounds(index, index); |
|
480 if (!cellRect.contains(listPoint)) { |
|
481 // The point is not actually in a list cell |
|
482 index = -1; |
|
483 } |
|
484 } |
|
485 } |
|
486 |
|
487 // Has the index of the overlaid cell changed? |
|
488 if (this.index != index) { |
|
489 hidePopups(); |
|
490 |
|
491 if (index != -1) { |
|
492 this.index = index; |
|
493 |
|
494 if (isCellObscured()) { |
|
495 // We now need to figure out where to place popup windows |
|
496 // (containing scroll, which contains panel, which paints |
|
497 // the cell renderer) so that the cell renderer within the |
|
498 // popups aligns with the cell renderer in the list. |
|
499 |
|
500 // The size of the panel scroll pane border |
|
501 // (panelScroll.getInsets() doesn't work under some L&Fs |
|
502 // (like synth), so we are forced to do a layout and compare |
|
503 // points) |
|
504 panelScroll.setViewportView(panel); |
|
505 panelScroll.doLayout(); |
|
506 Point panelScrollInsets = SwingUtilities.convertPoint( |
|
507 panel, 0, 0, panelScroll); |
|
508 |
|
509 // The size of the panel border |
|
510 Insets panelInsets = panel.getInsets(); |
|
511 |
|
512 if (cellRect == null) { |
|
513 cellRect = list.getCellBounds(index, index); |
|
514 } |
|
515 |
|
516 Dimension panelScrollPrefSize = |
|
517 panelScroll.getPreferredSize(); |
|
518 |
|
519 // The bounds of the panel scroll pane in list coordinates |
|
520 Rectangle panelScrollBounds = new Rectangle( |
|
521 cellRect.x - panelInsets.left - panelScrollInsets.x, |
|
522 cellRect.y - panelInsets.top - panelScrollInsets.y, |
|
523 panelScrollPrefSize.width, panelScrollPrefSize.height); |
|
524 |
|
525 // Convert to the list scroll pane's coordinates |
|
526 panelScrollBounds = SwingUtilities.convertRectangle( |
|
527 list, panelScrollBounds, listScroll); |
|
528 |
|
529 List<Rectangle> rectList = new ArrayList<Rectangle>(); |
|
530 |
|
531 switch (style) { |
|
532 case SINGLE_POPUP: |
|
533 rectList.add(panelScrollBounds); |
|
534 break; |
|
535 |
|
536 default: |
|
537 case MULTI_POPUP: |
|
538 // The bounds of the list viewport in the list |
|
539 // scroll pane's coordinates |
|
540 Rectangle listViewBounds = |
|
541 listScroll.getViewport().getBounds(); |
|
542 |
|
543 Rectangle[] rects = |
|
544 SwingUtilities.computeDifference( |
|
545 panelScrollBounds, listViewBounds); |
|
546 |
|
547 CollectionUtil.addAll(rectList, rects); |
|
548 |
|
549 // Convert to the list scroll pane's coordinates |
|
550 cellRect = SwingUtilities.convertRectangle( |
|
551 list, cellRect, listScroll); |
|
552 |
|
553 // Now add one more rect to cover the visible area |
|
554 // of the cell entirely. This is necessary because |
|
555 // the renderer may be drawing its content truncated |
|
556 // (ie. "Foo bar..."), and we need it to be drawn at |
|
557 // full size. |
|
558 rectList.add(cellRect); |
|
559 } |
|
560 |
|
561 Point listScrollScreenPoint = |
|
562 listScroll.getLocationOnScreen(); |
|
563 |
|
564 for (Rectangle rect : rectList) { |
|
565 Point point = rect.getLocation(); |
|
566 point.translate(-panelScrollBounds.x, |
|
567 -panelScrollBounds.y); |
|
568 |
|
569 JViewport viewport = new JViewport(); |
|
570 viewport.setPreferredSize(new Dimension( |
|
571 rect.width, rect.height)); |
|
572 |
|
573 CellPanel cell = new SimpleCellPanel(panelScroll); |
|
574 |
|
575 if (debug) { |
|
576 JLabel label = new JLabel(); |
|
577 label.setOpaque(true); |
|
578 label.setBackground(ColorUtil.getRandomColor()); |
|
579 cell = new SimpleCellPanel(label); |
|
580 } |
|
581 |
|
582 viewport.setView(cell); |
|
583 viewport.setViewPosition(point); |
|
584 |
|
585 int x = rect.x + listScrollScreenPoint.x; |
|
586 int y = rect.y + listScrollScreenPoint.y; |
|
587 |
|
588 Popup popup = PopupFactory.getSharedInstance().getPopup( |
|
589 list, viewport, x, y); |
|
590 |
|
591 popups.put(popup, cell); |
|
592 } |
|
593 |
|
594 timer.restart(); |
|
595 } |
|
596 } |
|
597 } |
|
598 } |
|
599 |
|
600 private boolean getPopupsShowing() { |
|
601 return !popups.isEmpty(); |
|
602 } |
|
603 |
|
604 private void hidePopups() { |
|
605 timer.stop(); |
|
606 index = -1; |
|
607 for (Iterator<Popup> i = popups.keySet().iterator(); i.hasNext();) { |
|
608 Popup popup = i.next(); |
|
609 popup.hide(); |
|
610 i.remove(); |
|
611 } |
|
612 list.repaint(); |
|
613 } |
|
614 |
|
615 private boolean isCellObscured() { |
|
616 Rectangle cellRect = list.getCellBounds(index, index); |
|
617 Dimension panelSize = panel.getPreferredSize(); |
|
618 |
|
619 // Is the cell rect smaller than the preferred size? |
|
620 if (cellRect.width < panelSize.width || |
|
621 cellRect.height < panelSize.height) { |
|
622 return true; |
|
623 } |
|
624 |
|
625 // The bounds of panel at its preferred size, relative to listScroll |
|
626 Rectangle panelBounds = SwingUtilities.convertRectangle(list, |
|
627 new Rectangle(cellRect), listScroll); |
|
628 |
|
629 Rectangle scrollBounds = new Rectangle(listScroll.getSize()); |
|
630 |
|
631 return !scrollBounds.contains(panelBounds); |
|
632 } |
|
633 |
|
634 private void modelChanged(ListModel oldModel, ListModel newModel) { |
|
635 if (oldModel != null) { |
|
636 oldModel.removeListDataListener(listModelListener); |
|
637 } |
|
638 |
|
639 if (newModel != null) { |
|
640 newModel.addListDataListener(listModelListener); |
|
641 } |
|
642 } |
|
643 |
|
644 private void repaintPopups() { |
|
645 if (getPopupsShowing()) { |
|
646 for (CellPanel cell : popups.values()) { |
|
647 cell.repaint(); |
|
648 } |
|
649 } |
|
650 } |
|
651 } |