All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fife.ui.rsyntaxtextarea.templates.StaticCodeTemplate Maven / Gradle / Ivy

The newest version!
/*
 * 02/21/2005
 *
 * CodeTemplate.java - A "template" (macro) for commonly-typed code.
 * 
 * This library is distributed under a modified BSD license.  See the included
 * RSyntaxTextArea.License.txt file for details.
 */
package org.fife.ui.rsyntaxtextarea.templates;

import java.io.IOException;
import java.io.ObjectInputStream;
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import javax.swing.text.Element;

import org.fife.ui.rsyntaxtextarea.RSyntaxDocument;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.RSyntaxUtilities;


/**
 * A code template that inserts static text before and after the caret.

* * For example, you can associate the identifier forb * (short for "for-block") with the following code:

* *

 *   for (<caret>) {
 *
 *   }
 * 
* * Then, whenever you type forb followed by a trigger * (e.g., a space) into a text area with this CodeTemplate, * the code snippet is added in place of forb. Further, * the caret is placed at the position denoted by <caret>. * * @author Robert Futrell * @version 0.1 * @see CodeTemplate */ public class StaticCodeTemplate extends AbstractCodeTemplate { private static final long serialVersionUID = 1; /** * The code inserted before the caret position. */ private String beforeCaret; /** * The code inserted after the caret position. */ private String afterCaret; /** * Cached value representing whether beforeCaret contains * one or more newlines. */ private transient int firstBeforeNewline; /** * Cached value representing whether afterCaret contains * one or more newlines. */ private transient int firstAfterNewline; private static final String EMPTY_STRING = ""; /** * Constructor. This constructor only exists to support persistance * through serialization. */ public StaticCodeTemplate() { } /** * Constructor. * * @param id The ID of this code template. * @param beforeCaret The text to place before the caret. * @param afterCaret The text to place after the caret. */ public StaticCodeTemplate(String id, String beforeCaret, String afterCaret){ super(id); setBeforeCaretText(beforeCaret); setAfterCaretText(afterCaret); } /** * Returns the text that will be placed after the caret. * * @return The text. * @see #setAfterCaretText */ public String getAfterCaretText() { return afterCaret; } /** * Returns the text that will be placed before the caret. * * @return The text. * @see #setBeforeCaretText */ public String getBeforeCaretText() { return beforeCaret; } /** * Returns the "after caret" text, with each new line indented by * the specified amount. * * @param indent The amount to indent. * @return The "after caret" text. */ private String getAfterTextIndented(String indent) { return getTextIndented(getAfterCaretText(), firstAfterNewline, indent); } /** * Returns the "before caret" text, with each new line indented by * the specified amount. * * @param indent The amount to indent. * @return The "before caret" text. */ private String getBeforeTextIndented(String indent) { return getTextIndented(getBeforeCaretText(),firstBeforeNewline,indent); } /** * Returns text with newlines indented by the specified amount. * * @param text The original text. * @param firstNewline The index of the first '\n' character. * @param indent The amount to indent. * @return The indented text. */ private String getTextIndented(String text,int firstNewline,String indent) { if (firstNewline==-1) { return text; } int pos = 0; int old = firstNewline+1; StringBuilder sb = new StringBuilder(text.substring(0, old)); sb.append(indent); while ((pos=text.indexOf('\n', old))>-1) { sb.append(text.substring(old, pos+1)); sb.append(indent); old = pos+1; } if (old




© 2015 - 2024 Weber Informatics LLC | Privacy Policy