public abstract class DataObject extends Object implements Node.Cookie, Serializable, HelpCtx.Provider, Lookup.Provider
DataObject.getLookup()
lookup pattern. Since version 6.0
this class implements Lookup.Provider
.Modifier and Type | Class and Description |
---|---|
static interface |
DataObject.Container
Interface for objects that can contain other data objects.
|
static interface |
DataObject.Factory
Factory interface for converting file object to data objects.
|
static interface |
DataObject.Registration
Registers new file type into the system.
|
static interface |
DataObject.Registrations
May be used to allow multiple
DataObject.Registration at one place. |
static class |
DataObject.Registry
Registry of modified data objects.
|
Modifier and Type | Field and Description |
---|---|
static String |
PROP_COOKIE
Name of the property used during notification of changes in the set of cookies attached to this object.
|
static String |
PROP_FILES
Name of files property.
|
static String |
PROP_HELP
Name of the help context property.
|
static String |
PROP_MODIFIED
Name of the modified property.
|
static String |
PROP_NAME
Name of the name property.
|
static String |
PROP_PRIMARY_FILE
Name of primary file property.
|
static String |
PROP_TEMPLATE
Name of the template property.
|
static String |
PROP_VALID
Name of valid property.
|
Constructor and Description |
---|
DataObject(FileObject pf,
DataLoader loader)
Create a new data object.
|
Modifier and Type | Method and Description |
---|---|
void |
addPropertyChangeListener(PropertyChangeListener l)
Add a property change listener.
|
void |
addVetoableChangeListener(VetoableChangeListener l)
Add a listener to vetoable changes.
|
DataObject |
copy(DataFolder f)
Copy this object to a folder.
|
DataObject |
createFromTemplate(DataFolder f)
Create a new object from template (with a name depending on the template).
|
DataObject |
createFromTemplate(DataFolder f,
String name)
Create a new object from template.
|
DataObject |
createFromTemplate(DataFolder f,
String name,
Map<String,? extends Object> parameters)
More generic way how to instantiate a
DataObject . |
protected Node |
createNodeDelegate()
Provides node that should represent this data object.
|
DataShadow |
createShadow(DataFolder f)
Creates shadow for this object in specified folder.
|
void |
delete()
Delete this object.
|
protected void |
dispose()
Allows subclasses to discard the object.
|
Set<FileObject> |
files()
Get all contained files.
|
static DataObject |
find(FileObject fo)
Finds the data object for a specified file object.
|
protected void |
firePropertyChange(String name,
Object oldValue,
Object newValue)
Fires property change notification to all listeners registered via
DataObject.addPropertyChangeListener(java.beans.PropertyChangeListener) . |
protected void |
fireVetoableChange(String name,
Object oldValue,
Object newValue)
Fires vetoable change notification.
|
<T extends Node.Cookie> |
getCookie(Class<T> c)
Obtain a cookie from the data object.
|
protected <T extends Node.Cookie> |
getCookie(DataShadow shadow,
Class<T> clazz)
When a request for a cookie is done on a DataShadow of this DataObject
this methods gets called (by default) so the DataObject knows which
DataShadow is asking and extract some information from the shadow itself.
|
DataFolder |
getFolder()
Get the folder this data object is stored in.
|
abstract HelpCtx |
getHelpCtx()
Get help context for this object.
|
DataLoader |
getLoader()
Get the loader that created this data object.
|
Lookup |
getLookup()
Represents a context of the data object.
|
String |
getName()
Get the name of the data object.
|
Node |
getNodeDelegate()
Get the node delegate.
|
FileObject |
getPrimaryFile()
Get the primary file for this data object.
|
static DataObject.Registry |
getRegistry()
Get the registry containing all modified objects.
|
protected abstract DataObject |
handleCopy(DataFolder f)
Copy this object to a folder (implemented by subclasses).
|
protected DataObject |
handleCopyRename(DataFolder f,
String name,
String ext)
Copy and rename this object to a folder (implemented by subclasses).
|
protected abstract DataObject |
handleCreateFromTemplate(DataFolder df,
String name)
Create a new data object from template (implemented in subclasses).
|
protected DataShadow |
handleCreateShadow(DataFolder f)
Creates shadow for this object in specified folder (overridable in subclasses).
|
protected abstract void |
handleDelete()
Delete this object (implemented by subclasses).
|
protected abstract FileObject |
handleMove(DataFolder df)
Move this object to another folder (implemented in subclasses).
|
protected abstract FileObject |
handleRename(String name)
Rename this object (implemented in subclasses).
|
abstract boolean |
isCopyAllowed()
Test whether the object may be copied.
|
abstract boolean |
isDeleteAllowed()
Test whether the object may be deleted.
|
boolean |
isModified()
Test whether the object is modified.
|
abstract boolean |
isMoveAllowed()
Test whether the object may be moved.
|
abstract boolean |
isRenameAllowed()
Test whether the object may be renamed.
|
boolean |
isShadowAllowed()
Test whether the object may create shadows.
|
boolean |
isTemplate()
Get the template status of this data object.
|
boolean |
isValid()
Test whether the data object is still valid and usable.
|
protected void |
markFiles()
Mark all contained files as belonging to this loader.
|
void |
move(DataFolder df)
Move this object to another folder.
|
void |
removePropertyChangeListener(PropertyChangeListener l)
Remove a property change listener.
|
void |
removeVetoableChangeListener(VetoableChangeListener l)
Add a listener to vetoable changes.
|
void |
rename(String name)
Rename this object.
|
void |
setModified(boolean modif)
Set whether the object is considered modified.
|
void |
setTemplate(boolean newTempl)
Set the template status of this data object.
|
void |
setValid(boolean valid)
Setter that allows to destroy this data object.
|
protected FileLock |
takePrimaryFileLock()
Obtains lock for primary file.
|
String |
toString() |
Object |
writeReplace()
The Serialization replacement for this object stores the primary file instead.
|
public static final String PROP_TEMPLATE
public static final String PROP_NAME
public static final String PROP_HELP
public static final String PROP_MODIFIED
public static final String PROP_COOKIE
public static final String PROP_VALID
public static final String PROP_PRIMARY_FILE
public static final String PROP_FILES
public DataObject(FileObject pf, DataLoader loader) throws DataObjectExistsException
pf
- primary file object for this data objectloader
- loader that created the data objectDataObjectExistsException
- if there is already a data object
for this primary fileprotected void dispose()
The normal use of this method is to change the type of a data object. Because this would usually only be invoked from the original data object, it is protected.
public void setValid(boolean valid) throws PropertyVetoException
valid
- should be falsePropertyVetoException
- if the invalidation has been vetoedpublic final boolean isValid()
The object can become invalid when it is deleted, its files are deleted, or
DataObject.dispose()
is called.
When the validity of the object changes a property change event is fired, so anyone can listen and be notified when the object is deleted/disposed.
public final DataLoader getLoader()
protected final void markFiles() throws IOException
IOException
public Set<FileObject> files()
The default implementation returns a set consisting only of the primary file.
public final Node getNodeDelegate()
creates it
(if it does not
already exist) or
returns a previously created instance of it.protected Node createNodeDelegate()
The default implementation creates an instance of DataNode
.
Most subclasses will override this method to provide a DataNode
(usually subclassed).
This method is called only once per data object.
It is strongly recommended that the resulting node will, when asked for
the cookie DataObject.class
, return this same data object.
It is also recommended that the node:
DataObject.getName()
.
DataNode.getShowFileExtensions()
.
protected FileLock takePrimaryFileLock() throws IOException
IOException
- if taking the lock failspublic final void setTemplate(boolean newTempl) throws IOException
newTempl
- true
if the object should be a templateIOException
- if setting the template state failspublic final boolean isTemplate()
true
if it is a templatepublic abstract boolean isDeleteAllowed()
true
if it maypublic abstract boolean isCopyAllowed()
true
if it maypublic abstract boolean isMoveAllowed()
true
if it maypublic boolean isShadowAllowed()
The default implementation returns true
.
true
if it maypublic abstract boolean isRenameAllowed()
true
if it maypublic boolean isModified()
true
if it is modifiedpublic void setModified(boolean modif)
true
, the data object is added into a registry
of opened data objects.
If the new value is false
,
the data object is removed from the registry.public abstract HelpCtx getHelpCtx()
getHelpCtx
in interface HelpCtx.Provider
public final FileObject getPrimaryFile()
*.java
and *.class
files but the primary one is
always *.java
. Please note that two data objects are equivalent
if
they use the same primary file.
Warning: do not call Node.getHandle()
or DefaultHandle.createHandle(org.openide.nodes.Node)
in this method.
public static DataObject find(FileObject fo) throws DataObjectNotFoundException
fo
- file objectDataObjectNotFoundException
- if the file does not have a
data objectpublic static DataObject.Registry getRegistry()
public String getName()
The default implementation uses the name of the primary file.
public final DataFolder getFolder()
null
if the primary file
is the root
of its filesystempublic final DataObject copy(DataFolder f) throws IOException
An event is fired, and atomicity is implemented.
f
- the folder to copy the object toIOException
- if something went wrongprotected abstract DataObject handleCopy(DataFolder f) throws IOException
f
- target folderIOException
- if an error occuresprotected DataObject handleCopyRename(DataFolder f, String name, String ext) throws IOException
f
- target foldername
- new file nameext
- new file extensionIOException
- if an error occures or the file cannot be copied/renamedpublic final void delete() throws IOException
Events are fired and atomicity is implemented.
IOException
- if an error occuresprotected abstract void handleDelete() throws IOException
IOException
- if an error occurespublic final void rename(String name) throws IOException
Events are fired and atomicity is implemented.
name
- the new nameIOException
- if an error occursprotected abstract FileObject handleRename(String name) throws IOException
name
- name to rename the object toIOException
- if an error occurespublic final void move(DataFolder df) throws IOException
An event is fired and atomicity is implemented.
df
- folder to move object toIOException
- if an error occursprotected abstract FileObject handleMove(DataFolder df) throws IOException
df
- target data folderIOException
- if an error occuresprotected DataShadow handleCreateShadow(DataFolder f) throws IOException
The default implementation creates a reference data shadow and pastes it into the specified folder.
f
- the folder to create a shortcut inIOException
public final DataShadow createShadow(DataFolder f) throws IOException
An event is fired and atomicity is implemented.
f
- the folder to create shortcut inIOException
public final DataObject createFromTemplate(DataFolder f) throws IOException
f
- folder to create object inIOException
- if an error occuredDataObject.createFromTemplate(DataFolder,String)
public final DataObject createFromTemplate(DataFolder f, String name) throws IOException
DataObject.handleCreateFromTemplate(org.openide.loaders.DataFolder, java.lang.String)
.f
- folder to create object inname
- name of object that should be created, or null
if the
name should be same as that of the template (or otherwise mechanically generated)IOException
- if an error occuredpublic final DataObject createFromTemplate(DataFolder f, String name, Map<String,? extends Object> parameters) throws IOException
DataObject
. One can
not only specify its name, but also pass a map of parameters that
can influence the copying of the stream.f
- folder to create object inname
- name of object that should be created, or null
if the
name should be same as that of the template (or otherwise mechanically generated)parameters
- map of named objects that are going to be used when
creating the new objectIOException
- if an error occuredprotected abstract DataObject handleCreateFromTemplate(DataFolder df, String name) throws IOException
df
- data folder to create object inname
- name to give to the new object (or null
if the name should be chosen according to the template)IOException
- if an error occuredpublic void addPropertyChangeListener(PropertyChangeListener l)
l
- the listener to addpublic void removePropertyChangeListener(PropertyChangeListener l)
l
- the listener to removeprotected final void firePropertyChange(String name, Object oldValue, Object newValue)
DataObject.addPropertyChangeListener(java.beans.PropertyChangeListener)
.name
- of propertyoldValue
- old valuenewValue
- new valuepublic void addVetoableChangeListener(VetoableChangeListener l)
l
- the listener to addDataObject.PROP_VALID
public void removeVetoableChangeListener(VetoableChangeListener l)
l
- the listener to removeDataObject.PROP_VALID
protected final void fireVetoableChange(String name, Object oldValue, Object newValue) throws PropertyVetoException
name
- of propertyoldValue
- old valuenewValue
- new valuePropertyVetoException
- if the change has been vetoedpublic <T extends Node.Cookie> T getCookie(Class<T> c)
The default implementation tests if this object is of the requested class and if so, returns it.
Warning: the DataObject.getCookie(java.lang.Class<T>)
method and DataObject.getLookup()
method are ment to be interchangable - e.g. if you override one of them
be sure to override also the other and try as much as possible to
keep the same content in each of them. The default implementation tries
to do that as much as possible.
c
- class of requested cookienull
if such cookies are not supportedpublic Lookup getLookup()
DataObject.getCookie(java.lang.Class<T>)
and should preferably
be used instead of the old method. The default implementation
inside a data object
returns the getNodeDelegate().getLookup()
- which is
the most compatible behaviour with previous versions. However
this code has significant potential to deadlocks. That is why the
preferred advice is to override the method to:
class MyDataObject extends MultiDataObject
{
public @Override Lookup getLookup() {
return getCookieSet().getLookup();
}
}
Warning: the DataObject.getCookie(java.lang.Class<T>)
method and DataObject.getLookup()
method are ment to be interchangable - e.g. if you override one of them
be sure to override also the other and try as much as possible to
keep the same content in each of them. The default implementation tries
to do that as much as possible.
getLookup
in interface Lookup.Provider
protected <T extends Node.Cookie> T getCookie(DataShadow shadow, Class<T> clazz)
Subclasses can override this method with better logic, but the default
implementation just delegates to getCookie (Class)
.
clazz
- class to search forshadow
- the shadow for which is askingnull
public Object writeReplace()