1 19 24 25 package org.netbeans.swing.plaf.aqua; 26 27 import org.netbeans.swing.plaf.util.UIUtils; 28 29 import javax.swing.*; 30 import javax.swing.BoxLayout ; 31 import javax.swing.border.Border ; 32 import javax.swing.plaf.ButtonUI ; 33 import javax.swing.plaf.ComponentUI ; 34 import javax.swing.plaf.basic.BasicToolBarUI ; 35 import java.awt.*; 36 import java.awt.event.ContainerEvent ; 37 import java.awt.event.ContainerListener ; 38 import java.awt.geom.*; 39 import java.awt.image.BufferedImage ; 40 import java.util.HashMap ; 41 import java.util.Map ; 42 43 48 public class PlainAquaToolbarUI extends BasicToolBarUI implements ContainerListener { 49 private static final AquaTbBorder aquaborder = new AquaTbBorder(); 50 52 private static final Color UPPER_GRADIENT_TOP = new Color(255,255,255); 53 private static final Color UPPER_GRADIENT_BOTTOM = new Color (228,230,232); 54 55 private static final Color LOWER_GRADIENT_TOP = new Color(228,227,215); 56 private static final Color LOWER_GRADIENT_BOTTOM = new Color(249,249,249); 57 58 59 60 public PlainAquaToolbarUI() { 61 } 62 63 public static ComponentUI createUI(JComponent c) { 64 return new PlainAquaToolbarUI(); 65 } 66 67 public void installUI( JComponent c ) { 68 super.installUI(c); 69 UIManager.put ("Nb.Editor.Toolbar.border", aquaborder); 71 c.setBorder(aquaborder); 72 c.setOpaque(true); 73 c.addContainerListener(this); 74 75 installButtonUIs (c); 76 } 77 78 public void uninstallUI (JComponent c) { 79 super.uninstallUI (c); 80 c.setBorder (null); 81 c.removeContainerListener(this); 82 } 83 84 87 private static Map <Integer , BufferedImage > icache = new HashMap <Integer , BufferedImage >(); 88 89 private BufferedImage getCacheImage(JComponent c) { 90 BufferedImage img = icache.get(new Integer (c.getHeight())); 91 if (img == null && c.getWidth() > (arcsize * 2) + 24 && c.getHeight() > 12) { 96 img = new BufferedImage (c.getWidth(), c.getHeight(), 97 BufferedImage.TYPE_INT_ARGB_PRE); 99 paintInto (img.getGraphics(), c); 100 icache.put (Integer.valueOf(c.getHeight()), img); 101 } 102 return img; 103 } 104 105 public void update (Graphics g, JComponent c) { 106 paint (g, c); 107 } 108 109 public void paint(Graphics g, JComponent c) { 110 if (!c.isOpaque() || c.getWidth() < 4 || c.getHeight() < 4) { 111 return; 112 } 113 BufferedImage img = getCacheImage(c); 114 if (img == null) { 115 paintInto (g, c); 116 } else { 117 paintImage ((Graphics2D) g, img, c); 122 } 123 } 124 125 130 private void paintImage (Graphics2D g2d, BufferedImage img, JComponent c) { 131 int w = c.getWidth(); 132 int h = c.getHeight(); 133 int imgw = img.getWidth(); 134 AffineTransform nullTransform = AffineTransform.getTranslateInstance(0,0); 135 if (w == imgw) { 136 g2d.drawRenderedImage (img, nullTransform); 138 } else if (w > imgw) { 139 g2d.drawRenderedImage (img, nullTransform); 142 int uncovered = w - (imgw - (arcsize * 2)); 143 int x = imgw - arcsize; 144 do { 146 BufferedImage mid = img.getSubimage (arcsize, 0, Math.min(uncovered, imgw - (arcsize * 2)), h); 147 g2d.drawRenderedImage (mid, AffineTransform.getTranslateInstance(x, 0)); 149 x += mid.getWidth(); 150 uncovered -= mid.getWidth(); 151 } while (x < w - arcsize); 152 BufferedImage rightEdge = img.getSubimage (imgw - arcsize, 0, arcsize, h); 153 g2d.drawRenderedImage(rightEdge, AffineTransform.getTranslateInstance(w - arcsize, 0)); 154 } else if (w < imgw) { 155 BufferedImage left = img.getSubimage (0, 0, imgw - arcsize, h); 157 g2d.drawRenderedImage(left, nullTransform); 159 BufferedImage right = img.getSubimage (imgw - arcsize, 0, arcsize, h); 160 g2d.drawRenderedImage(right, AffineTransform.getTranslateInstance(w - arcsize, 0)); 161 } 162 } 163 164 private void paintInto (Graphics g, JComponent c) { 165 UIUtils.configureRenderingHints(g); 166 Color temp = g.getColor(); 167 Dimension size = c.getSize(); 168 169 Shape s = aquaborder.getInteriorShape(size.width, size.height); 170 Shape clip = g.getClip(); 171 if (clip != null) { 172 Area a = new Area(clip); 173 a.intersect(new Area(s)); 174 g.setClip (a); 175 } else { 176 g.setClip(s); 177 } 178 179 Graphics2D g2d = (Graphics2D) g; 180 182 g2d.setPaint (aquaborder.getUpperPaint(size.width,size.height)); 183 g2d.fill (aquaborder.getUpperBevelShape(size.width, size.height)); 184 g2d.setPaint (aquaborder.getLowerPaint(size.width,size.height)); 185 g2d.fill (aquaborder.getLowerBevelShape(size.width, size.height)); 186 187 188 g.setClip (clip); 189 g.setColor(temp); 190 } 191 192 193 protected Border createRolloverBorder() { 194 return BorderFactory.createEmptyBorder(2,2,2,2); 195 } 196 197 protected Border createNonRolloverBorder() { 198 return createRolloverBorder(); 199 } 200 201 private Border createNonRolloverToggleBorder() { 202 return createRolloverBorder(); 203 } 204 205 protected void setBorderToRollover(Component c) { 206 if (c instanceof AbstractButton) { 207 ((AbstractButton) c).setBorderPainted(false); 208 ((AbstractButton) c).setBorder(BorderFactory.createEmptyBorder()); 209 ((AbstractButton) c).setOpaque(false); 211 } 212 if (c instanceof JComponent) { 213 ((JComponent) c).setOpaque(false); 214 } 215 } 216 217 protected void setBorderToNormal(Component c) { 218 if (c instanceof AbstractButton) { 219 ((AbstractButton) c).setBorderPainted(false); 220 ((AbstractButton) c).setOpaque(false); 222 } 223 if (c instanceof JComponent) { 224 ((JComponent) c).setOpaque(false); 225 } 226 } 227 228 public void setFloating(boolean b, Point p) { 229 } 231 232 private void installButtonUI (Component c) { 233 if (c instanceof AbstractButton) { 234 ((AbstractButton) c).setUI(buttonui); 235 } 236 if (c instanceof JComponent) { 237 ((JComponent) c).setOpaque(false); 238 } 239 } 240 241 private void installButtonUIs (Container parent) { 242 Component[] c = parent.getComponents(); 243 for (int i=0; i < c.length; i++) { 244 installButtonUI(c[i]); 245 } 246 } 247 248 private static final ButtonUI buttonui = new AquaToolBarButtonUI(); 249 public void componentAdded(ContainerEvent e) { 250 installButtonUI (e.getChild()); 251 Container c = (Container) e.getSource(); 252 if ("editorToolbar".equals (c.getName())) { 253 Dimension min = new Dimension (32, 34); 258 ((JComponent)e.getContainer()).setPreferredSize(min); 259 } 260 } 261 262 public void componentRemoved(ContainerEvent e) { 263 } 265 266 private static final boolean isFinderLook (Component c) { 267 return Boolean.getBoolean ("apple.awt.brushMetalLook"); 268 } 269 270 static int arcsize = 13; 271 static class AquaTbBorder implements Border { 272 273 public Insets getBorderInsets(Component c) { 274 return new Insets (2,4,0,0); 275 } 276 277 public boolean isBorderOpaque() { 278 return false; 279 } 280 281 282 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 283 UIUtils.configureRenderingHints(g); 284 285 boolean finderLook = isFinderLook (c); 286 Color col; 287 if (finderLook) { 288 col = mezi(UIManager.getColor("controlShadow"), 289 UIManager.getColor("control")); } else { 291 col = UIManager.getColor("controlShadow"); 292 } 293 294 g.setColor(col); 295 296 int ytop = y; 297 298 drawUpper (g, x, y, ytop, w, h); 299 300 g.setColor (mezi (col, UIManager.getColor("control"))); if (finderLook) { 302 drawUpper (g, x+1, y, ytop+1, w-2, h-1); 303 } 304 305 if (finderLook) { 306 col = mezi(UIManager.getColor("controlShadow"), 307 UIManager.getColor("control")); drawLower (g, x, y, w, h, col, finderLook); 309 } else { 310 drawLower (g, x, y-1, w, h, col, finderLook); 312 g.setColor(new Color(200,200,200)); 313 g.drawLine (x+(arcsize/2)-3, y+h-1, x+w-(arcsize/2), y+h-1); 314 } 315 } 316 317 private void drawLower (Graphics g, int x, int y, int w, int h, Color col, boolean finderLook) { 318 319 g.setColor(col); 320 g.drawLine(x, y+(arcsize/2), x, y+h-(arcsize / 2)); 321 g.drawLine(x+w-1, y+(arcsize/2), x+w-1, y+h-(arcsize / 2)); 322 323 if (!finderLook) { 324 g.setColor(new Color(220,220,220)); 325 g.drawArc (x-1, y+1+h-arcsize, arcsize, arcsize, 180, 90); 326 g.drawArc ((x+1)+w-(arcsize+1), y+1+h-(arcsize+1), arcsize, arcsize, 270, 90); 327 g.setColor(col); 328 } 329 330 g.drawArc (x, y+h-arcsize, arcsize, arcsize, 180, 90); 331 g.drawArc (x+w-(arcsize+1), y+h-(arcsize+1), arcsize, arcsize, 270, 90); 332 333 if (!finderLook) { 334 335 g.setColor (new Color(80,80,80)); 336 } 337 g.drawLine (x+(arcsize/2)-3, y+h-1, x+w-(arcsize/2), y+h-1); 338 } 339 340 private void drawUpper (Graphics g, int x, int y, int ytop, int w, int h) { 341 g.drawArc (x, ytop, arcsize, arcsize, 90, 90); 342 343 g.drawArc (x+w-(arcsize+1), ytop, arcsize, arcsize, 90, -90); 344 345 g.drawLine(x+(arcsize/2), ytop, x+w-(arcsize/2), ytop); 346 } 347 348 Paint getUpperPaint (Color top, Color bottom, int w, int h) { 349 GradientPaint result = 350 UIUtils.getGradientPaint (0, h/4, top, 0, (h/2) + (h/4), 351 bottom, false); 352 return result; 353 } 354 355 Paint getLowerPaint (Color top, Color bottom, int w, int h) { 356 GradientPaint result = 357 UIUtils.getGradientPaint (0, h/2, top, 0, (h/2) + (h/4), 358 bottom, false); 359 360 return result; 361 } 362 363 Paint getUpperPaint (int w, int h) { 364 return getUpperPaint (UPPER_GRADIENT_TOP, UPPER_GRADIENT_BOTTOM, w, h); 365 } 366 367 Paint getLowerPaint (int w, int h) { 368 return getLowerPaint (LOWER_GRADIENT_TOP, 369 LOWER_GRADIENT_BOTTOM , w, h); 370 } 371 372 373 Shape getInteriorShape(int w, int h) { 374 RoundRectangle2D r2d = new RoundRectangle2D.Double(0, 0, w, h, arcsize, arcsize); 375 return r2d; 376 } 377 378 Shape getUpperBevelShape(int w, int h) { 379 int[] xpoints = new int[] { 380 0, 381 0, 382 h / 2, 383 w - (h / 4), 384 w, 385 w, 386 0 387 }; 388 389 int[] ypoints = new int[] { 390 0, 391 h - (h / 4), 392 h / 2, 393 h / 2, 394 h / 4, 395 0, 396 0 397 }; 398 Polygon p = new Polygon (xpoints, ypoints, ypoints.length); 399 return p; 400 } 401 402 Shape getLowerBevelShape(int w, int h) { 403 int[] xpoints = new int[] { 404 0, 405 0, 406 h / 4, 407 w - (h / 4), 408 w, 409 w, 410 0 411 }; 412 413 int[] ypoints = new int[] { 414 h, 415 h - (h / 4), 416 h / 2, 417 h / 2, 418 h / 4, 419 h, 420 h 421 422 }; 423 Polygon p = new Polygon (xpoints, ypoints, ypoints.length); 424 return p; 425 } 426 } 427 428 private static Color mezi (Color c1, Color c2) { 429 return new Color((c1.getRed() + c2.getRed()) / 2, 430 (c1.getGreen() + c2.getGreen()) / 2, 431 (c1.getBlue() + c2.getBlue()) / 2); 432 } 433 434 } 435 | Popular Tags |