KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > plaf > CompiereUtils


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.plaf;
15
16 import java.awt.Color JavaDoc;
17 import java.awt.Component JavaDoc;
18 import java.awt.Container JavaDoc;
19 import java.awt.GradientPaint JavaDoc;
20 import java.awt.Graphics2D JavaDoc;
21 import java.awt.Image JavaDoc;
22 import java.awt.MediaTracker JavaDoc;
23 import java.awt.Paint JavaDoc;
24 import java.awt.Rectangle JavaDoc;
25 import java.awt.Toolkit JavaDoc;
26 import java.awt.geom.RectangularShape JavaDoc;
27 import java.awt.geom.RoundRectangle2D JavaDoc;
28 import java.awt.image.BufferedImage JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.net.MalformedURLException JavaDoc;
33 import java.net.URL JavaDoc;
34
35 import javax.swing.JComponent JavaDoc;
36
37 import com.sun.image.codec.jpeg.JPEGCodec;
38 import com.sun.image.codec.jpeg.JPEGImageDecoder;
39
40 /**
41  * UI utilities
42  *
43  * @author Jorg Janke
44  * @version $Id: CompiereUtils.java,v 1.9 2003/09/27 11:08:53 jjanke Exp $
45  */

46 public class CompiereUtils
47 {
48     /**
49      * Fill Background with Color.
50      * (Ususlly called from update methods)
51      *
52      * @param g2D Graphics
53      * @param c Component
54      * @param round paint round corners
55      */

56     public static void fillRectange(Graphics2D JavaDoc g2D, JComponent JavaDoc c, boolean round)
57     {
58         // Paint in CompiereColor?
59
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 JavaDoc e)
66         {
67             stdCC = true;
68         }
69         if (stdCC)
70             cc = CompiereColor.getDefaultBackground();
71
72         // Paint CompiereColor
73
if (cc != null)
74         {
75             // bounds is often not within Panel bouunds
76
cc.paint(g2D, c);
77         }
78         // Paint Flat Color
79
else
80         {
81             Paint JavaDoc paint = c.getBackground();
82             g2D.setPaint(paint);
83             //
84
RectangularShape JavaDoc rec = null;
85             if (round)
86                 rec = new RoundRectangle2D.Float JavaDoc(0, 0, c.getWidth(),c.getHeight(), 15,15);
87             else
88                 rec = new Rectangle JavaDoc(0,0, c.getWidth(),c.getHeight());
89             g2D.fill(rec);
90         }
91     } // fill Rectangle
92

93     /** Top Top Color - white 128 the higher the ligher */
94     static public final Color JavaDoc COL_1TOP = new Color JavaDoc(255, 255, 255, 128);
95     /** End Top Color - white 0 */
96     static public final Color JavaDoc COL_1END = new Color JavaDoc(255, 255, 255, 0);
97     /** Top End Color - black 0 */
98     static public final Color JavaDoc COL_2TOP = new Color JavaDoc(0, 0, 0, 0);
99     /** End End Color - black 64 the higher the darker */
100     static public final Color JavaDoc COL_2END = new Color JavaDoc(0, 0, 0, 64);
101
102     /**
103      * Paint 3D effect in (lighten in upper half, darken in lowerhalf)
104      * (called from paint methods)
105      *
106      * @param g2D Graphics
107      * @param r Ractangle
108      * @param round paint round corners
109      * @param out paint sticking out (not pressed)
110      */

111     public static void paint3Deffect (Graphics2D JavaDoc g2D, Rectangle JavaDoc r, boolean round, boolean out)
112     {
113         // paint upper gradient
114
GradientPaint JavaDoc topPaint = null;
115         if (out)
116             topPaint = new GradientPaint JavaDoc(r.x, r.y, COL_1TOP, r.x, r.y+r.height/2, COL_1END);
117         else
118             topPaint = new GradientPaint JavaDoc(r.x, r.y, COL_2END, r.x, r.y+r.height/2, COL_2TOP);
119         g2D.setPaint(topPaint);
120         //
121
RectangularShape JavaDoc topRec = null;
122         if (round)
123             topRec = new RoundRectangle2D.Float JavaDoc(r.x,r.y, r.width,r.height/2, 15,15);
124         else
125             topRec = new Rectangle JavaDoc(r.x,r.y, r.width,r.height/2);
126         g2D.fill(topRec);
127
128         // paint lower gradient
129
GradientPaint JavaDoc endPaint = null; // upper left corner to lower left
130
if (out)
131             endPaint = new GradientPaint JavaDoc(r.x, r.y+r.height/2, COL_2TOP, r.x, r.y+r.height, COL_2END);
132         else
133             endPaint = new GradientPaint JavaDoc(r.x, r.y+r.height/2, COL_1END, r.x, r.y+r.height, COL_1TOP);
134         g2D.setPaint(endPaint);
135         //
136
RectangularShape JavaDoc endRec = null;
137         if (round)
138             endRec = new RoundRectangle2D.Float JavaDoc(r.x, r.y+r.height/2, r.width, r.height/2, 15,15);
139         else
140             endRec = new Rectangle JavaDoc(r.x, r.y+r.height/2, r.width, r.height/2);
141         g2D.fill(endRec);
142     } // paint3Deffect
143

144     /**
145      * Paint 3D effect in (lighten in upper half, darken in lowerhalf)
146      * (called from paint methods)
147      *
148      * @param g2D Graphics
149      * @param c Component
150      * @param round paint round corners
151      * @param out paint sticking out (not pressed)
152      */

153     public static void paint3Deffect (Graphics2D JavaDoc g2D, JComponent JavaDoc c, boolean round, boolean out)
154     {
155         // paint upper gradient
156
GradientPaint JavaDoc topPaint = null;
157         if (out)
158             topPaint = new GradientPaint JavaDoc(0,0, COL_1TOP, 0,c.getHeight()/2, COL_1END);
159         else
160             topPaint = new GradientPaint JavaDoc(0,0, COL_2END, 0,c.getHeight()/2, COL_2TOP);
161         g2D.setPaint(topPaint);
162         //
163
RectangularShape JavaDoc topRec = null;
164         if (round)
165             topRec = new RoundRectangle2D.Float JavaDoc(0, 0, c.getWidth(),c.getHeight()/2, 15,15);
166         else
167             topRec = new Rectangle JavaDoc(0,0, c.getWidth(),c.getHeight()/2);
168         g2D.fill(topRec);
169
170         // paint lower gradient
171
GradientPaint JavaDoc endPaint = null;
172         if (out)
173             endPaint = new GradientPaint JavaDoc(0, c.getHeight()/2, COL_2TOP, 0,c.getHeight(), COL_2END);
174         else
175             endPaint = new GradientPaint JavaDoc(0, c.getHeight()/2, COL_1END, 0,c.getHeight(), COL_1TOP);
176         g2D.setPaint(endPaint);
177         //
178
RectangularShape JavaDoc endRec = null;
179         if (round)
180             endRec = new RoundRectangle2D.Float JavaDoc(0, c.getHeight()/2, c.getWidth(),c.getHeight()/2, 15,15);
181         else
182             endRec = new Rectangle JavaDoc(0, c.getHeight()/2, c.getWidth(), c.getHeight()/2);
183         g2D.fill(endRec);
184     } // paint3Deffect
185

186     /*************************************************************************/
187
188     /**
189      * Helper to simplify creation of translucent colors
190      * @param c Color
191      * @param alpha
192      * @return Translucent Color
193      */

194     static public Color JavaDoc getTranslucentColor(Color JavaDoc c, int alpha)
195     {
196         return new Color JavaDoc(c.getRed(), c.getGreen(), c.getBlue(), alpha);
197     } // getTranslucentColor
198

199
200     /**
201      * Set Not Buffered. Recursive for all contained components
202      * @param c
203      */

204     static public void setNotBuffered (Component JavaDoc c)
205     {
206         if (c instanceof JComponent JavaDoc)
207             ((JComponent JavaDoc)c).setDoubleBuffered(false);
208         if (c instanceof Container JavaDoc)
209         {
210             Component JavaDoc[] cc = ((Container JavaDoc)c).getComponents();
211             for (int i = 0; i < cc.length; i++)
212                 setNotBuffered(cc[i]);
213         }
214     } // setNotBuffered
215

216
217     /*************************************************************************/
218
219     /** Component for media tracker */
220     protected final static Component JavaDoc s_component = new Component JavaDoc() {};
221     /** Media tracker */
222     protected final static MediaTracker JavaDoc s_tracker = new MediaTracker JavaDoc(s_component);
223
224     /**
225      * load the image located at path.
226      *
227      * @param path location of image file in local file system
228      * - otherwise relative to class
229      * @return loaded image at path or url
230      * @see java.io.File#toUrl()
231      */

232     public static synchronized Image JavaDoc loadImage(String JavaDoc path)
233     {
234         Image JavaDoc image = null;
235         try
236         {
237             File JavaDoc file = new File JavaDoc(path);
238             URL JavaDoc url = file.toURL();
239             image = loadImage(url);
240         }
241         catch (MalformedURLException JavaDoc e)
242         {
243             System.err.println("CompiereUtils.loadImage(path): " + e.getMessage());
244         }
245         return image;
246     } // loadImage
247

248     /**
249      * Load the image located at URL.
250      *
251      * @param url URL where the image file is located.
252      * @return loaded image at path or url
253      * @see java.io.File#toUrl
254      */

255     public static synchronized Image JavaDoc loadImage(URL JavaDoc url)
256     {
257         Image JavaDoc 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 JavaDoc 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     } // loadImage
294

295     /**
296      * Load an image from a given file into a BufferedImage.
297      * The image is returned in the format defined by the imageType parameter.
298      * Note that this is special cased for JPEG images where loading is performed
299      * outside the standard media tracker, for efficiency reasons.
300      *
301      * @param file File where the image file is located.
302      * @param imageType one of the image type defined in the BufferedImage class.
303      * @return loaded image at path or url
304      * @see java.awt.image.BufferedImage
305      */

306     public static synchronized BufferedImage JavaDoc loadBufferedImage(File JavaDoc file, int imageType)
307     {
308         BufferedImage JavaDoc image = null;
309         try
310         {
311             URL JavaDoc url = file.toURL();
312             image = loadBufferedImage(url, imageType);
313         }
314         catch (MalformedURLException JavaDoc e)
315         {
316             System.err.println("CompiereUtils.loadBufferedImage(file-t): " + e.getMessage());
317         }
318         return image;
319     } // loadBufferedImage
320

321     /**
322      * Load an image from a given path into a BufferedImage.
323      * The image is returned in the format defined by the imageType parameter.
324      * Note that this is special cased for JPEG images where loading is performed
325      * outside the standard media tracker, for efficiency reasons.
326      *
327      * @param path Name of file where the image file is located.
328      * @param imageType one of the image type defined in the BufferedImage class.
329      * @return loaded image at path or url
330      * @see java.awt.image.BufferedImage
331      */

332     public static synchronized BufferedImage JavaDoc loadBufferedImage(String JavaDoc path, int imageType)
333     {
334         File JavaDoc file = new File JavaDoc(path);
335         BufferedImage JavaDoc image = null;
336         try
337         {
338              URL JavaDoc url = file.toURL();
339              image = loadBufferedImage(url, imageType);
340         }
341         catch (MalformedURLException JavaDoc e)
342         {
343             System.err.println("CompiereUtils.loadBufferedImage(path-t): " + e.getMessage());
344         }
345         return image;
346     } // loadBufferedImage
347

348     /**
349      * Loads an image from a given URL into a BufferedImage.
350      * The image is returned in the format defined by the imageType parameter.
351      * Note that this is special cased for JPEG images where loading is performed
352      * outside the standard media tracker, for efficiency reasons.
353      *
354      * @param url URL where the image file is located.
355      * @param imageType one of the image type defined in the BufferedImage class.
356      * @return loaded image at path or url
357      * @see java.awt.image.BufferedImage
358      */

359     public static synchronized BufferedImage JavaDoc loadBufferedImage(URL JavaDoc url, int imageType)
360     {
361         BufferedImage JavaDoc image = null;
362         // Special handling for JPEG images to avoid extra processing if possible.
363
if (url == null || !url.toString().toLowerCase().endsWith(".jpg"))
364         {
365             Image JavaDoc tmpImage = loadImage(url);
366             if (tmpImage != null)
367             {
368                 image = new BufferedImage JavaDoc(tmpImage.getWidth(null), tmpImage.getHeight(null), imageType);
369                 Graphics2D JavaDoc g = image.createGraphics();
370                 g.drawImage(tmpImage, 0, 0, null);
371                 g.dispose();
372             }
373         }
374         else
375         {
376             BufferedImage JavaDoc tmpImage = loadBufferedJPEGImage(url);
377             if (tmpImage != null)
378             {
379                 if (tmpImage.getType() != imageType)
380                 {
381                     // System.out.println("Incompatible JPEG image type: creating new buffer image");
382
image = new BufferedImage JavaDoc(tmpImage.getWidth(null), tmpImage.getHeight(null), imageType);
383                     Graphics2D JavaDoc 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     } // loadBufferedImage
393

394     /**
395      * Load a JPEG image from a given location.
396      *
397      * @param url URL where the image file is located.
398      * @return loaded image at path or url
399      */

400     public static synchronized BufferedImage JavaDoc loadBufferedJPEGImage (URL JavaDoc url)
401     {
402         BufferedImage JavaDoc image = null;
403         if (url != null)
404         {
405             InputStream JavaDoc in = null;
406             try
407             {
408                 in = url.openStream();
409                 JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
410                 image = decoder.decodeAsBufferedImage();
411             }
412             catch (Exception JavaDoc 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 JavaDoc ioe)
425                 {
426                     System.err.println("CompiereUtils.loadJPEGImage: " + ioe.getMessage());
427                 }
428             }
429             if (image != null)
430             {
431                 // System.out.println("Image type : " + image.getType());
432
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     /**
445      * Convenience function for determining ComponentOrientation.
446      * Copied from MetalUtils
447      * @param c Component
448      * @return true, if left to right
449      */

450     public static boolean isLeftToRight (Component JavaDoc c)
451     {
452         return c.getComponentOrientation().isLeftToRight();
453     } // isLeftToRight
454

455     /** Debug SequenceNo */
456     private static int s_no = 0;
457
458     /**
459      * Print Oarent of Component
460      * @param c
461      */

462     static void printParents (JComponent JavaDoc 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         //
470
Container JavaDoc 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 JavaDoc)
476                 System.out.print (" bg=" + (((JComponent JavaDoc)container).getClientProperty(CompierePLAF.BACKGROUND) != null));
477             System.out.println ();
478             container = container.getParent();
479         }
480      } // printParents
481

482 } // CompiereTuils
483
Popular Tags