List of usage examples for javax.swing.event DocumentEvent getOffset
public int getOffset();
From source file:Main.java
public static void main(String[] argv) throws Exception { JTextComponent textcomp = new JTextPane(); textcomp.setText("Initial Text"); textcomp.getDocument().addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent evt) { int off = evt.getOffset(); System.out.println("off:" + off); int len = evt.getLength(); System.out.println("len:" + len); try { String str = evt.getDocument().getText(off, len); System.out.println(str); } catch (BadLocationException e) { }/*from www. ja va 2 s . c o m*/ } public void removeUpdate(DocumentEvent evt) { int off = evt.getOffset(); System.out.println("off:" + off); int len = evt.getLength(); System.out.println("len:" + len); } public void changedUpdate(DocumentEvent evt) { int off = evt.getOffset(); System.out.println("off:" + off); int len = evt.getLength(); System.out.println("len:" + len); } }); }
From source file:ListenerSample.java
public void printInfo(DocumentEvent documentEvent) { System.out.println("Offset: " + documentEvent.getOffset()); System.out.println("Length: " + documentEvent.getLength()); DocumentEvent.EventType type = documentEvent.getType(); String typeString = null;/*from w w w. j av a 2 s . c om*/ if (type.equals(DocumentEvent.EventType.CHANGE)) { typeString = "Change"; } else if (type.equals(DocumentEvent.EventType.INSERT)) { typeString = "Insert"; } else if (type.equals(DocumentEvent.EventType.REMOVE)) { typeString = "Remove"; } System.out.println("Type : " + typeString); Document documentSource = documentEvent.getDocument(); Element rootElement = documentSource.getDefaultRootElement(); DocumentEvent.ElementChange change = documentEvent.getChange(rootElement); System.out.println("Change: " + change); }
From source file:LiveParenMatcher.java
public void insertUpdate_3(DocumentEvent de) { Document doc = de.getDocument(); int offset = de.getOffset(); int length = de.getLength(); Segment seg = new Segment(); try {//from w ww.ja v a2s .com doc.getText(offset, length, seg); // text placed in Segment } catch (BadLocationException ble) { } // iterate through the Segment for (char ch = seg.first(); ch != seg.DONE; ch = seg.next()) if (ch == '(' || ch == '[' || ch == '{' || ch == ')' || ch == ']' || ch == '}') { SwingUtilities.invokeLater(this); // will call run() return; // no need to check further } }
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 {//from www . ja va 2s. 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:TextAreaDemo.java
public void insertUpdate(DocumentEvent ev) { if (ev.getLength() != 1) { return;// w w w .ja v a2 s . co m } int pos = ev.getOffset(); String content = null; try { content = textArea.getText(0, pos + 1); } catch (BadLocationException e) { e.printStackTrace(); } // Find where the word starts int w; for (w = pos; w >= 0; w--) { if (!Character.isLetter(content.charAt(w))) { break; } } if (pos - w < 2) { // Too few chars return; } String prefix = content.substring(w + 1).toLowerCase(); int n = Collections.binarySearch(words, prefix); if (n < 0 && -n <= words.size()) { String match = words.get(-n - 1); if (match.startsWith(prefix)) { // A completion is found String completion = match.substring(pos - w); // We cannot modify Document from within notification, // so we submit a task that does the change later SwingUtilities.invokeLater(new CompletionTask(completion, pos + 1)); } } else { // Nothing found mode = Mode.INSERT; } }
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 w w w .j ava2 s . 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:Console.java
public synchronized void insertUpdate(DocumentEvent e) { int len = e.getLength(); int off = e.getOffset(); if (outputMark > off) { outputMark += len;/*from w w w . j a v a 2 s . c o m*/ } }
From source file:Console.java
public synchronized void removeUpdate(DocumentEvent e) { int len = e.getLength(); int off = e.getOffset(); if (outputMark > off) { if (outputMark >= off + len) { outputMark -= len;//from w w w . ja va2 s . c om } else { outputMark = off; } } }
From source file:ca.uhn.hl7v2.testpanel.ui.editor.Hl7V2MessageEditorPanel.java
private void initLocal() { myDocumentListener = new DocumentListener() { public void changedUpdate(DocumentEvent theE) { ourLog.info("Document change: " + theE); handleChange(theE);//from ww w. jav a2s . c o m } private void handleChange(DocumentEvent theE) { myDontRespondToSourceMessageChanges = true; try { long start = System.currentTimeMillis(); String newSource = myMessageEditor.getText(); int changeStart = theE.getOffset(); int changeEnd = changeStart + theE.getLength(); myMessage.updateSourceMessage(newSource, changeStart, changeEnd); ourLog.info("Handled document update in {} ms", System.currentTimeMillis() - start); } finally { myDontRespondToSourceMessageChanges = false; } } public void insertUpdate(DocumentEvent theE) { ourLog.info("Document insert: " + theE); handleChange(theE); } public void removeUpdate(DocumentEvent theE) { ourLog.info("Document removed: " + theE); handleChange(theE); } }; myMessageEditor.getDocument().addDocumentListener(myDocumentListener); myMessageEditor.addCaretListener(new CaretListener() { public void caretUpdate(final CaretEvent theE) { removeMostHighlights(); if (!myDisableCaretUpdateHandling) { myController.invokeInBackground(new Runnable() { @Override public void run() { myMessage.setHighlitedPathBasedOnRange(new Range(theE.getDot(), theE.getMark())); myTreePanel.repaint(); } }); } } }); updateOutboundConnectionsBox(); myOutboundConnectionsListener = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent theEvt) { updateOutboundConnectionsBox(); } }; myController.getOutboundConnectionList().addPropertyChangeListener(OutboundConnectionList.PROP_LIST, myOutboundConnectionsListener); myOutboundInterfaceCombo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent theE) { if (!myOutboundInterfaceComboModelIsUpdating) { updateSendButton(); } } }); JMenuItem copyMenuItem = new JMenuItem("Copy to Clipboard"); copyMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent theE) { String selection = myTerserPathTextField.getText(); StringSelection data = new StringSelection(selection); Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); clipboard.setContents(data, data); } }); myTerserPathPopupMenu.add(copyMenuItem); myProfilesListener = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent theEvt) { myProfileComboboxModel.update(); registerProfileNamesListeners(); } }; myController.getProfileFileList().addPropertyChangeListener(ProfileFileList.PROP_FILES, myProfilesListener); registerProfileNamesListeners(); myProfilesNamesListener = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent theEvt) { myProfileComboboxModel.update(); } }; }
From source file:org.apache.cayenne.swing.components.textpane.JCayenneTextPane.java
public JCayenneTextPane(SyntaxConstant syntaxConstant) { super();// w w w . ja v a2 s .c om Dimension dimention = new Dimension(15, 15); setMinimumSize(dimention); setPreferredSize(dimention); setMinimumSize(dimention); setBackground(new Color(245, 238, 238)); setBorder(null); pane = new JTextPaneScrollable(new EditorKit(syntaxConstant)) { public void paint(Graphics g) { super.paint(g); JCayenneTextPane.this.repaint(); } }; pane.setFont(SQLSyntaxConstants.DEFAULT_FONT); pane.setBorder(new LineNumberedBorder(this)); scrollPane = new JScrollPane(pane); scrollPane.setBorder(BorderFactory.createLineBorder(new Color(115, 115, 115))); this.painter = new UnderlineHighlighterForText.UnderlineHighlightPainter(Color.red); pane.getDocument().addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent evt) { try { String text = pane.getText(evt.getOffset(), 1).toString(); if (text.equals("/") || text.equals("*")) { removeHighlightText(); pane.repaint(); } if (text.equals(" ") || text.equals("\t") || text.equals("\n")) { pane.repaint(); } } catch (Exception e) { logObj.warn("Error: ", e); } } public void removeUpdate(DocumentEvent evt) { } public void changedUpdate(DocumentEvent evt) { } }); }