| 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.text; |
| 31 |
|
| 32 |
import java.awt.*; |
| 33 |
|
| 34 |
import javax.swing.Icon; |
| 35 |
import javax.swing.JViewport; |
| 36 |
import javax.swing.border.Border; |
| 37 |
import javax.swing.border.EmptyBorder; |
| 38 |
import javax.swing.plaf.UIResource; |
| 39 |
|
| 40 |
import org.pushingpixels.lafwidget.*; |
| 41 |
|
| 42 |
/** |
| 43 |
* Border with "lock" indication. |
| 44 |
* |
| 45 |
* @author Kirill Grouchnikov |
| 46 |
*/ |
| 47 |
public class LockBorder implements Border, UIResource { |
| 48 |
/** |
| 49 |
* The original (decorated) border. |
| 50 |
*/ |
| 51 |
private Border originalBorder; |
| 52 |
|
| 53 |
/** |
| 54 |
* Constructs a new lock border. |
| 55 |
* |
| 56 |
* @param originalBorder |
| 57 |
* The original border. |
| 58 |
*/ |
| 59 |
public LockBorder(Border originalBorder) { |
| 60 |
if (originalBorder != null) |
| 61 |
this.originalBorder = originalBorder; |
| 62 |
else |
| 63 |
this.originalBorder = new EmptyBorder(0, 0, 0, 0); |
| 64 |
} |
| 65 |
|
| 66 |
/* |
| 67 |
* (non-Javadoc) |
| 68 |
* |
| 69 |
* @see javax.swing.border.Border#getBorderInsets(java.awt.Component) |
| 70 |
*/ |
| 71 |
@Override |
| 72 |
public Insets getBorderInsets(Component c) { |
| 73 |
LafWidgetSupport lafSupport = LafWidgetRepository.getRepository() |
| 74 |
.getLafSupport(); |
| 75 |
Icon lockIcon = lafSupport.getLockIcon(c); |
| 76 |
if (lockIcon == null) |
| 77 |
lockIcon = LafWidgetUtilities.getSmallLockIcon(); |
| 78 |
|
| 79 |
Insets origInsets = this.originalBorder.getBorderInsets(c); |
| 80 |
|
| 81 |
if (c.getComponentOrientation().isLeftToRight()) { |
| 82 |
return new Insets(origInsets.top, Math.max(origInsets.left, |
| 83 |
lockIcon.getIconWidth() + 2), origInsets.bottom, |
| 84 |
origInsets.right); |
| 85 |
} else { |
| 86 |
// support for RTL |
| 87 |
return new Insets(origInsets.top, origInsets.left, |
| 88 |
origInsets.bottom, Math.max(origInsets.right, lockIcon |
| 89 |
.getIconWidth() + 2)); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/* |
| 94 |
* (non-Javadoc) |
| 95 |
* |
| 96 |
* @see javax.swing.border.Border#isBorderOpaque() |
| 97 |
*/ |
| 98 |
@Override |
| 99 |
public boolean isBorderOpaque() { |
| 100 |
return this.originalBorder.isBorderOpaque(); |
| 101 |
} |
| 102 |
|
| 103 |
public Border getOriginalBorder() { |
| 104 |
return originalBorder; |
| 105 |
} |
| 106 |
|
| 107 |
/* |
| 108 |
* (non-Javadoc) |
| 109 |
* |
| 110 |
* @see javax.swing.border.Border#paintBorder(java.awt.Component, |
| 111 |
* java.awt.Graphics, int, int, int, int) |
| 112 |
*/ |
| 113 |
@Override |
| 114 |
public void paintBorder(Component c, Graphics g, int x, int y, int width, |
| 115 |
int height) { |
| 116 |
this.originalBorder.paintBorder(c, g, x, y, width, height); |
| 117 |
LafWidgetSupport lafSupport = LafWidgetRepository.getRepository() |
| 118 |
.getLafSupport(); |
| 119 |
Icon lockIcon = lafSupport.getLockIcon(c); |
| 120 |
if (lockIcon == null) |
| 121 |
lockIcon = LafWidgetUtilities.getSmallLockIcon(); |
| 122 |
|
| 123 |
int offsetY = 0; |
| 124 |
if (c.getParent() instanceof JViewport) { |
| 125 |
// enhancement 9 - show the lock icon of components |
| 126 |
// in JScrollPane so that it is visible in the bottom |
| 127 |
// corner of the scroll pane |
| 128 |
JViewport viewport = (JViewport) c.getParent(); |
| 129 |
// have to set to simple scroll mode since the default (blit) |
| 130 |
// results in visual artifacts due to optimized buffer-copy |
| 131 |
// painting. |
| 132 |
if (viewport.getScrollMode() != JViewport.SIMPLE_SCROLL_MODE) { |
| 133 |
viewport.setScrollMode(JViewport.SIMPLE_SCROLL_MODE); |
| 134 |
} |
| 135 |
Rectangle viewRect = viewport.getViewRect(); |
| 136 |
offsetY = c.getHeight() - viewRect.y - viewRect.height; |
| 137 |
} |
| 138 |
|
| 139 |
if (c.getComponentOrientation().isLeftToRight()) { |
| 140 |
lockIcon.paintIcon(c, g, x, y + height - lockIcon.getIconHeight() |
| 141 |
- offsetY); |
| 142 |
} else { |
| 143 |
// support for RTL |
| 144 |
lockIcon.paintIcon(c, g, x + width - lockIcon.getIconWidth(), y |
| 145 |
+ height - lockIcon.getIconHeight() - offsetY); |
| 146 |
} |
| 147 |
} |
| 148 |
} |