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();
}
Aggregations