1 14 package org.compiere.plaf; 15 16 import java.awt.Color ; 17 import java.awt.Component ; 18 import java.awt.Container ; 19 import java.awt.GradientPaint ; 20 import java.awt.Graphics2D ; 21 import java.awt.Image ; 22 import java.awt.MediaTracker ; 23 import java.awt.Paint ; 24 import java.awt.Rectangle ; 25 import java.awt.Toolkit ; 26 import java.awt.geom.RectangularShape ; 27 import java.awt.geom.RoundRectangle2D ; 28 import java.awt.image.BufferedImage ; 29 import java.io.File ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.net.MalformedURLException ; 33 import java.net.URL ; 34 35 import javax.swing.JComponent ; 36 37 import com.sun.image.codec.jpeg.JPEGCodec; 38 import com.sun.image.codec.jpeg.JPEGImageDecoder; 39 40 46 public class CompiereUtils 47 { 48 56 public static void fillRectange(Graphics2D g2D, JComponent c, boolean round) 57 { 58 CompiereColor cc = null; 60 boolean stdCC = c.getClientProperty(CompierePLAF.BACKGROUND_FILL) != null; 61 try 62 { 63 cc = (CompiereColor)c.getClientProperty(CompierePLAF.BACKGROUND); 64 } 65 catch (Exception e) 66 { 67 stdCC = true; 68 } 69 if (stdCC) 70 cc = CompiereColor.getDefaultBackground(); 71 72 if (cc != null) 74 { 75 cc.paint(g2D, c); 77 } 78 else 80 { 81 Paint paint = c.getBackground(); 82 g2D.setPaint(paint); 83 RectangularShape rec = null; 85 if (round) 86 rec = new RoundRectangle2D.Float (0, 0, c.getWidth(),c.getHeight(), 15,15); 87 else 88 rec = new Rectangle (0,0, c.getWidth(),c.getHeight()); 89 g2D.fill(rec); 90 } 91 } 93 94 static public final Color COL_1TOP = new Color (255, 255, 255, 128); 95 96 static public final Color COL_1END = new Color (255, 255, 255, 0); 97 98 static public final Color COL_2TOP = new Color (0, 0, 0, 0); 99 100 static public final Color COL_2END = new Color (0, 0, 0, 64); 101 102 111 public static void paint3Deffect (Graphics2D g2D, Rectangle r, boolean round, boolean out) 112 { 113 GradientPaint topPaint = null; 115 if (out) 116 topPaint = new GradientPaint (r.x, r.y, COL_1TOP, r.x, r.y+r.height/2, COL_1END); 117 else 118 topPaint = new GradientPaint (r.x, r.y, COL_2END, r.x, r.y+r.height/2, COL_2TOP); 119 g2D.setPaint(topPaint); 120 RectangularShape topRec = null; 122 if (round) 123 topRec = new RoundRectangle2D.Float (r.x,r.y, r.width,r.height/2, 15,15); 124 else 125 topRec = new Rectangle (r.x,r.y, r.width,r.height/2); 126 g2D.fill(topRec); 127 128 GradientPaint endPaint = null; if (out) 131 endPaint = new GradientPaint (r.x, r.y+r.height/2, COL_2TOP, r.x, r.y+r.height, COL_2END); 132 else 133 endPaint = new GradientPaint (r.x, r.y+r.height/2, COL_1END, r.x, r.y+r.height, COL_1TOP); 134 g2D.setPaint(endPaint); 135 RectangularShape endRec = null; 137 if (round) 138 endRec = new RoundRectangle2D.Float (r.x, r.y+r.height/2, r.width, r.height/2, 15,15); 139 else 140 endRec = new Rectangle (r.x, r.y+r.height/2, r.width, r.height/2); 141 g2D.fill(endRec); 142 } 144 153 public static void paint3Deffect (Graphics2D g2D, JComponent c, boolean round, boolean out) 154 { 155 GradientPaint topPaint = null; 157 if (out) 158 topPaint = new GradientPaint (0,0, COL_1TOP, 0,c.getHeight()/2, COL_1END); 159 else 160 topPaint = new GradientPaint (0,0, COL_2END, 0,c.getHeight()/2, COL_2TOP); 161 g2D.setPaint(topPaint); 162 RectangularShape topRec = null; 164 if (round) 165 topRec = new RoundRectangle2D.Float (0, 0, c.getWidth(),c.getHeight()/2, 15,15); 166 else 167 topRec = new Rectangle (0,0, c.getWidth(),c.getHeight()/2); 168 g2D.fill(topRec); 169 170 GradientPaint endPaint = null; 172 if (out) 173 endPaint = new GradientPaint (0, c.getHeight()/2, COL_2TOP, 0,c.getHeight(), COL_2END); 174 else 175 endPaint = new GradientPaint (0, c.getHeight()/2, COL_1END, 0,c.getHeight(), COL_1TOP); 176 g2D.setPaint(endPaint); 177 RectangularShape endRec = null; 179 if (round) 180 endRec = new RoundRectangle2D.Float (0, c.getHeight()/2, c.getWidth(),c.getHeight()/2, 15,15); 181 else 182 endRec = new Rectangle (0, c.getHeight()/2, c.getWidth(), c.getHeight()/2); 183 g2D.fill(endRec); 184 } 186 187 188 194 static public Color getTranslucentColor(Color c, int alpha) 195 { 196 return new Color (c.getRed(), c.getGreen(), c.getBlue(), alpha); 197 } 199 200 204 static public void setNotBuffered (Component c) 205 { 206 if (c instanceof JComponent ) 207 ((JComponent )c).setDoubleBuffered(false); 208 if (c instanceof Container ) 209 { 210 Component [] cc = ((Container )c).getComponents(); 211 for (int i = 0; i < cc.length; i++) 212 setNotBuffered(cc[i]); 213 } 214 } 216 217 218 219 220 protected final static Component s_component = new Component () {}; 221 222 protected final static MediaTracker s_tracker = new MediaTracker (s_component); 223 224 232 public static synchronized Image loadImage(String path) 233 { 234 Image image = null; 235 try 236 { 237 File file = new File (path); 238 URL url = file.toURL(); 239 image = loadImage(url); 240 } 241 catch (MalformedURLException e) 242 { 243 System.err.println("CompiereUtils.loadImage(path): " + e.getMessage()); 244 } 245 return image; 246 } 248 255 public static synchronized Image loadImage(URL url) 256 { 257 Image image = null; 258 image = Toolkit.getDefaultToolkit().getImage(url); 259 if (image != null) 260 { 261 s_tracker.addImage(image, 0); 262 try 263 { 264 s_tracker.waitForAll(); 265 } 266 catch (InterruptedException e) 267 { 268 System.err.println("CompiereUtils.loadImage(url): " + e.getMessage()); 269 s_tracker.removeImage(image); 270 image = null; 271 } 272 finally 273 { 274 if (image != null) 275 s_tracker.removeImage(image); 276 if (s_tracker.isErrorAny()) 277 { 278 System.err.println("CompiereUtils.loadImage(url-tracker): " + s_tracker.getErrorsAny()[0]); 279 image = null; 280 } 281 282 if (image != null) 283 { 284 if (image.getWidth(null) < 0 || image.getHeight(null) < 0) 285 { 286 System.err.println("CompiereUtils.loadImage(url-0)"); 287 image = null; 288 } 289 } 290 } 291 } 292 return image; 293 } 295 306 public static synchronized BufferedImage loadBufferedImage(File file, int imageType) 307 { 308 BufferedImage image = null; 309 try 310 { 311 URL url = file.toURL(); 312 image = loadBufferedImage(url, imageType); 313 } 314 catch (MalformedURLException e) 315 { 316 System.err.println("CompiereUtils.loadBufferedImage(file-t): " + e.getMessage()); 317 } 318 return image; 319 } 321 332 public static synchronized BufferedImage loadBufferedImage(String path, int imageType) 333 { 334 File file = new File (path); 335 BufferedImage image = null; 336 try 337 { 338 URL url = file.toURL(); 339 image = loadBufferedImage(url, imageType); 340 } 341 catch (MalformedURLException e) 342 { 343 System.err.println("CompiereUtils.loadBufferedImage(path-t): " + e.getMessage()); 344 } 345 return image; 346 } 348 359 public static synchronized BufferedImage loadBufferedImage(URL url, int imageType) 360 { 361 BufferedImage image = null; 362 if (url == null || !url.toString().toLowerCase().endsWith(".jpg")) 364 { 365 Image tmpImage = loadImage(url); 366 if (tmpImage != null) 367 { 368 image = new BufferedImage (tmpImage.getWidth(null), tmpImage.getHeight(null), imageType); 369 Graphics2D g = image.createGraphics(); 370 g.drawImage(tmpImage, 0, 0, null); 371 g.dispose(); 372 } 373 } 374 else 375 { 376 BufferedImage tmpImage = loadBufferedJPEGImage(url); 377 if (tmpImage != null) 378 { 379 if (tmpImage.getType() != imageType) 380 { 381 image = new BufferedImage (tmpImage.getWidth(null), tmpImage.getHeight(null), imageType); 383 Graphics2D g = image.createGraphics(); 384 g.drawImage(tmpImage, 0, 0, null); 385 g.dispose(); 386 } 387 else 388 image = tmpImage; 389 } 390 } 391 return image; 392 } 394 400 public static synchronized BufferedImage loadBufferedJPEGImage (URL url) 401 { 402 BufferedImage image = null; 403 if (url != null) 404 { 405 InputStream in = null; 406 try 407 { 408 in = url.openStream(); 409 JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in); 410 image = decoder.decodeAsBufferedImage(); 411 } 412 catch (Exception e) 413 { 414 System.err.println("CompiereUtils.loadJPEGImage: " + e.getMessage()); 415 image = null; 416 } 417 finally 418 { 419 try 420 { 421 if (in != null) 422 in.close(); 423 } 424 catch (IOException ioe) 425 { 426 System.err.println("CompiereUtils.loadJPEGImage: " + ioe.getMessage()); 427 } 428 } 429 if (image != null) 430 { 431 if (image.getWidth() <= 0 || image.getHeight() <= 0) 433 { 434 System.err.println("CompiereUtils.loadJPEGImage-0"); 435 image = null; 436 } 437 } 438 } 439 return image; 440 } 441 442 443 444 450 public static boolean isLeftToRight (Component c) 451 { 452 return c.getComponentOrientation().isLeftToRight(); 453 } 455 456 private static int s_no = 0; 457 458 462 static void printParents (JComponent c) 463 { 464 if (c.getName() == null) 465 c.setName("C" + String.valueOf(s_no++)); 466 System.out.print(c.getName()); 467 System.out.print(" - " + c.getClass().getName()); 468 System.out.println (" ** " + c.isOpaque() + " bg=" + (c.getClientProperty(CompierePLAF.BACKGROUND) != null)); 469 Container container = c.getParent(); 471 while (container != null) 472 { 473 System.out.print (" - " + container.getName() + " " + container.getClass().getName() 474 + " ** " + container.isOpaque()); 475 if (container instanceof JComponent ) 476 System.out.print (" bg=" + (((JComponent )container).getClientProperty(CompierePLAF.BACKGROUND) != null)); 477 System.out.println (); 478 container = container.getParent(); 479 } 480 } 482 } | Popular Tags |