List of usage examples for javax.swing.text StyledDocument remove
public void remove(int offs, int len) throws BadLocationException;
From source file:Main.java
/** * Affect a stylised text into an instance of JTextPane. * @param textPane Instance of JTextPane to affect. * @param textArray Array of strings.//from w w w . j av a 2 s .c o m * @param styleArray Array of styles. Must match the textArray. */ public static void setText(JTextPane textPane, String[] textArray, String[] styleArray) { StyledDocument doc = textPane.getStyledDocument(); try { doc.remove(0, doc.getLength()); // Erase all the previous text. for (int i = 0; i < textArray.length; i++) { int offset = doc.getLength(); javax.swing.text.Style style = textPane.getStyle(styleArray[i]); doc.insertString(offset, textArray[i], style); doc.setParagraphAttributes(offset, textArray[i].length(), style, true); } textPane.setCaretPosition(0); } catch (BadLocationException ignore) { ignore.printStackTrace(); } }
From source file:com.github.alexfalappa.nbspringboot.cfgprops.completion.CfgPropValueCompletionItem.java
@Override public void defaultAction(JTextComponent jtc) { try {/*from w w w . ja v a2 s . c o m*/ StyledDocument doc = (StyledDocument) jtc.getDocument(); //Here we remove the characters starting at the start offset //and ending at the point where the caret is currently found: doc.remove(dotOffset, caretOffset - dotOffset); doc.insertString(dotOffset, getText(), null); //This statement will close the code completion box: Completion.get().hideAll(); } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } }
From source file:com.junichi11.netbeans.php.enhancements.ui.actions.ConvertToAction.java
@Override public void actionPerformed(ActionEvent ev) { try {//from www.ja v a 2s .c o m JTextComponent editor = EditorRegistry.lastFocusedComponent(); if (editor == null) { return; } final StyledDocument document = context.openDocument(); if (editor.getDocument() != document) { return; } final String selectedText = editor.getSelectedText(); if (selectedText == null || selectedText.isEmpty()) { return; } final String convertedString = convert(selectedText); if (selectedText.equals(convertedString)) { return; } final int selectionStartPosition = editor.getSelectionStart(); NbDocument.runAtomic(document, new Runnable() { @Override public void run() { try { document.remove(selectionStartPosition, selectedText.length()); document.insertString(selectionStartPosition, convertedString, null); } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } } }); } catch (IOException ex) { Exceptions.printStackTrace(ex); } }
From source file:Main.java
private void initComponents() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextPane textPane = new JTextPane(); ((AbstractDocument) textPane.getDocument()).addDocumentListener(new DocumentListener() { @Override//from ww w . j a v a 2s . c o m public void insertUpdate(final DocumentEvent de) { SwingUtilities.invokeLater(new Runnable() { public void run() { try { StyledDocument doc = (StyledDocument) de.getDocument(); int start = Utilities.getRowStart(textPane, Math.max(0, de.getOffset() - 1)); int end = Utilities.getWordStart(textPane, de.getOffset() + de.getLength()); String text = doc.getText(start, end - start); for (String emoticon : imageTokens) { int i = text.indexOf(emoticon); while (i >= 0) { final SimpleAttributeSet attrs = new SimpleAttributeSet( doc.getCharacterElement(start + i).getAttributes()); if (StyleConstants.getIcon(attrs) == null) { switch (emoticon) { case imageToken: StyleConstants.setIcon(attrs, anImage); break; } doc.remove(start + i, emoticon.length()); doc.insertString(start + i, emoticon, attrs); } i = text.indexOf(emoticon, i + emoticon.length()); } } } catch (BadLocationException ex) { ex.printStackTrace(); } } }); } @Override public void removeUpdate(DocumentEvent e) { } @Override public void changedUpdate(DocumentEvent e) { } }); JScrollPane scrollPane = new JScrollPane(textPane); scrollPane.setPreferredSize(new Dimension(300, 300)); frame.add(scrollPane); frame.pack(); frame.setVisible(true); }
From source file:dataviewer.DataViewer.java
private void cb_tableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_tableActionPerformed try {/*from w ww . j a va2 s . c om*/ table = cb_table.getSelectedItem().toString(); DB db = new DB("./db/" + table + ".db"); db.open(); String[] cols = db.get_table_columns(table); db.close(); DefaultTableModel model = new DefaultTableModel(); //model.addColumn("Column Index"); model.addColumn("Field"); int i = 1; StyleContext sc = StyleContext.getDefaultStyleContext(); TabSet tabs = new TabSet(new TabStop[] { new TabStop(20) }); AttributeSet paraSet = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabs); tp_sql.setParagraphAttributes(paraSet, false); StyledDocument doc = tp_sql.getStyledDocument(); doc.remove(0, doc.getLength()); doc.insertString(doc.getLength(), "SELECT", null); doc.insertString(doc.getLength(), "\n", null); for (int j = 1; j < cols.length; ++j) { String s = cols[j]; //model.addRow(new Object[]{i++, s}); model.addRow(new Object[] { s }); doc.insertString(doc.getLength(), "\t", null); doc.insertString(doc.getLength(), s, null); if (j < cols.length - 1) { doc.insertString(doc.getLength(), ",", null); } doc.insertString(doc.getLength(), "\n", null); } doc.insertString(doc.getLength(), "FROM", null); doc.insertString(doc.getLength(), "\n", null); doc.insertString(doc.getLength(), "\t", null); doc.insertString(doc.getLength(), table, null); /*doc.insertString(doc.getLength(), "\n", null); doc.insertString(doc.getLength(), "WHERE", null); doc.insertString(doc.getLength(), "\n", null); doc.insertString(doc.getLength(), "\t", null); doc.insertString(doc.getLength(), "_N_ <= " + N, null); doc.insertString(doc.getLength(), ";", null);*/ tb_columns.setModel(model); TableRowSorter trs = new TableRowSorter(model); trs.setComparator(0, new IntComparator()); tb_columns.setRowSorter(trs); //highline(); } catch (Exception e) { txt_count.setText(e.getMessage()); } }
From source file:com.github.alexfalappa.nbspringboot.cfgprops.completion.CfgPropCompletionItem.java
@Override public void defaultAction(JTextComponent jtc) { try {/* www. j a v a2 s . co m*/ StyledDocument doc = (StyledDocument) jtc.getDocument(); // calculate the amount of chars to remove (by default from property start up to caret position) int lenToRemove = caretOffset - propStartOffset; if (overwrite) { // NOTE: the editor removes by itself the word at caret when ctrl + enter is pressed // the document state here is different from when the completion was invoked thus we have to // find again the offset of the equal sign in the line Element lineElement = doc.getParagraphElement(caretOffset); String line = doc.getText(lineElement.getStartOffset(), lineElement.getEndOffset() - lineElement.getStartOffset()); int equalSignIndex = line.indexOf('='); if (equalSignIndex >= 0) { // from property start to equal sign lenToRemove = lineElement.getStartOffset() + equalSignIndex - propStartOffset; } else { // from property start to end of line (except line terminator) lenToRemove = lineElement.getEndOffset() - 1 - propStartOffset; } } // remove characters from the property name start offset doc.remove(propStartOffset, lenToRemove); doc.insertString(propStartOffset, getText(), null); // close the code completion box Completion.get().hideAll(); } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } }
From source file:homenetapp.HomeNetAppGui.java
private void updateTextPane(final String text, final javax.swing.text.Style color) { SwingUtilities.invokeLater(new Runnable() { public void run() { javax.swing.text.StyledDocument doc = consoleTextPane.getStyledDocument(); try { // consoleTextPane.se if (color != null) { doc.setLogicalStyle(doc.getLength(), color); }/*from w w w . j a va 2s. c o m*/ doc.insertString(doc.getLength(), text, null); if (doc.getLength() > 10000) { doc.remove(0, text.length()); } } catch (javax.swing.text.BadLocationException e) { throw new RuntimeException(e); } consoleTextPane.setCaretPosition(doc.getLength() - 1); } }); }
From source file:ome.formats.importer.gui.GuiImporter.java
/** * This method appends data to the output window * /*from w w w . j a v a2s. c o m*/ * @param s - text to append */ public void appendToOutput(String s) { try { StyledDocument doc = (StyledDocument) outputTextPane.getDocument(); Style style = doc.addStyle("StyleName", null); StyleConstants.setForeground(style, Color.black); StyleConstants.setFontFamily(style, "SansSerif"); StyleConstants.setFontSize(style, 12); StyleConstants.setBold(style, false); doc.insertString(doc.getLength(), s, style); //trim the document size so it doesn't grow to big int maxChars = 200000; if (doc.getLength() > maxChars) doc.remove(0, doc.getLength() - maxChars); //outputTextPane.setDocument(doc); } catch (BadLocationException e) { } }
From source file:ome.formats.importer.gui.GuiImporter.java
/** * This method appends data to the output window. * /*from w ww.j a v a2 s . co m*/ * @param s - string to append */ public void appendToDebug(String s) { log.debug(s); try { StyledDocument doc = (StyledDocument) debugTextPane.getDocument(); Style style = doc.addStyle("StyleName", null); StyleConstants.setForeground(style, Color.black); StyleConstants.setFontFamily(style, "SansSerif"); StyleConstants.setFontSize(style, 12); StyleConstants.setBold(style, false); doc.insertString(doc.getLength(), s, style); //trim the document size so it doesn't grow to big int maxChars = 200000; if (doc.getLength() > maxChars) doc.remove(0, doc.getLength() - maxChars); //debugTextPane.setDocument(doc); } catch (BadLocationException e) { } }
From source file:org.fit.cssbox.scriptbox.demo.tester.ConsoleInjector.java
private static void clearStyledDocumentImpl(final StyledDocument doc) { try {/* w w w. j ava 2s . c o m*/ doc.remove(0, doc.getLength()); } catch (BadLocationException e) { e.printStackTrace(); } }