KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CssStyleData.java
22  *
23  * Created on December 17, 2004, 11:35 AM
24  */

25
26 package org.netbeans.modules.css.visual.model;
27
28 import java.awt.Font JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30 import java.beans.PropertyChangeSupport JavaDoc;
31 import java.io.StringWriter JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Properties JavaDoc;
40 import java.util.Set JavaDoc;
41 import java.util.TreeSet JavaDoc;
42 import javax.swing.JTextArea JavaDoc;
43 import org.openide.util.NbBundle;
44
45 /**
46  * Data structure for the Style Properties
47  * @author Winston Prakash
48  * @version 1.0
49  */

50 public class CssStyleData {
51     public final static String JavaDoc PREVIEW_NOT_SUPPORTED = NbBundle.getMessage(CssStyleData.class, "PREVIEW_NOT_SUPPORTED_MSG"); //NOI18N
52

53     public final static String JavaDoc NOT_SET = NbBundle.getMessage(CssStyleData.class, "NOT_SET"); //NOI18N
54
public final static String JavaDoc VALUE = NbBundle.getMessage(CssStyleData.class, "VALUE"); //NOI18N
55

56     Properties JavaDoc styleProperties = new Properties JavaDoc();
57     
58     Map JavaDoc fontFaceFamilyMap = new HashMap JavaDoc();
59     Set JavaDoc fontNames = new HashSet JavaDoc();
60     
61     Font JavaDoc defaultFont = new JTextArea JavaDoc().getFont();
62     String JavaDoc defaultFontName = defaultFont.getFontName();
63     int defaultFontSize = defaultFont.getSize();
64     int defaultFontStyle = defaultFont.getStyle();
65     
66     /**
67      * Holds value of property test.
68      */

69     private String JavaDoc test;
70     
71     /**
72      * Utility field used by bound properties.
73      */

74     private PropertyChangeSupport JavaDoc propertyChangeSupport = new PropertyChangeSupport JavaDoc(this);
75     
76     public CssStyleData(){
77     }
78     
79      
80     /**
81      * Get the Map of property value pair
82      */

83     public Map JavaDoc getPropertyMap(){
84         Map JavaDoc propertyMap = new HashMap JavaDoc();
85         for(Iterator JavaDoc iter = styleProperties.keySet().iterator(); iter.hasNext();){
86             String JavaDoc property = (String JavaDoc)iter.next();
87             String JavaDoc propertyValue = getProperty(property).trim();
88             if(!(propertyValue.equals(VALUE) || propertyValue.equals(NOT_SET) || propertyValue.equals(""))){
89                 propertyMap.put(property, propertyValue);
90             }
91         }
92         return propertyMap;
93     }
94     
95     /**
96      * Get the value of specified property from the set.
97      * @return Value of the specified property.
98      */

99     public String JavaDoc getProperty(String JavaDoc property) {
100         return (String JavaDoc)styleProperties.get(property);
101     }
102     
103     /**
104      * Add the specified property to the property set.
105      * @param property name & value of the property.
106      */

107     public void addProperty(String JavaDoc property, String JavaDoc value) {
108         if(value != null){
109             if (styleProperties.containsKey(property)) {
110                 styleProperties.remove(property);
111             }
112             styleProperties.put(property, value.trim());
113         }
114     }
115     
116     /**
117      * Modify the specified property in the property set.
118      * @param property name & value of the property.
119      */

120     public void modifyProperty(String JavaDoc property, String JavaDoc newValue) {
121         String JavaDoc oldValue = null;
122         newValue = newValue.trim();
123         if (styleProperties.containsKey(property)) {
124             oldValue = getProperty(property);
125             //if ((newValue != null) && oldValue.equals(newValue.trim())) return;
126
styleProperties.remove(property);
127         }
128         if((newValue != null) && (!newValue.equals(""))){
129             styleProperties.put(property, newValue);
130         }
131         propertyChangeSupport.firePropertyChange("property", oldValue, newValue); //NOI18N
132
}
133     
134     public String JavaDoc getStyleValue(){
135         return toString();
136     }
137     
138     /**
139      * Reove the specified property from the property set.
140      * @param property name & value of the property.
141      */

142     public void removeProperty(String JavaDoc property) {
143         if (styleProperties.containsKey(property)) {
144             String JavaDoc oldValue = getProperty(property);
145             styleProperties.remove(property);
146             propertyChangeSupport.firePropertyChange("property", oldValue, null); //NOI18N
147
}
148     }
149     
150     public String JavaDoc toString(){
151         StringWriter JavaDoc strWriter = new StringWriter JavaDoc();
152         for(Iterator JavaDoc iter = styleProperties.keySet().iterator(); iter.hasNext();){
153             String JavaDoc property = (String JavaDoc)iter.next();
154             strWriter.write(property);
155             strWriter.write(":");
156             strWriter.write(getProperty(property));
157             if(iter.hasNext()) strWriter.write("; ");
158         }
159         //return strWriter.toString().replaceAll("\"","""); //NOI18N
160
return strWriter.toString();
161     }
162     
163     public String JavaDoc getFormattedString(){
164         StringWriter JavaDoc strWriter = new StringWriter JavaDoc();
165         strWriter.write("\n"); //NOI18N
166
TreeSet JavaDoc sortedProperties = new TreeSet JavaDoc();
167         for(Iterator JavaDoc iter = styleProperties.keySet().iterator(); iter.hasNext();){
168             sortedProperties.add(iter.next());
169         }
170         for(Iterator JavaDoc iter = sortedProperties.iterator(); iter.hasNext();){
171             String JavaDoc property = (String JavaDoc)iter.next();
172             String JavaDoc propertyValue = getProperty(property).trim();
173             if(!(propertyValue.equals(NOT_SET) || propertyValue.equals(""))){
174                 strWriter.write(" " + property);
175                 strWriter.write(": ");
176                 strWriter.write(propertyValue);
177                 if(iter.hasNext()) {
178                     strWriter.write(";\n"); //NOI18N
179
}else{
180                     strWriter.write("\n"); //NOI18N
181
}
182             }
183         }
184         return strWriter.toString();
185     }
186     
187     /**
188      * Adds a PropertyChangeListener to the listener list.
189      * @param listener The listener to add.
190      */

191     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
192         propertyChangeSupport.addPropertyChangeListener(listener);
193     }
194     
195     /**
196      * Removes a PropertyChangeListener from the listener list.
197      * @param listener The listener to remove.
198      */

199     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
200         propertyChangeSupport.removePropertyChangeListener(listener);
201     }
202 }
203
Popular Tags