KickJava   Java API By Example, From Geeks To Geeks.

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


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.AlphaComposite JavaDoc;
17 import java.awt.BasicStroke JavaDoc;
18 import java.awt.Color JavaDoc;
19 import java.awt.Container JavaDoc;
20 import java.awt.GradientPaint JavaDoc;
21 import java.awt.Graphics JavaDoc;
22 import java.awt.Graphics2D JavaDoc;
23 import java.awt.Point JavaDoc;
24 import java.awt.Rectangle JavaDoc;
25 import java.awt.TexturePaint JavaDoc;
26 import java.awt.Window JavaDoc;
27 import java.awt.image.BufferedImage JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.ResourceBundle JavaDoc;
31
32 import javax.swing.JComponent JavaDoc;
33 import javax.swing.JDialog JavaDoc;
34 import javax.swing.JFrame JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36 import javax.swing.JWindow JavaDoc;
37 import javax.swing.SwingConstants JavaDoc;
38 import javax.swing.UIManager JavaDoc;
39 import javax.swing.plaf.ColorUIResource JavaDoc;
40
41 import org.compiere.util.KeyNamePair;
42 import org.compiere.util.ValueNamePair;
43
44 /**
45  * Compiere Background Color
46  *
47  * @author Jorg Janke
48  * @version $Id: CompiereColor.java,v 1.18 2003/09/27 11:08:53 jjanke Exp $
49  */

50 public class CompiereColor implements Serializable JavaDoc
51 {
52     /** Background type Flat */
53     public static final String JavaDoc TYPE_FLAT = "F";
54     /** Background type Gradient */
55     public static final String JavaDoc TYPE_GRADIENT = "G";
56     /** Background type Lines */
57     public static final String JavaDoc TYPE_LINES = "L";
58     /** Background type Texture */
59     public static final String JavaDoc TYPE_TEXTURE = "T";
60
61     /** Names */
62     private static ResourceBundle JavaDoc s_res = ResourceBundle.getBundle("org.compiere.plaf.PlafRes");
63
64     /** Type Values */
65     public static final String JavaDoc[] TYPE_VALUES = new String JavaDoc[] {
66         TYPE_FLAT, TYPE_GRADIENT, TYPE_LINES, TYPE_TEXTURE
67     };
68     /** Type Names */
69     public static final String JavaDoc[] TYPE_NAMES = new String JavaDoc[] {
70         s_res.getString("BackColType_Flat"),
71         s_res.getString("BackColType_Gradient"),
72         s_res.getString("BackColType_Lines"),
73         s_res.getString("BackColType_Texture")
74     };
75     /** Types */
76     public static final ValueNamePair[] TYPES = new ValueNamePair[] {
77         new ValueNamePair(TYPE_VALUES[0], TYPE_NAMES[0]),
78         new ValueNamePair(TYPE_VALUES[1], TYPE_NAMES[1]),
79         new ValueNamePair(TYPE_VALUES[2], TYPE_NAMES[2]),
80         new ValueNamePair(TYPE_VALUES[3], TYPE_NAMES[3])
81     };
82
83     /** Gradient Starting Values */
84     public static final int[] GRADIENT_SP_VALUES = new int[] {
85         SwingConstants.NORTH, SwingConstants.NORTH_EAST,
86         SwingConstants.EAST, SwingConstants.SOUTH_EAST,
87         SwingConstants.SOUTH, SwingConstants.SOUTH_WEST,
88         SwingConstants.WEST, SwingConstants.NORTH_WEST
89     };
90     /** Gradient Starting Names */
91     public static final String JavaDoc[] GRADIENT_SP_NAMES = new String JavaDoc[] {
92         "North", "North-East",
93         "East", "South-East",
94         "South", "South-West",
95         "West", "North-West"
96     };
97     /** Gradient Starting Point */
98     public static final KeyNamePair[] GRADIENT_SP = new KeyNamePair[] {
99         new KeyNamePair(GRADIENT_SP_VALUES[0], GRADIENT_SP_NAMES[0]),
100         new KeyNamePair(GRADIENT_SP_VALUES[1], GRADIENT_SP_NAMES[1]),
101         new KeyNamePair(GRADIENT_SP_VALUES[2], GRADIENT_SP_NAMES[2]),
102         new KeyNamePair(GRADIENT_SP_VALUES[3], GRADIENT_SP_NAMES[3]),
103         new KeyNamePair(GRADIENT_SP_VALUES[4], GRADIENT_SP_NAMES[4]),
104         new KeyNamePair(GRADIENT_SP_VALUES[5], GRADIENT_SP_NAMES[5]),
105         new KeyNamePair(GRADIENT_SP_VALUES[6], GRADIENT_SP_NAMES[6]),
106         new KeyNamePair(GRADIENT_SP_VALUES[7], GRADIENT_SP_NAMES[7])
107     };
108     /** Exception text */
109     private static final String JavaDoc EXCEPTION_TEXT = "Arguments cannot be NULL";
110
111     /*************************************************************************/
112
113     /**
114      * Set Background of Component to default color
115      * @param c component
116      */

117     public static void setBackground (JComponent JavaDoc c)
118     {
119         setBackground (c, CompierePanelUI.getDefaultBackground());
120     } // setBackground
121

122     /**
123      * Set Background of Component
124      * @param c Component
125      * @param cc Color
126      */

127     public static void setBackground (JComponent JavaDoc c, CompiereColor cc)
128     {
129         c.putClientProperty(CompierePLAF.BACKGROUND, cc);
130     } // setBackground
131

132     /**
133      * Get Background Color of component
134      * @param c JComponent
135      * @return Color
136      */

137     public static CompiereColor getBackground (JComponent JavaDoc c)
138     {
139         CompiereColor bg = null;
140         try
141         {
142             bg = (CompiereColor)c.getClientProperty(CompierePLAF.BACKGROUND);
143         }
144         catch (Exception JavaDoc e)
145         {
146             System.err.println("CompiereColor - ClientProperty: " + e.getMessage());
147         }
148         return bg;
149     } // getBackground
150

151     /**
152      * Set Background of Window Content Pane to default color
153      * @param win window
154      */

155     public static void setBackground (Window JavaDoc win)
156     {
157         setBackground (win, CompierePanelUI.getDefaultBackground());
158     } // setBackground
159

160     /**
161      * Set Background of Window Content Pane
162      * @param win window
163      * @param cc compiere color
164      */

165     public static void setBackground (Window JavaDoc win, CompiereColor cc)
166     {
167         if (win instanceof JDialog JavaDoc)
168         {
169             ((JPanel JavaDoc)((JDialog JavaDoc)win).getContentPane()).putClientProperty(CompierePLAF.BACKGROUND, cc);
170         // ((JPanel)((JDialog)win).getContentPane()).setName("contentPane");
171
}
172         else if (win instanceof JFrame JavaDoc)
173         {
174             ((JPanel JavaDoc)((JFrame JavaDoc)win).getContentPane()).putClientProperty(CompierePLAF.BACKGROUND, cc);
175         // ((JPanel)((JFrame)win).getContentPane()).setName("contentPane");
176
}
177         else if (win instanceof JWindow JavaDoc)
178         {
179             ((JPanel JavaDoc)((JWindow JavaDoc)win).getContentPane()).putClientProperty(CompierePLAF.BACKGROUND, cc);
180         // ((JPanel)((JWindow)win).getContentPane()).setName("contentPane");
181
}
182     } // setBackground
183

184     /**
185      * Set Default Background
186      * @param bg Background Color
187      * @see CompierePanelUI#setDefaultBackground
188      */

189     public static void setDefaultBackground (CompiereColor bg)
190     {
191         CompierePanelUI.setDefaultBackground(bg);
192     } // setDefaultBackground
193

194     /**
195      * Get Default Background
196      * @return Background
197      * @see CompierePanelUI#getDefaultBackground
198      */

199     public static CompiereColor getDefaultBackground()
200     {
201         return CompierePanelUI.getDefaultBackground();
202     } // getDefaultBackground
203

204     /**
205      * Set Default Background
206      * @param setDefault if true, the background will be set to the default color
207      * @see CompierePanelUI#setSetDefault
208      */

209     public static void setSetDefault (boolean setDefault)
210     {
211         CompierePanelUI.setSetDefault(setDefault);
212     } // setSetDefault
213

214     /**
215      * Is the Default Background set by default
216      * @return true if default background is set
217      * @see CompierePanelUI#isSetDefault
218      */

219     public static boolean isSetDefault()
220     {
221         return CompierePanelUI.isSetDefault();
222     } // isSetDefault
223

224     /**
225      * Parse attributes and return CompiereColor
226      * @param attributes attributes
227      * @return CompiereColor
228      */

229     public static CompiereColor parse (String JavaDoc attributes)
230     {
231         CompiereColor cc = new CompiereColor ();
232         try
233         {
234             if (attributes != null && attributes.length() > 0)
235                 cc.parseAttributres (attributes);
236         }
237         catch (Exception JavaDoc e)
238         {
239             System.err.println("CompiereColor.parse(" + attributes + ") - " + e.toString());
240         }
241         return cc;
242     } // parse
243

244     /************************************************************************/
245
246     /**
247      * Create Gradient Background Color (Window System Color - White)
248      */

249     public CompiereColor()
250     {
251         this (TYPE_GRADIENT);
252     } // CompiereColor
253

254     /**
255      * Create Default Background Colors of Type
256      * @param type Background type (see constants TYPE_*)
257      */

258     public CompiereColor (String JavaDoc type)
259     {
260         if (type == null)
261             new java.lang.IllegalArgumentException JavaDoc (EXCEPTION_TEXT);
262         if (type.equals(TYPE_FLAT) || type.equals(TYPE_GRADIENT)
263             || type.equals(TYPE_TEXTURE) || type.equals(TYPE_LINES))
264         {
265             m_type = type;
266         }
267         else
268             new java.lang.IllegalArgumentException JavaDoc ("Invalid Type");
269     } // CompiereColor
270

271     /**
272      * Create Flat Background Color
273      * @param bg background
274      */

275     public CompiereColor (Color JavaDoc bg)
276     {
277         this (bg, true);
278     } // CompiereColor
279

280     /**
281      * Create Background Color
282      * @param bg Color
283      * @param flat if true create Flat color otherwise Gradient color with white lower color
284      */

285     public CompiereColor (Color JavaDoc bg, boolean flat)
286     {
287         if (bg == null)
288             new java.lang.IllegalArgumentException JavaDoc (EXCEPTION_TEXT);
289         m_type = flat ? TYPE_FLAT : TYPE_GRADIENT;
290         m_primaryColor = bg;
291     } // CompiereColor
292

293     /**
294      * Set Background to Gradient colors
295      * @param upperColor upper Color
296      * @param lowerColor lower Color
297      * @param startPoint Starting point - e.g. SOUTH_WEST see SwingConstants, default NORTH_WEST
298      * @param repeatDistance X/Y Distance to repeat gradient in points - 0 no repeats
299      */

300     public CompiereColor (Color JavaDoc upperColor, Color JavaDoc lowerColor, int startPoint, int repeatDistance)
301     {
302         if (upperColor == null || lowerColor == null)
303             new java.lang.IllegalArgumentException JavaDoc (EXCEPTION_TEXT);
304         m_type = TYPE_GRADIENT;
305         m_primaryColor = upperColor;
306         m_secondaryColor = lowerColor;
307         m_startPoint = startPoint;
308         m_repeatDistance = repeatDistance;
309     } // CompiereColor
310

311     /**
312      * Set Background to Gradient colors.
313      * Starting in the north, repeat after 100 pt
314      * @param upperColor upper color
315      * @param lowerColor lower color
316      */

317     public CompiereColor (Color JavaDoc upperColor, Color JavaDoc lowerColor)
318     {
319         this (upperColor, lowerColor, SwingConstants.NORTH_WEST, 100);
320     } // CompiereColor
321

322     /**
323      * Set Background to Texture
324      *
325      * @param textureURL URL to a *.gif or *.jpg graphic file
326      * @param taint Color to taint the texture (use white for not tainting it)
327      * @param compositeAlpha Value from 0(no) to 1(full) taining
328      */

329     public CompiereColor (URL JavaDoc textureURL, Color JavaDoc taint, float compositeAlpha)
330     {
331         if (textureURL == null || taint == null)
332             new java.lang.IllegalArgumentException JavaDoc (EXCEPTION_TEXT);
333         m_type = TYPE_TEXTURE;
334         m_textureURL = textureURL;
335         m_primaryColor = taint;
336         m_compositeAlpha = compositeAlpha;
337     } // CompiereColor
338

339     /**
340      * Set Background to Texture
341      *
342      * @param textureURL URL to a *.gif or *.jpg graphic file
343      * @param taint Color to taint the texture (use white for not tainting it)
344      * @param compositeAlpha Tainting value from 0 (no - FullGraph) to 1 (full - NoGraph)
345      */

346     public CompiereColor (String JavaDoc textureURL, Color JavaDoc taint, float compositeAlpha)
347     {
348         if (textureURL == null || taint == null)
349             new java.lang.IllegalArgumentException JavaDoc (EXCEPTION_TEXT);
350         m_type = TYPE_TEXTURE;
351         setTextureURL(textureURL);
352         m_primaryColor = taint;
353         m_compositeAlpha = compositeAlpha;
354     } // CompiereColor
355

356     /**
357      * Set Background to Lines
358      *
359      * @param lineColor line color
360      * @param backColor background color
361      * @param lineWidth Stroke width in point
362      * @param lineDistance Distance between lines in points
363      */

364     public CompiereColor (Color JavaDoc lineColor, Color JavaDoc backColor, float lineWidth, int lineDistance)
365     {
366         if (lineColor == null || backColor == null)
367             new java.lang.IllegalArgumentException JavaDoc (EXCEPTION_TEXT);
368         m_type = TYPE_LINES;
369         m_primaryColor = backColor;
370         m_secondaryColor = lineColor;
371         m_lineWidth = lineWidth;
372         m_lineDistance = lineDistance;
373     } // CompiereColor
374

375     /**
376      * Copy Color
377      * @param cc color
378      */

379     public CompiereColor (CompiereColor cc)
380     {
381         if (cc == null)
382             return;
383         setColor(cc);
384     } // CompiereColor
385

386     /*************************************************************************/
387
388     /** Type - Default: Gradient */
389     private String JavaDoc m_type = TYPE_GRADIENT;
390
391     /** Primary Color - Default Panel back */
392     private Color JavaDoc m_primaryColor = UIManager.getColor("Panel.background");
393     /** Secondary Color - Default: white */
394     private Color JavaDoc m_secondaryColor = Color.white;
395
396     /** Texture Graph URL */
397     private URL JavaDoc m_textureURL = null;
398     /** Texture Graph */
399     private BufferedImage JavaDoc m_image = null;
400     /** Texture Alpha - Default: 0.7 */
401     private float m_compositeAlpha = 0.7f;
402
403     /** Line Width - Default: 1 */
404     private float m_lineWidth = 1.0f;
405     /** Line Distance - Default: 5 */
406     private int m_lineDistance = 5;
407
408     /** Gradient Starting point - Default: NORTH_WEST */
409     private int m_startPoint = SwingConstants.NORTH_WEST;
410     /** Gradient repeat distance in points - Default: 100 */
411     private int m_repeatDistance = 100;
412
413     /** Background */
414     private ColorBackground m_back = null;
415
416     /** Diry marker for repaining Background */
417     private boolean m_dirty = true;
418
419     /*************************************************************************/
420
421     /**
422      * Get BackgroundType (Flat, Gradient, Lines, Texture)
423      * @return Background Type (see TYPE_* constants)
424      */

425     public String JavaDoc getType()
426     {
427         return m_type;
428     } // getType
429

430     /**
431      * Flat Background Type (default)
432      * @return true if Flat background
433      */

434     public boolean isFlat()
435     {
436         return TYPE_FLAT.equals(getType());
437     } // isFlat
438

439     /**
440      * Gradient Background Type
441      * @return true if Gradient background
442      */

443     public boolean isGradient()
444     {
445         return TYPE_GRADIENT.equals(getType());
446     } // isGradient
447

448     /**
449      * Line Background Type
450      * @return true if Line background
451      */

452     public boolean isLine()
453     {
454         return TYPE_LINES.equals(getType());
455     } // isLine
456

457     /**
458      * Texture Background Type
459      * @return true if Texture background
460      */

461     public boolean isTexture()
462     {
463         return TYPE_TEXTURE.equals(getType());
464     } // isTexture
465

466     /**********/
467
468     /**
469      * Get Flat Color
470      * @return Primary Color
471      */

472     public Color JavaDoc getFlatColor()
473     {
474         return m_primaryColor;
475     } // getFlatColor
476

477     /**
478      * Set Flat Color
479      * @param color flat color
480      */

481     public void setFlatColor(Color JavaDoc color)
482     {
483         if (!isFlat() || color == null)
484             return;
485         m_primaryColor = color;
486         m_dirty = true;
487     } // getFlatColor
488

489     /**********/
490
491     /**
492      * Gradient Upper Color
493      * @return Color or null
494      */

495     public Color JavaDoc getGradientUpperColor()
496     {
497         if (!isGradient())
498             return null;
499         return m_primaryColor;
500     } // getGradientUpperColor
501

502     /**
503      * Gradient Upper Color
504      * @param color upper color
505      */

506     public void setGradientUpperColor(Color JavaDoc color)
507     {
508         if (!isGradient() || color == null)
509             return;
510         m_primaryColor = color;
511         m_dirty = true;
512     } // getGradientUpperColor
513

514     /**
515      * Gradient Lower Color
516      * @return Color or null
517      */

518     public Color JavaDoc getGradientLowerColor()
519     {
520         if (!isGradient())
521             return null;
522         return m_secondaryColor;
523     } // getGradientLowerColor
524

525     /**
526      * Gradient Lower Color
527      * @param color lower color
528      */

529     public void setGradientLowerColor(Color JavaDoc color)
530     {
531         if (!isGradient() || color == null)
532             return;
533         m_secondaryColor = color;
534         m_dirty = true;
535     } // setGradientLowerColor
536

537     /**
538      * Gradient Starting Point
539      * @return starting point - e.g. NORTH - or 0
540      * @see SwingConstants
541      */

542     public int getGradientStartPoint ()
543     {
544         if (!isGradient())
545             return 0;
546         return m_startPoint;
547     } // getGradientStartPoint
548

549     /**
550      * Gradient Starting Point
551      * @param startPoint starting point - e.g. NORTH
552      * @see SwingConstants
553      */

554     public void setGradientStartPoint (int startPoint)
555     {
556         if (!isGradient())
557             return;
558         m_startPoint = startPoint;
559         m_dirty = true;
560     } // setGradientStartPoint
561

562     /**
563      * Gradient Repeat Distance in point
564      * @return Repeat Distance - or 0
565      */

566     public int getGradientRepeatDistance ()
567     {
568         if (!isGradient())
569             return 0;
570         return m_repeatDistance;
571     } // getGradientRepeatDistance
572

573     /**
574      * Gradient Repeat Distance.
575      * Zero stands for no repeats
576      * @param repeatDistance repeat gradient after point x+repeat / y+repeat (depending on direction)
577      */

578     public void setGradientRepeatDistance (int repeatDistance)
579     {
580         if (!isGradient())
581             return;
582         m_repeatDistance = repeatDistance;
583         m_dirty = true;
584     } // setGradientRepeatDistance
585

586     /**
587      * Gradient Repeat Distance.
588      * Zero stands for no repeats
589      * @param repeatDistanceString repeat gradient after point x+repeat / y+repeat (depending on direction)
590      */

591     public void setGradientRepeatDistance (String JavaDoc repeatDistanceString)
592     {
593         if (!isGradient())
594             return;
595         try
596         {
597             setGradientRepeatDistance(Integer.parseInt(repeatDistanceString));
598         }
599         catch (Exception JavaDoc e)
600         {
601             System.err.println("CompiereColor.setGradientRepeatDistance - Parsing="
602                 + repeatDistanceString + " - " + e.getMessage());
603         }
604     } // setGradientRepeatDistance
605

606     /**********/
607
608     /**
609      * Texture Url
610      * @return URL (if not found, org.compiere.plaf.background.jpg is used)
611      */

612     public URL JavaDoc getTextureURL()
613     {
614         if (!isTexture())
615             return null;
616         if (m_textureURL == null)
617             m_textureURL = CompiereColor.class.getResource("Compiere200x100.gif");
618         return m_textureURL;
619     } // getTextureURL
620

621     /**
622      * Get Texture Image based on Texture URL
623      * @return Image
624      */

625     public BufferedImage JavaDoc getTextureImage()
626     {
627         if (m_image == null)
628         {
629             URL JavaDoc url = getTextureURL();
630             m_image = CompiereUtils.loadBufferedImage(url, BufferedImage.TYPE_INT_ARGB_PRE);
631         }
632         return m_image;
633     } // getTextureImage
634

635     /**
636      * Texture Url
637      * @param url URL to graphic file (jpg)
638      */

639     public void setTextureURL(URL JavaDoc url)
640     {
641         if (!isTexture() || url == null)
642             return;
643         m_textureURL = url;
644         m_image = null;
645         m_dirty = true;
646     } // setTextureURL
647

648     /**
649      * Texture Url
650      * @param urlString URL to graphic file (jpg)
651      */

652     public void setTextureURL(String JavaDoc urlString)
653     {
654         if (!isTexture() || urlString == null)
655             return;
656         try
657         {
658             setTextureURL (new URL JavaDoc(urlString));
659         }
660         catch (Exception JavaDoc e)
661         {
662             System.err.println("CompiereColor.setTextureURL - Parsing URL="
663                 + urlString + " - " + e.getMessage());
664         }
665     } // setTextureURL
666

667     /**
668      * Texture Taint Color
669      * @return Color or null
670      */

671     public Color JavaDoc getTextureTaintColor()
672     {
673         if (!isTexture())
674             return null;
675         return m_primaryColor;
676     } // getTextureTaintColor
677

678     /**
679      * Texture Taint Color
680      * @param color taint color
681      */

682     public void setTextureTaintColor(Color JavaDoc color)
683     {
684         if (!isTexture() || color == null)
685             return;
686         m_primaryColor = color;
687         m_dirty = true;
688     } // setTextureTaintColor
689

690     /**
691      * Texture Composite Alpha
692      * @return Composite Ampha or 0f
693      */

694     public float getTextureCompositeAlpha()
695     {
696         if (!isTexture())
697             return 0f;
698         return m_compositeAlpha;
699     } // getTextureCompositeAlpha
700

701     /**
702      * Texture Composite Alpha
703      * @param alpha alpha value
704      */

705     public void setTextureCompositeAlpha(float alpha)
706     {
707         if (!isTexture())
708             return;
709         m_compositeAlpha = alpha;
710         m_dirty = true;
711     } // setTextureCompositeAlpha
712

713     /**
714      * Texture Composite Alpha
715      * @param alphaString String to be parsed
716      */

717     public void setTextureCompositeAlpha(String JavaDoc alphaString)
718     {
719         if (!isTexture() || alphaString == null)
720             return;
721         try
722         {
723             setTextureCompositeAlpha(Float.parseFloat(alphaString));
724         }
725         catch (Exception JavaDoc e)
726         {
727             System.err.println("CompiereColor.setTextureCompositeAlpha - Parsing="
728                 + alphaString + " - " + e.getMessage());
729         }
730     } // setTextureCompositeAlpha
731

732     /**********/
733
734     /**
735      * Line Color
736      * @return Color or null
737      */

738     public Color JavaDoc getLineColor()
739     {
740         if (!isLine())
741             return null;
742         return m_secondaryColor;
743     } // getLineColor
744

745     /**
746      * Line Color
747      * @param color line color
748      */

749     public void setLineColor(Color JavaDoc color)
750     {
751         if (!isLine() || color == null)
752             return;
753         m_secondaryColor = color;
754         m_dirty = true;
755     } // setLineColor
756

757     /**
758      * Line Background Color
759      * @return Color or null
760      */

761     public Color JavaDoc getLineBackColor()
762     {
763         if (!isLine())
764             return null;
765         return m_primaryColor;
766     } // getLineBackColor
767

768     /**
769      * Line Background Color
770      * @param color background color
771      */

772     public void setLineBackColor(Color JavaDoc color)
773     {
774         if (!isLine() || color == null)
775             return;
776         m_primaryColor = color;
777         m_dirty = true;
778     } // setLineBackColor
779

780     /**
781      * Background Line Width
782      * @return width or 0f
783      */

784     public float getLineWidth()
785     {
786         if (!isLine())
787             return 0f;
788         return m_lineWidth;
789     } // getLineWidth
790

791     /**
792      * Background Line Width
793      * @param width line width
794      */

795     public void setLineWidth(float width)
796     {
797         if (!isLine())
798             return;
799         m_lineWidth = width;
800         m_dirty = true;
801     } // setLineWidth
802

803     /**
804      * Background Line Width
805      * @param widthString line width
806      */

807     public void setLineWidth(String JavaDoc widthString)
808     {
809         if (!isLine() || widthString == null)
810             return;
811         try
812         {
813             setLineWidth(Float.parseFloat(widthString));
814         }
815         catch (Exception JavaDoc e)
816         {
817             System.err.println("CompiereColor.setLineWidth - Parsing="
818                 + widthString + " - " + e.getMessage());
819         }
820     } // setLineWidth
821

822     /**
823      * Background Line distance in pt
824      * @return distance or 0
825      */

826     public int getLineDistance()
827     {
828         if (!isLine())
829             return 0;
830         return m_lineDistance;
831     } // getLineDistance
832

833     /**
834      * Background Line distance in pt
835      * @param distance line distance
836      */

837     public void setLineDistance(int distance)
838     {
839         if (!isLine())
840             return;
841         m_lineDistance = distance;
842         m_dirty = true;
843     } // setLineDistance
844

845     /**
846      * Background Line distance in pt
847      * @param distanceString line distance
848      */

849     public void setLineDistance(String JavaDoc distanceString)
850     {
851         if (!isLine())
852             return;
853         try
854         {
855             setLineDistance(Integer.parseInt(distanceString));
856         }
857         catch (Exception JavaDoc e)
858         {
859             System.err.println("CompiereColor.setLineDistance - Parsing="
860                 + distanceString + " - " + e.getMessage());
861         }
862     } // setLineDistance
863

864     /**
865      * Set Prinary Color
866      * @param color primary color
867      */

868     protected void setPrimaryColor (Color JavaDoc color)
869     {
870         if (color != null)
871             m_primaryColor = color;
872     } // setPrimaryColor
873

874     /**
875      * Set CompiereColor from CompiereColor
876      * @param cc CompiereColor
877      */

878     public void setColor (CompiereColor cc)
879     {
880         if (cc == null)
881             return;
882         m_type = cc.getType();
883         //
884
if (cc.isFlat())
885             m_primaryColor = cc.getFlatColor();
886         else if (cc.isGradient())
887         {
888             m_primaryColor = cc.getGradientUpperColor();
889             m_secondaryColor = cc.getGradientLowerColor();
890             m_startPoint = cc.getGradientStartPoint();
891             m_repeatDistance = cc.getGradientRepeatDistance();
892         }
893         else if (cc.isTexture())
894         {
895             setTextureURL(cc.getTextureURL());
896             m_primaryColor = cc.getTextureTaintColor();
897             m_compositeAlpha = cc.getTextureCompositeAlpha();
898         }
899         else if (cc.isLine())
900         {
901             m_primaryColor = cc.getLineBackColor();
902             m_secondaryColor = cc.getLineColor();
903             m_lineWidth = cc.getLineWidth();
904             m_lineDistance = cc.getLineDistance();
905         }
906         else
907             System.err.println("CompiereColor.setColor - Invalid Color");
908         //
909
m_dirty = true;
910     } // setColor
911

912     /*************************************************************************/
913
914     /**
915      * Fill with CompiereColor Background
916      * @param g the <code>Graphics</code> context in which to paint
917      * @param c the component being painted
918      */

919     public void paint (Graphics JavaDoc g, JComponent JavaDoc c)
920     {
921         getColorBackground(c).paint (g, c);
922     } // paint
923

924     /**
925      * Fill with Compiere Background
926      * @param g graphics
927      * @param c component
928      * @param x x pos
929      * @param y y pos
930      * @param w with
931      * @param h height
932      */

933     public void paintRect (Graphics JavaDoc g, JComponent JavaDoc c, int x, int y, int w, int h)
934     {
935         getColorBackground(c).paintRect (g,c, x,y, w,h);
936     } // paintRect
937

938     /**
939      * Get Background
940      * @param c Componenr
941      * @return Background
942      */

943     private ColorBackground getColorBackground (JComponent JavaDoc c)
944     {
945         if (m_back == null)
946         {
947             Rectangle JavaDoc bounds = c.getBounds();
948             Container JavaDoc container = c.getParent();
949             while (container != null)
950             {
951                 bounds = container.getBounds(bounds);
952                 container = container.getParent();
953             }
954             m_back = new ColorBackground (bounds);
955         }
956         return m_back;
957     } // getBackground
958

959     /*************************************************************************/
960
961     /**
962      * String representation
963      * @return string representation
964      */

965     public String JavaDoc toString()
966     {
967         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("CompiereColor[");
968         if (isFlat())
969             sb.append("Flat")
970                 .append(" ").append(CompiereTheme.getColorAsString(getFlatColor()));
971         else if (isGradient())
972             sb.append("Gradient")
973                 .append(" Upper=").append(CompiereTheme.getColorAsString(getGradientUpperColor()))
974                 .append(",Lower=").append(CompiereTheme.getColorAsString(getGradientLowerColor()))
975                 .append(",Start=").append(getGradientStartPoint())
976                 .append(",RDistance=").append(getGradientRepeatDistance());
977         else if (isLine())
978             sb.append("Line")
979                 .append(" Color=").append(CompiereTheme.getColorAsString(getLineColor()))
980                 .append(",BackColor=").append(CompiereTheme.getColorAsString(getLineBackColor()))
981                 .append(",Width=").append(getLineWidth())
982                 .append(",Distance=").append(getLineDistance());
983         else if (isTexture())
984             sb.append("Texture")
985                 .append(" GraphURL=").append(getTextureURL())
986                 .append(",Taint=").append(CompiereTheme.getColorAsString(getTextureTaintColor()))
987                 .append(",Alpha=").append(getTextureCompositeAlpha());
988         sb.append("]");
989         return sb.toString();
990     } // toString
991

992     /**
993      * Parse String Representation and set Attributes
994      * @param str parse string
995      */

996     private void parseAttributres (String JavaDoc str)
997     {
998         if (str.indexOf("[Flat ") != -1)
999         {
1000            m_type = TYPE_FLAT;
1001            m_primaryColor = CompiereTheme.parseColor(str,
1002                new ColorUIResource JavaDoc(m_primaryColor));
1003        }
1004        else if (str.indexOf("[Gradient ") != -1)
1005        {
1006            m_type = TYPE_GRADIENT;
1007            m_primaryColor = CompiereTheme.parseColor(str.substring(str.indexOf(" Upper=")+7, str.indexOf(",Lower=")),
1008                new ColorUIResource JavaDoc(m_primaryColor));
1009            m_secondaryColor = CompiereTheme.parseColor(str.substring(str.indexOf(",Lower=")+7, str.indexOf(",Start=")),
1010                new ColorUIResource JavaDoc(m_secondaryColor));
1011            m_startPoint = Integer.parseInt(str.substring(str.indexOf(",Start=")+7, str.indexOf(",RDistance=")));
1012            setGradientRepeatDistance(str.substring(str.indexOf(",RDistance=")+11, str.lastIndexOf("]")));
1013        }
1014        else if (str.indexOf("[Line ") != -1)
1015        {
1016            m_type = TYPE_LINES;
1017            m_primaryColor = CompiereTheme.parseColor(str.substring(str.indexOf(" Color=")+7, str.indexOf(",BackColor=")),
1018                new ColorUIResource JavaDoc(m_primaryColor));
1019            m_secondaryColor = CompiereTheme.parseColor(str.substring(str.indexOf(",BackColor=")+11, str.indexOf(",Width=")),
1020                new ColorUIResource JavaDoc(m_secondaryColor));
1021            setLineWidth(str.substring(str.indexOf(",Width=")+7, str.indexOf(",Distance=")));
1022            setLineDistance(str.substring(str.indexOf(",Distance=")+10, str.lastIndexOf("]")));
1023        }
1024        else if (str.indexOf("[Texture ") != -1)
1025        {
1026            m_type = TYPE_TEXTURE;
1027            setTextureURL (str.substring(str.indexOf(" GraphURL=")+10, str.indexOf(",Taint=")));
1028            m_primaryColor = CompiereTheme.parseColor(str.substring(str.indexOf(",Taint=")+7, str.indexOf(",Alpha=")),
1029                new ColorUIResource JavaDoc(m_primaryColor));
1030            setTextureCompositeAlpha (str.substring(str.indexOf(",Alpha=")+7, str.lastIndexOf("]")));
1031        }
1032    } // parseString
1033

1034    /**
1035     * Does the background needs to be redone
1036     * @return true if there were changes
1037     */

1038    boolean isDirty()
1039    {
1040        return m_dirty;
1041    } // isDirty
1042

1043    /**
1044     * Set Dirty
1045     * @param dirty if true, the background will be re-painted
1046     */

1047    void setDirty (boolean dirty)
1048    {
1049        m_dirty = dirty;
1050    } // setDirty
1051

1052    /*************************************************************************/
1053
1054/**
1055 * Background contains a Buffered Image with the background.
1056 * The initial size is determined by the constructor.
1057 * It is resized if required when painting.
1058 * <br>
1059 * The Buffered image is a 8-bit RGBA color components packed into integer pixels.
1060 * The image has a DirectColorModel with alpha. The color data in this image
1061 * is considered to be premultiplied with alpha
1062 */

1063public class ColorBackground
1064{
1065    /**
1066     * Create Color Background
1067     * @param bounds Rectangle to fit in
1068     */

1069    public ColorBackground (Rectangle JavaDoc bounds)
1070    {
1071        createColorBackground (bounds);
1072        fillColorBackground ();
1073    } // Background
1074

1075    private int m_height = 200;
1076    private int m_width = 200;
1077    private BufferedImage JavaDoc m_back;
1078    private int m_colorBlind = ColorBlind.getColorType();
1079
1080    /**
1081     * Create Color Background
1082     * @param bounds Rectangle to fit in
1083     */

1084    private void createColorBackground (Rectangle JavaDoc bounds)
1085    {
1086        m_height = Math.max(bounds.y + bounds.height, m_height);
1087        m_width = Math.max(bounds.x + bounds.width, m_width);
1088        m_back = new BufferedImage JavaDoc (m_width, m_height, BufferedImage.TYPE_INT_ARGB_PRE);
1089    } // create Background
1090

1091
1092    /**
1093     * Fill Background with Color
1094     */

1095    public void fillColorBackground ()
1096    {
1097        Graphics2D JavaDoc g2D = m_back.createGraphics();
1098
1099        if (isGradient())
1100        {
1101            Point JavaDoc start = null;
1102            Point JavaDoc end = null;
1103            int r = 1; // repeats
1104
switch (m_startPoint)
1105            {
1106                case SwingConstants.NORTH_WEST:
1107                    start = new Point JavaDoc (0, 0);
1108                    if (m_repeatDistance > 0)
1109                        end = new Point JavaDoc (m_repeatDistance, m_repeatDistance);
1110                    // end = new Point (Math.min(m_repeatDistance, m_width), Math.min(m_repeatDistance, height));
1111
else
1112                        end = new Point JavaDoc (m_width/r, m_height/r);
1113                    break;
1114                case SwingConstants.WEST:
1115                    start = new Point JavaDoc (0, m_height/2);
1116                    if (m_repeatDistance > 0)
1117                        end = new Point JavaDoc (m_repeatDistance, m_height/2);
1118                    // end = new Point (Math.min(m_repeatDistance, m_width), m_height/2);
1119
else
1120                        end = new Point JavaDoc (m_width/r, m_height/2);
1121                    break;
1122                case SwingConstants.SOUTH_WEST:
1123                    start = new Point JavaDoc (0, m_height);
1124                    if (m_repeatDistance > 0)
1125                        end = new Point JavaDoc (m_repeatDistance, m_height-m_repeatDistance);
1126                    // end = new Point (Math.min(m_width, m_repeatDistance), Math.max(0, m_height-m_repeatDistance));
1127
else
1128                        end = new Point JavaDoc (m_width/r, m_height-(m_height/r));
1129                    break;
1130                case SwingConstants.SOUTH:
1131                    start = new Point JavaDoc (0, m_height);
1132                    if (m_repeatDistance > 0)
1133                        end = new Point JavaDoc (0, m_height-m_repeatDistance);
1134                    // end = new Point (0, Math.max(0, m_height-m_repeatDistance));
1135
else
1136                        end = new Point JavaDoc (0, m_height-(m_height/r));
1137                    break;
1138                case SwingConstants.SOUTH_EAST:
1139                    start = new Point JavaDoc (m_width, m_height);
1140                    if (m_repeatDistance > 0)
1141                        end = new Point JavaDoc (m_width-m_repeatDistance, m_height-m_repeatDistance);
1142                    // end = new Point (Math.min(0, m_width-m_repeatDistance), Math.max(0, m_height-m_repeatDistance));
1143
else
1144                        end = new Point JavaDoc (m_width-(m_width/r), m_height-(m_height/r));
1145                    break;
1146                case SwingConstants.EAST:
1147                    start = new Point JavaDoc (m_width, m_height/2);
1148                    if (m_repeatDistance > 0)
1149                        end = new Point JavaDoc (m_width-m_repeatDistance, m_height/2);
1150                    // end = new Point (Math.min(0, m_width-m_repeatDistance), m_height/2);
1151
else
1152                        end = new Point JavaDoc (m_width-(m_width/r), m_height/2);
1153                    break;
1154                case SwingConstants.NORTH_EAST:
1155                    start = new Point JavaDoc (m_width, 0);
1156                    if (m_repeatDistance > 0)
1157                        end = new Point JavaDoc (m_width-m_repeatDistance, m_repeatDistance);
1158                    // end = new Point (Math.min(0, m_width-m_repeatDistance), Math.min(m_height, m_repeatDistance));
1159
else
1160                        end = new Point JavaDoc (m_width-(m_width/r), m_height/r);
1161                    break;
1162                default:
1163                case SwingConstants.NORTH:
1164                    start = new Point JavaDoc (0, 0);
1165                    if (m_repeatDistance > 0)
1166                        end = new Point JavaDoc (0, m_repeatDistance);
1167                    // end = new Point (0, Math.min(m_height, m_repeatDistance));
1168
else
1169                        end = new Point JavaDoc (0, m_height/r);
1170            }
1171            GradientPaint JavaDoc paint = new GradientPaint JavaDoc(
1172                start,
1173                ColorBlind.getDichromatColor(getGradientUpperColor()),
1174                end,
1175                ColorBlind.getDichromatColor(getGradientLowerColor()),
1176                true); // cyclic
1177
g2D.setPaint(paint);
1178            g2D.fillRect(0, 0, m_width, m_height);
1179        }
1180        else if (isTexture())
1181        {
1182            BufferedImage JavaDoc image = getTextureImage();
1183            if (image == null)
1184            {
1185                g2D.setPaint(ColorBlind.getDichromatColor(getFlatColor()));
1186                g2D.fillRect(0, 0, m_width, m_height);
1187            }
1188            else
1189            {
1190                Rectangle JavaDoc anchor = new Rectangle JavaDoc (0,0, image.getWidth(), image.getHeight());
1191                TexturePaint JavaDoc texture = new TexturePaint JavaDoc (image, anchor);
1192                g2D.setPaint(texture);
1193                g2D.fillRect(0, 0, m_width, m_height);
1194                g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getTextureCompositeAlpha()));
1195                g2D.setPaint(ColorBlind.getDichromatColor(getTextureTaintColor()));
1196                g2D.fillRect(0, 0, m_width, m_height);
1197            }
1198        }
1199        else if (isLine())
1200        {
1201            // Background
1202
g2D.setPaint(ColorBlind.getDichromatColor(getLineBackColor()));
1203            g2D.fillRect(0, 0, m_width, m_height);
1204            // Lines
1205
g2D.setPaint(ColorBlind.getDichromatColor(getLineColor()));
1206            g2D.setStroke(new BasicStroke JavaDoc(getLineWidth()));
1207            for (int y = 0; y < m_height; y += getLineDistance())
1208                g2D.drawLine(0, y, m_width, y);
1209        }
1210        else // flat
1211
{
1212            g2D.setPaint(ColorBlind.getDichromatColor(getFlatColor()));
1213            g2D.fillRect(0, 0, m_width, m_height);
1214        }
1215        setDirty (false);
1216    } // fillBackground
1217

1218    /**
1219     * Paint/copy background to component
1220     * @param g graphics
1221     * @param c component
1222     */

1223    public void paint (Graphics JavaDoc g, JComponent JavaDoc c)
1224    {
1225        Rectangle JavaDoc bounds = c.getBounds();
1226        check (bounds);
1227        //
1228
int h = c.getHeight();
1229        int w = c.getWidth();
1230        // Copy Background
1231
g.drawImage (m_back,
1232            0, 0, // destination start point
1233
w, h, // destination end point
1234
bounds.x, bounds.y, // source start
1235
bounds.x+w, bounds.y+h, // source end
1236
c);
1237    } // paint
1238

1239    /**
1240     * Paint/copy background to component rectangle
1241     * @param g graphics
1242     * @param c compnent
1243     * @param x x pos
1244     * @param y y pos
1245     * @param w width
1246     * @param h height
1247     */

1248    public void paintRect (Graphics JavaDoc g, JComponent JavaDoc c, int x, int y, int w, int h)
1249    {
1250        Rectangle JavaDoc bounds = c.getBounds();
1251        check (bounds);
1252        // Copy Background
1253
g.drawImage (m_back,
1254            x, y, // destination start point
1255
x+w, h+y, // destination end point
1256
x, y, // source start
1257
x+w, y+h, // source end
1258
c);
1259    } // paint
1260

1261    /**
1262     * Check size of background and repaint if required
1263     * @param bounds Bounds of component
1264     */

1265    private void check (Rectangle JavaDoc bounds)
1266    {
1267        // Re-Create, if Color Type changed
1268
if (ColorBlind.getColorType() != m_colorBlind)
1269        {
1270            m_colorBlind = ColorBlind.getColorType();
1271            setDirty(true);
1272        }
1273        // we need to create new background
1274
if ((m_height < (bounds.y + bounds.height))
1275            || (m_width < (bounds.x + bounds.width)))
1276        {
1277            createColorBackground (bounds);
1278            fillColorBackground();
1279        }
1280        else if (isDirty())
1281            fillColorBackground();
1282    } // check
1283

1284} // ColorBackground
1285

1286} // CompiereColor
1287
Popular Tags