1 19 20 package org.netbeans.swing.tabcontrol.plaf; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.awt.Font ; 26 import java.awt.FontMetrics ; 27 import java.awt.Graphics ; 28 import java.awt.Graphics2D ; 29 import java.awt.Insets ; 30 import java.awt.Point ; 31 import java.awt.Rectangle ; 32 import org.netbeans.swing.tabcontrol.TabDisplayer; 33 34 import javax.swing.plaf.ComponentUI ; 35 import java.awt.event.MouseEvent ; 36 37 import java.util.HashMap ; 38 import java.util.Map ; 39 import javax.swing.Icon ; 40 import javax.swing.JComponent ; 41 import javax.swing.UIManager ; 42 import org.netbeans.swing.tabcontrol.event.TabActionEvent; 43 44 45 import org.openide.awt.HtmlRenderer; 46 47 52 public final class WinVistaViewTabDisplayerUI extends AbstractViewTabDisplayerUI { 53 54 55 56 59 private static final int TXT_X_PAD = 9; 60 private static final int TXT_Y_PAD = 3; 61 62 private static final int ICON_X_PAD = 4; 63 64 private static final int BUMP_X_PAD = 3; 65 private static final int BUMP_Y_PAD_UPPER = 6; 66 private static final int BUMP_Y_PAD_BOTTOM = 3; 67 68 69 70 73 private static boolean colorsReady = false; 74 75 private static Color 76 unselFillBrightUpperC, 77 unselFillDarkUpperC, 78 unselFillBrightLowerC, 79 unselFillDarkLowerC, 80 selFillC, 81 focusFillUpperC, 82 focusFillBrightLowerC, 83 focusFillDarkLowerC, 84 mouseOverFillBrightUpperC, 85 mouseOverFillDarkUpperC, 86 mouseOverFillBrightLowerC, 87 mouseOverFillDarkLowerC, 88 txtC, 89 borderC, 90 selBorderC, 91 borderInnerC; 92 93 private static Map <Integer , String []> buttonIconPaths; 94 95 98 99 private Dimension prefSize; 100 101 105 private Rectangle tempRect = new Rectangle (); 106 107 110 private WinVistaViewTabDisplayerUI(TabDisplayer displayer) { 111 super(displayer); 112 prefSize = new Dimension (100, 17); 113 } 114 115 public static ComponentUI createUI(JComponent c) { 116 return new WinVistaViewTabDisplayerUI((TabDisplayer)c); 117 } 118 119 public void installUI (JComponent c) { 120 super.installUI(c); 121 initColors(); 122 c.setOpaque(true); 123 } 124 125 protected AbstractViewTabDisplayerUI.Controller createController() { 126 return new OwnController(); 127 } 128 129 public Dimension getPreferredSize(JComponent c) { 130 FontMetrics fm = getTxtFontMetrics(); 131 int height = fm == null ? 132 17 : fm.getAscent() + 2 * fm.getDescent() + 3; 133 Insets insets = c.getInsets(); 134 prefSize.height = height + insets.bottom + insets.top; 135 return prefSize; 136 } 137 138 protected void paintTabContent(Graphics g, int index, String text, int x, 139 int y, int width, int height) { 140 FontMetrics fm = getTxtFontMetrics(); 141 g.setFont(getTxtFont()); 143 if( 0 == index ) 144 x++; 145 int txtWidth = width; 146 if (isSelected(index)) { 147 Component buttons = getControlButtons(); 148 if( null != buttons ) { 149 Dimension buttonsSize = buttons.getPreferredSize(); 150 txtWidth = width - (buttonsSize.width + ICON_X_PAD + 2*TXT_X_PAD); 151 buttons.setLocation( x + txtWidth+2*TXT_X_PAD, y + (height-buttonsSize.height)/2 ); 152 } 153 } else { 154 txtWidth = width - 2 * TXT_X_PAD; 155 } 156 157 ColorUtil.paintVistaTabDragTexture(getDisplayer(), g, x + BUMP_X_PAD, y 159 + BUMP_Y_PAD_UPPER, height - (BUMP_Y_PAD_UPPER 160 + BUMP_Y_PAD_BOTTOM)); 161 HtmlRenderer.renderString(text, g, x + TXT_X_PAD, y + fm.getAscent() 162 + TXT_Y_PAD, txtWidth, height, getTxtFont(), 163 txtC, 164 HtmlRenderer.STYLE_TRUNCATE, true); 165 } 166 167 protected void paintTabBorder(Graphics g, int index, int x, int y, 168 int width, int height) { 169 boolean isFirst = index == 0; 170 boolean isHighlighted = isTabHighlighted(index); 171 172 g.translate(x, y); 173 174 Color borderColor = isHighlighted ? selBorderC : borderC; 175 g.setColor(borderColor); 176 int left = 0; 177 if (isFirst ) 179 g.drawLine(0, 0, 0, height - 2); 180 g.drawLine(0, 0, width - 1, 0); 182 if( index < getDataModel().size()-1 && isTabHighlighted(index+1) ) 184 g.setColor( selBorderC ); 185 g.drawLine(width - 1, 0, width - 1, height - 2); 186 g.setColor(borderC); 188 g.drawLine(0, height - 1, width - 1, height - 1); 189 190 g.setColor(borderInnerC); 192 if (isFirst) 194 g.drawLine(1, 1, 1, height - 2); 195 else 196 g.drawLine(0, 1, 0, height - 2); 197 g.drawLine(width-2, 1, width-2, height - 2); 199 g.drawLine(1, 1, width-2, 1); 201 202 g.translate(-x, -y); 203 } 204 205 protected void paintTabBackground(Graphics g, int index, int x, int y, 206 int width, int height) { 207 y += 2; 209 height -= 2; 210 boolean selected = isSelected(index); 212 boolean focused = selected && isActive(); 213 boolean attention = isAttention(index); 214 boolean mouseOver = isMouseOver(index); 215 if (focused && !attention) { 216 ColorUtil.vistaFillRectGradient((Graphics2D ) g, x, y, width, height, 217 focusFillUpperC, 218 focusFillBrightLowerC, focusFillDarkLowerC ); 219 } else if (selected && !attention) { 220 g.setColor(selFillC); 221 g.fillRect(x, y, width, height); 222 } else if (mouseOver && !attention) { 223 ColorUtil.vistaFillRectGradient((Graphics2D ) g, x, y, width, height, 224 mouseOverFillBrightUpperC, mouseOverFillDarkUpperC, 225 mouseOverFillBrightLowerC, mouseOverFillDarkLowerC ); 226 } else if (attention) { 227 Color a = new Color (255, 255, 128); 228 Color b = new Color (230, 200, 64); 229 ColorUtil.xpFillRectGradient((Graphics2D ) g, x, y, width, height, a, b); 230 } else { 231 ColorUtil.vistaFillRectGradient((Graphics2D ) g, x, y, width, height, 232 unselFillBrightUpperC, unselFillDarkUpperC, 233 unselFillBrightLowerC, unselFillDarkLowerC ); 234 } 235 } 236 237 240 protected Font getTxtFont() { 241 Font font = super.getTxtFont(); 242 if (!font.isBold()) { 243 font = font.deriveFont(Font.BOLD); 244 } 245 return font; 246 } 247 248 251 private boolean isTabHighlighted(int index) { 252 if (((OwnController) getController()).getMouseIndex() == index) { 253 return true; 254 } 255 return isSelected(index) && isActive(); 256 } 257 258 262 private boolean isMouseOver(int index) { 263 return ((OwnController) getController()).getMouseIndex() == index 264 && !isSelected(index); 265 } 266 267 270 private static void initColors() { 271 if (!colorsReady) { 272 txtC = UIManager.getColor("TabbedPane.foreground"); 274 selFillC = UIManager.getColor("tab_sel_fill"); 276 focusFillUpperC = UIManager.getColor("tab_focus_fill_upper"); focusFillBrightLowerC = UIManager.getColor("tab_focus_fill_bright_lower"); focusFillDarkLowerC = UIManager.getColor("tab_focus_fill_dark_lower"); 280 unselFillBrightUpperC = UIManager.getColor("tab_unsel_fill_bright_upper"); unselFillDarkUpperC = UIManager.getColor("tab_unsel_fill_dark_upper"); unselFillBrightLowerC = UIManager.getColor("tab_unsel_fill_bright_lower"); unselFillDarkLowerC = UIManager.getColor("tab_unsel_fill_dark_lower"); 285 mouseOverFillBrightUpperC = UIManager.getColor("tab_mouse_over_fill_bright_upper"); mouseOverFillDarkUpperC = UIManager.getColor("tab_mouse_over_fill_dark_upper"); mouseOverFillBrightLowerC = UIManager.getColor("tab_mouse_over_fill_bright_lower"); mouseOverFillDarkLowerC = UIManager.getColor("tab_mouse_over_fill_dark_lower"); 290 borderC = UIManager.getColor("tab_border"); selBorderC = UIManager.getColor("tab_sel_border"); borderInnerC = UIManager.getColor("tab_border_inner"); 294 colorsReady = true; 295 } 296 } 297 298 private static void initIcons() { 299 if( null == buttonIconPaths ) { 300 buttonIconPaths = new HashMap <Integer , String []>(7); 301 302 String [] iconPaths = new String [4]; 304 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/vista_bigclose_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/vista_bigclose_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 307 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/vista_bigclose_rollover.png"; buttonIconPaths.put( TabControlButton.ID_CLOSE_BUTTON, iconPaths ); 309 310 iconPaths = new String [4]; 312 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/vista_slideright_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/vista_slideright_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 315 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/vista_slideright_rollover.png"; buttonIconPaths.put( TabControlButton.ID_SLIDE_RIGHT_BUTTON, iconPaths ); 317 318 iconPaths = new String [4]; 319 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/vista_slideleft_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/vista_slideleft_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 322 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/vista_slideleft_rollover.png"; buttonIconPaths.put( TabControlButton.ID_SLIDE_LEFT_BUTTON, iconPaths ); 324 325 iconPaths = new String [4]; 326 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/vista_slidebottom_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/vista_slidebottom_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 329 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/vista_slidebottom_rollover.png"; buttonIconPaths.put( TabControlButton.ID_SLIDE_DOWN_BUTTON, iconPaths ); 331 332 iconPaths = new String [4]; 333 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/vista_pin_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/vista_pin_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 336 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/vista_pin_rollover.png"; buttonIconPaths.put( TabControlButton.ID_PIN_BUTTON, iconPaths ); 338 } 339 } 340 341 public Icon getButtonIcon(int buttonId, int buttonState) { 342 Icon res = null; 343 initIcons(); 344 String [] paths = buttonIconPaths.get( buttonId ); 345 if( null != paths && buttonState >=0 && buttonState < paths.length ) { 346 res = TabControlButtonFactory.getIcon( paths[buttonState] ); 347 } 348 return res; 349 } 350 351 public void postTabAction(TabActionEvent e) { 352 super.postTabAction(e); 353 if( TabDisplayer.COMMAND_MAXIMIZE.equals( e.getActionCommand() ) ) { 354 ((OwnController)getController()).updateHighlight( -1 ); 355 } 356 } 357 358 361 private class OwnController extends Controller { 362 363 367 private int lastIndex = -1; 369 370 373 public int getMouseIndex() { 374 return lastIndex; 375 } 376 377 381 public void mouseMoved(MouseEvent e) { 382 super.mouseMoved(e); 383 Point pos = e.getPoint(); 384 updateHighlight(getLayoutModel().indexOfPoint(pos.x, pos.y)); 385 } 386 387 390 public void mouseExited(MouseEvent e) { 391 super.mouseExited(e); 392 if( !inControlButtonsRect(e.getPoint())) { 393 updateHighlight(-1); 394 } 395 } 396 397 400 private void updateHighlight(int curIndex) { 401 if (curIndex == lastIndex) { 402 return; 403 } 404 TabLayoutModel tlm = getLayoutModel(); 406 int x, y, w, h; 407 Rectangle repaintRect = null; 408 if (curIndex != -1) { 409 x = tlm.getX(curIndex)-1; 410 y = tlm.getY(curIndex); 411 w = tlm.getW(curIndex)+2; 412 h = tlm.getH(curIndex); 413 repaintRect = new Rectangle (x, y, w, h); 414 } 415 if ((lastIndex != -1) && (lastIndex < getDataModel().size())) { 417 x = tlm.getX(lastIndex)-1; 418 y = tlm.getY(lastIndex); 419 w = tlm.getW(lastIndex)+2; 420 h = tlm.getH(lastIndex); 421 if (repaintRect != null) { 422 repaintRect = 423 repaintRect.union(new Rectangle (x, y, w, h)); 424 } else { 425 repaintRect = new Rectangle (x, y, w, h); 426 } 427 } 428 if (repaintRect != null) { 430 getDisplayer().repaint(repaintRect); 431 } 432 lastIndex = curIndex; 433 } 434 } } 436 | Popular Tags |