use of org.openide.filesystems.FileObject in project Universal-G-Code-Sender by winder.
the class ActionRegistrationService method registerAction.
/**
* Registers an action with the platform along with optional shortcuts and
* menu items.
*
* @param id The unique id of the action
* @param name Display name of the action.
* @param category Category in the Keymap tool.
* @param shortcut Default shortcut, use an empty string or null for none.
* @param menuPath Menu location starting with "Menu", like "Menu/Head/Hats"
* @param menuPosition Defines how the menu item should be ordered
* @param localMenu Localized menu location starting with "Menu", like "Menu/Cabeza/Sombreros"
* @param action an action object to attach to the action entry.
* @throws IOException if the action couldn't be registered
*/
public void registerAction(String id, String name, String category, String shortcut, String menuPath, int menuPosition, String localMenu, Action action) throws IOException {
// /////////////////////
// Add/Update Action //
// /////////////////////
String originalFile = "Actions/" + category + "/" + id + ".instance";
FileObject root = FileUtil.getConfigRoot();
FileObject in = FileUtil.createFolder(root, "Actions/" + category);
in.refresh();
FileObject obj = in.getFileObject(id, "instance");
if (obj == null) {
obj = in.createData(id, "instance");
}
action.putValue(Action.NAME, name);
obj.setAttribute("instanceCreate", action);
obj.setAttribute("instanceClass", action.getClass().getName());
// ///////////////////
if (StringUtils.isNotEmpty(menuPath) && StringUtils.isNotEmpty(id)) {
in = createAndLocalizeFullMenu(menuPath, localMenu);
obj = in.getFileObject(id, SHADOW);
// Create if missing.
if (obj == null) {
obj = in.createData(id, SHADOW);
obj.setAttribute("originalFile", originalFile);
}
obj.setAttribute("position", menuPosition);
}
// ///////////////////////
if (shortcut != null && shortcut.length() > 0) {
in = FileUtil.createFolder(root, "Shortcuts");
obj = in.getFileObject(shortcut, SHADOW);
if (obj == null) {
obj = in.createData(shortcut, SHADOW);
obj.setAttribute("originalFile", originalFile);
}
}
invalidateCache();
}
use of org.openide.filesystems.FileObject in project Universal-G-Code-Sender by winder.
the class ActionRegistrationService method createAndLocalizeFullMenu.
/**
* Creates a folder path in the netbeans filesystem and sets a localized.
* display name or each level of the path.
*/
public FileObject createAndLocalizeFullMenu(String path, String localizedPath) throws IOException {
FileObject root = FileUtil.getConfigRoot();
String[] paths = path.split("/");
String[] names = localizedPath.split("/");
if (paths.length != names.length) {
throw new IllegalArgumentException("Path length must equal localized path length: " + path + ", " + localizedPath);
}
if (!paths[0].equals(names[0])) {
throw new IllegalArgumentException("Path and localized path must be in the same top level directory. Found: " + paths[0] + " and " + names[0]);
}
String fullPath = paths[0];
FileObject in = FileUtil.createFolder(root, fullPath);
for (int i = 1; i < paths.length; i++) {
fullPath = fullPath + "/" + paths[i];
in = FileUtil.createFolder(root, fullPath);
in.setAttribute("displayName", names[i]);
in.refresh();
}
invalidateCache();
return in;
}
use of org.openide.filesystems.FileObject in project Universal-G-Code-Sender by winder.
the class ActionRegistrationService method recursiveAddCategoryActions.
/**
* A recursive method that walks through all children of a file object and tries to find all actions.
*
* @param category the file object for a category or directory
* @param categoryActions the result map with all categories and their actions to add all children to
* @return a map with all categories and their lists of actions
*/
private Map<String, List<ActionReference>> recursiveAddCategoryActions(final FileObject category, Map<String, List<ActionReference>> categoryActions) {
List<ActionReference> actionList = categoryActions.getOrDefault(category.getPath(), new ArrayList<>());
FileObject[] children = category.getChildren();
for (FileObject child : children) {
if (child.getPath().endsWith(".instance")) {
getActionFromFileObject(child).ifPresent(actionList::add);
}
recursiveAddCategoryActions(child, categoryActions);
}
if (!actionList.isEmpty()) {
actionList.sort(Comparator.comparing(action -> {
String name = action.getClass().getSimpleName();
if (action.getAction().getValue(Action.NAME) != null) {
name = action.getAction().getValue(Action.NAME).toString();
}
return name.replaceAll("&", "");
}));
categoryActions.put(category.getPath(), actionList);
}
return categoryActions;
}
use of org.openide.filesystems.FileObject in project Universal-G-Code-Sender by winder.
the class MacroService method reInitActions.
public void reInitActions() {
String menuPath = LocalizingService.MENU_MACROS;
String actionCategory = "Macro";
String localized = String.format("Menu/%s/%s", Localization.getString("platform.menu.machine"), Localization.getString("platform.menu.macros"));
try {
FileObject root = FileUtil.getConfigRoot();
// Clear out the menu items.
FileUtil.createFolder(root, menuPath).delete();
FileUtil.createFolder(root, menuPath);
String actionPath = "/Actions/" + actionCategory;
FileUtil.createFolder(root, actionPath).delete();
ActionRegistrationService ars = Lookup.getDefault().lookup(ActionRegistrationService.class);
BackendAPI backend = CentralLookup.getDefault().lookup(BackendAPI.class);
Settings settings = backend.getSettings();
List<Macro> macros = settings.getMacros();
macros.forEach(macro -> {
int index = macros.indexOf(macro);
try {
String text;
if (Strings.isNullOrEmpty(macro.getNameAndDescription())) {
text = Integer.toString(index + 1);
} else {
text = macro.getNameAndDescription();
}
ars.registerAction(MacroAction.class.getCanonicalName() + "." + macro.getName(), text, actionCategory, null, menuPath, index, localized, new MacroAction(macro));
} catch (IOException e) {
logger.log(Level.WARNING, "Couldn't register macro action: \"" + macro.getName() + "\"", e);
}
});
} catch (Exception e) {
logger.log(Level.WARNING, "Couldn't register macro actions", e);
}
}
use of org.openide.filesystems.FileObject in project Universal-G-Code-Sender by winder.
the class InvalidGrblCommandErrorParserTest method setupParser.
@Before
public void setupParser() {
FileObject fileObject = mock(FileObject.class);
BackendAPI backendAPI = mock(BackendAPI.class);
IController grblController = mock(GrblController.class);
when(backendAPI.getController()).thenReturn(grblController);
when(backendAPI.isConnected()).thenReturn(true);
parser = new InvalidGrblCommandErrorParser(fileObject, backendAPI);
}
Aggregations