Class NbHandle

java.lang.Object
uno.anahata.asi.agi.resource.handle.AbstractResourceHandle
uno.anahata.asi.nb.resources.handle.NbHandle
All Implemented Interfaces:
EventListener, org.openide.filesystems.FileChangeListener, uno.anahata.asi.agi.resource.handle.ResourceHandle, uno.anahata.asi.persistence.Rebindable

public class NbHandle extends uno.anahata.asi.agi.resource.handle.AbstractResourceHandle implements org.openide.filesystems.FileChangeListener, uno.anahata.asi.persistence.Rebindable
A NetBeans-native resource handle that wraps a FileObject.

This is the ultimate handle for the IDE environment. It leverages NetBeans VFS to handle local files, JAR entries, and remote files (via plugins) uniformly. It is reactive: it listens for IDE events and notifies the owner Resource when the content is modified or moved.

Archive Awareness: It correctly identifies JAR and Read-Only filesystems to disable writability.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private org.openide.filesystems.FileObject
    The live NetBeans FileObject.
    private String
    Cached path for secondary resolution.
    private org.openide.loaders.OperationListener
    Strong reference to the operation listener adapter.
    private @NonNull URI
    The unique identifier URI for the resource.
    private org.openide.filesystems.FileChangeListener
    Weak listener to prevent memory leaks.
    private org.openide.loaders.OperationListener
    Weak operation listener registered with the IDE's DataLoaderPool to monitor global file moves and lifecycle changes (such as drag-and-drop or cut-and-paste across folders) without creating memory leaks.

    Fields inherited from class uno.anahata.asi.agi.resource.handle.AbstractResourceHandle

    owner
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new NbHandle from a URI.
    NbHandle(org.openide.filesystems.FileObject fileObject)
    Direct constructor for an existing FileObject.
  • Method Summary

    Modifier and Type
    Method
    Description
    Currently unsued.
    void
    boolean
    void
    fileAttributeChanged(org.openide.filesystems.FileAttributeEvent fe)
    void
    fileChanged(org.openide.filesystems.FileEvent fe)
    void
    fileDataCreated(org.openide.filesystems.FileEvent fe)
    void
    fileDeleted(org.openide.filesystems.FileEvent fe)
    void
    fileFolderCreated(org.openide.filesystems.FileEvent fe)
    void
    fileRenamed(org.openide.filesystems.FileRenameEvent fe)
    org.openide.filesystems.FileObject
    Ensures the FileObject is resolved and listeners are attached.
    long
    private void
    handleOperationMove(org.openide.loaders.OperationEvent.Move ev)
    Processes global file move operations fired by the IDE loaders layer.
    boolean
    boolean
    boolean
    long
    void
    private void
    Attaches the IDE filesystem listener.
    private void
    Re-establishes or initializes the global operation listener.
    private void
    setUri(URI uri)
    Authoritatively sets and normalizes the resource URI.
    void
    write(String content)

    Methods inherited from class uno.anahata.asi.agi.resource.handle.AbstractResourceHandle

    getOwner, setOwner

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface uno.anahata.asi.agi.resource.handle.ResourceHandle

    asBytes, asText, getHeader, getUri, isStale, isTextual
  • Field Details

    • uri

      @NonNull private @NonNull URI uri
      The unique identifier URI for the resource.
    • path

      private String path
      Cached path for secondary resolution.
    • fileObject

      private transient org.openide.filesystems.FileObject fileObject
      The live NetBeans FileObject.
    • weakListener

      private transient org.openide.filesystems.FileChangeListener weakListener
      Weak listener to prevent memory leaks.
    • weakOpListener

      private transient org.openide.loaders.OperationListener weakOpListener
      Weak operation listener registered with the IDE's DataLoaderPool to monitor global file moves and lifecycle changes (such as drag-and-drop or cut-and-paste across folders) without creating memory leaks.
    • strongOpListener

      private transient org.openide.loaders.OperationListener strongOpListener
      Strong reference to the operation listener adapter. This field anchors the listener in memory to prevent the JVM's Garbage Collector from reclaiming it while the NbHandle is alive, preserving global file-movement monitoring.
  • Constructor Details

    • NbHandle

      public NbHandle(URI uri)
      Constructs a new NbHandle from a URI.
      Parameters:
      uri - The resource URI (file:, jar:, etc.).
    • NbHandle

      public NbHandle(org.openide.filesystems.FileObject fileObject)
      Direct constructor for an existing FileObject.
      Parameters:
      fileObject - The NetBeans FileObject to wrap.
  • Method Details

    • setUri

      private void setUri(URI uri)
      Authoritatively sets and normalizes the resource URI. For local files, it forces the triple-slash (file:///) format and extracts the physical path to maintain identity consistency within the manager.
      Parameters:
      uri - The URI to set.
    • getFileObject

      public org.openide.filesystems.FileObject getFileObject()
      Ensures the FileObject is resolved and listeners are attached.
      Returns:
      The FileObject instance.
    • setupListener

      private void setupListener()
      Attaches the IDE filesystem listener.
    • setupOperationListener

      private void setupOperationListener()
      Re-establishes or initializes the global operation listener.

      Leverages the DataLoaderPool.createWeakOperationListener(org.openide.loaders.OperationListener, java.lang.Object) utility to bind a weak listener that automatically unloads if this resource handle is GCed.

    • handleOperationMove

      private void handleOperationMove(org.openide.loaders.OperationEvent.Move ev)
      Processes global file move operations fired by the IDE loaders layer.

      If the original primary file of the moved DataObject matches our active FileObject, this handle detaches its old file listeners, updates its internally managed URI and path to point to the new destination file, re-attaches new listeners, and marks the parent resource dirty.

      Parameters:
      ev - The IDE-fired move operation event.
    • getName

      public String getName()

      Implementation details: Resolves name via the NetBeans FileObject or the URI path component.

      Specified by:
      getName in interface uno.anahata.asi.agi.resource.handle.ResourceHandle
    • getHtmlDisplayName

      public String getHtmlDisplayName()

      Implementation details: Extracts the HTML display name from the IDE's DataObject delegate to show Git status or compiler errors.

      Specified by:
      getHtmlDisplayName in interface uno.anahata.asi.agi.resource.handle.ResourceHandle
    • getMimeType

      public String getMimeType()

      Implementation details: Leverages NetBeans native FileObject MIME detection with a Tika fallback for unrecognized types.

      Specified by:
      getMimeType in interface uno.anahata.asi.agi.resource.handle.ResourceHandle
    • asTextFromEditorIfOpenInEditor

      public String asTextFromEditorIfOpenInEditor() throws IOException
      Currently unsued. Prioritizes the active NetBeans Document content if the file is open in the editor, use this to show unsaved changes to the model.
      Returns:
      The text content of the active document in the editor, or file content if not open.
      Throws:
      IOException - If reading the document or stream fails.
    • getLastModified

      public long getLastModified()

      Implementation details: Retrieves the authoritative last modified timestamp from the NetBeans VFS.

      Specified by:
      getLastModified in interface uno.anahata.asi.agi.resource.handle.ResourceHandle
    • isModified

      public boolean isModified()

      Implementation details: Checks if the NetBeans DataObject associated with this FileObject is currently marked as modified in the IDE (i.e. has unsaved editor changes).

      Specified by:
      isModified in interface uno.anahata.asi.agi.resource.handle.ResourceHandle
    • exists

      public boolean exists()

      Implementation details: Checks the validity of the NetBeans FileObject.

      Specified by:
      exists in interface uno.anahata.asi.agi.resource.handle.ResourceHandle
    • openStream

      public InputStream openStream() throws IOException

      Implementation details: Opens an InputStream directly via the NetBeans FileObject.

      Specified by:
      openStream in interface uno.anahata.asi.agi.resource.handle.ResourceHandle
      Throws:
      IOException
    • isWritable

      public boolean isWritable()

      Implementation details: Checks both filesystem read-only status (e.g., JARs) and FileObject OS-level writability.

      Specified by:
      isWritable in interface uno.anahata.asi.agi.resource.handle.ResourceHandle
    • write

      public void write(String content) throws IOException

      Implementation details: Writes content via the NetBeans FileObject output stream, using the handle's detected charset.

      Specified by:
      write in interface uno.anahata.asi.agi.resource.handle.ResourceHandle
      Throws:
      IOException
    • isVirtual

      public boolean isVirtual()

      Implementation details: returns false as this represents a physical persistent resource within the IDE.

      Specified by:
      isVirtual in interface uno.anahata.asi.agi.resource.handle.ResourceHandle
    • length

      public long length()

      Returns the size of the NetBeans FileObject.

      Specified by:
      length in interface uno.anahata.asi.agi.resource.handle.ResourceHandle
    • getCharset

      public Charset getCharset()

      Implementation details: Resolves the charset using the NetBeans FileEncodingQuery to ensure project alignment.

      Specified by:
      getCharset in interface uno.anahata.asi.agi.resource.handle.ResourceHandle
    • rebind

      public void rebind()

      Implementation details: Refreshes the FileObject resolution and re-attaches listeners after deserialization.

      Specified by:
      rebind in interface uno.anahata.asi.persistence.Rebindable
      Overrides:
      rebind in class uno.anahata.asi.agi.resource.handle.AbstractResourceHandle
    • dispose

      public void dispose()

      Implementation details: Detaches the weak IDE filesystem listener to prevent leaks.

      Specified by:
      dispose in interface uno.anahata.asi.agi.resource.handle.ResourceHandle
    • fileChanged

      public void fileChanged(org.openide.filesystems.FileEvent fe)

      Implementation details: Notifies the owner resource of content changes detected by the IDE using the new unified markDirty() marker.

      Specified by:
      fileChanged in interface org.openide.filesystems.FileChangeListener
    • fileRenamed

      public void fileRenamed(org.openide.filesystems.FileRenameEvent fe)

      Implementation details: Updates internal URI/Path and notifies owner of the identity change within the IDE using the unified dirty marker.

      Specified by:
      fileRenamed in interface org.openide.filesystems.FileChangeListener
    • fileDataCreated

      public void fileDataCreated(org.openide.filesystems.FileEvent fe)
      Specified by:
      fileDataCreated in interface org.openide.filesystems.FileChangeListener
    • fileFolderCreated

      public void fileFolderCreated(org.openide.filesystems.FileEvent fe)
      Specified by:
      fileFolderCreated in interface org.openide.filesystems.FileChangeListener
    • fileDeleted

      public void fileDeleted(org.openide.filesystems.FileEvent fe)

      Implementation details: Notifies owner that the source is gone.

      Specified by:
      fileDeleted in interface org.openide.filesystems.FileChangeListener
    • fileAttributeChanged

      public void fileAttributeChanged(org.openide.filesystems.FileAttributeEvent fe)
      Specified by:
      fileAttributeChanged in interface org.openide.filesystems.FileChangeListener