List of usage examples for javax.swing.text Document getText
public String getText(int offset, int length) throws BadLocationException;
From source file:Main.java
private String addWhiteSpace(FilterBypass fb, int offset, String text) throws BadLocationException { Document doc = fb.getDocument(); Element root = doc.getDefaultRootElement(); int line = root.getElementIndex(offset); int i = root.getElement(line).getStartOffset(); StringBuilder whiteSpace = new StringBuilder(text); while (true) { String temp = doc.getText(i, 1); if (temp.equals(" ") || temp.equals("\t")) { System.out.println("added"); whiteSpace.append(temp);/*w w w.ja va 2s . c o m*/ i++; } else { break; } } return whiteSpace.toString(); }
From source file:Main.java
public void actionPerformed(ActionEvent e) { JTextComponent comp = getTextComponent(e); if (comp == null) return;// w w w . j a v a 2 s . com Document doc = comp.getDocument(); int start = comp.getSelectionStart(); int end = comp.getSelectionEnd(); try { int left = Utilities.getWordStart(comp, start); int right = Utilities.getWordEnd(comp, end); String word = doc.getText(left, right - left); doc.remove(left, right - left); doc.insertString(left, word.toUpperCase(), null); comp.setSelectionStart(start); comp.setSelectionEnd(end); } catch (Exception ble) { return; } }
From source file:Main.java
public void actionPerformed(ActionEvent e) { JTextComponent comp = getTextComponent(e); if (comp == null) return;//from w w w . j a v a 2 s . co m Document doc = comp.getDocument(); int start = comp.getSelectionStart(); int end = comp.getSelectionEnd(); try { int left = Utilities.getWordStart(comp, start); int right = Utilities.getWordEnd(comp, end); String word = doc.getText(left, right - left); doc.remove(left, right - left); doc.insertString(left, word.toUpperCase(), null); comp.setSelectionStart(start); comp.setSelectionEnd(end); } catch (BadLocationException ble) { return; } }
From source file:io.wcm.tooling.netbeans.sightly.completion.classLookup.MemberLookupCompleter.java
@Override public List<CompletionItem> getCompletionItems(Document document, String filter, int startOffset, int caretOffset) { List<CompletionItem> ret = new ArrayList<>(); try {/* w ww.ja va 2s . c o m*/ String text = document.getText(0, caretOffset); List<String> variables = resolveVariable(filter, text); // if the filter is not the complete variable, we first autocomplete the actual variable for (String variable : variables) { if (!StringUtils.equals(filter, variable)) { ret.add(new BasicCompletionItem(variable, true, startOffset, caretOffset)); } } // otherwise we lookup the defined class and return all of it's members if (ret.isEmpty()) { Set<String> items = resolveClass(filter, text, document); for (String item : items) { if (StringUtils.startsWith(item, filter)) { ret.add(new BasicCompletionItem(item, false, startOffset, caretOffset)); } } } } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } return ret; }
From source file:KeymapExample.java
public void actionPerformed(ActionEvent e) { JTextComponent comp = getTextComponent(e); if (comp == null) return;/* w w w. j a v a2s. c o m*/ Document doc = comp.getDocument(); int start = comp.getSelectionStart(); int end = comp.getSelectionEnd(); try { int left = javax.swing.text.Utilities.getWordStart(comp, start); int right = javax.swing.text.Utilities.getWordEnd(comp, end); String word = doc.getText(left, right - left); doc.remove(left, right - left); doc.insertString(left, word.toUpperCase(), null); comp.setSelectionStart(start); comp.setSelectionEnd(end); } catch (BadLocationException ble) { return; } }
From source file:LiveParenMatcher.java
public void insertUpdate_2(DocumentEvent de) { Document doc = de.getDocument(); int offset = de.getOffset(); int length = de.getLength(); String inserted = ""; try {/* ww w . j ava2s.c o m*/ inserted = doc.getText(offset, length); } catch (BadLocationException ble) { } for (int j = 0; j < inserted.length(); j += 1) { char ch = inserted.charAt(j); if (ch == '(' || ch == '[' || ch == '{' || ch == ')' || ch == ']' || ch == '}') { SwingUtilities.invokeLater(this); // will call run() return; // no need to check further } } }
From source file:ShowHTMLViews.java
public static void displayElement(Document doc, Element e, int indent, PrintStream out) { for (int i = 0; i < indent; i++) { out.print(" "); }/* w w w . j a va 2 s. com*/ out.println("===== Element Class: " + getShortClassName(e.getClass())); for (int i = 0; i < indent; i++) { out.print(" "); } int startOffset = e.getStartOffset(); int endOffset = e.getEndOffset(); out.println("Offsets [" + startOffset + ", " + endOffset + "]"); AttributeSet a = e.getAttributes(); Enumeration x = a.getAttributeNames(); for (int i = 0; i < indent; i++) { out.print(" "); } out.println("ATTRIBUTES:"); while (x.hasMoreElements()) { for (int i = 0; i < indent; i++) { out.print(" "); } Object attr = x.nextElement(); out.println(" (" + attr + ", " + a.getAttribute(attr) + ")" + " [" + getShortClassName(attr.getClass()) + "/" + getShortClassName(a.getAttribute(attr).getClass()) + "] "); } // Display the text for a leaf element if (e.isLeaf()) { try { String str = doc.getText(startOffset, endOffset - startOffset); if (str.length() > 40) { str = str.substring(0, 40); } if (str.length() > 0) { for (int i = 0; i < indent; i++) { out.print(" "); } out.println("[" + str + "]"); } } catch (BadLocationException ex) { } } // Display child elements int count = e.getElementCount(); for (int i = 0; i < count; i++) { displayElement(doc, e.getElement(i), indent + 1, out); } }
From source file:edu.virginia.iath.oxygenplugins.juel.JUELPluginMenu.java
public JUELPluginMenu(StandalonePluginWorkspace spw, LocalOptions ops) { super(name, true); ws = spw;/*from w w w . j a va 2 s . co m*/ options = ops; // setup the options //options.readStorage(); // Find names JMenuItem search = new JMenuItem("Find Name"); search.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent selection) { String label = "Find Name"; final JTextField lastName = new JTextField("", 30); lastName.setPreferredSize(new Dimension(350, 25)); //JTextField projectName = new JTextField("", 30); final JComboBox possibleVals = new JComboBox(); possibleVals.setEnabled(false); possibleVals.setPreferredSize(new Dimension(350, 25)); JButton search = new JButton("Search"); search.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent selection) { // query the database try { String json = ""; String line; URL url = new URL("http://juel.iath.virginia.edu/academical_db/people/find_people?term=" + lastName.getText()); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); while ((line = in.readLine()) != null) { json += line; } JSONArray obj = new JSONArray(json); // Read the JSON and update possibleVals possibleVals.removeAllItems(); possibleVals.setEnabled(false); for (int i = 0; i < obj.length(); i++) { JSONObject cur = obj.getJSONObject(i); String name = cur.getString("label"); String id = String.format("P%05d", cur.getInt("value")); possibleVals.addItem(new ComboBoxObject(name, id)); possibleVals.setEnabled(true); } } catch (Exception e) { e.printStackTrace(); possibleVals.setEnabled(false); } return; } }); search.setPreferredSize(new Dimension(100, 25)); JButton insert = new JButton("Insert"); insert.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent sel) { // Insert into the page // Get the selected value, grab the ID, then insert into the document // Get the editor WSTextEditorPage ed = null; WSEditor editorAccess = ws .getCurrentEditorAccess(StandalonePluginWorkspace.MAIN_EDITING_AREA); if (editorAccess != null && editorAccess.getCurrentPage() instanceof WSTextEditorPage) { ed = (WSTextEditorPage) editorAccess.getCurrentPage(); } String result = "key=\"" + ((ComboBoxObject) possibleVals.getSelectedItem()).id + "\""; // Update the text in the document ed.beginCompoundUndoableEdit(); int selectionOffset = ed.getSelectionStart(); ed.deleteSelection(); javax.swing.text.Document doc = ed.getDocument(); try { if (selectionOffset > 0 && !doc.getText(selectionOffset - 1, 1).equals(" ")) result = " " + result; if (selectionOffset > 0 && !doc.getText(selectionOffset, 1).equals(" ") && !doc.getText(selectionOffset, 1).equals(">")) result = result + " "; doc.insertString(selectionOffset, result, javax.swing.text.SimpleAttributeSet.EMPTY); } catch (javax.swing.text.BadLocationException b) { // Okay if it doesn't work } ed.endCompoundUndoableEdit(); return; } }); insert.setPreferredSize(new Dimension(100, 25)); java.awt.GridLayout layoutOuter = new java.awt.GridLayout(3, 1); java.awt.FlowLayout layout = new java.awt.FlowLayout(FlowLayout.RIGHT); // rows, columns JPanel addPanel = new JPanel(); JPanel addPanelInner = new JPanel(); addPanel.setLayout(layoutOuter); addPanel.add(new JLabel("Search for last name, then choose a full name from the list below")); addPanelInner.setLayout(layout); addPanelInner.add(new JLabel("Search Last Name: ")); addPanelInner.add(lastName); addPanelInner.add(search); addPanel.add(addPanelInner); addPanelInner = new JPanel(); addPanelInner.setLayout(layout); addPanelInner.add(new JLabel("Narrow Search: ")); addPanelInner.add(possibleVals); addPanelInner.add(insert); addPanel.add(addPanelInner); JOptionPane.showMessageDialog((java.awt.Frame) ws.getParentFrame(), addPanel, label, JOptionPane.PLAIN_MESSAGE); } }); this.add(search); // Find places search = new JMenuItem("Find Place"); search.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent selection) { String label = "Find Place"; final JTextField searchText = new JTextField("", 30); searchText.setPreferredSize(new Dimension(350, 25)); //JTextField projectName = new JTextField("", 30); final JComboBox possibleVals = new JComboBox(); possibleVals.setEnabled(false); possibleVals.setPreferredSize(new Dimension(350, 25)); JButton search = new JButton("Search"); search.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent selection) { // query the database try { String json = ""; String line; URL url = new URL( "http://academical.village.virginia.edu/academical_db/places/find_places?term=" + searchText.getText()); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); while ((line = in.readLine()) != null) { json += line; } JSONArray obj = new JSONArray(json); // Read the JSON and update possibleVals possibleVals.removeAllItems(); possibleVals.setEnabled(false); for (int i = 0; i < obj.length(); i++) { JSONObject cur = obj.getJSONObject(i); String id = String.format("PL%04d", cur.getInt("value")); String name = cur.getString("label") + " (" + id + ")"; possibleVals.addItem(new ComboBoxObject(name, id)); possibleVals.setEnabled(true); } } catch (Exception e) { e.printStackTrace(); possibleVals.setEnabled(false); } return; } }); search.setPreferredSize(new Dimension(100, 25)); JButton insert = new JButton("Insert"); insert.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent sel) { // Insert into the page // Get the selected value, grab the ID, then insert into the document // Get the editor WSTextEditorPage ed = null; WSEditor editorAccess = ws .getCurrentEditorAccess(StandalonePluginWorkspace.MAIN_EDITING_AREA); if (editorAccess != null && editorAccess.getCurrentPage() instanceof WSTextEditorPage) { ed = (WSTextEditorPage) editorAccess.getCurrentPage(); } String result = "key=\"" + ((ComboBoxObject) possibleVals.getSelectedItem()).id + "\""; // Update the text in the document ed.beginCompoundUndoableEdit(); int selectionOffset = ed.getSelectionStart(); ed.deleteSelection(); javax.swing.text.Document doc = ed.getDocument(); try { if (selectionOffset > 0 && !doc.getText(selectionOffset - 1, 1).equals(" ")) result = " " + result; if (selectionOffset > 0 && !doc.getText(selectionOffset, 1).equals(" ") && !doc.getText(selectionOffset, 1).equals(">")) result = result + " "; doc.insertString(selectionOffset, result, javax.swing.text.SimpleAttributeSet.EMPTY); } catch (javax.swing.text.BadLocationException b) { // Okay if it doesn't work } ed.endCompoundUndoableEdit(); return; } }); insert.setPreferredSize(new Dimension(100, 25)); java.awt.GridLayout layoutOuter = new java.awt.GridLayout(3, 1); java.awt.FlowLayout layout = new java.awt.FlowLayout(FlowLayout.RIGHT); // rows, columns JPanel addPanel = new JPanel(); JPanel addPanelInner = new JPanel(); addPanel.setLayout(layoutOuter); addPanel.add(new JLabel("Search for a place name, then choose one from the list below")); addPanelInner.setLayout(layout); addPanelInner.add(new JLabel("Search Keyword: ")); addPanelInner.add(searchText); addPanelInner.add(search); addPanel.add(addPanelInner); addPanelInner = new JPanel(); addPanelInner.setLayout(layout); addPanelInner.add(new JLabel("Narrow Search: ")); addPanelInner.add(possibleVals); addPanelInner.add(insert); addPanel.add(addPanelInner); JOptionPane.showMessageDialog((java.awt.Frame) ws.getParentFrame(), addPanel, label, JOptionPane.PLAIN_MESSAGE); } }); this.add(search); // Find corporate bodies search = new JMenuItem("Find Corporate Body"); search.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent selection) { String label = "Find Corporate Body"; final JTextField searchText = new JTextField("", 30); searchText.setPreferredSize(new Dimension(350, 25)); //JTextField projectName = new JTextField("", 30); final JComboBox possibleVals = new JComboBox(); possibleVals.setEnabled(false); possibleVals.setPreferredSize(new Dimension(350, 25)); JButton search = new JButton("Search"); search.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent selection) { // query the database try { String json = ""; String line; URL url = new URL( "http://academical.village.virginia.edu/academical_db/corporate_bodies/find?term=" + searchText.getText()); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); while ((line = in.readLine()) != null) { json += line; } JSONArray obj = new JSONArray(json); // Read the JSON and update possibleVals possibleVals.removeAllItems(); possibleVals.setEnabled(false); for (int i = 0; i < obj.length(); i++) { JSONObject cur = obj.getJSONObject(i); String id = String.format("CB%04d", cur.getInt("value")); String name = cur.getString("label") + " (" + id + ")"; possibleVals.addItem(new ComboBoxObject(name, id)); possibleVals.setEnabled(true); } } catch (Exception e) { e.printStackTrace(); possibleVals.setEnabled(false); } return; } }); search.setPreferredSize(new Dimension(100, 25)); JButton insert = new JButton("Insert"); insert.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent sel) { // Insert into the page // Get the selected value, grab the ID, then insert into the document // Get the editor WSTextEditorPage ed = null; WSEditor editorAccess = ws .getCurrentEditorAccess(StandalonePluginWorkspace.MAIN_EDITING_AREA); if (editorAccess != null && editorAccess.getCurrentPage() instanceof WSTextEditorPage) { ed = (WSTextEditorPage) editorAccess.getCurrentPage(); } String result = "key=\"" + ((ComboBoxObject) possibleVals.getSelectedItem()).id + "\""; // Update the text in the document ed.beginCompoundUndoableEdit(); int selectionOffset = ed.getSelectionStart(); ed.deleteSelection(); javax.swing.text.Document doc = ed.getDocument(); try { if (selectionOffset > 0 && !doc.getText(selectionOffset - 1, 1).equals(" ")) result = " " + result; if (selectionOffset > 0 && !doc.getText(selectionOffset, 1).equals(" ") && !doc.getText(selectionOffset, 1).equals(">")) result = result + " "; doc.insertString(selectionOffset, result, javax.swing.text.SimpleAttributeSet.EMPTY); } catch (javax.swing.text.BadLocationException b) { // Okay if it doesn't work } ed.endCompoundUndoableEdit(); return; } }); insert.setPreferredSize(new Dimension(100, 25)); java.awt.GridLayout layoutOuter = new java.awt.GridLayout(3, 1); java.awt.FlowLayout layout = new java.awt.FlowLayout(FlowLayout.RIGHT); // rows, columns JPanel addPanel = new JPanel(); JPanel addPanelInner = new JPanel(); addPanel.setLayout(layoutOuter); addPanel.add(new JLabel("Search for a corporate body, then one from the list below")); addPanelInner.setLayout(layout); addPanelInner.add(new JLabel("Search Keyword: ")); addPanelInner.add(searchText); addPanelInner.add(search); addPanel.add(addPanelInner); addPanelInner = new JPanel(); addPanelInner.setLayout(layout); addPanelInner.add(new JLabel("Narrow Search: ")); addPanelInner.add(possibleVals); addPanelInner.add(insert); addPanel.add(addPanelInner); JOptionPane.showMessageDialog((java.awt.Frame) ws.getParentFrame(), addPanel, label, JOptionPane.PLAIN_MESSAGE); } }); this.add(search); // Find Courses search = new JMenuItem("Find Course"); search.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent selection) { String label = "Find Course"; final JTextField searchText = new JTextField("", 30); searchText.setPreferredSize(new Dimension(350, 25)); //JTextField projectName = new JTextField("", 30); final JComboBox possibleVals = new JComboBox(); possibleVals.setEnabled(false); possibleVals.setPreferredSize(new Dimension(350, 25)); JButton search = new JButton("Search"); search.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent selection) { // query the database try { String json = ""; String line; URL url = new URL( "http://academical.village.virginia.edu/academical_db/courses/find?term=" + searchText.getText()); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); while ((line = in.readLine()) != null) { json += line; } JSONArray obj = new JSONArray(json); // Read the JSON and update possibleVals possibleVals.removeAllItems(); possibleVals.setEnabled(false); for (int i = 0; i < obj.length(); i++) { JSONObject cur = obj.getJSONObject(i); String id = String.format("C%04d", cur.getInt("value")); String name = cur.getString("label") + " (" + id + ")"; possibleVals.addItem(new ComboBoxObject(name, id)); possibleVals.setEnabled(true); } } catch (Exception e) { e.printStackTrace(); possibleVals.setEnabled(false); } return; } }); search.setPreferredSize(new Dimension(100, 25)); JButton insert = new JButton("Insert"); insert.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent sel) { // Insert into the page // Get the selected value, grab the ID, then insert into the document // Get the editor WSTextEditorPage ed = null; WSEditor editorAccess = ws .getCurrentEditorAccess(StandalonePluginWorkspace.MAIN_EDITING_AREA); if (editorAccess != null && editorAccess.getCurrentPage() instanceof WSTextEditorPage) { ed = (WSTextEditorPage) editorAccess.getCurrentPage(); } String result = "key=\"" + ((ComboBoxObject) possibleVals.getSelectedItem()).id + "\""; // Update the text in the document ed.beginCompoundUndoableEdit(); int selectionOffset = ed.getSelectionStart(); ed.deleteSelection(); javax.swing.text.Document doc = ed.getDocument(); try { if (selectionOffset > 0 && !doc.getText(selectionOffset - 1, 1).equals(" ")) result = " " + result; if (selectionOffset > 0 && !doc.getText(selectionOffset, 1).equals(" ") && !doc.getText(selectionOffset, 1).equals(">")) result = result + " "; doc.insertString(selectionOffset, result, javax.swing.text.SimpleAttributeSet.EMPTY); } catch (javax.swing.text.BadLocationException b) { // Okay if it doesn't work } ed.endCompoundUndoableEdit(); return; } }); insert.setPreferredSize(new Dimension(100, 25)); java.awt.GridLayout layoutOuter = new java.awt.GridLayout(3, 1); java.awt.FlowLayout layout = new java.awt.FlowLayout(FlowLayout.RIGHT); // rows, columns JPanel addPanel = new JPanel(); JPanel addPanelInner = new JPanel(); addPanel.setLayout(layoutOuter); addPanel.add(new JLabel("Search for a course, then choose one from the list below")); addPanelInner.setLayout(layout); addPanelInner.add(new JLabel("Search Keyword: ")); addPanelInner.add(searchText); addPanelInner.add(search); addPanel.add(addPanelInner); addPanelInner = new JPanel(); addPanelInner.setLayout(layout); addPanelInner.add(new JLabel("Narrow Search: ")); addPanelInner.add(possibleVals); addPanelInner.add(insert); addPanel.add(addPanelInner); JOptionPane.showMessageDialog((java.awt.Frame) ws.getParentFrame(), addPanel, label, JOptionPane.PLAIN_MESSAGE); } }); this.add(search); }
From source file:net.sf.jasperreports.engine.util.JEditorPaneRtfMarkupProcessor.java
@Override public String convert(String srcText) { JEditorPane editorPane = new JEditorPane("text/rtf", srcText); editorPane.setEditable(false);// w ww. ja v a 2s . c o m List<Element> elements = new ArrayList<Element>(); Document document = editorPane.getDocument(); Element root = document.getDefaultRootElement(); if (root != null) { addElements(elements, root); } String chunk = null; Element element = null; int startOffset = 0; int endOffset = 0; JRStyledText styledText = new JRStyledText(); styledText.setGlobalAttributes(new HashMap<Attribute, Object>()); for (int i = 0; i < elements.size(); i++) { if (chunk != null) { styledText.append(chunk); styledText.addRun( new JRStyledText.Run(getAttributes(element.getAttributes()), startOffset, endOffset)); } chunk = null; element = elements.get(i); startOffset = element.getStartOffset(); endOffset = element.getEndOffset(); try { chunk = document.getText(startOffset, endOffset - startOffset); } catch (BadLocationException e) { if (log.isDebugEnabled()) { log.debug("Error converting markup.", e); } } } if (chunk != null && !"\n".equals(chunk)) { styledText.append(chunk); styledText.addRun(new JRStyledText.Run(getAttributes(element.getAttributes()), startOffset, endOffset)); } return JRStyledTextParser.getInstance().write(styledText); }
From source file:ch.zhaw.iamp.rct.ui.GrammarWindow.java
private void removeTabIfPossilbe(KeyEvent event, JTextComponent component) { try {//ww w . j a v a 2 s. c o m Document doc = component.getDocument(); int caretPostion = component.getCaretPosition(); int lineStartIndex = doc.getText(0, caretPostion).lastIndexOf('\n') + 1; lineStartIndex = lineStartIndex < 0 ? 0 : lineStartIndex; lineStartIndex = lineStartIndex >= doc.getLength() ? doc.getLength() - 1 : lineStartIndex; int scanEndIndex = lineStartIndex + 4 <= doc.getLength() ? lineStartIndex + 4 : doc.getLength(); for (int i = 0; i < 4 && i + lineStartIndex < scanEndIndex; i++) { if (doc.getText(lineStartIndex, 1).matches(" ")) { doc.remove(lineStartIndex, 1); } else if (doc.getText(lineStartIndex, 1).matches("\t")) { doc.remove(lineStartIndex, 1); break; } else { break; } } event.consume(); } catch (BadLocationException ex) { System.out.println("Could not insert a tab: " + ex.getMessage()); } }