1 24 25 26 package org.netbeans.modules.javadoc.httpfs; 27 28 import java.io.*; 29 import java.beans.*; 30 import java.net.*; 31 import java.util.*; 32 33 import org.openide.filesystems.*; 34 import org.openide.filesystems.FileSystem; 35 import org.openide.util.NbBundle; 36 import org.openide.util.SharedClassObject; 37 import org.openide.util.actions.SystemAction; 38 39 44 public class HTTPFileSystem extends FileSystem implements VetoableChangeListener { 45 46 51 public static final String PROP_URL = "URL"; 57 public static final String PROP_REFRESH_RATE = "RefreshRate"; 63 public static final String PROP_STATE = "State"; 69 public static final int STATE_UNKNOWN = 0; 70 75 public static final int STATE_READING = 1; 76 81 public static final int STATE_COMPLETE = 2; 82 83 private static final long serialVersionUID = 200104; 84 private static final String DEFAULT_URL = "http://www.netbeans.org/download/apis/"; 87 88 transient URL baseURL; 90 transient HTTPRootFileObject rootFileObject; 92 transient int refreshRate; 94 transient int currentState; 96 97 102 public HTTPFileSystem() { 103 104 setHidden( true ); 105 addVetoableChangeListener( this ); 106 refreshRate = 0; 107 currentState = STATE_UNKNOWN; 108 109 try{ 110 111 setURL( DEFAULT_URL ); 114 } catch( PropertyVetoException e ) { 115 116 e.printStackTrace( ); 118 119 } 120 121 } 122 123 124 131 private void writeObject(ObjectOutputStream out) throws IOException { 132 133 out.writeObject( baseURL.toString( ) ); 135 136 out.writeInt( refreshRate ); 138 139 } 140 141 142 149 private void readObject(ObjectInputStream in) 150 throws IOException, ClassNotFoundException { 151 152 addVetoableChangeListener( this ); 154 try { 155 156 setURL( (String )in.readObject( ) ); 158 159 try { 161 162 refreshRate = in.readInt( ); 164 165 } catch( IOException e ) { 167 168 refreshRate = 0; 170 171 } 172 173 } catch( PropertyVetoException e ) { 174 175 throw new IOException( e.getMessage( ) ); 176 177 } 178 179 } 180 181 182 191 public String getURL( ) { 192 193 return baseURL.toString(); 194 195 } 196 197 198 210 public synchronized void setURL( String url ) 211 throws PropertyVetoException { 212 213 URL oldURL; 215 HTTPRootFileObject oldRootFileObject; 217 218 219 oldURL = baseURL; 221 oldRootFileObject = rootFileObject; 222 223 try { 224 225 try { 227 228 baseURL = new URL( url ); 229 230 } 231 catch( java.net.MalformedURLException mlfEx ){ 232 233 throw new PropertyVetoException( mlfEx.toString( ), new PropertyChangeEvent( this, PROP_URL, oldURL != null ? oldURL.toExternalForm( ) : null, url ) ); 234 235 } 236 rootFileObject = new HTTPRootFileObject( this ); 238 fireVetoableChange( PROP_URL, oldURL != null ? oldURL.toExternalForm( ) : null, url ); 240 241 setSystemName( this.getClass( ).getName( ) + "/" + baseURL.toExternalForm( ) ); 244 } catch( PropertyVetoException e ) { 245 246 baseURL = oldURL; 248 rootFileObject = oldRootFileObject; 249 throw e; 250 251 } 252 firePropertyChange( PROP_URL, oldURL != null ? oldURL.toExternalForm( ) : null, url ); 253 firePropertyChange( PROP_ROOT, oldRootFileObject, rootFileObject ); 254 255 } 256 257 258 267 public int getRefreshRate( 268 ) { 269 270 return refreshRate; 271 272 } 273 274 275 285 public void setRefreshRate( 286 int newRefreshRate 287 ) throws PropertyVetoException { 288 289 int oldRefreshRate; 290 291 292 oldRefreshRate = refreshRate; 293 try { 294 295 refreshRate = newRefreshRate; 296 297 fireVetoableChange( PROP_REFRESH_RATE, new Integer ( oldRefreshRate ), new Integer ( newRefreshRate ) ); 299 300 } catch( PropertyVetoException e ) { 301 302 refreshRate = oldRefreshRate; 304 throw e; 305 306 } 307 firePropertyChange( PROP_REFRESH_RATE, new Integer ( oldRefreshRate ), new Integer ( newRefreshRate ) ); 308 309 } 310 311 312 319 public int getState( 320 ) { 321 322 return currentState; 323 324 } 325 326 327 334 void setState( 335 int newState 336 ) { 337 338 int oldState; 340 String oldDisplayName; 342 343 344 if( newState != currentState ) { 346 347 oldDisplayName = getDisplayName( ); 349 350 oldState = currentState; 352 currentState = newState; 353 firePropertyChange( PROP_STATE, new Integer ( oldState ), new Integer ( newState ) ); 354 355 if( !oldDisplayName.equals( getDisplayName( ) ) ) { 357 358 firePropertyChange( PROP_DISPLAY_NAME, oldDisplayName, getDisplayName( ) ); 360 361 } 362 363 } 364 365 } 366 367 368 376 public void vetoableChange( 377 PropertyChangeEvent propertyChangeEvent 378 ) throws PropertyVetoException { 379 380 URL newURL; 382 int newRefreshRate; 384 385 386 if( propertyChangeEvent.getSource( ) == this && propertyChangeEvent.getPropertyName( ).equals( PROP_URL ) ) { 388 389 try { 391 392 newURL = new URL( (String )propertyChangeEvent.getNewValue( ) ); 393 394 } 395 catch( MalformedURLException mlfEx ){ 396 397 throw new PropertyVetoException( mlfEx.toString( ), propertyChangeEvent ); 398 399 } 400 401 if( !newURL.getProtocol( ).equals( "http" ) && !newURL.getProtocol( ).equals( "https" ) ) { 404 throw new PropertyVetoException( NbBundle.getMessage(HTTPFileSystem.class, "MSG_NotHTTPProtocol" ), propertyChangeEvent ); 407 } 408 if( !newURL.toExternalForm( ).endsWith( "/" ) ){ 411 throw new PropertyVetoException( NbBundle.getMessage(HTTPFileSystem.class, "MSG_NotDirectory" ), propertyChangeEvent ); 414 } 415 416 } else if( propertyChangeEvent.getSource( ) == this && propertyChangeEvent.getPropertyName( ).equals( PROP_REFRESH_RATE ) ) { 418 419 newRefreshRate = ( (Integer )propertyChangeEvent.getNewValue( ) ).intValue( ); 420 421 if( newRefreshRate < 0 ) { 423 424 throw new PropertyVetoException( NbBundle.getMessage(HTTPFileSystem.class, "MSG_RefreshRateCannotBeNegative" ), propertyChangeEvent ); 427 } 428 429 } 430 431 } 432 433 434 441 public FileObject getRoot() { 442 443 return rootFileObject; 444 445 } 446 447 448 456 public String getDisplayName( ) { 457 458 String messageKey; 460 Object replacementValues[]; 462 463 if( getState( ) == STATE_READING ) { 465 466 messageKey = "DisplayName_Scanning"; } else { 470 471 messageKey = "DisplayName_Normal"; } 474 475 return NbBundle.getMessage(HTTPFileSystem.class, messageKey, baseURL.toExternalForm( ) ); 476 477 } 478 479 480 490 public FileObject findResource(String resourceName) { 491 492 StringTokenizer pathParser; 494 HTTPFileObject foundFileObject; 496 497 498 pathParser = new StringTokenizer( resourceName, "/" ); foundFileObject = (HTTPFileObject)getRoot( ); 501 502 while( foundFileObject != null && pathParser.hasMoreElements( ) ) { 504 505 foundFileObject = foundFileObject.child( (String )pathParser.nextElement( ) ); 506 507 } 508 return foundFileObject; 509 } 510 511 512 519 public boolean isReadOnly( ) { 520 521 return true; 522 } 523 524 525 535 public org.openide.util.actions.SystemAction[] getActions( 536 ) { 537 538 Class refreshActionClass; 540 RefreshAction refreshAction; 542 543 544 refreshActionClass = RefreshAction.class; 546 refreshAction = (RefreshAction)SharedClassObject.findObject( refreshActionClass, true ); 547 548 return new SystemAction[ ] { refreshAction }; 550 551 } 552 553 554 559 protected void finalize( ) throws Throwable { 560 561 removeVetoableChangeListener(this); 562 rootFileObject = null; 563 baseURL = null; 564 565 } 566 567 } 568 | Popular Tags |