1 19 20 21 package org.netbeans.modules.properties; 22 23 24 import java.io.*; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import java.util.HashMap ; 28 import javax.swing.text.BadLocationException ; 29 30 import org.openide.text.PositionBounds; 31 import org.openide.ErrorManager; 32 33 34 40 public class PropertiesStructure extends Element { 41 42 45 private Map <String ,Element.ItemElem> items; 46 47 48 private StructHandler handler; 49 50 51 static final long serialVersionUID = -78380271920882131L; 52 53 54 55 public PropertiesStructure(PositionBounds bounds, Map <String ,Element.ItemElem> items) { 56 super(bounds); 57 for (Element.ItemElem itemElem : items.values()) { 59 itemElem.setParent(this); 60 } 61 this.items = items; 62 } 63 64 65 68 public void update(PropertiesStructure struct) { 69 synchronized(getParent()) { 70 synchronized(getParentBundleStructure()) { 71 boolean structChanged = false; 72 Element.ItemElem oldItem; 73 74 Map <String ,Element.ItemElem> new_items = struct.items; 75 Map <String ,Element.ItemElem> changed = new HashMap <String ,Element.ItemElem>(); 76 Map <String ,Element.ItemElem> inserted = new HashMap <String ,Element.ItemElem>(); 77 Map <String ,Element.ItemElem> deleted = new HashMap <String ,Element.ItemElem>(); 78 79 for (Element.ItemElem curItem : new_items.values()) { 80 curItem.setParent(this); 81 oldItem = getItem(curItem.getKey()); 82 if (oldItem == null) { 83 inserted.put(curItem.getKey(), curItem); 84 } else { 85 if (!curItem.equals(oldItem)) { 86 changed.put(curItem.getKey(), curItem); 87 } 88 items.remove(oldItem.getKey()); 89 } 90 } 91 92 deleted = items; 93 if ((deleted.size() > 0) || (inserted.size() > 0)) { 94 structChanged = true; 95 } 96 items = new_items; 98 99 this.bounds = struct.getBounds(); 101 102 if (structChanged) { 104 structureChanged(changed, inserted, deleted); 105 } else { 106 for (Element.ItemElem itemElem : changed.values()) { 108 itemChanged(itemElem); 109 } 110 } 111 } 112 } 113 } 114 115 116 void setParent(StructHandler parent) { 117 handler = parent; 118 } 119 120 122 public StructHandler getParent() { 123 if (handler == null) { 124 throw new IllegalStateException (); 125 } 126 return handler; 127 } 128 129 130 private BundleStructure getParentBundleStructure() { 131 PropertiesDataObject dataObj; 132 dataObj = (PropertiesDataObject) getParent().getEntry().getDataObject(); 133 return dataObj.getBundleStructure(); 134 } 135 136 138 public String getDocumentString() { 139 StringBuilder sb = new StringBuilder (); 140 for (Element.ItemElem item : items.values()) { 141 sb.append(item.getDocumentString()); 142 } 143 144 return sb.toString(); 145 } 146 147 149 public String toString() { 150 StringBuilder sb = new StringBuilder (); 151 for (Element.ItemElem item : items.values()) { 152 sb.append(item.toString()); 153 sb.append("- - -\n"); } 155 156 return sb.toString(); 157 } 158 159 163 public Element.ItemElem getItem(String key) { 164 return items.get(key); 165 } 166 167 173 public boolean renameItem(String oldKey, String newKey) { 174 synchronized(getParent()) { 175 synchronized(getParentBundleStructure()) { 176 Element.ItemElem item = getItem(newKey); 177 if (item == null) { 178 item = getItem(oldKey); 179 if (item == null) { 180 return false; 181 } 182 items.remove(oldKey); 183 items.put(newKey, item); 184 item.setKey(newKey); return true; 186 } 187 else { 188 return false; 189 } 190 } 191 } 192 } 193 194 196 public boolean deleteItem(String key) { 197 synchronized(getParent()) { 198 synchronized(getParentBundleStructure()) { 199 Element.ItemElem item = getItem(key); 200 201 if (item == null) { 202 return false; 203 } 204 try { 205 item.getBounds().setText(""); items.remove(key); 207 structureChanged(); return true; 209 } catch (IOException e) { 210 ErrorManager.getDefault().notify(e); 211 return false; 212 } catch (BadLocationException e) { 213 ErrorManager.getDefault().notify(e); 214 return false; 215 } 216 } 217 } 218 } 219 220 226 public boolean addItem(String key, String value, String comment) { 227 Element.ItemElem item = getItem(key); 228 if (item != null) { 229 return false; 230 } 231 item = new Element.ItemElem(null, 233 new Element.KeyElem (null, key), 234 new Element.ValueElem (null, value), 235 new Element.CommentElem(null, comment)); 236 try { 238 synchronized(getParent()) { 239 synchronized(getParentBundleStructure()) { 240 PositionBounds pos = getBounds(); 241 242 PositionBounds itemBounds 243 = pos.insertAfter("\n") 244 .insertAfter(item.getDocumentString()); 245 item.bounds = itemBounds; 246 247 item.setParent(this); 249 items.put(key, item); 250 structureChanged(); 251 252 return true; 253 } 254 } 255 } catch (IOException ioe) { 256 return false; 257 } catch (BadLocationException ble) { 258 return false; 259 } 260 } 261 262 263 public Iterator <Element.ItemElem> allItems() { 264 return items.values().iterator(); 265 } 266 267 268 void itemChanged(Element.ItemElem elem) { 269 getParentBundleStructure().notifyItemChanged(this, elem); 270 } 271 272 273 void structureChanged() { 274 getParentBundleStructure().notifyOneFileChanged(getParent()); 275 } 276 277 279 void structureChanged(Map <String ,Element.ItemElem> changed, 280 Map <String ,Element.ItemElem> inserted, 281 Map <String ,Element.ItemElem> deleted) { 282 getParentBundleStructure().notifyOneFileChanged( 283 getParent(), 284 changed, 285 inserted, 286 deleted); 287 } 288 289 293 void itemKeyChanged(String oldKey, Element.ItemElem newElem) { 294 Map <String ,Element.ItemElem> changed = new HashMap <String ,Element.ItemElem>(); 297 Map <String ,Element.ItemElem> inserted = new HashMap <String ,Element.ItemElem>(); 298 Map <String ,Element.ItemElem> deleted = new HashMap <String ,Element.ItemElem>(); 299 300 Element.ItemElem item = getItem(oldKey); 302 if (item == null) { 303 Element.ItemElem emptyItem = new Element.ItemElem( 305 null, 306 new Element.KeyElem(null, oldKey), 307 new Element.ValueElem(null, ""), new Element.CommentElem(null, "")); deleted.put(oldKey, emptyItem); 310 } else { 311 changed.put(item.getKey(), item); 313 } 314 inserted.put(newElem.getKey(), newElem); 316 317 structureChanged(changed, inserted, deleted); 318 } 319 } 320 | Popular Tags |