KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CssMetaModel.java
22  *
23  * Created on February 2, 2005, 4:14 PM
24  */

25
26 package org.netbeans.modules.css.visual.model;
27
28 import org.netbeans.modules.css.loader.CssDataObject;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import org.openide.loaders.DataObject;
34
35 // XXX - This class is a hack. Must find better way of sharing the data
36
// among Editor, Navigator window & Style builder.
37

38 /**
39  * Meta model for the whole CSS file
40  * @author Winston Prakash
41  * @version 1.0
42  */

43 public class CssMetaModel {
44
45     static DataObject dataObject = null;
46
47     private CssRuleData selectedRule;
48
49     boolean showStyleBuilder = true;
50     boolean showStylePreview = true;
51
52     static Map JavaDoc modelMap = new HashMap JavaDoc();
53
54     List JavaDoc ruleSet = new ArrayList JavaDoc();
55
56     List JavaDoc listeners = new ArrayList JavaDoc();
57
58     static CssMetaModel currentModel;
59
60     /** Creates a new instance of CssMetaModel */
61     private CssMetaModel() {
62     }
63
64     synchronized static public CssMetaModel getInstance(){
65         if(modelMap.containsKey(dataObject)){
66             return (CssMetaModel) modelMap.get(dataObject);
67         }else{
68             return null;
69         }
70     }
71
72     public static CssMetaModel getInstance(CssDataObject cssDataObject) {
73         if(modelMap.containsKey(cssDataObject)){
74             return (CssMetaModel) modelMap.get(cssDataObject);
75         }else{
76             return null;
77         }
78     }
79
80     /**
81      * Useful for setting the associated DataObject
82      */

83     public static void setDataObject(DataObject dObject){
84         if((dataObject == null) || (dataObject != dObject)){
85             dataObject = dObject;
86             if(!modelMap.containsKey(dataObject)){
87                 modelMap.put(dataObject, new CssMetaModel());
88             }
89         }
90     }
91
92     /**
93      * Useful for getting the associated DataObject
94      */

95     public static DataObject getDataObject(){
96         return dataObject;
97     }
98
99     public static void removeDataObject(DataObject cssDataObject) {
100         if(modelMap.containsKey(cssDataObject)){
101             modelMap.remove(cssDataObject);
102         }
103         if(cssDataObject.equals(dataObject)){
104             dataObject = null;
105         }
106     }
107
108     synchronized public void addModelListener(CssModelListener listener){
109         listeners.add(listener);
110     }
111
112     synchronized public void removeModelListener(CssModelListener listener){
113         //System.out.println("removeModelListeners");
114
if(listeners.contains(listener)){
115             listeners.remove(listener);
116         }
117     }
118
119     synchronized public void setModelListeners(List JavaDoc listeners){
120         //System.out.println("CssMetaModel Set ModelListeners");
121
this.listeners = listeners;
122     }
123
124     synchronized public List JavaDoc getModelListeners(){
125         //System.out.println("CssMetaModel Get ModelListeners");
126
List JavaDoc listenersCopy = new ArrayList JavaDoc(listeners);
127         return listenersCopy;
128     }
129
130     synchronized public void removeAllModelListener(){
131         //System.out.println("CssMetaModel RemoveModelListeners");
132
listeners.clear();
133     }
134
135     synchronized public void setStylePreviewVisibility(boolean show){
136         showStylePreview = show;
137         for( int i=0; i< listeners.size(); i++){
138             CssModelListener listener = (CssModelListener)listeners.get(i);
139             listener.viewChanged(new CssModelChangeEvent(showStylePreview));
140         }
141     }
142
143     synchronized public boolean getStylePreviewVisibility(){
144         return showStylePreview;
145     }
146
147     synchronized public void setStyleBuilderVisibility(boolean show){
148         showStyleBuilder = show;
149         for( int i=0; i< listeners.size(); i++){
150             CssModelListener listener = (CssModelListener)listeners.get(i);
151             listener.viewChanged(new CssModelChangeEvent(showStyleBuilder));
152         }
153     }
154
155     public boolean getStyleBuilderVisibility(){
156         return showStyleBuilder;
157     }
158     
159     synchronized public CssRuleData[] getRuleSet(){
160         return (CssRuleData[]) ruleSet.toArray(new CssRuleData[ruleSet.size()]);
161     }
162     
163     synchronized public void setRuleSet(List JavaDoc rules){
164         ruleSet = rules;
165         for( int i=0; i< listeners.size(); i++){
166             CssModelListener listener = (CssModelListener)listeners.get(i);
167             listener.ruleAdded(new CssModelChangeEvent(null));
168         }
169     }
170     
171     synchronized public void addRule(CssRuleData rule){
172         if(!ruleSet.contains(rule.getRule())){
173             ruleSet.add(rule);
174             for( int i=0; i< listeners.size(); i++){
175                 CssModelListener listener = (CssModelListener)listeners.get(i);
176                 listener.ruleAdded(new CssModelChangeEvent(rule));
177             }
178         }
179     }
180     
181     synchronized public void removeRule(CssRuleData rule){
182         if(ruleSet.contains(rule)){
183             ruleSet.remove(rule);
184             for( int i=0; i< listeners.size(); i++){
185                 CssModelListener listener = (CssModelListener)listeners.get(i);
186                 listener.ruleRemoved(new CssModelChangeEvent(rule));
187             }
188         }
189     }
190     
191     synchronized public void setSelectedRule(CssRuleData rule){
192         if(selectedRule != rule){
193             selectedRule = rule;
194             for( int i=0; i< listeners.size(); i++){
195                 CssModelListener listener = (CssModelListener)listeners.get(i);
196                 listener.selectionChanged(new CssModelChangeEvent(rule));
197             }
198         }
199     }
200     
201     synchronized public CssRuleData getSelectedRule() {
202         return selectedRule;
203     }
204
205     public void setSelectedRuleName(String JavaDoc ruleName) {
206         CssRuleData selRule = null;
207         for(int i=0; i< ruleSet.size(); i++){
208             CssRuleData rule = (CssRuleData) ruleSet.get(i);
209             if (rule.getRule().equals(ruleName)){
210                 selRule = rule;
211                 break;
212             }
213         }
214         if(selRule != null){
215            setSelectedRule(selRule);
216         }
217     }
218    
219     public static interface CssModelChangeListener{
220          
221    }
222              
223     public static interface CssModelListener{
224         public void ruleAdded(CssModelChangeEvent evt);
225         public void ruleRemoved(CssModelChangeEvent evt);
226         public void selectionChanged(CssModelChangeEvent evt);
227         public void viewChanged(CssModelChangeEvent evt);
228         //public void dataObjectChanged(CssModelChangeEvent evt);
229
}
230     
231     public static class CssModelChangeEvent{
232         CssRuleData cssRule = null;
233         boolean changeView;
234         
235         public CssModelChangeEvent(boolean changeView){
236             this.changeView = changeView;
237         }
238         
239         public CssModelChangeEvent(CssRuleData rule){
240             cssRule = rule;
241         }
242         
243         public CssRuleData setRule(){
244             return cssRule;
245         }
246         
247         public CssRuleData getRule(){
248             return cssRule;
249         }
250         
251         public boolean changeView(){
252             return changeView;
253         }
254     }
255 }
256
Popular Tags