Java JTextComponent Insert insertSimpleTemplate(JTextComponent target, String template)

Here you can find the source of insertSimpleTemplate(JTextComponent target, String template)

Description

Expand the string template and replaces the selection with the expansion of the template.

License

Apache License

Parameter

Parameter Description
target a parameter
template a parameter

Declaration

public static void insertSimpleTemplate(JTextComponent target, String template) 

Method Source Code

//package com.java2s;
/*//  www.j a  va 2s.c o m
 * Copyright 2008 Ayman Al-Sairafi ayman.alsairafi@gmail.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License 
 *       at http://www.apache.org/licenses/LICENSE-2.0 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.  
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.text.JTextComponent;

public class Main {
    /**
     * The Pattern to use for PTags in insertSimpleTemplate
     */
    public static final Pattern PTAGS_PATTERN = Pattern.compile("\\#\\{p:([^}]*)\\}");
    public static final String TEMPLATE_SELECTION = "#{selection}";

    /**
     * Expand the string template and replaces the selection with the expansion
     * of the template.  The template String may contain any of the following
     * special tags.
     *
     * <li>{@code #{selection}} replaced with the selection, if any.  If there is
     * no selection, then the {@code #{selection}} tag will be removed.
     * <li>{@code #{p:AnyText}} will be replaced by {@code any text} and then
     * set the text selection to {@code AnyText}
     *
     * This methood does NOT perform any indentation and the template should
     * generally span one line only
     *
     * @param target
     * @param template
     */
    public static void insertSimpleTemplate(JTextComponent target, String template) {
        String selected = target.getSelectedText();
        selected = (selected == null) ? "" : selected;
        StringBuffer sb = new StringBuffer(template.length());
        Matcher pm = PTAGS_PATTERN.matcher(template.replace(TEMPLATE_SELECTION, selected));
        int selStart = -1, selEnd = -1;
        int lineStart = 0;
        while (pm.find()) {
            selStart = pm.start() + lineStart;
            pm.appendReplacement(sb, pm.group(1));
            selEnd = sb.length();
        }
        pm.appendTail(sb);
        // String expanded = template.replace(TEMPLATE_SELECTION, selected);

        if (selStart >= 0) {
            selStart += target.getSelectionStart();
            selEnd += target.getSelectionStart();
        }
        target.replaceSelection(sb.toString());
        if (selStart >= 0) {
            // target.setCaretPosition(selStart);
            target.select(selStart, selEnd);
        }
    }
}

Related

  1. insert(final JTextComponent component, final String string)
  2. insertLineAfter(JTextComponent textComponent)
  3. insertLineBefore(JTextComponent textComponent)
  4. insertMagicString(JTextComponent target, int dot, String toInsert)
  5. insertText(JTextComponent comp, String s)