Java Code Examples for javax.swing.text.JTextComponent#addCaretListener()
The following examples show how to use
javax.swing.text.JTextComponent#addCaretListener() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BreadCrumbsScheduler.java From netbeans with Apache License 2.0 | 6 votes |
protected void setEditor (JTextComponent editor) { if (currentEditor != null) { currentEditor.removeCaretListener (caretListener); } currentEditor = editor; if (editor != null) { if (listensOnSettings.compareAndSet(false,true)) { BreadcrumbsController.addBreadCrumbsEnabledListener(new AListener ()); } if (caretListener == null) { caretListener = new ACaretListener (); } editor.addCaretListener (caretListener); Document document = editor.getDocument (); if (currentDocument == document) return; currentDocument = document; final Source source = Source.create (currentDocument); schedule (source, new CursorMovedSchedulerEvent (this, editor.getCaret ().getDot (), editor.getCaret ().getMark ()) {}); } else { currentDocument = null; schedule(null, null); } }
Example 2
Source File: SelectCodeElementAction.java From netbeans with Apache License 2.0 | 6 votes |
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { int selectionStartOffset = target.getSelectionStart(); int selectionEndOffset = target.getSelectionEnd(); if (selectionEndOffset > selectionStartOffset || selectNext) { SelectionHandler handler = (SelectionHandler)target.getClientProperty(SelectionHandler.class); if (handler == null) { handler = new SelectionHandler(target, getShortDescription()); target.addCaretListener(handler); // No need to remove the listener above as the handler // is stored is the client-property of the component itself target.putClientProperty(SelectionHandler.class, handler); } if (selectNext) { // select next element handler.selectNext(); } else { // select previous handler.selectPrevious(); } } } }
Example 3
Source File: ElementTreePanel.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
/** * Resets the JTextComponent to <code>editor</code>. This will update * the tree accordingly. */ public void setEditor(JTextComponent editor) { if (this.editor == editor) { return; } if (this.editor != null) { Document oldDoc = this.editor.getDocument(); oldDoc.removeDocumentListener(this); this.editor.removePropertyChangeListener(this); this.editor.removeCaretListener(this); } this.editor = editor; if (editor == null) { treeModel = null; tree.setModel(null); } else { Document newDoc = editor.getDocument(); newDoc.addDocumentListener(this); editor.addPropertyChangeListener(this); editor.addCaretListener(this); treeModel = new ElementTreeModel(newDoc); tree.setModel(treeModel); } }
Example 4
Source File: ElementTreePanel.java From hottub with GNU General Public License v2.0 | 5 votes |
/** * Resets the JTextComponent to <code>editor</code>. This will update * the tree accordingly. */ public void setEditor(JTextComponent editor) { if (this.editor == editor) { return; } if (this.editor != null) { Document oldDoc = this.editor.getDocument(); oldDoc.removeDocumentListener(this); this.editor.removePropertyChangeListener(this); this.editor.removeCaretListener(this); } this.editor = editor; if (editor == null) { treeModel = null; tree.setModel(null); } else { Document newDoc = editor.getDocument(); newDoc.addDocumentListener(this); editor.addPropertyChangeListener(this); editor.addCaretListener(this); treeModel = new ElementTreeModel(newDoc); tree.setModel(treeModel); } }
Example 5
Source File: CAccessible.java From hottub with GNU General Public License v2.0 | 5 votes |
public void addNotificationListeners(Component c) { if (c instanceof JTextComponent) { JTextComponent tc = (JTextComponent) c; AXTextChangeNotifier listener = new AXTextChangeNotifier(); tc.getDocument().addDocumentListener(listener); tc.addCaretListener(listener); } if (c instanceof JProgressBar) { JProgressBar pb = (JProgressBar) c; pb.addChangeListener(new AXProgressChangeNotifier()); } else if (c instanceof JSlider) { JSlider slider = (JSlider) c; slider.addChangeListener(new AXProgressChangeNotifier()); } }
Example 6
Source File: ElementTreePanel.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
/** * Resets the JTextComponent to <code>editor</code>. This will update * the tree accordingly. */ public void setEditor(JTextComponent editor) { if (this.editor == editor) { return; } if (this.editor != null) { Document oldDoc = this.editor.getDocument(); oldDoc.removeDocumentListener(this); this.editor.removePropertyChangeListener(this); this.editor.removeCaretListener(this); } this.editor = editor; if (editor == null) { treeModel = null; tree.setModel(null); } else { Document newDoc = editor.getDocument(); newDoc.addDocumentListener(this); editor.addPropertyChangeListener(this); editor.addCaretListener(this); treeModel = new ElementTreeModel(newDoc); tree.setModel(treeModel); } }
Example 7
Source File: ComponentPeer.java From netbeans with Apache License 2.0 | 5 votes |
/** Creates a new instance of ComponentPeer */ private ComponentPeer(JTextComponent pane) { this.pane = pane; // reschedule(); pane.addPropertyChangeListener(this); pane.addCaretListener(this); pane.addAncestorListener(this); document = pane.getDocument(); weakDocL = WeakListeners.document(this, document); document.addDocumentListener(weakDocL); ancestorAdded(null); }
Example 8
Source File: ElementTreePanel.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
/** * Resets the JTextComponent to <code>editor</code>. This will update * the tree accordingly. */ public void setEditor(JTextComponent editor) { if (this.editor == editor) { return; } if (this.editor != null) { Document oldDoc = this.editor.getDocument(); oldDoc.removeDocumentListener(this); this.editor.removePropertyChangeListener(this); this.editor.removeCaretListener(this); } this.editor = editor; if (editor == null) { treeModel = null; tree.setModel(null); } else { Document newDoc = editor.getDocument(); newDoc.addDocumentListener(this); editor.addPropertyChangeListener(this); editor.addCaretListener(this); treeModel = new ElementTreeModel(newDoc); tree.setModel(treeModel); } }
Example 9
Source File: ElementTreePanel.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
/** * Resets the JTextComponent to <code>editor</code>. This will update * the tree accordingly. */ public void setEditor(JTextComponent editor) { if (this.editor == editor) { return; } if (this.editor != null) { Document oldDoc = this.editor.getDocument(); oldDoc.removeDocumentListener(this); this.editor.removePropertyChangeListener(this); this.editor.removeCaretListener(this); } this.editor = editor; if (editor == null) { treeModel = null; tree.setModel(null); } else { Document newDoc = editor.getDocument(); newDoc.addDocumentListener(this); editor.addPropertyChangeListener(this); editor.addCaretListener(this); treeModel = new ElementTreeModel(newDoc); tree.setModel(treeModel); } }
Example 10
Source File: ElementTreePanel.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
/** * Resets the JTextComponent to <code>editor</code>. This will update * the tree accordingly. */ public void setEditor(JTextComponent editor) { if (this.editor == editor) { return; } if (this.editor != null) { Document oldDoc = this.editor.getDocument(); oldDoc.removeDocumentListener(this); this.editor.removePropertyChangeListener(this); this.editor.removeCaretListener(this); } this.editor = editor; if (editor == null) { treeModel = null; tree.setModel(null); } else { Document newDoc = editor.getDocument(); newDoc.addDocumentListener(this); editor.addPropertyChangeListener(this); editor.addCaretListener(this); treeModel = new ElementTreeModel(newDoc); tree.setModel(treeModel); } }
Example 11
Source File: CAccessible.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
public void addNotificationListeners(Component c) { if (c instanceof JTextComponent) { JTextComponent tc = (JTextComponent) c; AXTextChangeNotifier listener = new AXTextChangeNotifier(); tc.getDocument().addDocumentListener(listener); tc.addCaretListener(listener); } if (c instanceof JProgressBar) { JProgressBar pb = (JProgressBar) c; pb.addChangeListener(new AXProgressChangeNotifier()); } else if (c instanceof JSlider) { JSlider slider = (JSlider) c; slider.addChangeListener(new AXProgressChangeNotifier()); } }
Example 12
Source File: AbbrevDetection.java From netbeans with Apache License 2.0 | 5 votes |
private AbbrevDetection(JTextComponent component) { this.component = component; component.addCaretListener(this); doc = component.getDocument(); if (doc != null) { listenOnDoc(); } String mimeType = DocumentUtilities.getMimeType(component); if (mimeType != null) { mimePath = MimePath.parse(mimeType); prefs = MimeLookup.getLookup(mimePath).lookup(Preferences.class); prefs.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, this, prefs)); } // Load the settings preferenceChange(null); component.addKeyListener(this); component.addPropertyChangeListener(this); surroundsWithTimer = new Timer(0, new ActionListener() { public void actionPerformed(ActionEvent e) { // #124515, give up when the document is locked otherwise we are likely // to cause a deadlock. if (!DocumentUtilities.isReadLocked(doc)) { showSurroundWithHint(); } } }); surroundsWithTimer.setRepeats(false); }
Example 13
Source File: ElementTreePanel.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
/** * Resets the JTextComponent to <code>editor</code>. This will update * the tree accordingly. */ public void setEditor(JTextComponent editor) { if (this.editor == editor) { return; } if (this.editor != null) { Document oldDoc = this.editor.getDocument(); oldDoc.removeDocumentListener(this); this.editor.removePropertyChangeListener(this); this.editor.removeCaretListener(this); } this.editor = editor; if (editor == null) { treeModel = null; tree.setModel(null); } else { Document newDoc = editor.getDocument(); newDoc.addDocumentListener(this); editor.addPropertyChangeListener(this); editor.addCaretListener(this); treeModel = new ElementTreeModel(newDoc); tree.setModel(treeModel); } }
Example 14
Source File: ElementTreePanel.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Resets the JTextComponent to <code>editor</code>. This will update * the tree accordingly. */ public void setEditor(JTextComponent editor) { if (this.editor == editor) { return; } if (this.editor != null) { Document oldDoc = this.editor.getDocument(); oldDoc.removeDocumentListener(this); this.editor.removePropertyChangeListener(this); this.editor.removeCaretListener(this); } this.editor = editor; if (editor == null) { treeModel = null; tree.setModel(null); } else { Document newDoc = editor.getDocument(); newDoc.addDocumentListener(this); editor.addPropertyChangeListener(this); editor.addCaretListener(this); treeModel = new ElementTreeModel(newDoc); tree.setModel(treeModel); } }
Example 15
Source File: CAccessible.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
public void addNotificationListeners(Component c) { if (c instanceof JTextComponent) { JTextComponent tc = (JTextComponent) c; AXTextChangeNotifier listener = new AXTextChangeNotifier(); tc.getDocument().addDocumentListener(listener); tc.addCaretListener(listener); } if (c instanceof JProgressBar) { JProgressBar pb = (JProgressBar) c; pb.addChangeListener(new AXProgressChangeNotifier()); } else if (c instanceof JSlider) { JSlider slider = (JSlider) c; slider.addChangeListener(new AXProgressChangeNotifier()); } }
Example 16
Source File: ElementTreePanel.java From hottub with GNU General Public License v2.0 | 4 votes |
@SuppressWarnings("LeakingThisInConstructor") public ElementTreePanel(JTextComponent editor) { this.editor = editor; Document document = editor.getDocument(); // Create the tree. treeModel = new ElementTreeModel(document); tree = new JTree(treeModel) { @Override public String convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { // Should only happen for the root if (!(value instanceof Element)) { return value.toString(); } Element e = (Element) value; AttributeSet as = e.getAttributes().copyAttributes(); String asString; if (as != null) { StringBuilder retBuffer = new StringBuilder("["); Enumeration names = as.getAttributeNames(); while (names.hasMoreElements()) { Object nextName = names.nextElement(); if (nextName != StyleConstants.ResolveAttribute) { retBuffer.append(" "); retBuffer.append(nextName); retBuffer.append("="); retBuffer.append(as.getAttribute(nextName)); } } retBuffer.append(" ]"); asString = retBuffer.toString(); } else { asString = "[ ]"; } if (e.isLeaf()) { return e.getName() + " [" + e.getStartOffset() + ", " + e. getEndOffset() + "] Attributes: " + asString; } return e.getName() + " [" + e.getStartOffset() + ", " + e. getEndOffset() + "] Attributes: " + asString; } }; tree.addTreeSelectionListener(this); tree.setDragEnabled(true); // Don't show the root, it is fake. tree.setRootVisible(false); // Since the display value of every node after the insertion point // changes every time the text changes and we don't generate a change // event for all those nodes the display value can become off. // This can be seen as '...' instead of the complete string value. // This is a temporary workaround, increase the needed size by 15, // hoping that will be enough. tree.setCellRenderer(new DefaultTreeCellRenderer() { @Override public Dimension getPreferredSize() { Dimension retValue = super.getPreferredSize(); if (retValue != null) { retValue.width += 15; } return retValue; } }); // become a listener on the document to update the tree. document.addDocumentListener(this); // become a PropertyChangeListener to know when the Document has // changed. editor.addPropertyChangeListener(this); // Become a CaretListener editor.addCaretListener(this); // configure the panel and frame containing it. setLayout(new BorderLayout()); add(new JScrollPane(tree), BorderLayout.CENTER); // Add a label above tree to describe what is being shown JLabel label = new JLabel("Elements that make up the current document", SwingConstants.CENTER); label.setFont(new Font("Dialog", Font.BOLD, 14)); add(label, BorderLayout.NORTH); setPreferredSize(new Dimension(400, 400)); }
Example 17
Source File: CaretMarkProvider.java From netbeans with Apache License 2.0 | 4 votes |
/** Creates a new instance of AnnotationMarkProvider */ public CaretMarkProvider(JTextComponent component) { this.component = component; component.addCaretListener(this); marks = createMarks(); }
Example 18
Source File: ElementTreePanel.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
@SuppressWarnings("LeakingThisInConstructor") public ElementTreePanel(JTextComponent editor) { this.editor = editor; Document document = editor.getDocument(); // Create the tree. treeModel = new ElementTreeModel(document); tree = new JTree(treeModel) { @Override public String convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { // Should only happen for the root if (!(value instanceof Element)) { return value.toString(); } Element e = (Element) value; AttributeSet as = e.getAttributes().copyAttributes(); String asString; if (as != null) { StringBuilder retBuffer = new StringBuilder("["); Enumeration names = as.getAttributeNames(); while (names.hasMoreElements()) { Object nextName = names.nextElement(); if (nextName != StyleConstants.ResolveAttribute) { retBuffer.append(" "); retBuffer.append(nextName); retBuffer.append("="); retBuffer.append(as.getAttribute(nextName)); } } retBuffer.append(" ]"); asString = retBuffer.toString(); } else { asString = "[ ]"; } if (e.isLeaf()) { return e.getName() + " [" + e.getStartOffset() + ", " + e. getEndOffset() + "] Attributes: " + asString; } return e.getName() + " [" + e.getStartOffset() + ", " + e. getEndOffset() + "] Attributes: " + asString; } }; tree.addTreeSelectionListener(this); tree.setDragEnabled(true); // Don't show the root, it is fake. tree.setRootVisible(false); // Since the display value of every node after the insertion point // changes every time the text changes and we don't generate a change // event for all those nodes the display value can become off. // This can be seen as '...' instead of the complete string value. // This is a temporary workaround, increase the needed size by 15, // hoping that will be enough. tree.setCellRenderer(new DefaultTreeCellRenderer() { @Override public Dimension getPreferredSize() { Dimension retValue = super.getPreferredSize(); if (retValue != null) { retValue.width += 15; } return retValue; } }); // become a listener on the document to update the tree. document.addDocumentListener(this); // become a PropertyChangeListener to know when the Document has // changed. editor.addPropertyChangeListener(this); // Become a CaretListener editor.addCaretListener(this); // configure the panel and frame containing it. setLayout(new BorderLayout()); add(new JScrollPane(tree), BorderLayout.CENTER); // Add a label above tree to describe what is being shown JLabel label = new JLabel("Elements that make up the current document", SwingConstants.CENTER); label.setFont(new Font("Dialog", Font.BOLD, 14)); add(label, BorderLayout.NORTH); setPreferredSize(new Dimension(400, 400)); }
Example 19
Source File: ElementTreePanel.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
@SuppressWarnings("LeakingThisInConstructor") public ElementTreePanel(JTextComponent editor) { this.editor = editor; Document document = editor.getDocument(); // Create the tree. treeModel = new ElementTreeModel(document); tree = new JTree(treeModel) { @Override public String convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { // Should only happen for the root if (!(value instanceof Element)) { return value.toString(); } Element e = (Element) value; AttributeSet as = e.getAttributes().copyAttributes(); String asString; if (as != null) { StringBuilder retBuffer = new StringBuilder("["); Enumeration names = as.getAttributeNames(); while (names.hasMoreElements()) { Object nextName = names.nextElement(); if (nextName != StyleConstants.ResolveAttribute) { retBuffer.append(" "); retBuffer.append(nextName); retBuffer.append("="); retBuffer.append(as.getAttribute(nextName)); } } retBuffer.append(" ]"); asString = retBuffer.toString(); } else { asString = "[ ]"; } if (e.isLeaf()) { return e.getName() + " [" + e.getStartOffset() + ", " + e. getEndOffset() + "] Attributes: " + asString; } return e.getName() + " [" + e.getStartOffset() + ", " + e. getEndOffset() + "] Attributes: " + asString; } }; tree.addTreeSelectionListener(this); tree.setDragEnabled(true); // Don't show the root, it is fake. tree.setRootVisible(false); // Since the display value of every node after the insertion point // changes every time the text changes and we don't generate a change // event for all those nodes the display value can become off. // This can be seen as '...' instead of the complete string value. // This is a temporary workaround, increase the needed size by 15, // hoping that will be enough. tree.setCellRenderer(new DefaultTreeCellRenderer() { @Override public Dimension getPreferredSize() { Dimension retValue = super.getPreferredSize(); if (retValue != null) { retValue.width += 15; } return retValue; } }); // become a listener on the document to update the tree. document.addDocumentListener(this); // become a PropertyChangeListener to know when the Document has // changed. editor.addPropertyChangeListener(this); // Become a CaretListener editor.addCaretListener(this); // configure the panel and frame containing it. setLayout(new BorderLayout()); add(new JScrollPane(tree), BorderLayout.CENTER); // Add a label above tree to describe what is being shown JLabel label = new JLabel("Elements that make up the current document", SwingConstants.CENTER); label.setFont(new Font("Dialog", Font.BOLD, 14)); add(label, BorderLayout.NORTH); setPreferredSize(new Dimension(400, 400)); }
Example 20
Source File: ElementTreePanel.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
@SuppressWarnings("LeakingThisInConstructor") public ElementTreePanel(JTextComponent editor) { this.editor = editor; Document document = editor.getDocument(); // Create the tree. treeModel = new ElementTreeModel(document); tree = new JTree(treeModel) { @Override public String convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { // Should only happen for the root if (!(value instanceof Element)) { return value.toString(); } Element e = (Element) value; AttributeSet as = e.getAttributes().copyAttributes(); String asString; if (as != null) { StringBuilder retBuffer = new StringBuilder("["); Enumeration names = as.getAttributeNames(); while (names.hasMoreElements()) { Object nextName = names.nextElement(); if (nextName != StyleConstants.ResolveAttribute) { retBuffer.append(" "); retBuffer.append(nextName); retBuffer.append("="); retBuffer.append(as.getAttribute(nextName)); } } retBuffer.append(" ]"); asString = retBuffer.toString(); } else { asString = "[ ]"; } if (e.isLeaf()) { return e.getName() + " [" + e.getStartOffset() + ", " + e. getEndOffset() + "] Attributes: " + asString; } return e.getName() + " [" + e.getStartOffset() + ", " + e. getEndOffset() + "] Attributes: " + asString; } }; tree.addTreeSelectionListener(this); tree.setDragEnabled(true); // Don't show the root, it is fake. tree.setRootVisible(false); // Since the display value of every node after the insertion point // changes every time the text changes and we don't generate a change // event for all those nodes the display value can become off. // This can be seen as '...' instead of the complete string value. // This is a temporary workaround, increase the needed size by 15, // hoping that will be enough. tree.setCellRenderer(new DefaultTreeCellRenderer() { @Override public Dimension getPreferredSize() { Dimension retValue = super.getPreferredSize(); if (retValue != null) { retValue.width += 15; } return retValue; } }); // become a listener on the document to update the tree. document.addDocumentListener(this); // become a PropertyChangeListener to know when the Document has // changed. editor.addPropertyChangeListener(this); // Become a CaretListener editor.addCaretListener(this); // configure the panel and frame containing it. setLayout(new BorderLayout()); add(new JScrollPane(tree), BorderLayout.CENTER); // Add a label above tree to describe what is being shown JLabel label = new JLabel("Elements that make up the current document", SwingConstants.CENTER); label.setFont(new Font("Dialog", Font.BOLD, 14)); add(label, BorderLayout.NORTH); setPreferredSize(new Dimension(400, 400)); }