javax.swing.text.TextAction Java Examples

The following examples show how to use javax.swing.text.TextAction. 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: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  JTextArea textArea = new JTextArea(TEXT);
  textArea.getActionMap().put(DefaultEditorKit.selectWordAction, new TextAction(DefaultEditorKit.selectWordAction) {
    @Override public void actionPerformed(ActionEvent e) {
      JTextComponent target = getTextComponent(e);
      if (target != null) {
        try {
          int offs = target.getCaretPosition();
          int begOffs = TextUtils.getWordStart(target, offs);
          int endOffs = TextUtils.getWordEnd(target, offs);
          target.setCaretPosition(begOffs);
          target.moveCaretPosition(endOffs);
        } catch (BadLocationException ex) {
          UIManager.getLookAndFeel().provideErrorFeedback(target);
        }
      }
    }
  });
  JSplitPane split = new JSplitPane();
  split.setResizeWeight(.5);
  split.setLeftComponent(makeTitledPanel("Default", new JTextArea(TEXT)));
  split.setRightComponent(makeTitledPanel("Break words: _ and -", textArea));
  add(split);
  setPreferredSize(new Dimension(320, 240));
}
 
Example #2
Source File: GspKit.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected Action[] createActions() {
    Action[] superActions = super.createActions();
    
    return TextAction.augmentList(superActions, new Action[] {
        new GspDeleteCharAction(deletePrevCharAction, false),
        new GspDefaultKeyTypedAction(),
        CslActions.createSelectCodeElementAction(true),
        CslActions.createSelectCodeElementAction(false),
        CslActions.createCamelCasePositionAction(findAction(superActions, nextWordAction), true),
        CslActions.createCamelCasePositionAction(findAction(superActions, previousWordAction), false),
        CslActions.createSelectCamelCasePositionAction(findAction(superActions, selectionNextWordAction), true),
        CslActions.createSelectCamelCasePositionAction(findAction(superActions, selectionPreviousWordAction), false),
        CslActions.createDeleteToCamelCasePositionAction(findAction(superActions, removeNextWordAction), true),
        CslActions.createDeleteToCamelCasePositionAction(findAction(superActions, removePreviousWordAction), false),
        CslActions.createInstantRenameAction()
     });
}
 
Example #3
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Fetches the text component that currently has focus. It delegates to 
 * TextAction.getFocusedComponent().
 * @return the component
 */
public static JTextComponent getFocusedComponent() {
    /** Fake action for getting the focused component */
    class FocusedComponentAction extends TextAction {
        
        FocusedComponentAction() {
            super("focused-component"); // NOI18N
        }
        
        /** adding this method because of protected final getFocusedComponent */
        JTextComponent getFocusedComponent2() {
            return getFocusedComponent();
        }
        
        public @Override void actionPerformed(ActionEvent evt){}
    }
    
    if (focusedComponentAction == null) {
        focusedComponentAction = new FocusedComponentAction();
    }
    
    return ((FocusedComponentAction)focusedComponentAction).getFocusedComponent2();
}
 
Example #4
Source File: LanguagesEditorKit.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected @Override Action[] createActions() {
    Action[] myActions = new Action[] {
        new BraceCompletionInsertAction (),
        new BraceCompletionDeleteAction (),
        //new IndentAction (),
        new InstantRenameAction(),
        new LanguagesGenerateFoldPopupAction (),
        new org.netbeans.modules.languages.features.ToggleCommentAction(),
        new org.netbeans.modules.languages.features.CodeCommentAction(),
        new org.netbeans.modules.languages.features.CodeUncommentAction()
    };
    return TextAction.augmentList (
        super.createActions (), 
        myActions
    );
}
 
Example #5
Source File: KeymapViewModelTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static String getName (Object action) {
    if (action instanceof TextAction)
        return (String) ((TextAction) action).getValue (Action.SHORT_DESCRIPTION);
    if (action instanceof Action)
        return (String) ((Action) action).getValue (Action.NAME);
    return action.toString ();
}
 
Example #6
Source File: DefaultSyntaxKit.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Add keyboard actions to this control using the Configuration we have
 * @param map
 * @param prefix 
 */
public void addSyntaxActions(Keymap map, String prefix) {
    // look at all keys that either start with prefix.Action, or
    // that start with Action.

    Pattern splitter = CONFIG.getValueSeparator(prefix);
    Configuration actionsConf = CONFIG.subConfig(prefix, "Action.");

    for (String actionName : actionsConf.stringPropertyNames()) {
        String[] values = splitter.split(
                actionsConf.getProperty(actionName));
        String actionClass = values[0];
        SyntaxAction action = editorActions.get(actionClass);
        if (action == null) {
            action = createAction(actionClass);
            action.config(CONFIG, prefix, actionName);
        }
        String keyStrokeString = values[1];
        KeyStroke ks = KeyStroke.getKeyStroke(keyStrokeString);
        // KeyEvent.VK_QUOTEDBL
        if (ks == null) {
            throw new IllegalArgumentException("Invalid KeyStroke: " +
                    keyStrokeString);
        }
        TextAction ta = action.getAction(actionName);
        if(ta == null) {
            throw new IllegalArgumentException("Invalid ActionName: " +
                    actionName);
        }
        map.addActionForKeyStroke(ks, ta);
    }
}
 
Example #7
Source File: FindReplaceActions.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public TextAction getAction(String key) {
    if(key.equals("FIND") ) {
    return findDialogAction;
    } else if(key.equals("REPLACE")) {
        return findDialogAction;
    } else if(key.equals("FIND_NEXT")) {
        return findNextAction;
    } else {
        throw new IllegalArgumentException("Bad Action: " + key);
    }
}
 
Example #8
Source File: NbEditorKit.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected @Override Action[] createActions() {
        Action[] nbEditorActions = new Action[] {
                                       nbUndoActionDef,
                                       nbRedoActionDef,
                                       new GenerateFoldPopupAction(),
                                       new NavigationHistoryBackAction(),
                                       new NavigationHistoryForwardAction(),
//                                       new ToggleToolbarAction(),
//                                       new NbToggleLineNumbersAction(),
                                       new NbGenerateGoToPopupAction(),
                                   };
        return TextAction.augmentList(super.createActions(), nbEditorActions);
    }
 
Example #9
Source File: JavaKit.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected Action[] createActions() {
    Action[] javaActions = new Action[] {
                               new JavaDefaultKeyTypedAction(),
                               new PrefixMakerAction(makeGetterAction, "get", getSetIsPrefixes), // NOI18N
                               new PrefixMakerAction(makeSetterAction, "set", getSetIsPrefixes), // NOI18N
                               new PrefixMakerAction(makeIsAction, "is", getSetIsPrefixes), // NOI18N
                               new AbbrevDebugLineAction(),
                           };
    return TextAction.augmentList(super.createActions(), javaActions);
}
 
Example #10
Source File: HtmlKit.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected Action[] createActions() {
    Action[] HtmlActions = new Action[]{
        CslActions.createSelectCodeElementAction(true),
        CslActions.createSelectCodeElementAction(false),
        CslActions.createInstantRenameAction(),
        CslActions.createToggleBlockCommentAction(),
        new ExtKit.CommentAction(""), //NOI18N
        new ExtKit.UncommentAction(""), //NOI18N
        CslActions.createGoToMarkOccurrencesAction(false),
        CslActions.createGoToMarkOccurrencesAction(true),
        CslActions.createGoToDeclarationAction()
    };
    return TextAction.augmentList(super.createActions(), HtmlActions);
}
 
Example #11
Source File: XMLKit.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Provide XML related actions.
 */
protected @Override Action[] createActions() {
    Action[] actions = new Action[] {
        new XMLCommentAction(),
        new XMLUncommentAction(),
        new ToggleBlockCommentAction(new XmlCommentHandler()),
        new TestAction(),
    };
    return TextAction.augmentList(super.createActions(), actions);
}
 
Example #12
Source File: CslEditorKit.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected Action[] createActions() {
    Action[] superActions = super.createActions();
    Language language = LanguageRegistry.getInstance().getLanguageByMimeType(mimeType);
    ArrayList<Action> actions = new ArrayList<Action>(30);

    actions.add(new GsfDefaultKeyTypedAction());
    actions.add(new GsfInsertBreakAction());
    actions.add(new GsfDeleteCharAction(deletePrevCharAction, false));
    
    // The php needs to handle special cases of toggle comment. There has to be 
    // registered ToggleBlockCommentAction in PHP that handles these special cases,
    // but the current way, how the actions are registered, doesn't allow to overwrite the action
    // registered here.
    // See issue #204616. This hack can be removed, when issue #204616 will be done.
    if (!mimeType.equals("text/x-php5")) {
        actions.add(new ToggleBlockCommentAction());
    }
    actions.add(new GenerateFoldPopupAction());
    actions.add(new InstantRenameAction());
    actions.add(CslActions.createGoToDeclarationAction());
    actions.add(new GenericGenerateGoToPopupAction());
    actions.add(new SelectCodeElementAction(SelectCodeElementAction.selectNextElementAction, true));
    actions.add(new SelectCodeElementAction(SelectCodeElementAction.selectPreviousElementAction, false));

    if (language == null) {
        LOG.log(Level.WARNING, "Language missing for MIME type {0}", mimeType);
    } else if (language.hasOccurrencesFinder()) {
        actions.add(new GoToMarkOccurrencesAction(false));
        actions.add(new GoToMarkOccurrencesAction(true));
    }

    return TextAction.augmentList(superActions,
        actions.toArray(new Action[actions.size()]));
}
 
Example #13
Source File: ActionsSearchProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static ActionEvent createActionEvent (Action action) {
    Object evSource = null;
    int evId = ActionEvent.ACTION_PERFORMED;

    // text (editor) actions
    if (action instanceof TextAction) {
        EditorCookie ec = Utilities.actionsGlobalContext().lookup(EditorCookie.class);
        if (ec == null) {
            return null;
        }

        JEditorPane[] editorPanes = ec.getOpenedPanes();
        if (editorPanes == null || editorPanes.length <= 0) {
            return null;
        }
        evSource = editorPanes[0];
    }

    if (evSource == null) {
        evSource = TopComponent.getRegistry().getActivated();
    }
    if (evSource == null) {
        evSource = WindowManager.getDefault().getMainWindow();
    }

    
    return new ActionEvent(evSource, evId, null);
}
 
Example #14
Source File: BaseCaretTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected Action[] getDeclaredActions() {
    Action swa = new TextAction("") {
        @Override
        public void actionPerformed(ActionEvent evt) {
            selectWordCalled = true;
        }
    };
    swa.putValue(Action.NAME, BaseKit.selectWordAction);
    
    Action[] actions = new Action[] {
        swa
    };
    return TextAction.augmentList(super.createActions(), actions);
}
 
Example #15
Source File: ExtKit.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected @Override Action[] createActions() {
        ArrayList<Action> actions = new ArrayList<Action>();

        actions.add(new ExtDefaultKeyTypedAction());
// XXX: remove
//        if (!ExtCaret.NO_HIGHLIGHT_BRACE_LAYER) {
//            actions.add(new MatchBraceAction(matchBraceAction, false));
//            actions.add(new MatchBraceAction(selectionMatchBraceAction, true));
//        }
        actions.add(new CommentAction()); // to make ctrl-shift-T in Netbeans55 profile work
        actions.add(new UncommentAction()); // to make ctrl-shift-D in Netbeans55 profile work
                
        return TextAction.augmentList(super.createActions(), actions.toArray(new Action[actions.size()]));
    }
 
Example #16
Source File: PropertiesKit.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected Action[] createActions() {
    Action[]  actions = new Action[] {
        new ToggleCommentAction("#"), //NOI18N
    };
    return TextAction.augmentList(super.createActions(), actions);
}
 
Example #17
Source File: FindReplaceActions.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
public TextAction getFindNextAction() {
    return findNextAction;
}
 
Example #18
Source File: HTMLKit.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected Action[] createActions() {
    Action[] HTMLActions = new Action[] {
                               new HTMLShiftBreakAction()
                           };
    return TextAction.augmentList(super.createActions(), HTMLActions);
}
 
Example #19
Source File: ViewMenu.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
private JCheckBoxMenuItem createAndAddOption(TextAction action, JMenu menu) {
    JCheckBoxMenuItem option = new JCheckBoxMenuItem(action);
    option.setSelected(false);
    menu.add(option);
    return option;
}
 
Example #20
Source File: LatteEditorKit.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
protected Action[] createActions() {
    return TextAction.augmentList(super.createActions(), new Action[] {
        new ToggleBlockCommentAction(),
        CslActions.createInstantRenameAction()});
}
 
Example #21
Source File: PairAction.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
public TextAction getAction(String key) {
    return this;
}
 
Example #22
Source File: MapCompletionAction.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
public TextAction getAction(String key) {
    return this;
}
 
Example #23
Source File: GotoLineAction.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
public TextAction getAction(String key) {
    return this;
}
 
Example #24
Source File: UndoAction.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
public TextAction getAction(String key) {
    return this;
}
 
Example #25
Source File: IndentAction.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
public TextAction getAction(String key) {
    return this;
}
 
Example #26
Source File: TwigEditorKit.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
protected Action[] createActions() {
    return TextAction.augmentList(super.createActions(), new Action[] {
        new ToggleBlockCommentAction(),
        CslActions.createInstantRenameAction()});
}
 
Example #27
Source File: JUnindentAction.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
public TextAction getAction(String key) {
    return this;
}
 
Example #28
Source File: FindReplaceActions.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
public TextAction getFindDialogAction() {
    return findDialogAction;
}
 
Example #29
Source File: RedoAction.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
public TextAction getAction(String key) {
    return this;
}
 
Example #30
Source File: UnindentAction.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
public TextAction getAction(String key) {
    return this;
}