Code example for JTextComponent

Methods: getDocumentgetSelectionEndgetSelectionStartsetCaretPosition

0
	 * Completes the macro in the document 
	 * @param jtc 
	 * @param dotOffset 
	 */ 
	public void process(JTextComponent jtc, int dotOffset) {
		StyledDocument doc = (StyledDocument) jtc.getDocument();
		try { 
			doc.insertString(dotOffset, getMacro(), null);
			if(isPair) {
				// FIXME get rid of this 
				// used when text selected (useless since completion appears only after { char) 
				doc.insertString(jtc.getSelectionEnd(), getEndMacro(), null);
			} else { 
				doc.remove(jtc.getSelectionStart(), jtc.getSelectionEnd() - jtc.getSelectionStart());
			} 
			jtc.setCaretPosition(dotOffset + macro.length() + syntax.opening().length() + syntax.closing().length());		// moves caret after (start)
		} catch(Exception ex) {
			Logger.getLogger(LatteCommentMacro.class.getName()).log(Level.SEVERE, null, ex);
		} 
	} 
 
Experience pair programming with AI  Get Codota for Java