1 30 31 package com.jgoodies.looks.plastic; 32 33 import java.awt.Color ; 34 import java.awt.Font ; 35 import java.awt.FontMetrics ; 36 import java.awt.Graphics ; 37 import java.awt.Rectangle ; 38 39 import javax.swing.Icon ; 40 import javax.swing.JInternalFrame ; 41 import javax.swing.SwingUtilities ; 42 import javax.swing.plaf.metal.MetalInternalFrameTitlePane ; 43 44 50 public final class PlasticInternalFrameTitlePane 51 extends MetalInternalFrameTitlePane { 52 53 private PlasticBumps paletteBumps; 54 55 private final PlasticBumps activeBumps = 56 new PlasticBumps( 57 0, 58 0, 59 PlasticLookAndFeel.getPrimaryControlHighlight(), 60 PlasticLookAndFeel.getPrimaryControlDarkShadow(), 61 PlasticLookAndFeel.getPrimaryControl()); 62 63 private final PlasticBumps inactiveBumps = 64 new PlasticBumps( 65 0, 66 0, 67 PlasticLookAndFeel.getControlHighlight(), 68 PlasticLookAndFeel.getControlDarkShadow(), 69 PlasticLookAndFeel.getControl()); 70 71 public PlasticInternalFrameTitlePane(JInternalFrame frame) { 72 super(frame); 73 } 74 75 public void paintPalette(Graphics g) { 76 boolean leftToRight = PlasticUtils.isLeftToRight(frame); 77 78 int width = getWidth(); 79 int height = getHeight(); 80 81 if (paletteBumps == null) { 82 paletteBumps = 83 new PlasticBumps( 84 0, 85 0, 86 PlasticLookAndFeel.getPrimaryControlHighlight(), 87 PlasticLookAndFeel.getPrimaryControlInfo(), 88 PlasticLookAndFeel.getPrimaryControlShadow()); 89 } 90 91 Color background = PlasticLookAndFeel.getPrimaryControlShadow(); 92 Color darkShadow = PlasticLookAndFeel.getControlDarkShadow(); 93 94 g.setColor(background); 95 g.fillRect(0, 0, width, height); 96 97 g.setColor(darkShadow); 98 g.drawLine(0, height - 1, width, height - 1); 99 100 int buttonsWidth = getButtonsWidth(); 101 int xOffset = leftToRight ? 4 : buttonsWidth + 4; 102 int bumpLength = width - buttonsWidth - 2 * 4; 103 int bumpHeight = getHeight() - 4; 104 paletteBumps.setBumpArea(bumpLength, bumpHeight); 105 paletteBumps.paintIcon(this, g, xOffset, 2); 106 } 107 108 public void paintComponent(Graphics g) { 109 if (isPalette) { 110 paintPalette(g); 111 return; 112 } 113 114 boolean leftToRight = PlasticUtils.isLeftToRight(frame); 115 boolean isSelected = frame.isSelected(); 116 117 int width = getWidth(); 118 int height = getHeight(); 119 120 Color background = null; 121 Color foreground = null; 122 Color shadow = null; 123 124 PlasticBumps bumps; 125 126 if (isSelected) { 127 background = PlasticLookAndFeel.getWindowTitleBackground(); 128 foreground = PlasticLookAndFeel.getWindowTitleForeground(); 129 bumps = activeBumps; 130 } else { 131 background = PlasticLookAndFeel.getWindowTitleInactiveBackground(); 132 foreground = PlasticLookAndFeel.getWindowTitleInactiveForeground(); 133 bumps = inactiveBumps; 134 } 135 shadow = PlasticLookAndFeel.getControlDarkShadow(); 137 138 169 g.setColor(background); 170 g.fillRect(0, 0, width, height); 171 172 g.setColor(shadow); 173 g.drawLine(0, height - 1, width, height - 1); 174 g.drawLine(0, 0, 0, 0); 175 g.drawLine(width - 1, 0, width - 1, 0); 176 177 int titleLength = 0; 178 int xOffset = leftToRight ? 5 : width - 5; 179 String frameTitle = frame.getTitle(); 180 181 Icon icon = frame.getFrameIcon(); 182 if (icon != null) { 183 if (!leftToRight) 184 xOffset -= icon.getIconWidth(); 185 int iconY = ((height / 2) - (icon.getIconHeight() / 2)); 186 icon.paintIcon(frame, g, xOffset, iconY); 187 xOffset += leftToRight ? icon.getIconWidth() + 5 : -5; 188 } 189 190 if (frameTitle != null) { 191 Font f = getFont(); 192 g.setFont(f); 193 FontMetrics fm = g.getFontMetrics(); 194 196 g.setColor(foreground); 197 198 int yOffset = ((height - fm.getHeight()) / 2) + fm.getAscent(); 199 200 Rectangle rect = new Rectangle (0, 0, 0, 0); 201 if (frame.isIconifiable()) { 202 rect = iconButton.getBounds(); 203 } else if (frame.isMaximizable()) { 204 rect = maxButton.getBounds(); 205 } else if (frame.isClosable()) { 206 rect = closeButton.getBounds(); 207 } 208 int titleW; 209 210 if (leftToRight) { 211 if (rect.x == 0) { 212 rect.x = frame.getWidth() - frame.getInsets().right - 2; 213 } 214 titleW = rect.x - xOffset - 4; 215 frameTitle = getTitle(frameTitle, fm, titleW); 216 } else { 217 titleW = xOffset - rect.x - rect.width - 4; 218 frameTitle = getTitle(frameTitle, fm, titleW); 219 xOffset -= SwingUtilities.computeStringWidth(fm, frameTitle); 220 } 221 222 titleLength = SwingUtilities.computeStringWidth(fm, frameTitle); 223 g.drawString(frameTitle, xOffset, yOffset); 224 xOffset += leftToRight ? titleLength + 5 : -5; 225 } 226 227 int bumpXOffset; 228 int bumpLength; 229 int buttonsWidth = getButtonsWidth(); 230 if (leftToRight) { 231 bumpLength = width - buttonsWidth - xOffset - 5; 232 bumpXOffset = xOffset; 233 } else { 234 bumpLength = xOffset - buttonsWidth - 5; 235 bumpXOffset = buttonsWidth + 5; 236 } 237 int bumpYOffset = 3; 238 int bumpHeight = getHeight() - (2 * bumpYOffset); 239 bumps.setBumpArea(bumpLength, bumpHeight); 240 bumps.paintIcon(this, g, bumpXOffset, bumpYOffset); 241 } 242 243 protected String getTitle( 244 String text, 245 FontMetrics fm, 246 int availTextWidth) { 247 if ((text == null) || (text.equals(""))) 248 return ""; 249 int textWidth = SwingUtilities.computeStringWidth(fm, text); 250 String clipString = "..."; 251 if (textWidth > availTextWidth) { 252 int totalWidth = SwingUtilities.computeStringWidth(fm, clipString); 253 int nChars; 254 for (nChars = 0; nChars < text.length(); nChars++) { 255 totalWidth += fm.charWidth(text.charAt(nChars)); 256 if (totalWidth > availTextWidth) { 257 break; 258 } 259 } 260 text = text.substring(0, nChars) + clipString; 261 } 262 return text; 263 } 264 265 private int getButtonsWidth() { 266 boolean leftToRight = PlasticUtils.isLeftToRight(frame); 267 268 int w = getWidth(); 269 int x = leftToRight ? w : 0; 270 int spacing; 271 272 int buttonWidth = closeButton.getIcon().getIconWidth(); 275 276 if (frame.isClosable()) { 277 if (isPalette) { 278 spacing = 3; 279 x += leftToRight ? -spacing - (buttonWidth + 2) : spacing; 280 if (!leftToRight) 281 x += (buttonWidth + 2); 282 } else { 283 spacing = 4; 284 x += leftToRight ? -spacing - buttonWidth : spacing; 285 if (!leftToRight) 286 x += buttonWidth; 287 } 288 } 289 290 if (frame.isMaximizable() && !isPalette) { 291 spacing = frame.isClosable() ? 10 : 4; 292 x += leftToRight ? -spacing - buttonWidth : spacing; 293 if (!leftToRight) 294 x += buttonWidth; 295 } 296 297 if (frame.isIconifiable() && !isPalette) { 298 spacing = 299 frame.isMaximizable() ? 2 : (frame.isClosable() ? 10 : 4); 300 x += leftToRight ? -spacing - buttonWidth : spacing; 301 if (!leftToRight) 302 x += buttonWidth; 303 } 304 305 return leftToRight ? w - x : x; 306 } 307 308 } 309 | Popular Tags |