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.Insets ; 29 import java.awt.Rectangle ; 30 import org.netbeans.swing.tabcontrol.TabDisplayer; 31 32 import javax.swing.plaf.ComponentUI ; 33 import java.util.HashMap ; 34 import java.util.Map ; 35 import javax.swing.Icon ; 36 import javax.swing.JComponent ; 37 import javax.swing.JTabbedPane ; 38 import javax.swing.UIManager ; 39 import javax.swing.plaf.synth.Region ; 40 import javax.swing.plaf.synth.SynthConstants ; 41 import javax.swing.plaf.synth.SynthContext ; 42 import javax.swing.plaf.synth.SynthLookAndFeel ; 43 import javax.swing.plaf.synth.SynthPainter ; 44 import javax.swing.plaf.synth.SynthStyle ; 45 import javax.swing.plaf.synth.SynthStyleFactory ; 46 import org.openide.awt.HtmlRenderer; 47 48 54 public final class GtkViewTabDisplayerUI extends AbstractViewTabDisplayerUI { 55 56 59 60 private static final int BUMP_X_PAD = 0; 61 private static final int BUMP_WIDTH = 0; 62 private static final int TXT_X_PAD = 3; 63 private static final int TXT_Y_PAD = 6; 64 65 private static final int ICON_X_PAD = 2; 66 67 private static Map <Integer , String []> buttonIconPaths; 68 69 private static JTabbedPane dummyTab; 70 71 74 75 private Dimension prefSize; 76 77 81 private Rectangle tempRect = new Rectangle (); 82 83 86 private GtkViewTabDisplayerUI(TabDisplayer displayer) { 87 super(displayer); 88 prefSize = new Dimension (100, 19); 89 } 90 91 public static ComponentUI createUI(JComponent c) { 92 return new GtkViewTabDisplayerUI((TabDisplayer) c); 93 } 94 95 public Dimension getPreferredSize(JComponent c) { 96 FontMetrics fm = getTxtFontMetrics(); 97 int height = fm == null ? 98 19 : fm.getAscent() + 2 * fm.getDescent() + 5; 99 Insets insets = c.getInsets(); 100 prefSize.height = height + insets.bottom + insets.top; 101 return prefSize; 102 } 103 104 107 public void paint(Graphics g, JComponent c) { 108 109 ColorUtil.setupAntialiasing(g); 110 111 Color col = c.getBackground(); 112 if (col != null) { 113 g.setColor (col); 114 g.fillRect (0, 0, c.getWidth(), c.getHeight()); 115 } 116 paintOverallBorder(g, c); 117 super.paint(g, c); 118 } 119 120 123 protected void paintOverallBorder(Graphics g, JComponent c) { 124 return; 125 } 126 127 protected Font getTxtFont() { 128 Font result = UIManager.getFont("controlFont"); 129 if (result != null) { 130 return result; 131 } 132 return super.getTxtFont(); 133 } 134 135 protected void paintTabContent(Graphics g, int index, String text, int x, 136 int y, int width, int height) { 137 height--; 139 y -= 2; FontMetrics fm = getTxtFontMetrics(); 141 g.setFont(getTxtFont()); 143 int txtWidth = width; 144 if (isSelected(index)) { 145 Component buttons = getControlButtons(); 146 if( null != buttons ) { 147 Dimension buttonsSize = buttons.getPreferredSize(); 148 txtWidth = width - (buttonsSize.width + ICON_X_PAD + 2*TXT_X_PAD); 149 buttons.setLocation(x + txtWidth + 2 * TXT_X_PAD, y + (height - buttonsSize.height)/2 + (TXT_Y_PAD / 2)); 150 } 151 } else { 152 txtWidth = width - 2 * TXT_X_PAD; 153 } 154 drawBump(g, index, x + 4, y + 6, BUMP_WIDTH, height - 8); 156 157 Color txtC = UIManager.getColor("TabbedPane.foreground"); 160 HtmlRenderer.renderString(text, g, x + TXT_X_PAD, y + fm.getAscent() 161 + TXT_Y_PAD, 162 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 170 return; 171 } 172 173 private static void paintTabBackgroundNative (Graphics g, int index, int state, 174 int x, int y, int w, int h) { 175 if (dummyTab == null) { 176 dummyTab = new JTabbedPane (); 177 } 178 Region region = Region.TABBED_PANE_TAB; 179 SynthLookAndFeel laf = (SynthLookAndFeel ) UIManager.getLookAndFeel(); 180 SynthStyleFactory sf = laf.getStyleFactory(); 181 SynthStyle style = sf.getStyle(dummyTab, region); 182 SynthContext context = 183 new SynthContext (dummyTab, region, style, state); 184 SynthPainter painter = style.getPainter(context); 185 painter.paintTabbedPaneTabBackground(context, g, x, y, w, h, index); 186 } 187 188 protected void paintTabBackground(Graphics g, int index, int x, int y, 189 int width, int height) { 190 if (isSelected(index)) { 191 paintTabBackgroundNative(g, 0, SynthConstants.SELECTED, 192 x, y, width, height); 193 } else { 194 paintTabBackgroundNative(g, 0, 0, 195 x, y + 2, width, height - 2); 196 } 197 } 198 199 202 private void drawBump(Graphics g, int index, int x, int y, int width, 203 int height) { 204 return; 206 } 207 208 private static void initIcons() { 209 if( null == buttonIconPaths ) { 210 buttonIconPaths = new HashMap <Integer , String []>(7); 211 212 String [] iconPaths = new String [4]; 214 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/gtk_bigclose_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/gtk_bigclose_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 217 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/gtk_bigclose_rollover.png"; buttonIconPaths.put( TabControlButton.ID_CLOSE_BUTTON, iconPaths ); 219 220 iconPaths = new String [4]; 222 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/gtk_slideright_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/gtk_slideright_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 225 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/gtk_slideright_rollover.png"; buttonIconPaths.put( TabControlButton.ID_SLIDE_RIGHT_BUTTON, iconPaths ); 227 228 iconPaths = new String [4]; 229 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/gtk_slideleft_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/gtk_slideleft_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 232 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/gtk_slideleft_rollover.png"; buttonIconPaths.put( TabControlButton.ID_SLIDE_LEFT_BUTTON, iconPaths ); 234 235 iconPaths = new String [4]; 236 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/gtk_slidebottom_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/gtk_slidebottom_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 239 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/gtk_slidebottom_rollover.png"; buttonIconPaths.put( TabControlButton.ID_SLIDE_DOWN_BUTTON, iconPaths ); 241 242 iconPaths = new String [4]; 243 iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/gtk_pin_enabled.png"; iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/gtk_pin_pressed.png"; iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT]; 246 iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/gtk_pin_rollover.png"; buttonIconPaths.put( TabControlButton.ID_PIN_BUTTON, iconPaths ); 248 } 249 } 250 251 public Icon getButtonIcon(int buttonId, int buttonState) { 252 Icon res = null; 253 initIcons(); 254 String [] paths = buttonIconPaths.get( buttonId ); 255 if( null != paths && buttonState >=0 && buttonState < paths.length ) { 256 res = TabControlButtonFactory.getIcon( paths[buttonState] ); 257 } 258 return res; 259 } 260 261 } 262 | Popular Tags |