| 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.*; |
| 33 |
import java.awt.image.BufferedImage; |
| 34 |
|
| 35 |
import javax.swing.JTabbedPane; |
| 36 |
|
| 37 |
import org.pushingpixels.lafwidget.LafWidgetUtilities; |
| 38 |
|
| 39 |
/** |
| 40 |
* Default implementation of the tab preview painter. The tab preview is a |
| 41 |
* scaled-down (as necessary) thumbnail of the relevant tab. |
| 42 |
* |
| 43 |
* @author Kirill Grouchnikov |
| 44 |
*/ |
| 45 |
public class DefaultTabPreviewPainter extends TabPreviewPainter { |
| 46 |
/* |
| 47 |
* (non-Javadoc) |
| 48 |
* |
| 49 |
* @see org.pushingpixels.lafwidget.tabbed.TabPreviewPainter#hasPreview(javax.swing.JTabbedPane, |
| 50 |
* int) |
| 51 |
*/ |
| 52 |
@Override |
| 53 |
public boolean hasPreview(JTabbedPane tabPane, int tabIndex) { |
| 54 |
return (tabPane.getComponentAt(tabIndex) != null); |
| 55 |
} |
| 56 |
|
| 57 |
/* |
| 58 |
* (non-Javadoc) |
| 59 |
* |
| 60 |
* @see org.pushingpixels.lafwidget.tabbed.TabPreviewPainter#isSensitiveToEvents(javax.swing.JTabbedPane, |
| 61 |
* int) |
| 62 |
*/ |
| 63 |
@Override |
| 64 |
public boolean isSensitiveToEvents(JTabbedPane tabPane, int tabIndex) { |
| 65 |
return tabPane.isEnabledAt(tabIndex); |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
* (non-Javadoc) |
| 70 |
* |
| 71 |
* @see org.pushingpixels.lafwidget.tabbed.TabPreviewPainter#previewTab(javax.swing.JTabbedPane, |
| 72 |
* int, java.awt.Graphics, int, int, int, int) |
| 73 |
*/ |
| 74 |
@Override |
| 75 |
public void previewTab(JTabbedPane tabPane, int tabIndex, Graphics g, |
| 76 |
int x, int y, int w, int h) { |
| 77 |
Component tabComponent = tabPane.getComponentAt(tabIndex); |
| 78 |
if (tabComponent == null) |
| 79 |
return; |
| 80 |
// if (!tabComponent.isShowing()) |
| 81 |
// return; |
| 82 |
int compWidth = tabComponent.getWidth(); |
| 83 |
int compHeight = tabComponent.getHeight(); |
| 84 |
|
| 85 |
if ((compWidth > 0) && (compHeight > 0)) { |
| 86 |
// draw tab component |
| 87 |
BufferedImage tempCanvas = new BufferedImage(compWidth, compHeight, |
| 88 |
BufferedImage.TYPE_INT_ARGB); |
| 89 |
Graphics tempCanvasGraphics = tempCanvas.getGraphics(); |
| 90 |
tabComponent.paint(tempCanvasGraphics); |
| 91 |
|
| 92 |
// check if need to scale down |
| 93 |
double coef = Math.min((double) w / (double) compWidth, (double) h |
| 94 |
/ (double) compHeight); |
| 95 |
// fix for issue 177 in Substance - disabled tabs painted in |
| 96 |
// 50% opacity. |
| 97 |
Graphics2D g2 = (Graphics2D) g.create(); |
| 98 |
if (!tabPane.isEnabledAt(tabIndex)) { |
| 99 |
g2.setComposite(AlphaComposite.getInstance( |
| 100 |
AlphaComposite.SRC_OVER, 0.5f)); |
| 101 |
} |
| 102 |
if (coef < 1.0) { |
| 103 |
int sdWidth = (int) (coef * compWidth); |
| 104 |
int sdHeight = (int) (coef * compHeight); |
| 105 |
int dx = (w - sdWidth) / 2; |
| 106 |
int dy = (h - sdHeight) / 2; |
| 107 |
|
| 108 |
g2.drawImage(LafWidgetUtilities.createThumbnail(tempCanvas, |
| 109 |
sdWidth), dx, dy, null); |
| 110 |
|
| 111 |
} else { |
| 112 |
// System.out.println("Putting " + frame.hashCode() + " |
| 113 |
// -> " + snapshot.hashCode()); |
| 114 |
g2.drawImage(tempCanvas, 0, 0, null); |
| 115 |
} |
| 116 |
g2.dispose(); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/* |
| 121 |
* (non-Javadoc) |
| 122 |
* |
| 123 |
* @see org.pushingpixels.lafwidget.tabbed.TabPreviewPainter#hasPreviewWindow(javax.swing.JTabbedPane, |
| 124 |
* int) |
| 125 |
*/ |
| 126 |
@Override |
| 127 |
public boolean hasPreviewWindow(JTabbedPane tabPane, int tabIndex) { |
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
/* |
| 132 |
* (non-Javadoc) |
| 133 |
* |
| 134 |
* @see org.pushingpixels.lafwidget.tabbed.TabPreviewPainter#hasOverviewDialog(javax.swing.JTabbedPane) |
| 135 |
*/ |
| 136 |
@Override |
| 137 |
public boolean hasOverviewDialog(JTabbedPane tabPane) { |
| 138 |
return true; |
| 139 |
} |
| 140 |
} |