1 19 20 package org.netbeans.swing.tabcontrol; 21 22 import java.awt.*; 23 import java.awt.geom.AffineTransform ; 24 import javax.swing.AbstractButton ; 25 import javax.swing.ButtonModel ; 26 import javax.swing.JComponent ; 27 import javax.swing.SwingUtilities ; 28 29 import javax.swing.plaf.ComponentUI ; 30 import javax.swing.plaf.basic.BasicHTML ; 31 import javax.swing.plaf.basic.BasicToggleButtonUI ; 32 import javax.swing.text.View ; 33 import org.netbeans.swing.tabcontrol.SlideBarDataModel; 34 import org.netbeans.swing.tabcontrol.SlidingButton; 35 36 49 public class SlidingButtonUI extends BasicToggleButtonUI { 50 51 private static final BasicToggleButtonUI INSTANCE = new SlidingButtonUI(); 53 54 55 protected SlidingButtonUI() { 56 } 57 58 public static ComponentUI createUI(JComponent c) { 59 return INSTANCE; 60 } 61 62 protected String getPropertyPrefix() { 63 return "ToggleButton."; 65 } 66 67 public void paint(Graphics g, JComponent c) { 68 AbstractButton b = (AbstractButton ) c; 69 ButtonModel model = b.getModel(); 70 Dimension size = b.getSize(); 71 FontMetrics fm = g.getFontMetrics(); 72 Insets i = c.getInsets(); 73 Rectangle viewRect = new Rectangle(size); 74 Rectangle iconRect = new Rectangle(); 75 Rectangle textRect = new Rectangle(); 76 Font f = c.getFont(); 77 g.setFont(f); 78 79 Rectangle rotatedViewRect; 80 Rectangle rotatedIconRect = new Rectangle(); 81 Rectangle rotatedTextRect = new Rectangle(); 82 SlidingButton slide = (SlidingButton)b; 83 Graphics2D g2d = (Graphics2D)g; 84 int orientation = slide.getOrientation(); 85 if (orientation == SlideBarDataModel.SOUTH) { 86 rotatedViewRect = new Rectangle(0, 0, viewRect.width, viewRect.height); 87 } else { 88 rotatedViewRect = new Rectangle(0, 0, viewRect.height, viewRect.width); 89 } 90 91 String text = SwingUtilities.layoutCompoundLabel( 93 c, fm, b.getText(), b.getIcon(), 94 b.getVerticalAlignment(), b.getHorizontalAlignment(), 95 b.getVerticalTextPosition(), b.getHorizontalTextPosition(), 96 rotatedViewRect, rotatedIconRect, rotatedTextRect, 97 b.getText() == null ? 0 : b.getIconTextGap()); 98 99 if (orientation == SlideBarDataModel.SOUTH) { 100 iconRect = new Rectangle(viewRect.x + rotatedIconRect.x, viewRect.y + rotatedIconRect.y, 101 rotatedIconRect.width, rotatedIconRect.height); 102 textRect = new Rectangle(viewRect.x + rotatedTextRect.x, viewRect.y + rotatedTextRect.y, 103 rotatedTextRect.width, rotatedTextRect.height); 104 } 105 if (orientation == SlideBarDataModel.WEST) { 106 iconRect = new Rectangle(viewRect.x + rotatedIconRect.y, 107 viewRect.y + viewRect.height - rotatedIconRect.x - rotatedIconRect.width, 108 rotatedIconRect.height, 109 rotatedIconRect.width); 110 textRect = new Rectangle(viewRect.x + rotatedTextRect.y, 111 viewRect.y + viewRect.height - rotatedTextRect.y - rotatedTextRect.width, 112 rotatedTextRect.height, 113 rotatedTextRect.width); 114 } 115 if (orientation == SlideBarDataModel.EAST) { 116 iconRect = new Rectangle(viewRect.x + viewRect.width - rotatedIconRect.y - rotatedIconRect.height, 117 viewRect.y + rotatedIconRect.x, 118 rotatedIconRect.height, 119 rotatedIconRect.width); 120 textRect = new Rectangle(viewRect.x + viewRect.width - rotatedTextRect.y - rotatedTextRect.height, 121 viewRect.y + rotatedTextRect.x, 122 rotatedTextRect.height, 123 rotatedTextRect.width); 124 } 125 126 g.setColor(b.getBackground()); 127 128 if (model.isArmed() && model.isPressed() || model.isSelected() || model.isRollover()) { 129 paintButtonPressed(g,b); 130 } else if (b.isOpaque()) { 131 paintBackground(g2d, b); 132 } 133 134 135 136 if(b.getIcon() != null) { 138 paintIcon(g, b, iconRect); 139 } 140 141 if(text != null && !text.equals("")) { 143 144 145 AffineTransform saveTr = g2d.getTransform(); 146 if (orientation != SlideBarDataModel.SOUTH) { 147 if (orientation == SlideBarDataModel.WEST) { 148 g2d.rotate( -Math.PI / 2 ); 150 g2d.translate(-c.getHeight(), 0 ); 151 } else { 152 g2d.rotate( Math.PI / 2 ); 154 g2d.translate( 0, - c.getWidth() ); 155 } 156 } 157 158 159 View v = (View ) c.getClientProperty(BasicHTML.propertyKey); 160 if (v != null) { 161 v.paint(g, rotatedTextRect); 162 } else { 163 paintText(g, b, rotatedTextRect, text); 164 } 165 166 167 g2d.setTransform(saveTr); 169 } 170 171 if (b.isFocusPainted() && b.hasFocus()) { 173 paintFocus(g, b, viewRect, textRect, iconRect); 174 } 175 176 } 177 178 protected void paintBackground(Graphics2D g, AbstractButton b) { 179 Dimension size = b.getSize(); 180 181 Insets insets = b.getInsets(); 182 Insets margin = b.getMargin(); 183 184 g.fillRect(insets.left - margin.left, 185 insets.top - margin.top, 186 size.width - (insets.left-margin.left) - (insets.right - margin.right), 187 size.height - (insets.top-margin.top) - (insets.bottom - margin.bottom)); 188 } 189 190 191 192 public Dimension getMinimumSize(JComponent c) { 193 return getPreferredSize(c); 194 } 195 196 public Dimension getPreferredSize(JComponent c) { 197 SlidingButton b = (SlidingButton) c; 198 Dimension prefSize = super.getPreferredSize(c); 199 int orientation = b.getOrientation(); 200 201 if (orientation == SlideBarDataModel.SOUTH) { 202 return prefSize; 203 } 204 else { 205 return new Dimension(prefSize.height, prefSize.width); 206 } 207 } 208 209 public Dimension getMaximumSize(JComponent c) { 210 return getPreferredSize(c); 211 } 212 } 213 | Popular Tags |