public abstract class FolderInstance extends Task implements InstanceCookie
DataObject.Container
. It implements
InstanceCookie
, so it
can be used as a cookie for a node or data object.
When created on a container and started by invoking run method, it scans its content (in a separate thread) and creates a list of instances from which the new instance of this object should be composed. The object automatically listens to changes of components in the container, and if some change occurs, it allows the subclass to create a new object.
Subclasses shall override the following methods:
FolderInstance.createInstance(InstanceCookie[])
(required): this method is
called whenever the content has been changed. Its implementation
shall build up the data structures and perform the actions required
by this implementation.FolderInstance.acceptDataObject(DataObject)
, FolderInstance.acceptCookie(InstanceCookie)
,
FolderInstance.acceptFolder(DataFolder)
and
FolderInstance.acceptContainer(DataObject.Container)
(optional): the standard
way is to override one or several of the latter methods. Overriding
FolderInstance.acceptDataObject(DataObject)
more deeply
modifies the default behavior, because the default implementation of
FolderInstance.acceptDataObject(DataObject)
calls the
other 3 filter methods. See the method documentation for details.InstanceCookie
methods
FolderInstance.instanceClass()
(optional but recommended)
to inform about the class implemented by the return value of
FolderInstance.instanceCreate()
.FolderInstance.postCreationTask(java.lang.Runnable)
and/or FolderInstance.instanceForCookie(org.openide.loaders.DataObject, org.openide.cookies.InstanceCookie)
, but it is not common to need these.InstanceCookie.Of
Modifier and Type | Field and Description |
---|---|
protected DataFolder |
folder
Folder to work with.
|
Constructor and Description |
---|
FolderInstance(DataFolder df)
Create new folder instance.
|
FolderInstance(DataObject.Container container)
A new object that listens on changes in a container.
|
Modifier and Type | Method and Description |
---|---|
protected InstanceCookie |
acceptContainer(DataObject.Container container)
Allows subclasses to decide how they want to work with an object
that implements a DataObject.Container.
|
protected InstanceCookie |
acceptCookie(InstanceCookie cookie)
Allows subclasses to decide whether they want to work with
the specified
InstanceCookie or not. |
protected InstanceCookie |
acceptDataObject(DataObject dob)
Allows subclasses to decide whether they want to work with the specified
DataObject or not. |
protected InstanceCookie |
acceptFolder(DataFolder df)
Allows subclasses to decide how they want to work with a
provided folder.
|
protected abstract Object |
createInstance(InstanceCookie[] cookies)
Notifies subclasses that the set of cookies for this folder
has changed.
|
Class<?> |
instanceClass()
Returns the root class of all objects.
|
Object |
instanceCreate()
Creates instance.
|
void |
instanceFinished()
Wait for instance initialization to finish.
|
protected Object |
instanceForCookie(DataObject obj,
InstanceCookie cookie)
Method that is called when a the folder instance really wants to
create an object from provided cookie.
|
String |
instanceName()
The name of the class that we create.
|
protected Task |
postCreationTask(Runnable run)
Invokes the creation of objects in a "safe" thread.
|
void |
recreate()
Starts recreation of the instance in special thread.
|
void |
run()
Synchronously starts the creation of the instance.
|
String |
toString() |
void |
waitFinished()
Overrides the instance finished to deal with
internal state correctly.
|
addTaskListener, isFinished, notifyFinished, notifyRunning, removeTaskListener, waitFinished
protected DataFolder folder
public FolderInstance(DataFolder df)
df
- data folder to create instances frompublic FolderInstance(DataObject.Container container)
container
- the object to associate withpublic String instanceName()
instanceName
in interface InstanceCookie
public Class<?> instanceClass() throws IOException, ClassNotFoundException
instanceClass
in interface InstanceCookie
IOException
- an I/O error occuredClassNotFoundException
- the class has not been foundpublic Object instanceCreate() throws IOException, ClassNotFoundException
instanceCreate
in interface InstanceCookie
IOException
- an I/O error occuredClassNotFoundException
- the class has not been foundpublic final void instanceFinished()
public void waitFinished()
waitFinished
in class Task
public void run()
protected InstanceCookie acceptDataObject(DataObject dob)
DataObject
or not.
The default implementation roughly performs the following steps:
dob
has an InstanceCookie
FolderInstance.acceptCookie(InstanceCookie)
is called on that cookiedob
has a DataFolder
cookie,
FolderInstance.acceptFolder(DataFolder)
is called on that folderdob
has a DataObject.Container
cookie,
FolderInstance.acceptContainer(DataObject.Container)
is called on that
containerThe first of the aforementioned steps which returns a non-null
cookie and does not throw an exception determines the return value. If
none of the steps succeeds, null
is returned.
dob
- a DataObject
to testDataObject
or null
if it should not be usedprotected InstanceCookie acceptCookie(InstanceCookie cookie) throws IOException, ClassNotFoundException
InstanceCookie
or not.
The default implementation simply
returns the same cookie, but subclasses may
decide to return null
or a different cookie.
Compare FolderInstance.acceptDataObject(DataObject)
to learn when this method
is called.
cookie
- the instance cookie to testnull
if this cookie should not
be usedIOException
- if an I/O error occurred calling a cookie methodClassNotFoundException
- if a class is not found in a call to a cookie methodprotected InstanceCookie acceptFolder(DataFolder df)
The default implementation simply calls FolderInstance.acceptContainer(DataObject.Container)
.
A common override of this method is to return a new
FolderInstance
based on the subfolder, permitting
recursion.
Compare FolderInstance.acceptDataObject(DataObject)
to learn when this method
is called.
df
- data folder to create cookie fornull
if this folder should not
be usedprotected InstanceCookie acceptContainer(DataObject.Container container)
By default this returns null
to indicated that subfolders
(as well as DataShadow
s, etc.) should be ignored.
A common override of this method is to return a new
FolderInstance
based on the subfolder, permitting
recursion.
Compare FolderInstance.acceptDataObject(DataObject)
to learn when this method
is called.
container
- the container to accept or notnull
if this object should
be ignoredprotected abstract Object createInstance(InstanceCookie[] cookies) throws IOException, ClassNotFoundException
It may be poor style for this method to have side-effects. A
common way to use FolderInstance
is to have this
method set some global state which is then used as the resulting
instance. Better is to treat the FolderInstance
as
pure SPI and assign it to a variable of type
InstanceCookie
. Then use the FolderInstance.instanceCreate()
method to get the final result. However in some
cases there is a singleton live object which must be updated
in-place, and it only makes sense to do so here (in which case
the InstanceCookie
methods are unused).
cookies
- updated array of instance cookies for the folderIOException
- an I/O error occuredClassNotFoundException
- a class has not been foundprotected Object instanceForCookie(DataObject obj, InstanceCookie cookie) throws IOException, ClassNotFoundException
InstanceCookie.instanceCreate()
).obj
- the data object that is the source of the cookiecookie
- the instance cookie to read the instance fromIOException
- when there I/O errorClassNotFoundException
- if the class cannot be foundpublic void recreate()
protected Task postCreationTask(Runnable run)
The default implementation invokes the creation logic in the request processor in non-blocking mode (no other tasks will block on this).
run
- runnable to run