| 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.scroll; |
| 31 |
|
| 32 |
import java.awt.event.HierarchyEvent; |
| 33 |
import java.awt.event.HierarchyListener; |
| 34 |
import java.beans.PropertyChangeEvent; |
| 35 |
import java.beans.PropertyChangeListener; |
| 36 |
|
| 37 |
import javax.swing.JScrollPane; |
| 38 |
import javax.swing.plaf.basic.ComboPopup; |
| 39 |
|
| 40 |
import org.pushingpixels.lafwidget.*; |
| 41 |
import org.pushingpixels.lafwidget.preview.PreviewPainter; |
| 42 |
|
| 43 |
/** |
| 44 |
* Widget that decorates scroll panes with selector. |
| 45 |
* |
| 46 |
* @author Kirill Grouchnikov |
| 47 |
*/ |
| 48 |
public class ScrollPaneSelectorWidget extends LafWidgetAdapter<JScrollPane> { |
| 49 |
/** |
| 50 |
* The scroll pane selector for the associated scroll pane. |
| 51 |
*/ |
| 52 |
protected ScrollPaneSelector scrollPaneSelector; |
| 53 |
|
| 54 |
/** |
| 55 |
* Hierarchy listener - remove the selector in the scroll pane of a combo |
| 56 |
* popup. |
| 57 |
*/ |
| 58 |
protected HierarchyListener hierarchyListener; |
| 59 |
|
| 60 |
/** |
| 61 |
* Property change listener - listens on the changes to |
| 62 |
* {@link LafWidget#COMPONENT_PREVIEW_PAINTER} property. |
| 63 |
*/ |
| 64 |
protected PropertyChangeListener propertyChangeListener; |
| 65 |
|
| 66 |
/* |
| 67 |
* (non-Javadoc) |
| 68 |
* |
| 69 |
* @see org.pushingpixels.lafwidget.LafWidget#requiresCustomLafSupport() |
| 70 |
*/ |
| 71 |
@Override |
| 72 |
public boolean requiresCustomLafSupport() { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
/* |
| 77 |
* (non-Javadoc) |
| 78 |
* |
| 79 |
* @see org.pushingpixels.lafwidget.LafWidgetAdapter#installUI() |
| 80 |
*/ |
| 81 |
@Override |
| 82 |
public void installUI() { |
| 83 |
if (LafWidgetRepository.getRepository().getLafSupport() |
| 84 |
.toInstallExtraElements(this.jcomp)) { |
| 85 |
|
| 86 |
PreviewPainter pPainter = LafWidgetUtilities2 |
| 87 |
.getComponentPreviewPainter(this.jcomp); |
| 88 |
if (pPainter == null) |
| 89 |
return; |
| 90 |
this.scrollPaneSelector = new ScrollPaneSelector(); |
| 91 |
this.scrollPaneSelector.installOnScrollPane(this.jcomp); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/* |
| 96 |
* (non-Javadoc) |
| 97 |
* |
| 98 |
* @see org.pushingpixels.lafwidget.LafWidgetAdapter#uninstallUI() |
| 99 |
*/ |
| 100 |
@Override |
| 101 |
public void uninstallUI() { |
| 102 |
if (this.scrollPaneSelector != null) { |
| 103 |
this.scrollPaneSelector.uninstallFromScrollPane(); |
| 104 |
this.scrollPaneSelector = null; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public void installListeners() { |
| 110 |
this.hierarchyListener = new HierarchyListener() { |
| 111 |
/* |
| 112 |
* (non-Javadoc) |
| 113 |
* |
| 114 |
* @see |
| 115 |
* java.awt.event.HierarchyListener#hierarchyChanged(java.awt.event |
| 116 |
* .HierarchyEvent) |
| 117 |
*/ |
| 118 |
@Override |
| 119 |
public void hierarchyChanged(HierarchyEvent e) { |
| 120 |
if (jcomp.getParent() instanceof ComboPopup) { |
| 121 |
if (scrollPaneSelector != null) { |
| 122 |
scrollPaneSelector.uninstallFromScrollPane(); |
| 123 |
scrollPaneSelector = null; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
}; |
| 128 |
this.jcomp.addHierarchyListener(this.hierarchyListener); |
| 129 |
|
| 130 |
this.propertyChangeListener = new PropertyChangeListener() { |
| 131 |
@Override |
| 132 |
public void propertyChange(PropertyChangeEvent evt) { |
| 133 |
if (LafWidget.COMPONENT_PREVIEW_PAINTER.equals(evt |
| 134 |
.getPropertyName())) { |
| 135 |
PreviewPainter pPainter = LafWidgetUtilities2 |
| 136 |
.getComponentPreviewPainter(jcomp); |
| 137 |
// Uninstall old scroll pane selector |
| 138 |
if (scrollPaneSelector != null) { |
| 139 |
scrollPaneSelector.uninstallFromScrollPane(); |
| 140 |
scrollPaneSelector = null; |
| 141 |
} |
| 142 |
// Install new scroll pane selector |
| 143 |
if (pPainter != null |
| 144 |
&& LafWidgetRepository.getRepository() |
| 145 |
.getLafSupport().toInstallExtraElements( |
| 146 |
jcomp)) { |
| 147 |
scrollPaneSelector = new ScrollPaneSelector(); |
| 148 |
scrollPaneSelector.installOnScrollPane(jcomp); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
}; |
| 153 |
this.jcomp.addPropertyChangeListener(this.propertyChangeListener); |
| 154 |
} |
| 155 |
|
| 156 |
/* |
| 157 |
* (non-Javadoc) |
| 158 |
* |
| 159 |
* @see org.pushingpixels.lafwidget.LafWidgetAdapter#uninstallListeners() |
| 160 |
*/ |
| 161 |
@Override |
| 162 |
public void uninstallListeners() { |
| 163 |
this.jcomp.removeHierarchyListener(this.hierarchyListener); |
| 164 |
this.hierarchyListener = null; |
| 165 |
|
| 166 |
this.jcomp.removePropertyChangeListener(this.propertyChangeListener); |
| 167 |
this.propertyChangeListener = null; |
| 168 |
} |
| 169 |
} |