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.FontMetrics ; 26 import java.awt.Graphics ; 27 import java.awt.Insets ; 28 import java.awt.Rectangle ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 import javax.swing.Icon ; 32 import javax.swing.JComponent ; 33 import javax.swing.UIManager ; 34 import org.netbeans.swing.tabcontrol.TabDisplayer; 35 36 import javax.swing.plaf.ComponentUI ; 37 38 39 import org.openide.awt.HtmlRenderer; 40 41 47 public final class MetalViewTabDisplayerUI extends AbstractViewTabDisplayerUI { 48 49 52 private static final int TXT_X_PAD = 5; 53 54 private static final int ICON_X_LEFT_PAD = 5; 55 private static final int ICON_X_RIGHT_PAD = 2; 56 57 private static final int BUMP_X_PAD = 5; 58 private static final int BUMP_Y_PAD = 4; 59 60 63 64 private static Color inactBgColor, actBgColor, borderHighlight, borderShadow; 65 66 private static Map <Integer , String []> buttonIconPaths; 67 68 71 72 private Dimension prefSize; 73 74 78 private Rectangle tempRect = new Rectangle (); 79 80 83 private MetalViewTabDisplayerUI(TabDisplayer displayer) { 84 super(displayer); 85 prefSize = new Dimension (100, 19); 86 } 87 88 public static ComponentUI createUI(JComponent c) { 89 return new MetalViewTabDisplayerUI((TabDisplayer) c); 90 } 91 92 public Dimension getPreferredSize(JComponent c) { 93 FontMetrics fm = getTxtFontMetrics(); 94 int height = fm == null ? 95 21 : fm.getAscent() + 2 * fm.getDescent() + 4; 96 Insets insets = c.getInsets(); 97 prefSize.height = height + insets.bottom + insets.top; 98 return prefSize; 99 } 100 101 105 public void paint(Graphics g, JComponent c) { 106 super.paint(g, c); 107 paintBottomBorder(g, c); 108 } 109 110 113 private void paintBottomBorder(Graphics g, JComponent c) { 114 Color color = isActive() ? getActBgColor() : getInactBgColor(); 115 g.setColor(color); 116 Rectangle bounds = c.getBounds(); 117 g.fillRect(1, bounds.height - 3, bounds.width - 1, 2); 118 g.setColor(getBorderShadow()); 119 g.drawLine(1, bounds.height - 1, bounds.width - 1, bounds.height - 1); 120 } 121 122 protected void paintTabContent(Graphics g, int index, String text, int x, 123 int y, int width, int height) { 124 FontMetrics fm = getTxtFontMetrics(); 125 g.setFont(getTxtFont()); 127 int txtWidth = width; 128 if (isSelected(index)) { 129 Component buttons = getControlButtons(); 130 int buttonsWidth = 0; 131 if( null != buttons ) { 132 Dimension buttonsSize = buttons.getPreferredSize(); 133 buttonsWidth = buttonsSize.width + ICON_X_LEFT_PAD + ICON_X_RIGHT_PAD; 134 txtWidth = width - (buttonsWidth + 2*TXT_X_PAD); 135 buttons.setLocation( x + txtWidth+2*TXT_X_PAD+ICON_X_LEFT_PAD, y + (height-buttonsSize.height)/2+1 ); 136 } 137 138 txtWidth = (int)HtmlRenderer.renderString(text, g, x + TXT_X_PAD, height - 139 fm.getDescent() - 4, txtWidth, height, getTxtFont(), 140 UIManager.getColor("textText"), 141 HtmlRenderer.STYLE_TRUNCATE, true); 142 int bumpWidth = width 143 - (TXT_X_PAD + txtWidth + BUMP_X_PAD + buttonsWidth); 144 if (bumpWidth > 0) { 145 paintBump(index, g, x + TXT_X_PAD + txtWidth + BUMP_X_PAD, 146 y + BUMP_Y_PAD, bumpWidth, height - 2 * BUMP_Y_PAD); 147 } 148 } else { 149 txtWidth = width - 2 * TXT_X_PAD; 150 HtmlRenderer.renderString(text, g, x + TXT_X_PAD, height - 151 fm.getDescent() - 4, txtWidth, height, getTxtFont(), 152 UIManager.getColor("textText"), 153 HtmlRenderer.STYLE_TRUNCATE, true); 154 } 155 } 156 157 protected void paintTabBorder(Graphics g, int index, int x, int y, 158 int width, int height) { 159 Color highlight = getBorderHighlight(); 160 Color shadow = getBorderShadow(); 161 boolean isSelected = isSelected(index); 162 163 boolean isFirst = index == 0; 164 boolean isLast = index == getDataModel().size() - 1; 165 166 g.translate(x, y); 167 168 g.setColor(shadow); 170 if (!isFirst) { 171 g.drawLine(0, 0, 0, height - 5); 172 } 173 if (!isSelected) { 174 g.drawLine(1, height - 5, isLast ? width - 1 : width, height - 5); 175 } 176 g.setColor(highlight); 178 g.drawLine(1, 0, width - 1, 0); 179 if (isFirst) { 180 g.drawLine(0, 0, 0, height - 2); 181 } 182 if (!isSelected) { 183 g.drawLine(0, height - 4, isLast ? width - 1 : width, height - 4); 184 } 185 186 g.translate(-x, -y); 187 } 188 189 protected void paintTabBackground(Graphics g, int index, int x, int y, 190 int width, int height) { 191 boolean selected = isSelected(index); 192 boolean highlighted = selected && isActive(); 193 boolean attention = isAttention (index); 194 if (highlighted && !attention) { 195 g.setColor(getActBgColor()); 196 g.fillRect(x, y, width, height - 3); 197 } else if (attention) { 198 g.setColor(MetalEditorTabCellRenderer.ATTENTION_COLOR); 199 g.fillRect(x, y, width, height - 3); 200 } else { 201 g.setColor(getInactBgColor()); 202 g.fillRect(x, y, width, height - 3); 203 } 204 } 205 206 private void paintBump(int index, Graphics g, int x, int y, int width, 207 int height) { 208 ColorUtil.paintViewTabBump(g, x, y, width, height, isFocused(index) ? 209 ColorUtil.FOCUS_TYPE : 210 ColorUtil.UNSEL_TYPE); 211 } 212 213 static Color getInactBgColor() { 214 if (inactBgColor == null) { 215 inactBgColor = (Color ) UIManager.get("inactiveCaption"); 216 if (inactBgColor == null) { 217 inactBgColor = new Color (204, 204, 204); 218 } 219 } 220 return inactBgColor; 221 } 222 223 static Color getActBgColor() { 224 if (actBgColor == null) { 225 actBgColor = (Color ) UIManager.get("activeCaption"); 226 if (actBgColor == null) { 227 actBgColor = new Color (204, 204, 255); 228 } 229 } 230 return actBgColor; 231 } 232 233 private Color getBorderHighlight() { 234 if (borderHighlight == null) { 235 borderHighlight = getInactBgColor().brighter(); 236 } 237 return borderHighlight; 238 } 239 240 private Color getBorderShadow() { 241 if (borderShadow == null) { 242 borderShadow = getInactBgColor().darker(); 243 } 244 return borderShadow; 245 } 246 247 248 private static void initIcons() { 249 if( null == buttonIconPaths ) { 250 buttonIconPaths = new HashMap <Integer , String []>(7); 251 252 String [] iconPaths = new String [4]; 254 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/metal_bigclose_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/metal_bigclose_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 257 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/metal_bigclose_rollover.png"; buttonIconPaths.put( TabControlButton.ID_CLOSE_BUTTON, iconPaths ); 259 260 iconPaths = new String [4]; 262 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/metal_slideright_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/metal_slideright_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 265 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/metal_slideright_rollover.png"; buttonIconPaths.put( TabControlButton.ID_SLIDE_RIGHT_BUTTON, iconPaths ); 267 268 iconPaths = new String [4]; 269 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/metal_slideleft_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/metal_slideleft_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 272 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/metal_slideleft_rollover.png"; buttonIconPaths.put( TabControlButton.ID_SLIDE_LEFT_BUTTON, iconPaths ); 274 275 iconPaths = new String [4]; 276 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/metal_slidebottom_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/metal_slidebottom_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 279 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/metal_slidebottom_rollover.png"; buttonIconPaths.put( TabControlButton.ID_SLIDE_DOWN_BUTTON, iconPaths ); 281 282 iconPaths = new String [4]; 283 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/metal_pin_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/metal_pin_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 286 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/metal_pin_rollover.png"; buttonIconPaths.put( TabControlButton.ID_PIN_BUTTON, iconPaths ); 288 } 289 } 290 291 public Icon getButtonIcon(int buttonId, int buttonState) { 292 Icon res = null; 293 initIcons(); 294 String [] paths = buttonIconPaths.get( buttonId ); 295 if( null != paths && buttonState >=0 && buttonState < paths.length ) { 296 res = TabControlButtonFactory.getIcon( paths[buttonState] ); 297 } 298 return res; 299 } 300 } 301 | Popular Tags |