1 19 20 package org.netbeans.api.debugger; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 31 115 public final class Session { 116 117 118 public static final String PROP_CURRENT_LANGUAGE = "currentLanguage"; 119 120 121 public static final String PROP_SUPPORTED_LANGUAGES = "supportedLanguages"; 122 123 124 126 private String name; 127 private String locationName; 128 private DebuggerEngine currentDebuggerEngine; 129 private String currentLanguage; 130 private String [] languages; 131 private DebuggerEngine[] engines; 132 private PropertyChangeSupport pcs; 134 private Lookup lookup; 135 Lookup privateLookup; 136 137 138 140 Session ( 141 String name, 142 String locationName, 143 String id, 144 Object [] services, 145 Lookup diLookup 146 ) { 147 this.name = name; 148 this.locationName = locationName; 149 this.languages = new String [0]; 150 this.engines = new DebuggerEngine [0]; 151 pcs = new PropertyChangeSupport (this); 152 153 Object [] s = new Object [services.length + 1]; 155 System.arraycopy (services, 0, s, 0, services.length); 156 s [s.length - 1] = this; 157 privateLookup = new Lookup.Compound ( 158 new Lookup.Instance (s), 159 new Lookup.MetaInf (id) 160 ); 161 this.lookup = new Lookup.Compound ( 162 diLookup, 163 privateLookup 164 ); 165 } 166 167 168 170 175 public String getName () { 176 return name; 177 } 178 179 186 190 195 public String getLocationName () { 196 return locationName; 197 } 198 199 204 public DebuggerEngine getCurrentEngine () { 205 return currentDebuggerEngine; 206 } 207 208 213 public String getCurrentLanguage () { 214 return currentLanguage; 215 } 216 217 223 public String [] getSupportedLanguages () { 224 return languages; 225 } 226 227 233 public List lookup (String folder, Class service) { 234 return lookup.lookup (folder, service); 235 } 236 237 243 public Object lookupFirst (String folder, Class service) { 244 return lookup.lookupFirst (folder, service); 245 } 246 247 252 public void kill () { 253 HashSet dead = new HashSet (Arrays.asList (engines)); 254 Iterator i = dead.iterator (); 255 while (i.hasNext ()) 256 ((DebuggerEngine) i.next ()).getActionsManager (). 257 doAction (ActionsManager.ACTION_KILL); 258 } 259 260 265 public DebuggerEngine getEngineForLanguage (String language) { 266 int i, k = languages.length; 267 for (i = 0; i < k; i++) 268 if (languages [i].equals (language)) 269 return engines [i]; 270 return null; 271 } 272 273 280 public void setCurrentLanguage (String language) { 281 int i, k = languages.length; 282 for (i = 0; i < k; i++) { 283 if (language.equals (languages [i])) { 284 Object oldL = currentLanguage; 285 currentLanguage = language; 286 currentDebuggerEngine = engines [i]; 287 pcs.firePropertyChange ( 288 PROP_CURRENT_LANGUAGE, 289 oldL, 290 currentLanguage 291 ); 292 } 293 } 294 } 295 296 297 299 Lookup getLookup () { 300 return lookup; 301 } 302 303 void addLanguage ( 304 String language, 305 DebuggerEngine engine 306 ) { 307 int i, k = languages.length; 309 for (i = 0; i < k; i++) 310 if (language.equals (languages [i])) { 311 engines [i] = engine; 312 return; 313 } 314 315 String [] newLanguages = new String [languages.length + 1]; 317 DebuggerEngine[] newEngines = new DebuggerEngine [engines.length + 1]; 318 System.arraycopy (languages, 0, newLanguages, 0, languages.length); 319 System.arraycopy (engines, 0, newEngines, 0, engines.length); 320 newLanguages [languages.length] = language; 321 newEngines [engines.length] = engine; 322 Object oldL = languages; 323 languages = newLanguages; 324 engines = newEngines; 325 DebuggerManager.getDebuggerManager ().addEngine (engine); 326 pcs.firePropertyChange ( 327 PROP_SUPPORTED_LANGUAGES, 328 oldL, 329 languages 330 ); 331 if (currentLanguage == null) { 336 setCurrentLanguage (language); 337 } 338 } 339 340 void removeEngine ( 341 DebuggerEngine engine 342 ) { 343 if (engines.length == 0) return; 344 int i, k = engines.length; 345 ArrayList newLanguages = new ArrayList (); 346 ArrayList newEngines = new ArrayList (); 347 for (i = 0; i < k; i++) 348 if (!engine.equals (engines [i])) { 349 newLanguages.add (languages [i]); 350 newEngines.add (engines [i]); 351 } 352 String [] oldL = languages; 353 languages = (String []) newLanguages.toArray 354 (new String [newLanguages.size ()]); 355 engines = (DebuggerEngine[]) newEngines.toArray 356 (new DebuggerEngine [newEngines.size ()]); 357 DebuggerManager.getDebuggerManager ().removeEngine (engine); 358 359 pcs.firePropertyChange ( 360 PROP_SUPPORTED_LANGUAGES, 361 oldL, 362 languages 363 ); 364 } 365 366 void removeLanguage ( 367 String language, 368 DebuggerEngine engine 369 ) { 370 int i, k = languages.length; 371 for (i = 0; i < k; i++) 372 if (language.equals (languages [i])) { 373 if (engines [i] != engine) 374 throw new IllegalArgumentException (); 375 break; 376 } 377 if (i >= k) return; 378 379 String [] newLanguages = new String [k - 1]; 380 DebuggerEngine[] newEngines = new DebuggerEngine [k - 1]; 381 if (i > 0) { 382 System.arraycopy (languages, 0, newLanguages, 0, i); 383 System.arraycopy (engines, 0, newEngines, 0, i); 384 } 385 System.arraycopy (languages, i + 1, newLanguages, i, k - i - 1); 386 System.arraycopy (engines, i + 1, newEngines, i, k - i - 1); 387 Object oldL = languages; 388 languages = newLanguages; 389 engines = newEngines; 390 pcs.firePropertyChange ( 391 PROP_SUPPORTED_LANGUAGES, 392 oldL, 393 languages 394 ); 395 396 k = engines.length; 397 for (i = 0; i < k; i++) 398 if (engines [i] == engine) 399 return; 400 DebuggerManager.getDebuggerManager ().removeEngine (engine); 401 } 402 403 408 public String toString () { 409 return "" + getClass ().getName () + " " + getLocationName () + ":" + 410 getName (); 411 } 412 413 414 416 417 422 public void addPropertyChangeListener (PropertyChangeListener l) { 423 pcs.addPropertyChangeListener (l); 424 } 425 426 431 public void removePropertyChangeListener (PropertyChangeListener l) { 432 pcs.removePropertyChangeListener (l); 433 } 434 435 441 public void addPropertyChangeListener (String propertyName, PropertyChangeListener l) { 442 pcs.addPropertyChangeListener (propertyName, l); 443 } 444 445 451 public void removePropertyChangeListener (String propertyName, PropertyChangeListener l) { 452 pcs.removePropertyChangeListener (propertyName, l); 453 } 454 455 456 458 } 478 479 480 | Popular Tags |