Search in sources :

Example 81 with FileObject

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();
}
Also used : FileObject(org.openide.filesystems.FileObject)

Example 82 with FileObject

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;
}
Also used : FileObject(org.openide.filesystems.FileObject)

Example 83 with FileObject

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;
}
Also used : Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) InstanceCookie(org.openide.cookies.InstanceCookie) Logger(java.util.logging.Logger) StringUtils(org.apache.commons.lang3.StringUtils) FileUtil(org.openide.filesystems.FileUtil) FileObject(org.openide.filesystems.FileObject) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DataObject(org.openide.loaders.DataObject) List(java.util.List) Exceptions(org.openide.util.Exceptions) Map(java.util.Map) ServiceProvider(org.openide.util.lookup.ServiceProvider) Optional(java.util.Optional) Comparator(java.util.Comparator) Collections(java.util.Collections) javax.swing(javax.swing) FileObject(org.openide.filesystems.FileObject)

Example 84 with FileObject

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);
    }
}
Also used : BackendAPI(com.willwinder.universalgcodesender.model.BackendAPI) Macro(com.willwinder.universalgcodesender.types.Macro) ActionRegistrationService(com.willwinder.ugs.nbp.lib.services.ActionRegistrationService) FileObject(org.openide.filesystems.FileObject) IOException(java.io.IOException) Settings(com.willwinder.universalgcodesender.utils.Settings) IOException(java.io.IOException)

Example 85 with FileObject

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);
}
Also used : IController(com.willwinder.universalgcodesender.IController) BackendAPI(com.willwinder.universalgcodesender.model.BackendAPI) FileObject(org.openide.filesystems.FileObject) Before(org.junit.Before)

Aggregations

FileObject (org.openide.filesystems.FileObject)86 File (java.io.File)28 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)11 DataObject (org.openide.loaders.DataObject)8 NotifyDescriptor (org.openide.NotifyDescriptor)7 Project (org.netbeans.api.project.Project)6 ActionEvent (java.awt.event.ActionEvent)5 PropertyChangeEvent (java.beans.PropertyChangeEvent)5 PropertyChangeListener (java.beans.PropertyChangeListener)5 InputStream (java.io.InputStream)5 JFileChooser (javax.swing.JFileChooser)5 JPanel (javax.swing.JPanel)5 ImportControllerUI (org.gephi.desktop.importer.api.ImportControllerUI)5 DialogFileFilter (org.gephi.ui.utils.DialogFileFilter)5 DialogDescriptor (org.openide.DialogDescriptor)5 ActionListener (java.awt.event.ActionListener)4 DocumentBuilder (javax.xml.parsers.DocumentBuilder)4 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)4 OpenCookie (org.openide.cookies.OpenCookie)4