1 19 20 25 26 package org.netbeans.modules.css.visual.model; 27 28 import org.netbeans.modules.css.loader.CssDataObject; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.List ; 32 import java.util.Map ; 33 import org.openide.loaders.DataObject; 34 35 38 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 modelMap = new HashMap (); 53 54 List ruleSet = new ArrayList (); 55 56 List listeners = new ArrayList (); 57 58 static CssMetaModel currentModel; 59 60 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 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 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 if(listeners.contains(listener)){ 115 listeners.remove(listener); 116 } 117 } 118 119 synchronized public void setModelListeners(List listeners){ 120 this.listeners = listeners; 122 } 123 124 synchronized public List getModelListeners(){ 125 List listenersCopy = new ArrayList (listeners); 127 return listenersCopy; 128 } 129 130 synchronized public void removeAllModelListener(){ 131 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 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 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 } 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 |