1 19 20 package org.openide.loaders; 21 22 23 import java.io.*; 24 import java.util.*; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 import org.openide.cookies.ConnectionCookie; 28 import org.openide.nodes.Node; 29 30 36 @Deprecated 37 public class ConnectionSupport extends Object implements ConnectionCookie { 38 39 private static final String EA_LISTENERS = "EA-OpenIDE-Connection"; 41 42 private MultiDataObject.Entry entry; 43 44 private ConnectionCookie.Type[] types; 45 46 private Set<ConnectionCookie.Type> typesSet; 47 48 50 private LinkedList<Pair> listeners; 51 52 56 public ConnectionSupport (MultiDataObject.Entry entry, ConnectionCookie.Type[] types) { 57 this.entry = entry; 58 this.types = types; 59 listeners = new LinkedList<Pair>(); 61 } 62 63 76 public synchronized void register (ConnectionCookie.Type type, Node listener) throws IOException { 77 testSupported (type); 79 80 boolean persistent = type.isPersistent (); 81 LinkedList<Pair> list; 82 83 if (persistent) { 84 list = (LinkedList<Pair>)entry.getFile ().getAttribute (EA_LISTENERS); 85 } else { 86 list = listeners; 87 } 88 89 if (list == null) { 90 list = new LinkedList<Pair> (); 92 } 93 94 98 Iterator<Pair> it = list.iterator (); 99 while (it.hasNext ()) { 100 Pair pair = it.next (); 101 if (type.equals (pair.getType ())) { 103 Node n; 104 try { 105 n = pair.getNode (); 106 } catch (IOException e) { 108 Logger.getLogger(ConnectionSupport.class.getName()).log(Level.WARNING, null, e); 110 it.remove (); 111 continue; 113 } 114 if (n.equals (listener)) { 116 it.remove(); 119 continue; 120 } 121 else { 122 } 124 } 125 } 126 list.add (persistent ? new Pair (type, listener.getHandle ()) : new Pair (type, listener)); 127 128 130 if (persistent) { 131 entry.getFile ().setAttribute (EA_LISTENERS, list); 133 } 134 135 } 136 137 143 public synchronized void unregister (ConnectionCookie.Type type, Node listener) throws IOException { 144 testSupported (type); 146 147 boolean persistent = type.isPersistent (); 148 LinkedList list; 149 150 if (persistent) { 151 list = (LinkedList)entry.getFile ().getAttribute (EA_LISTENERS); 152 } else { 153 list = listeners; 154 } 155 156 if (list == null) { 157 return; 159 } 160 161 165 Iterator it = list.iterator (); 166 while (it.hasNext ()) { 167 Pair pair = (Pair)it.next (); 168 169 if (type.equals (pair.getType ())) { 170 Node n; 171 try { 172 n = pair.getNode (); 173 } catch (IOException e) { 174 it.remove (); 176 continue; 178 } 179 if (n.equals (listener)) { 180 it.remove (); 182 184 continue; 185 } 186 } 187 } 188 189 191 if (persistent) { 192 entry.getFile ().setAttribute (EA_LISTENERS, list); 194 } 195 } 196 197 200 public Set<ConnectionCookie.Type> getTypes () { 201 if (typesSet == null) 202 typesSet = Collections.unmodifiableSet (new HashSet<ConnectionCookie.Type> (Arrays.asList (types))); 203 return typesSet; 204 } 205 206 211 public List<ConnectionCookie.Type> getRegisteredTypes() { 212 LinkedList<ConnectionCookie.Type> typesList = new LinkedList<ConnectionCookie.Type>(); 213 214 LinkedList<Pair> list = listeners; 215 for (int i = 0; i <= 1; i++) { 216 if (i == 1) 217 list = (LinkedList<Pair>)entry.getFile ().getAttribute (EA_LISTENERS); 218 219 if (list == null) 220 continue; 221 222 for (Pair p: list) { 223 typesList.add(p.getType()); 224 } 225 } 226 227 return typesList; 228 } 229 230 233 public void fireEvent (ConnectionCookie.Event ev) { 234 LinkedList<Pair> list; 235 ConnectionCookie.Type type; 236 boolean persistent; 237 238 synchronized (this) { 239 type = ev.getType (); 240 241 persistent = type.isPersistent (); 242 if (persistent) { 243 list = (LinkedList<Pair>)entry.getFile ().getAttribute (EA_LISTENERS); 244 } else { 245 list = listeners; 246 } 247 248 if (list == null) return; 249 250 list = (LinkedList<Pair>)list.clone (); 251 } 252 253 int size = list.size (); 254 255 Iterator<Pair> it = list.iterator (); 256 while (it.hasNext ()) { 257 Pair pair = it.next (); 258 259 if (pair.getType ().overlaps(ev.getType())) { 260 try { 261 ConnectionCookie.Listener l = (ConnectionCookie.Listener)pair.getNode ().getCookie (ConnectionCookie.Listener.class); 262 if (l != null) { 263 try { 264 l.notify (ev); 265 } catch (IllegalArgumentException e) { 266 it.remove (); 267 } catch (ClassCastException e) { 268 it.remove (); 269 } 270 } 271 } catch (IOException e) { 272 it.remove (); 273 } 274 } 275 } 276 277 if (persistent && list.size() != size) { 279 try { 281 entry.getFile ().setAttribute (EA_LISTENERS, list); 282 } catch (IOException e) { 283 } 285 } 286 } 287 288 292 public synchronized java.util.Set listenersFor (ConnectionCookie.Type type) { 293 LinkedList<Pair> list; 294 295 if (type.isPersistent ()) { 296 list = (LinkedList<Pair>)entry.getFile ().getAttribute (EA_LISTENERS); 297 } else { 298 list = listeners; 299 } 300 301 if (list == null) return Collections.emptySet(); 302 303 Iterator<Pair> it = list.iterator (); 304 HashSet<Node> set = new HashSet<Node> (7); 305 306 while (it.hasNext ()) { 307 Pair pair = (Pair)it.next (); 308 if (type.overlaps(pair.getType ())) { 309 try { 310 set.add (pair.getNode ()); 311 } catch (IOException e) { 312 } 314 } 315 } 316 317 return set; 318 } 319 320 321 325 private void testSupported (ConnectionCookie.Type t) throws InvalidObjectException { 326 for (int i = 0; i < types.length; i++) { 327 if (t.overlaps(types[i])) { 328 return; 329 } 330 } 331 throw new InvalidObjectException (t.toString ()); 332 } 333 334 336 private static final class Pair extends Object implements java.io.Serializable { 337 338 private ConnectionCookie.Type type; 339 340 private Object value; 341 342 static final long serialVersionUID =387180886175136728L; 343 346 public Pair (ConnectionCookie.Type t, Node n) { 347 type = t; 348 value = n; 349 } 350 351 355 public Pair (ConnectionCookie.Type t, Node.Handle h) throws IOException { 356 357 if (h == null) throw new IOException (); 358 359 type = t; 360 value = h; 361 } 362 363 365 public ConnectionCookie.Type getType () { 366 return type; 367 } 368 369 373 public Node getNode () throws IOException { 374 return value instanceof Node ? (Node)value : ((Node.Handle)value).getNode (); 375 } 376 } 377 } 378 | Popular Tags |