1 19 20 package org.netbeans.swing.tabcontrol.plaf; 21 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.awt.Font ; 25 import java.awt.FontMetrics ; 26 import java.awt.Graphics ; 27 import java.awt.Graphics2D ; 28 import java.awt.Insets ; 29 import java.awt.Rectangle ; 30 import org.netbeans.swing.tabcontrol.TabDisplayer; 31 import org.openide.awt.HtmlRenderer; 32 33 import javax.swing.plaf.ComponentUI ; 34 import java.awt.event.MouseEvent ; 35 import java.util.HashMap ; 36 import java.util.Map ; 37 import javax.swing.Icon ; 38 import javax.swing.JComponent ; 39 import javax.swing.UIManager ; 40 41 46 public final class AquaViewTabDisplayerUI extends AbstractViewTabDisplayerUI { 47 48 private static final int TXT_X_PAD = 5; 49 private static final int ICON_X_PAD = 2; 50 51 52 53 private static Map <Integer , String []> buttonIconPaths; 54 55 58 59 private Dimension prefSize; 60 64 private Rectangle tempRect = new Rectangle (); 65 66 69 private AquaViewTabDisplayerUI(TabDisplayer displayer) { 70 super(displayer); 71 prefSize = new Dimension (100, 19); } 73 74 public static ComponentUI createUI(JComponent c) { 75 return new AquaViewTabDisplayerUI((TabDisplayer) c); 76 } 77 78 protected AbstractViewTabDisplayerUI.Controller createController() { 79 return new OwnController(); 80 } 81 82 public Dimension getPreferredSize(JComponent c) { 83 FontMetrics fm = getTxtFontMetrics(); 84 int height = fm == null ? 85 21 : fm.getAscent() + 2 * fm.getDescent() + 3; 86 Insets insets = c.getInsets(); 87 prefSize.height = height + insets.bottom + insets.top; 88 return prefSize; 89 } 90 91 public void paint(Graphics g, JComponent c) { 92 ColorUtil.setupAntialiasing(g); 93 super.paint(g, c); 94 paintBottomBorder(g, c); 95 } 96 97 protected Font getTxtFont() { 98 return getDisplayer().getFont(); 99 } 100 101 104 private void paintBottomBorder(Graphics g, JComponent c) { 105 } 106 107 protected void paintTabContent(Graphics g, int index, String text, int x, 108 int y, int width, int height) { 109 FontMetrics fm = getTxtFontMetrics(); 110 111 g.setFont(getTxtFont()); 113 int textW = width; 114 115 if (isSelected(index)) { 116 Component buttons = getControlButtons(); 117 if( null != buttons ) { 118 Dimension buttonsSize = buttons.getPreferredSize(); 119 120 textW = width - (buttonsSize.width + ICON_X_PAD + 2*TXT_X_PAD); 121 if (index == getDataModel().size() - 1) { 122 textW -= 3; 123 } 124 buttons.setLocation( x + textW+2*TXT_X_PAD, y + (height-buttonsSize.height)/2 ); 125 } 126 } else { 127 textW = width - 2 * TXT_X_PAD; 128 } 129 130 if (text.length() == 0) { 131 return; 132 } 133 134 int textHeight = fm.getHeight(); 135 int textY; 136 int textX = x + TXT_X_PAD; 137 if (index == 0) 138 textX = x + 5; 139 140 if (textHeight > height) { 141 textY = (-1 * ((textHeight - height) / 2)) + fm.getAscent() 142 - 1; 143 } else { 144 textY = (height / 2) - (textHeight / 2) + fm.getAscent(); 145 } 146 147 HtmlRenderer.renderString(text, g, textX, textY, textW, height, getTxtFont(), 148 UIManager.getColor("textText"), 149 HtmlRenderer.STYLE_TRUNCATE, true); 150 } 151 152 protected void paintTabBorder(Graphics g, int index, int x, int y, 153 int width, int height) { 154 155 } 156 157 private static final ChicletWrapper chiclet = new ChicletWrapper(); 158 159 protected void paintTabBackground(Graphics g, int index, int x, int y, 160 int width, int height) { 161 boolean first = index == 0; 162 boolean last = index == getDataModel().size() - 1; 163 int state = 0; 164 if (isActive()) { 165 state |= GenericGlowingChiclet.STATE_ACTIVE; 166 } 167 if (isSelected(index)) { 168 state |= GenericGlowingChiclet.STATE_SELECTED; 169 } 170 if (isAttention(index)) { 171 state |= GenericGlowingChiclet.STATE_ATTENTION; 172 } 173 174 y+=1; 176 chiclet.setState(state); 177 chiclet.setBounds(x, y, width, height); 178 chiclet.setArcs(first ? 0.5f : 0f, last ? 0.5f : 0f, 179 first ? 0.0f : 0f, last ? 0.0f : 0f); 180 chiclet.setNotch(false, false); 181 g.translate (x, y); 182 chiclet.draw((Graphics2D ) g); 183 g.translate (-x, -y); 184 } 185 186 private boolean containsMouse = false; 187 188 private void setContainsMouse(boolean val) { 189 if (val != containsMouse) { 190 containsMouse = val; 191 getDisplayer().repaint(); 192 } 193 } 194 195 private boolean isContainsMouse() { 196 return containsMouse; 197 } 198 199 private static void initIcons() { 200 202 if( null == buttonIconPaths ) { 203 buttonIconPaths = new HashMap <Integer , String []>(7); 204 205 String [] iconPaths = new String [4]; 207 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/mac_bigclose_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/mac_bigclose_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 210 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/mac_bigclose_rollover.png"; buttonIconPaths.put( TabControlButton.ID_CLOSE_BUTTON, iconPaths ); 212 213 iconPaths = new String [4]; 215 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/mac_slideright_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/mac_slideright_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 218 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/mac_slideright_rollover.png"; buttonIconPaths.put( TabControlButton.ID_SLIDE_RIGHT_BUTTON, iconPaths ); 220 221 iconPaths = new String [4]; 222 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/mac_slideleft_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/mac_slideleft_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 225 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/mac_slideleft_rollover.png"; buttonIconPaths.put( TabControlButton.ID_SLIDE_LEFT_BUTTON, iconPaths ); 227 228 iconPaths = new String [4]; 229 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/mac_slidebottom_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/mac_slidebottom_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 232 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/mac_slidebottom_rollover.png"; buttonIconPaths.put( TabControlButton.ID_SLIDE_DOWN_BUTTON, iconPaths ); 234 235 iconPaths = new String [4]; 236 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/mac_pin_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/mac_pin_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 239 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/mac_pin_rollover.png"; buttonIconPaths.put( TabControlButton.ID_PIN_BUTTON, iconPaths ); 241 } 242 } 243 244 public Icon getButtonIcon(int buttonId, int buttonState) { 245 Icon res = null; 246 initIcons(); 247 String [] paths = buttonIconPaths.get( buttonId ); 248 if( null != paths && buttonState >=0 && buttonState < paths.length ) { 249 res = TabControlButtonFactory.getIcon( paths[buttonState] ); 250 } 251 return res; 252 } 253 254 257 private class OwnController extends Controller { 258 259 public void mouseEntered(MouseEvent me) { 260 super.mouseEntered(me); 261 setContainsMouse(true); 262 } 263 264 public void mouseExited(MouseEvent me) { 265 super.mouseExited(me); 266 setContainsMouse(false); 267 } 268 } 270 } 271 | Popular Tags |