KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > css > visual > model > ColorModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * ColorModel.java
22  *
23  * Created on October 27, 2004, 1:53 PM
24  */

25
26 package org.netbeans.modules.css.visual.model;
27
28 import java.awt.Color JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33 import javax.swing.DefaultComboBoxModel JavaDoc;
34
35 /**
36  * For holding and obtaining color related data
37  * @author Winston Prakash
38  * @version 1.0
39  */

40 public class ColorModel {
41     private Color JavaDoc color = Color.BLACK;
42
43     private Map JavaDoc colorNameHexMap = CssProperties.getColorNameHexMap();
44
45     public DefaultComboBoxModel JavaDoc getColorList(){
46         return new ColorList();
47     }
48
49     public Color JavaDoc getColor(){
50         return color;
51     }
52
53     public void setColor(Color JavaDoc newColor){
54         color = newColor;
55     }
56
57     public void setColor(String JavaDoc newColor){
58         setColor(Color.BLACK);
59         if(newColor.startsWith(CssStyleData.NOT_SET)){
60             return;
61         }else if(newColor.startsWith("#")){ //NOI18N
62
// Hex Color - #FFFFFF
63
setHexColor(newColor);
64         }else if (newColor.startsWith("rgb")){ //NOI18N
65
try{
66                 // RGB Color - rgb(255,255,255);
67
String JavaDoc rgbString = newColor.substring(newColor.indexOf("(") + 1, newColor.indexOf(")")); //NOI18N
68
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(rgbString,","); //NOI18N
69
int red = Integer.parseInt(st.nextToken().trim());
70                 int green = Integer.parseInt(st.nextToken().trim());
71                 int blue = Integer.parseInt(st.nextToken().trim());
72                 color = new Color JavaDoc(red,green,blue);
73             }catch(Exception JavaDoc exc){
74                 color = Color.BLACK;
75             }
76         }else {
77             String JavaDoc hexValue = (String JavaDoc) colorNameHexMap.get(newColor);
78             if(hexValue != null){
79                 setHexColor(hexValue);
80             }
81         }
82     }
83
84     public void setHexColor(String JavaDoc hexColor){
85         int start = 0;
86         if(hexColor.startsWith("#")){
87             start = 1;
88         }
89         try{
90             int red = Integer.parseInt(hexColor.substring(start,start+2),16);
91             int green = Integer.parseInt(hexColor.substring(start+2, start+4),16);
92             int blue = Integer.parseInt(hexColor.substring(start+4, start+6),16);
93             color = new Color JavaDoc(red,green,blue);
94         }catch(Exception JavaDoc exc){
95             color = Color.BLACK;
96         }
97     }
98     
99     public String JavaDoc getHexColor(Color JavaDoc col){
100         color = col;
101         return getHexColor();
102     }
103     
104     public String JavaDoc getHexColor(){
105         String JavaDoc redHex = Integer.toHexString(color.getRed());
106         if(redHex.length()<2) redHex = "0" + redHex; //NOI18N
107
String JavaDoc greenHex = Integer.toHexString(color.getGreen());
108         if(greenHex.length()<2) greenHex = "0" + greenHex; //NOI18N
109
String JavaDoc blueHex = Integer.toHexString(color.getBlue());
110         if(blueHex.length()<2) blueHex = "0" + blueHex; //NOI18N
111

112         String JavaDoc hexString = "#" + redHex + greenHex + blueHex; //NOI18N
113

114         return hexString;
115     }
116     
117     public class ColorList extends DefaultComboBoxModel JavaDoc{
118         public ColorList(){
119             addElement(CssStyleData.NOT_SET);
120             for(Iterator JavaDoc iter=colorNameHexMap.keySet().iterator(); iter.hasNext();){
121                 addElement(iter.next());
122             }
123         }
124     }
125     
126 }
127
Popular Tags