org.openide.awt 7.55.1

org.netbeans.spi.actions
Class AbstractSavable

java.lang.Object
  extended by org.netbeans.spi.actions.AbstractSavable
All Implemented Interfaces:
Savable

public abstract class AbstractSavable
extends Object
implements Savable

Default implementation of Savable interface and additional contracts, including dealing with Savable.REGISTRY. The human presentable name of the object to be saved is provided by implementing findDisplayName(). In case this object wants to be visually represented with an icon, it can also implement Icon interface (and delegate to ImageUtilities.loadImageIcon(java.lang.String, boolean) result). Here is example of typical implementation:

class MySavable extends AbstractSavable {
    private final Object obj;
    public MySavable(Object obj) {
        this.obj = obj;
        register();
    }
    protected String findDisplayName() {
        return "My name is " + obj.toString(); // get display name somehow
    }
    protected void handleSave() throws IOException {
        // save 'obj' somehow
    }
    public boolean equals(Object other) {
        if (other instanceof MySavable) {
            return ((MySavable)other).obj.equals(obj);
        }
        return false;
    }
    public int hashCode() {
        return obj.hashCode();
    }
}
 

Since:
7.33

Field Summary
 
Fields inherited from interface org.netbeans.api.actions.Savable
REGISTRY
 
Constructor Summary
protected AbstractSavable()
          Constructor for subclasses.
 
Method Summary
abstract  boolean equals(Object obj)
          Equals and hashCode() need to be properly implemented by subclasses to correctly implement equality contract.
protected abstract  String findDisplayName()
          Finds suitable display name for the object this Savable represents.
protected abstract  void handleSave()
          To be overriden by subclasses to handle the actual save of the object.
abstract  int hashCode()
          HashCode and equals(java.lang.Object) need to be properly implemented by subclasses, so two Savables representing the same object beneath are really equal and have the same hashCode().
protected  void register()
          Tells the system to register this Savable into Savable.REGISTRY.
 void save()
          Implementation of Savable.save() contract.
 String toString()
          Human descriptive, localized name of the savable.
protected  void unregister()
          Removes this Savable from the Savable.REGISTRY (if it is present there, by relying on equals(java.lang.Object) and hashCode()).
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractSavable

protected AbstractSavable()
Constructor for subclasses.

Method Detail

save

public final void save()
                throws IOException
Implementation of Savable.save() contract. Calls handleSave() and unregister().

Specified by:
save in interface Savable
Throws:
IOException - if call to handleSave() throws IOException

findDisplayName

protected abstract String findDisplayName()
Finds suitable display name for the object this Savable represents.

Returns:
human readable, localized short string name

handleSave

protected abstract void handleSave()
                            throws IOException
To be overriden by subclasses to handle the actual save of the object.

Throws:
IOException

equals

public abstract boolean equals(Object obj)
Equals and hashCode() need to be properly implemented by subclasses to correctly implement equality contract. Two Savables should be equal if they represent the same underlying object beneath them. Without correct implementation proper behavior of register() and unregister() cannot be guaranteed.

Overrides:
equals in class Object
Parameters:
obj - object to compare this one to,
Returns:
true or false

hashCode

public abstract int hashCode()
HashCode and equals(java.lang.Object) need to be properly implemented by subclasses, so two Savables representing the same object beneath are really equal and have the same hashCode().

Overrides:
hashCode in class Object
Returns:
integer hash
See Also:
equals(java.lang.Object)

register

protected final void register()
Tells the system to register this Savable into Savable.REGISTRY. Only one Savable (according to equals(java.lang.Object) and hashCode()) can be in the registry. New call to register() replaces any previously registered and equal Savables. After this call the Savable.REGISTRY holds a strong reference to this which prevents this object to be garbage collected until it is unregistered or replaced by equal one.


unregister

protected final void unregister()
Removes this Savable from the Savable.REGISTRY (if it is present there, by relying on equals(java.lang.Object) and hashCode()).


toString

public final String toString()
Description copied from interface: Savable
Human descriptive, localized name of the savable. It is advised that all implementations of Savable override the toString method to provide human readable name.

Specified by:
toString in interface Savable
Overrides:
toString in class Object
Returns:
human readable name representing the savable

org.openide.awt 7.55.1

Built on June 6 2013.  |  Portions Copyright 1997-2013 Oracle. All rights reserved.