Java JTextComponent Insert insertMagicString(JTextComponent target, int dot, String toInsert)

Here you can find the source of insertMagicString(JTextComponent target, int dot, String toInsert)

Description

Insert the given String into the textcomponent.

License

Apache License

Parameter

Parameter Description
target a parameter
dot a parameter
toInsert a parameter

Exception

Parameter Description

Declaration

public static void insertMagicString(JTextComponent target, int dot,
        String toInsert) throws BadLocationException 

Method Source Code

//package com.java2s;
/*/*from w w w.  ja  va2 s .  c  om*/
 * 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 javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

public class Main {
    /**
     * Insert the given String into the textcomponent.  If the string contains
     * the | vertical BAr char, then it will not be inserted, and the cursor will
     * be set to its location.
     * If there are TWO vertical bars, then the text between them will be selected
     * <b>FIXME: add following feature
     * If the String is multi-line, then it will be indented with the same
     * indentattion as the line with pos.</b>
     * @param target
     * @param dot
     * @param toInsert
     * @throws javax.swing.text.BadLocationException
     */
    public static void insertMagicString(JTextComponent target, int dot,
            String toInsert) throws BadLocationException {
        Document doc = target.getDocument();
        if (toInsert.indexOf('|') >= 0) {
            int ofst = toInsert.indexOf('|');
            int ofst2 = toInsert.indexOf('|', ofst + 1);
            toInsert = toInsert.replace("|", "");
            doc.insertString(dot, toInsert, null);
            dot = target.getCaretPosition();
            final int strLength = toInsert.length();
            if (ofst2 > 0) {
                // note that we already removed the first |, so end offset is now
                // one less than what it was.
                target.select(dot + ofst - strLength, dot + ofst2
                        - strLength - 1);
            } else {
                target.setCaretPosition(dot + ofst - strLength);
            }
        } else {
            doc.insertString(dot, toInsert, null);
        }
    }
}

Related

  1. insert(final JTextComponent component, final String string)
  2. insertLineAfter(JTextComponent textComponent)
  3. insertLineBefore(JTextComponent textComponent)
  4. insertSimpleTemplate(JTextComponent target, String template)
  5. insertText(JTextComponent comp, String s)