| 1 |
package org.pushingpixels.lafwidget.tabbed; |
| 2 |
|
| 3 |
import java.awt.*; |
| 4 |
import java.awt.image.BufferedImage; |
| 5 |
|
| 6 |
import javax.swing.*; |
| 7 |
import javax.swing.border.*; |
| 8 |
|
| 9 |
import org.pushingpixels.lafwidget.animation.AnimationConfigurationManager; |
| 10 |
import org.pushingpixels.lafwidget.utils.ShadowPopupBorder; |
| 11 |
import org.pushingpixels.trident.Timeline; |
| 12 |
|
| 13 |
/** |
| 14 |
* Control to display the a single tab preview. |
| 15 |
* |
| 16 |
* @author Kirill Grouchnikov |
| 17 |
*/ |
| 18 |
public class TabPreviewControl extends JPanel { |
| 19 |
/** |
| 20 |
* Label for the tab icon. |
| 21 |
*/ |
| 22 |
protected JLabel iconLabel; |
| 23 |
|
| 24 |
/** |
| 25 |
* Label for the tab title. |
| 26 |
*/ |
| 27 |
protected JLabel titleLabel; |
| 28 |
|
| 29 |
/** |
| 30 |
* Panel for the tab preview image. |
| 31 |
*/ |
| 32 |
protected JPanel previewImagePanel; |
| 33 |
|
| 34 |
/** |
| 35 |
* The preview image itself. |
| 36 |
*/ |
| 37 |
protected BufferedImage previewImage; |
| 38 |
|
| 39 |
/** |
| 40 |
* The associated tabbed pane. |
| 41 |
*/ |
| 42 |
protected JTabbedPane tabPane; |
| 43 |
|
| 44 |
private float alpha; |
| 45 |
|
| 46 |
private float zoom; |
| 47 |
|
| 48 |
/** |
| 49 |
* Creates a tab preview control. |
| 50 |
* |
| 51 |
* @param tabPane |
| 52 |
* Tabbed pane. |
| 53 |
* @param tabIndex |
| 54 |
* Tab index. |
| 55 |
*/ |
| 56 |
public TabPreviewControl(final JTabbedPane tabPane, final int tabIndex) { |
| 57 |
this.tabPane = tabPane; |
| 58 |
this.setLayout(new TabPreviewControlLayout()); |
| 59 |
this.iconLabel = new JLabel(tabPane.getIconAt(tabIndex)); |
| 60 |
this.titleLabel = new JLabel(tabPane.getTitleAt(tabIndex)); |
| 61 |
this.titleLabel |
| 62 |
.setFont(this.titleLabel.getFont().deriveFont(Font.BOLD)); |
| 63 |
|
| 64 |
// the panel with the preview image - perhaps use JLabel |
| 65 |
// instead? |
| 66 |
this.previewImagePanel = new JPanel() { |
| 67 |
@Override |
| 68 |
public void paintComponent(Graphics g) { |
| 69 |
super.paintComponent(g); |
| 70 |
paintTabThumbnail(g); |
| 71 |
}; |
| 72 |
}; |
| 73 |
this.add(this.iconLabel); |
| 74 |
this.add(this.titleLabel); |
| 75 |
this.add(this.previewImagePanel); |
| 76 |
|
| 77 |
final boolean isSelected = (tabPane.getSelectedIndex() == tabIndex); |
| 78 |
Border innerBorder = isSelected ? new LineBorder(Color.black, 2) |
| 79 |
: new LineBorder(Color.black, 1); |
| 80 |
this |
| 81 |
.setBorder(new CompoundBorder(new ShadowPopupBorder(), |
| 82 |
innerBorder)); |
| 83 |
|
| 84 |
this.alpha = 0.0f; |
| 85 |
this.zoom = 1.0f; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Paints the tab thumbnail on the specified graphics context. |
| 90 |
* |
| 91 |
* @param g |
| 92 |
* Graphics context. |
| 93 |
*/ |
| 94 |
public synchronized void paintTabThumbnail(Graphics g) { |
| 95 |
if (TabPreviewControl.this.previewImage != null) { |
| 96 |
int pw = TabPreviewControl.this.previewImage.getWidth(); |
| 97 |
int ph = TabPreviewControl.this.previewImage.getHeight(); |
| 98 |
int w = this.previewImagePanel.getWidth(); |
| 99 |
int h = this.previewImagePanel.getHeight(); |
| 100 |
|
| 101 |
Graphics2D g2 = (Graphics2D) g.create(); |
| 102 |
g2.setComposite(AlphaComposite.SrcOver.derive(alpha)); |
| 103 |
int dx = (w - pw) / 2; |
| 104 |
int dy = (h - ph) / 2; |
| 105 |
g2.drawImage(TabPreviewControl.this.previewImage, dx, dy, null); |
| 106 |
g2.dispose(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Stes the tab index. |
| 112 |
* |
| 113 |
* @param tabIndex |
| 114 |
* Tab index. |
| 115 |
*/ |
| 116 |
public void setTabIndex(int tabIndex) { |
| 117 |
this.iconLabel.setIcon(this.tabPane.getIconAt(tabIndex)); |
| 118 |
this.titleLabel.setText(this.tabPane.getTitleAt(tabIndex)); |
| 119 |
final boolean isSelected = (this.tabPane.getSelectedIndex() == tabIndex); |
| 120 |
Border innerBorder = isSelected ? new LineBorder(Color.black, 2) |
| 121 |
: new LineBorder(Color.black, 1); |
| 122 |
this |
| 123 |
.setBorder(new CompoundBorder(new ShadowPopupBorder(), |
| 124 |
innerBorder)); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Layout for the tab preview control. |
| 129 |
* |
| 130 |
* @author Kirill Grouchnikov |
| 131 |
*/ |
| 132 |
protected class TabPreviewControlLayout implements LayoutManager { |
| 133 |
/* |
| 134 |
* (non-Javadoc) |
| 135 |
* |
| 136 |
* @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, |
| 137 |
* java.awt.Component) |
| 138 |
*/ |
| 139 |
@Override |
| 140 |
public void addLayoutComponent(String name, Component comp) { |
| 141 |
} |
| 142 |
|
| 143 |
/* |
| 144 |
* (non-Javadoc) |
| 145 |
* |
| 146 |
* @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component) |
| 147 |
*/ |
| 148 |
@Override |
| 149 |
public void removeLayoutComponent(Component comp) { |
| 150 |
} |
| 151 |
|
| 152 |
/* |
| 153 |
* (non-Javadoc) |
| 154 |
* |
| 155 |
* @see java.awt.LayoutManager#layoutContainer(java.awt.Container) |
| 156 |
*/ |
| 157 |
@Override |
| 158 |
public void layoutContainer(Container parent) { |
| 159 |
int width = parent.getWidth(); |
| 160 |
int height = parent.getHeight(); |
| 161 |
|
| 162 |
Insets insets = TabPreviewControl.this.getInsets(); |
| 163 |
TabPreviewControl.this.iconLabel.setBounds(insets.left + 1, |
| 164 |
insets.top + 1, 16, 16); |
| 165 |
TabPreviewControl.this.titleLabel |
| 166 |
.setBounds(insets.left + 18, insets.top + 1, width - 18 |
| 167 |
- insets.left - insets.right, 16); |
| 168 |
TabPreviewControl.this.previewImagePanel.setBounds(insets.left + 1, |
| 169 |
insets.top + 17, width - insets.left - insets.right - 2, |
| 170 |
height - 17 - insets.top - insets.bottom); |
| 171 |
} |
| 172 |
|
| 173 |
/* |
| 174 |
* (non-Javadoc) |
| 175 |
* |
| 176 |
* @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container) |
| 177 |
*/ |
| 178 |
@Override |
| 179 |
public Dimension minimumLayoutSize(Container parent) { |
| 180 |
return parent.getSize(); |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
* (non-Javadoc) |
| 185 |
* |
| 186 |
* @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container) |
| 187 |
*/ |
| 188 |
@Override |
| 189 |
public Dimension preferredLayoutSize(Container parent) { |
| 190 |
return this.minimumLayoutSize(parent); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Sets the tab preview thumbnail. |
| 196 |
* |
| 197 |
* @param previewImage |
| 198 |
* Tab preview thumbnail. |
| 199 |
* @param toAnimate |
| 200 |
* if <code>true</code>, the image will be faded-in. |
| 201 |
*/ |
| 202 |
public void setPreviewImage(BufferedImage previewImage, boolean toAnimate) { |
| 203 |
this.previewImage = previewImage; |
| 204 |
if (toAnimate) { |
| 205 |
Timeline fadeTimeline = new Timeline(this); |
| 206 |
AnimationConfigurationManager.getInstance().configureTimeline( |
| 207 |
fadeTimeline); |
| 208 |
fadeTimeline.addPropertyToInterpolate("alpha", 0.0f, 1.0f); |
| 209 |
fadeTimeline.play(); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
public void setAlpha(float alpha) { |
| 214 |
this.alpha = alpha; |
| 215 |
this.repaint(); |
| 216 |
} |
| 217 |
|
| 218 |
public void setZoom(float zoom) { |
| 219 |
this.zoom = zoom; |
| 220 |
} |
| 221 |
|
| 222 |
public float getZoom() { |
| 223 |
return zoom; |
| 224 |
} |
| 225 |
} |