Search in sources :

Example 1 with InstanceCookie

use of org.openide.cookies.InstanceCookie in project Universal-G-Code-Sender by winder.

the class ActionRegistrationService method getActionFromFileObject.

/**
 * Tries to get an action from a file object. If the file object doesn't have an action instance attached to it
 * or if the action class couldn't be loaded this will return an empty optional.
 *
 * @param actionFileObject the file object to try and retrieve the action instance from.
 * @return an optional with an action if found or else an empty optional.
 */
private Optional<ActionReference> getActionFromFileObject(FileObject actionFileObject) {
    try {
        DataObject dob = DataObject.find(actionFileObject);
        InstanceCookie cookie = dob.getLookup().lookup(InstanceCookie.class);
        if (cookie != null) {
            Object o = cookie.instanceCreate();
            if (o instanceof Action) {
                ActionReference actionReference = new ActionReference();
                actionReference.setAction((Action) o);
                actionReference.setId(actionFileObject.getPath());
                return Optional.of(actionReference);
            }
        }
    } catch (IOException | ClassNotFoundException e) {
        LOGGER.warning(String.format("Could not load class for action %s", actionFileObject.getPath()));
    }
    return Optional.empty();
}
Also used : DataObject(org.openide.loaders.DataObject) InstanceCookie(org.openide.cookies.InstanceCookie) FileObject(org.openide.filesystems.FileObject) DataObject(org.openide.loaders.DataObject) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 InstanceCookie (org.openide.cookies.InstanceCookie)1 FileObject (org.openide.filesystems.FileObject)1 DataObject (org.openide.loaders.DataObject)1