/** * Copies the currently selected text to the system clipboard, with * any necessary style information (font, foreground color and background * color). Does nothing for {@code null} or empty selections. * * @see #copyAsStyledText(Theme) */ public void copyAsStyledText() { int selStart = getSelectionStart(); int selEnd = getSelectionEnd(); if (selStart==selEnd) { return; } // Get the selection as HTML String html = HtmlUtil.getTextAsHtml(this, selStart, selEnd); // Get the selection as RTF byte[] rtfBytes = getTextAsRtf(selStart, selEnd); // Set the system clipboard contents to the RTF selection. StyledTextTransferable contents = new StyledTextTransferable(html, rtfBytes); Clipboard cb = getToolkit().getSystemClipboard(); try { cb.setContents(contents, null); } catch (IllegalStateException ise) { UIManager.getLookAndFeel().provideErrorFeedback(null); } }
@Override public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { if (!textArea.isEditable() || !textArea.isEnabled()) { UIManager.getLookAndFeel().provideErrorFeedback(textArea); return; } RSyntaxTextArea sta = (RSyntaxTextArea)textArea; boolean noSelection= sta.getSelectionStart()==sta.getSelectionEnd(); // First, see if this language wants to handle inserting newlines // itself. boolean handled = false; if (noSelection) { RSyntaxDocument doc = (RSyntaxDocument)sta.getDocument(); handled = doc.insertBreakSpecialHandling(e); } // If not... if (!handled) { handleInsertBreak(sta, noSelection); } }
/** * Asserts that the text area's selection is at the expected location and * has the expected value. * * @param expected * @param offs * @param matchCase */ private void assertSelected(String expected, int offs, boolean matchCase) { String actual = textArea.getSelectedText(); if (matchCase) { assertEquals(expected, actual); } else { assertEqualsIgnoreCase(expected, actual); } int selOffs = textArea.getSelectionStart(); assertEquals("unexpected selection offset: ", offs, selOffs); }
int selStart = host.getSelectionStart(); int selEnd = host.getSelectionEnd();
int selStart = host.getSelectionStart(); int selEnd = host.getSelectionEnd();
int dot = textArea.getSelectionStart(); // Forgive variable name Element root = textArea.getDocument().getDefaultRootElement(); int dotLine = root.getElementIndex(dot);
/** * Return true iff the selection is already block commented. * @param scriptPanel Script panel * @return True iff the selection is already block commented */ private static boolean alreadyBlockCommented(RSyntaxTextArea scriptPanel) { try { return scriptPanel.getText(scriptPanel.getSelectionStart(), BLOCK_COMMENT_START.length()).equals(BLOCK_COMMENT_START) && scriptPanel.getText(scriptPanel.getSelectionEnd() - BLOCK_COMMENT_END.length(), BLOCK_COMMENT_END.length()).equals(BLOCK_COMMENT_END); } catch (BadLocationException e) { LOGGER.warn(I18N.tr("Assuming the selection is not already block commented."), e); } return false; }
private void resetaSelecaoDoTexto() { if (temTextSelecionado()) { textArea.setSelectionEnd(textArea.getSelectionStart()); // limpa a seleção } }
public String getCurrentSQLStatement() { String sql = scriptPanel.getSelectedText(); lastSQLStatementToReformatEnd = scriptPanel.getSelectionEnd(); lastSQLStatementToReformatStart = scriptPanel.getSelectionStart(); if (sql == null || sql.trim().length() == 0) { sql = getText(); lastSQLStatementToReformatEnd = -2; // int[] bounds = getBoundsOfCurrentSQLStatement(); // // if (bounds[0] >= bounds[1]) { // sql = ""; // } else { // sql = sql.substring(bounds[0], bounds[1]).trim(); // } } return sql != null ? sql : ""; }
public int[] getBoundsOfCurrentSQLStatement() { int[] bounds = new int[2]; bounds[0] = scriptPanel.getSelectionStart(); bounds[1] = scriptPanel.getSelectionEnd(); if (bounds[0] == bounds[1]) { bounds = getSqlBoundsBySeparatorRule(scriptPanel.getCaretPosition()); } return bounds; }
/** * Block uncomment the selected text in the given script panel. * * @param scriptPanel Script panel */ private static void blockUncomment(RSyntaxTextArea scriptPanel) { // Recover the index of the start of the selection. final int startOffset = scriptPanel.getSelectionStart(); final int endOffset = scriptPanel.getSelectionEnd(); // Delete the comment characters. scriptPanel.replaceRange("", endOffset - BLOCK_COMMENT_END.length(), endOffset); scriptPanel.replaceRange("", startOffset, startOffset + BLOCK_COMMENT_START.length()); // Select the uncommented selection. scriptPanel.setSelectionStart(startOffset); scriptPanel.setSelectionEnd(endOffset - BLOCK_COMMENT_START.length() - BLOCK_COMMENT_END.length()); }
private String findStartSymbol() { int carretCoordinates; while (true) { carretCoordinates = rSyntaxTextArea.getSelectionStart(); if (rSyntaxTextArea.getSelectedText().startsWith("\n") || rSyntaxTextArea.getSelectionStart() == 0) { if (rSyntaxTextArea.getSelectionStart() != 0) { rSyntaxTextArea.select(carretCoordinates + 1, rSyntaxTextArea.getSelectionEnd()); } else { rSyntaxTextArea.select(carretCoordinates, rSyntaxTextArea.getSelectionEnd()); } return rSyntaxTextArea.getSelectedText(); } else { carretCoordinates--; rSyntaxTextArea.select(carretCoordinates, rSyntaxTextArea.getSelectionEnd()); } } }
/** * Block comment the selected text in the given script panel. * * @param scriptPanel Script panel */ private static void blockComment(RSyntaxTextArea scriptPanel) { // Recover the index of the start of the selection. final int startOffset = scriptPanel.getSelectionStart(); // Comment the selection. final String commentedSelection = BLOCK_COMMENT_START + scriptPanel.getSelectedText() + BLOCK_COMMENT_END; scriptPanel.replaceSelection(commentedSelection); // Select the commented selection. scriptPanel.setSelectionStart(startOffset); scriptPanel.setSelectionEnd(startOffset + commentedSelection.length()); }
/** * Copies the currently selected text to the system clipboard, with * any necessary style information (font, foreground color and background * color). Does nothing for {@code null} or empty selections. * * @see #copyAsStyledText(Theme) */ public void copyAsStyledText() { int selStart = getSelectionStart(); int selEnd = getSelectionEnd(); if (selStart==selEnd) { return; } // Get the selection as HTML String html = HtmlUtil.getTextAsHtml(this, selStart, selEnd); // Get the selection as RTF byte[] rtfBytes = getTextAsRtf(selStart, selEnd); // Set the system clipboard contents to the RTF selection. StyledTextTransferable contents = new StyledTextTransferable(html, rtfBytes); Clipboard cb = getToolkit().getSystemClipboard(); try { cb.setContents(contents, null); } catch (IllegalStateException ise) { UIManager.getLookAndFeel().provideErrorFeedback(null); } }
/** * Uncomment the selected text in the given script panel. * * @param scriptPanel Script panel */ private static void uncommentSQL(RSyntaxTextArea scriptPanel, String commentCharacter) { final Element root = scriptPanel.getDocument().getDefaultRootElement(); final int numberOfLastLine = root.getElementIndex(scriptPanel.getSelectionEnd()); int currentLineNumber = root.getElementIndex(scriptPanel.getSelectionStart()); while (currentLineNumber <= numberOfLastLine) { try { int startOffset = root.getElement(currentLineNumber).getStartOffset(); if (scriptPanel.getText(startOffset, commentCharacter.length()).equals(commentCharacter)) { scriptPanel.replaceRange("", startOffset, startOffset + commentCharacter.length()); } } catch (BadLocationException e) { LOGGER.warn(I18N.tr("Invalid length or offset when trying to uncomment code."), e); } currentLineNumber++; } } }
/** * Test whether the selected text consists of an unbroken range of * commented lines. * * @param scriptPanel Script panel * @return True iff the selected text consists of an unbroken range of * commented lines */ private static boolean unbrokenRangeOfComments(RSyntaxTextArea scriptPanel, String commentCharacter) { final Element root = scriptPanel.getDocument().getDefaultRootElement(); final int numberOfLastLine = root.getElementIndex(scriptPanel.getSelectionEnd()); int currentLineNumber = root.getElementIndex(scriptPanel.getSelectionStart()); while (currentLineNumber <= numberOfLastLine) { try { int startOffset = root.getElement(currentLineNumber).getStartOffset(); if (!scriptPanel.getText(startOffset, commentCharacter.length()).equals(commentCharacter)) { return false; } } catch (BadLocationException e) { LOGGER.warn(I18N.tr("Problem when checking for an unbroken range of comments"), e); } currentLineNumber++; } return true; }
@Override public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { if (!textArea.isEditable() || !textArea.isEnabled()) { UIManager.getLookAndFeel().provideErrorFeedback(textArea); return; } RSyntaxTextArea sta = (RSyntaxTextArea)textArea; boolean noSelection= sta.getSelectionStart()==sta.getSelectionEnd(); // First, see if this language wants to handle inserting newlines // itself. boolean handled = false; if (noSelection) { RSyntaxDocument doc = (RSyntaxDocument)sta.getDocument(); handled = doc.insertBreakSpecialHandling(e); } // If not... if (!handled) { handleInsertBreak(sta, noSelection); } }
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { if (!textArea.isEditable() || !textArea.isEnabled()) { UIManager.getLookAndFeel().provideErrorFeedback(textArea); return; } RSyntaxTextArea sta = (RSyntaxTextArea)textArea; boolean noSelection= sta.getSelectionStart()==sta.getSelectionEnd(); // If we're auto-indenting... if (noSelection && sta.isAutoIndentEnabled()) { insertNewlineWithAutoIndent(sta); } else { textArea.replaceSelection("\n"); if (noSelection) { possiblyCloseCurlyBrace(sta, null); } } }
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { if (!textArea.isEditable() || !textArea.isEnabled()) { UIManager.getLookAndFeel().provideErrorFeedback(textArea); return; } RSyntaxTextArea sta = (RSyntaxTextArea)textArea; // If we're in insert-mode and auto-indenting... if (sta.getTextMode()==RTextArea.INSERT_MODE && sta.isAutoIndentEnabled() && sta.getSelectionStart()==sta.getSelectionEnd()) { insertNewlineWithAutoIndent(sta); } // Otherwise, we're in overwrite-mode or not auto-indenting. else { textArea.replaceSelection("\n"); } }
/** * Comment the selected text in the given script panel. * * @param scriptPanel Script panel */ private static void commentSQL(RSyntaxTextArea scriptPanel, String commentCharacter) { final Element root = scriptPanel.getDocument().getDefaultRootElement(); final int numberOfLastLine = root.getElementIndex(scriptPanel.getSelectionEnd()); int currentLineNumber = root.getElementIndex(scriptPanel.getSelectionStart()); while (currentLineNumber <= numberOfLastLine) { scriptPanel.insert(commentCharacter, root.getElement(currentLineNumber).getStartOffset()); currentLineNumber++; } }