Java Code Examples for javax.swing.text.EditorKit#getActions()

The following examples show how to use javax.swing.text.EditorKit#getActions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DiffContentPanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Action getAction(String key) {
    if (key == null) {
        return null;
    }

    // Try to find the action from kit.
    EditorKit kit = editorPane.getEditorKit();

    if (kit == null) { // kit is cleared in closeDocument()

        return null;
    }

    Action[] actions = kit.getActions();

    for (int i = 0; i < actions.length; i++) {
        if (key.equals(actions[i].getValue(Action.NAME))) {
            return actions[i];
        }
    }

    return null;
}
 
Example 2
Source File: EditorActionUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
DefaultSearchableKit(EditorKit kit) {
    for (Action action : kit.getActions()) {
        if (action != null) {
            name2actionRef.put((String)action.getValue(Action.NAME), new WeakReference<Action>(action));
        }
    }
}
 
Example 3
Source File: KeyBindingsUpdater.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void updateKits(Map<String,List<List<KeyStroke>>> actionName2binding, List<KitReference> kitRefs) {
    // Update kits without locks (a.putValue() is done)
    for (KitReference kitRef : kitRefs) {
        EditorKit kit = kitRef.get();
        if (kit == null) { // Might be null since this is a copy of orig. list
            continue;
        }
        Action[] actions = kit.getActions();
        for (int i = 0; i < actions.length; i++) {
            Action a = actions[i];
            String actionName = (String) a.getValue(Action.NAME);
            @SuppressWarnings("unchecked")
            List<List<KeyStroke>> origAccels = (List<List<KeyStroke>>) 
                    a.getValue(AbstractEditorAction.MULTI_ACCELERATOR_LIST_KEY);
            List<List<KeyStroke>> accels = actionName2binding.get(actionName);
            if (accels == null) {
                accels = Collections.emptyList();
            }
            if (origAccels == null || !origAccels.equals(accels)) {
                a.putValue(AbstractEditorAction.MULTI_ACCELERATOR_LIST_KEY, accels);
                if (accels.size() > 0) {
                    // First keystroke of first multi-key accelerator in the list
                    a.putValue(Action.ACCELERATOR_KEY, accels.get(0).get(0));
                }
            }
        }
    }
}
 
Example 4
Source File: EditorUtilitiesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAction() throws Exception {
    EditorKit editorKit = new DefaultEditorKit();
    String actionName = DefaultEditorKit.backwardAction;
    Action result = EditorUtilities.getAction(editorKit, actionName);
    for (Action expected : editorKit.getActions()) {
        if (actionName.equals(expected.getValue(Action.NAME))) {
            assertEquals(expected, result);
            return;
        }
    }
    fail("Action " + actionName + " not found.");
}
 
Example 5
Source File: EditorBridge.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Loads editor actions for given mimeType to editorActionsMap.
 */
private void initActionMap(String mimeType, Map<String, EditorAction> emptyMimePathActions) {

    // 1) get EditorKit
    EditorKit editorKit = null;
    if (mimeType == null) {
        editorKit = BaseKit.getKit(NbEditorKit.class);
    } else {
        Lookup mimeLookup = MimeLookup.getLookup(MimePath.parse(mimeType));
        editorKit = mimeLookup.lookup(EditorKit.class);
    }
    if (editorKit == null) {
        if (LOG.isLoggable(Level.WARNING)) {
            LOG.fine("EditorKit not found for: " + mimeType); //NOI18N
        }
        return;
    }

    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Loading actions for '" + (mimeType == null ? "" : mimeType) + "' using " + editorKit); //NOI18N
    }

    // 2) copy actions from EditorKit to actionMap
    Action[] as = editorKit.getActions();
    for (int i = 0; i < as.length; i++) {
        Object isHidden = as[i].getValue(BaseAction.NO_KEYBINDING);
        if (isHidden instanceof Boolean && ((Boolean) isHidden).booleanValue()) {
            if (LOG.isLoggable(Level.FINE)) {
                LOG.fine("! Action '" + as[i].getValue(Action.NAME) + "' is hidden, ignoring"); //NOI18N
            }
            continue; // ignore hidden actions
        }
        
        EditorAction action = new EditorAction(as [i]);
        String id = action.getId();

        // filter out actions inherited from an empty mime path (all editors actions)
        if (emptyMimePathActions != null && emptyMimePathActions.containsKey(id)) {
            if (LOG.isLoggable(Level.FINEST)) {
                LOG.finest("Action '" + id + "' was already listed among all alnguages actions, skipping"); //NOI18N
            }
            continue;
        }
        
        getEditorActionsMap().put(id, action);
        if (LOG.isLoggable(Level.FINE)) {
            LOG.fine("Action '" + id + "' loaded for '" + (mimeType == null ? "" : mimeType) + "'"); //NOI18N
        }

        Set<String> s = actionNameToMimeTypes.get(id);
        if (s == null) {
            // LinkedHS ensures that 'null' mime which is initialized 1st will be
            // the 1st enumerated from getMimeTypes(). This invariant is used in saveKeyMap
            s = new LinkedHashSet<String>();
            actionNameToMimeTypes.put(id, s);
        }
        s.add(mimeType);
    }

    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Actions for '" + (mimeType == null ? "" : mimeType) + "' loaded successfully"); //NOI18N
    }
}