1 19 package org.openide.nodes; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 25 import java.util.*; 26 27 28 34 public final class Sheet extends Object { 35 36 public static final String PROPERTIES = "properties"; 38 39 public static final String EXPERT = "expert"; 41 42 private ArrayList<Set> sets; 43 44 45 private Node.PropertySet[] array; 46 47 48 private PropertyChangeSupport supp = new PropertyChangeSupport (this); 49 50 51 private PropertyChangeListener propL = new PropertyChangeListener () { 52 public void propertyChange(PropertyChangeEvent ev) { 53 supp.firePropertyChange(null, null, null); 54 } 55 }; 56 57 59 public Sheet() { 60 this(new ArrayList<Set>(2)); 61 } 62 63 66 private Sheet(ArrayList<Set> ar) { 67 sets = ar; 68 } 69 70 73 public final Node.PropertySet[] toArray() { 74 Node.PropertySet[] l = array; 75 76 if (l != null) { 77 return l; 78 } 79 80 synchronized (this) { 81 if (array != null) { 82 return array; 83 } 84 85 array = new Node.PropertySet[sets.size()]; 86 sets.toArray(array); 87 88 return array; 89 } 90 } 91 92 94 public synchronized Sheet cloneSheet() { 95 int len = sets.size(); 96 ArrayList<Set> l = new ArrayList<Set>(len); 97 98 for (int i = 0; i < len; i++) { 99 l.add(sets.get(i).cloneSet()); 100 } 101 102 return new Sheet(l); 103 } 104 105 109 public synchronized Set get(String name) { 110 int indx = findIndex(name); 111 112 return (indx == -1) ? null : sets.get(indx); 113 } 114 115 122 public synchronized Set put(Set set) { 123 int indx = findIndex(set.getName()); 124 Set removed; 125 126 if (indx == -1) { 127 sets.add(set); 128 removed = null; 129 } else { 130 removed = sets.set(indx, set); 131 } 132 133 set.removePropertyChangeListener(propL); 134 135 if (removed == null) { 136 set.addPropertyChangeListener(propL); 137 } 138 139 refresh(); 140 141 return removed; 142 } 143 144 148 public synchronized Set remove(String set) { 149 int indx = findIndex(set); 150 151 if (indx != -1) { 152 Set s = sets.remove(indx); 153 s.removePropertyChangeListener(propL); 154 refresh(); 155 156 return s; 157 } else { 158 return null; 159 } 160 } 161 162 167 public static Sheet createDefault() { 168 Sheet newSheet = new Sheet(); 169 170 newSheet.put(createPropertiesSet()); 172 173 return newSheet; 174 } 175 176 180 public static Sheet.Set createPropertiesSet() { 181 Sheet.Set ps = new Sheet.Set(); 182 ps.setName(PROPERTIES); 183 ps.setDisplayName(Node.getString("Properties")); 184 ps.setShortDescription(Node.getString("HINT_Properties")); 185 186 return ps; 187 } 188 189 193 public static Sheet.Set createExpertSet() { 194 Sheet.Set ps = new Sheet.Set(); 195 ps.setExpert(true); 196 ps.setName(EXPERT); 197 ps.setDisplayName(Node.getString("Expert")); 198 ps.setShortDescription(Node.getString("HINT_Expert")); 199 200 return ps; 201 } 202 203 205 public void addPropertyChangeListener(PropertyChangeListener l) { 206 supp.addPropertyChangeListener(l); 207 } 208 209 211 public void removePropertyChangeListener(PropertyChangeListener l) { 212 supp.removePropertyChangeListener(l); 213 } 214 215 219 private int findIndex(String name) { 220 int s = sets.size(); 221 222 for (int i = 0; i < s; i++) { 223 Node.PropertySet p = (Node.PropertySet) sets.get(i); 224 225 if (p.getName().equals(name)) { 226 return i; 227 } 228 } 229 230 return -1; 231 } 232 233 235 private void refresh() { 236 array = null; 237 supp.firePropertyChange(null, null, null); 238 } 239 240 241 243 public static final class Set extends Node.PropertySet { 244 245 private List<Node.Property<?>> props; 246 247 248 private Node.Property<?>[] array; 249 250 251 private PropertyChangeSupport supp = new PropertyChangeSupport (this); 252 253 255 public Set() { 256 this(new ArrayList<Node.Property<?>>()); 257 } 258 259 261 private Set(List<Node.Property<?>> al) { 262 props = al; 263 } 264 265 268 public synchronized Set cloneSet() { 269 return new Set(new ArrayList<Node.Property<?>>(props)); 270 } 271 272 276 public Node.Property<?> get(String name) { 277 int indx = findIndex(name); 278 279 return (indx == -1) ? null : props.get(indx); 280 } 281 282 285 public Node.Property<?>[] getProperties() { 286 Node.Property<?>[] l = array; 287 288 if (l != null) { 289 return l; 290 } 291 292 synchronized (this) { 293 if (array != null) { 294 return array; 295 } 296 297 array = new Node.Property<?>[props.size()]; 298 props.toArray(array); 299 300 return array; 301 } 302 } 303 304 308 public synchronized Node.Property<?> put(Node.Property<?> p) { 309 int indx = findIndex(p.getName()); 310 Node.Property<?> removed; 311 312 if (indx != -1) { 313 removed = props.set(indx, p); 315 } else { 316 props.add(p); 318 removed = null; 319 } 320 321 refresh(); 323 324 return removed; 325 } 326 327 331 public synchronized void put(Node.Property<?>[] ar) { 332 for (int i = 0; i < ar.length; i++) { 333 Node.Property<?> p = ar[i]; 334 p = ar[i]; 335 336 int indx = findIndex(p.getName()); 337 338 if (indx != -1) { 339 props.set(indx, p); 341 } else { 342 props.add(p); 344 } 345 } 346 347 refresh(); 349 } 350 351 355 public synchronized Node.Property<?> remove(String name) { 356 int indx = findIndex(name); 357 358 if (indx != -1) { 359 try { 360 return props.remove(indx); 361 } finally { 362 refresh(); 364 } 365 } else { 366 return null; 367 } 368 } 369 370 372 public void addPropertyChangeListener(PropertyChangeListener l) { 373 supp.addPropertyChangeListener(l); 374 } 375 376 378 public void removePropertyChangeListener(PropertyChangeListener l) { 379 supp.removePropertyChangeListener(l); 380 } 381 382 386 private int findIndex(String name) { 387 int s = props.size(); 388 389 for (int i = 0; i < s; i++) { 390 Node.Property<?> p = props.get(i); 391 392 if (p.getName().equals(name)) { 393 return i; 394 } 395 } 396 397 return -1; 398 } 399 400 402 private void refresh() { 403 array = null; 404 supp.firePropertyChange(null, null, null); 405 } 406 } 407 } 409 | Popular Tags |