Java Code Examples for javax.swing.event.CaretEvent

The following examples show how to use javax.swing.event.CaretEvent. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
/**
 * After any user input, the value of the textfield is proofed. Depending on
 * being a valid date, the value is colored green or red.
 * 
 * @param event
 *            the caret event
 */
public void caretUpdate(CaretEvent event) {
	String text = getText().trim();
	String emptyMask = maskPattern.replace('#', placeholder);

	if (text.length() == 0 || text.equals(emptyMask)) {
		setForeground(Color.BLACK);
		return;
	}

	try {
		Date date = dateFormatter.parse(getText());
		if (dateUtil.checkDate(date)) {
			setForeground(darkGreen);
		} else {
			setForeground(Color.RED);
		}
	} catch (Exception e) {
		setForeground(Color.RED);
	}
}
 
Example 2
Source Project: MeteoInfo   Source File: JSpinField.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * After any user input, the value of the textfield is proofed. Depending on
 * being an integer, the value is colored green or red.
 * 
 * @param e
 *            the caret event
 */
public void caretUpdate(CaretEvent e) {
	try {
		int testValue = Integer.valueOf(textField.getText()).intValue();

		if ((testValue >= min) && (testValue <= max)) {
			textField.setForeground(darkGreen);
			setValue(testValue, false, true);
		} else {
			textField.setForeground(Color.red);
		}
	} catch (Exception ex) {
		if (ex instanceof NumberFormatException) {
			textField.setForeground(Color.red);
		}

		// Ignore all other exceptions, e.g. illegal state exception
	}

	textField.repaint();
}
 
Example 3
Source Project: netbeans   Source File: CaretMarkProvider.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void caretUpdate(CaretEvent e) {
    final List<Mark> old = getMarks();
    
    marks = createMarks();
    
    final List<Mark> nue = getMarks();
    
    //Do not fire this event under the document's write lock
    //may deadlock with other providers:
    RP.post(new Runnable() {
        @Override
        public void run() {
            firePropertyChange(PROP_MARKS, old, nue);
        }
    });
}
 
Example 4
Source Project: netbeans   Source File: ASTBrowserTopComponent.java    License: Apache License 2.0 6 votes vote down vote up
public void caretUpdate (CaretEvent e) {
    if (rootNode == null) return;
    ASTPath path = rootNode.findPath (e.getDot ());
    if (path == null) return;
    TreeNode tNode = (TreeNode) tree.getModel ().getRoot ();
    List<TreeNode> treePath = new ArrayList<TreeNode> ();
    Iterator it = path.listIterator ();
    if (!it.hasNext ()) return;
    it.next ();
    treePath.add (tNode);
    while (tNode instanceof TNode && it.hasNext ()) {
        tNode = ((TNode) tNode).getTreeNode (it.next ());
        if (tNode == null) throw new NullPointerException ();
        treePath.add (tNode);
    }
    TreePath treePath2 = new TreePath (treePath.toArray ());
    DefaultTreeModel model = new DefaultTreeModel ((TreeNode) tree.getModel ().getRoot ());
    tree.setModel (model);
    tree.setSelectionPath (treePath2);
    tree.scrollPathToVisible (treePath2);
}
 
Example 5
Source Project: netbeans   Source File: WikiEditPanel.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates new form WikiEditPanel
 */
public WikiEditPanel(String wikiLanguage, boolean editing, boolean switchable) {
    this.wikiLanguage = wikiLanguage;
    this.switchable = switchable;
    this.wikiFormatText = "";
    this.htmlFormatText = "";
    initComponents();
    pnlButtons.setVisible(switchable);
    textCode.getDocument().addDocumentListener(new RevalidatingListener());
    textPreview.getDocument().addDocumentListener(new RevalidatingListener());
    textCode.addCaretListener(new CaretListener() {
        @Override
        public void caretUpdate(CaretEvent e) {
            makeCaretVisible(textCode);
        }
    });
    textCode.getDocument().addDocumentListener(new EnablingListener());
    // A11Y - Issues 163597 and 163598
    UIUtils.fixFocusTraversalKeys(textCode);
    UIUtils.issue163946Hack(scrollCode);

    Spellchecker.register(textCode);
    textPreview.putClientProperty(JTextPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);

    setEditing(editing);
}
 
Example 6
public TableViewLayoutTest() {

        super("Code example for a TableView bug");
        setUndecorated(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        edit.setEditorKit(new CodeBugEditorKit());
        initCodeBug();
        this.getContentPane().add(new JScrollPane(edit));
        this.pack();
        this.setLocationRelativeTo(null);

        edit.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent e) {
                JTextComponent textComp = (JTextComponent) e.getSource();
                try {
                    Rectangle rect = textComp.getUI().modelToView(textComp, e.getDot());
                    yCaret = rect.getY();
                    xCaret = rect.getX();
                } catch (BadLocationException ex) {
                    throw new RuntimeException("Failed to get pixel position of caret", ex);
                }
            }
        });
    }
 
Example 7
@Override
public void caretUpdate(CaretEvent e)
{
    //  Get the line the caret is positioned on

    int caretPosition = component.getCaretPosition();
    Element root = component.getDocument().getDefaultRootElement();
    int currentLine = root.getElementIndex( caretPosition );

    //  Need to repaint so the correct line number can be highlighted

    if (lastLine != currentLine)
    {
        repaint();
        lastLine = currentLine;
    }
}
 
Example 8
Source Project: SikuliX1   Source File: EditorCurrentLineHighlighter.java    License: MIT License 5 votes vote down vote up
@Override
public void caretUpdate(CaretEvent evt) {
  JTextComponent comp = (JTextComponent) evt.getSource();
  if (comp != null) {
    if (comp.getSelectionStart() != comp.getSelectionEnd()) {
      // cancel line highlighting if selection exists
      removeLineHighlight(comp);
      comp.repaint();
      return;
    }
    int pos = comp.getCaretPosition();
    Element elem = Utilities.getParagraphElement(comp, pos);
    int start = elem.getStartOffset();
    int end = elem.getEndOffset();
    Document doc = comp.getDocument();
    Element root = doc.getDefaultRootElement();
    int line = root.getElementIndex(pos);
    Debug.log(5, "LineHighlight: Caret at " + pos + " line " + line + " for " + start + "-" + end);
    if (SikulixIDE.getStatusbar() != null) {
      SikulixIDE.getStatusbar().setCaretPosition(line + 1, pos - start + 1);
    }
    removeLineHighlight(comp);
    try {
      highlight = comp.getHighlighter().addHighlight(start, end, painter);
      comp.repaint();
    } catch (BadLocationException ex) {
      Debug.error(me + "Problem while highlighting line %d\n%s", pos, ex.getMessage());
    }
  }
}
 
Example 9
Source Project: FancyBing   Source File: Comment.java    License: GNU General Public License v3.0 5 votes vote down vote up
public Comment(Listener listener)
{
    m_listener = listener;
    m_textPane = new JTextPane();
    setFocusTraversalKeys(m_textPane);
    GuiUtil.addStyle(m_textPane, "marked", Color.white,
                     Color.decode("#38d878"), false);
    setPreferredSize();
    m_textPane.getDocument().addDocumentListener(this);
    CaretListener caretListener = new CaretListener()
        {
            public void caretUpdate(CaretEvent event)
            {
                if (m_listener == null)
                    return;
                m_listener.textSelected(m_textPane.getSelectedText());
            }
        };
    m_textPane.addCaretListener(caretListener);
    setViewportView(m_textPane);
    setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    if (Platform.isMac())
        m_normalFont = new Font("Lucida Grande", Font.PLAIN, 11);
    else
        m_normalFont = UIManager.getFont("TextPane.font");
    setMonoFont(false);
}
 
Example 10
@Override
		public void caretUpdate(CaretEvent e) {
//			if (justInserted) {
//				justInserted = false;
//			}
//			else {
//				timer.stop();
//			}
		}
 
Example 11
Source Project: netbeans   Source File: POMModelPanel.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void caretUpdate(CaretEvent e) {
    JTextComponent cc = currentComponent != null ? currentComponent.get() : null;
    if (e.getSource() != cc) {
        ((JTextComponent)e.getSource()).removeCaretListener(this);
        //just a double check we do't get a persistent leak here..
        return;
    }
    currentDot = e.getDot();
    caretTask.schedule(1000);
}
 
Example 12
Source Project: netbeans   Source File: SelectCodeElementAction.java    License: Apache License 2.0 5 votes vote down vote up
public void caretUpdate(CaretEvent e) {
    assert SwingUtilities.isEventDispatchThread();
    if (!ignoreNextCaretUpdate) {
        synchronized (this) {
            selectionInfos = null;
            selIndex = -1;
        }
    }
}
 
Example 13
public void caretUpdate(CaretEvent e) {
    JTextComponent component = componentRef == null ? null : componentRef.get();
    if (component == null) {
        return;
    }
    FileObject file = OpenedEditors.getFileObject(component);
    
    if (file != null) {
        setLastSelection(OpenedEditors.getFileObject(component), component.getSelectionStart(), component.getSelectionEnd());
        rescheduleTask.schedule(timeout);
    }
}
 
Example 14
Source Project: netbeans   Source File: CaretAwareJavaSourceTaskFactory.java    License: Apache License 2.0 5 votes vote down vote up
public void caretUpdate(CaretEvent e) {
    JTextComponent c = component.get();
    if (c == null) {
        return;
    }
    FileObject file = OpenedEditors.getFileObject(c);
    
    if (file != null) {
        setLastPosition(file, c.getCaretPosition());
        rescheduleTask.schedule(timeout);
    }
}
 
Example 15
Source Project: netbeans   Source File: ComponentPeer.java    License: Apache License 2.0 5 votes vote down vote up
public void caretUpdate(CaretEvent e) {
    synchronized (this) {
        lastCaretPosition = e.getDot();
    }
    
    LOG.fine("scheduling hints computation");
    
    computeHint.schedule(100);
}
 
Example 16
Source Project: netbeans   Source File: MarkOccurrencesSupport.java    License: Apache License 2.0 5 votes vote down vote up
public void caretUpdate (final CaretEvent e) {
    if (parsingTask != null) {
        parsingTask.cancel ();
    }
    parsingTask = PROC.post (new Runnable () {
        public void run () {
            refresh (e.getDot ());
        }
    }, 1000);
}
 
Example 17
Source Project: netbeans   Source File: LanguagesNavigator.java    License: Apache License 2.0 5 votes vote down vote up
public void caretUpdate (CaretEvent e) {
    LanguagesNavigatorModel model = (LanguagesNavigatorModel) tree.getModel ();
    TreePath treePath = model.getTreePath (e.getDot ());
    if (treePath == null) return;
    tree.setSelectionPath (treePath);
    tree.scrollPathToVisible (treePath);
}
 
Example 18
Source Project: netbeans   Source File: TokensBrowserTopComponent.java    License: Apache License 2.0 5 votes vote down vote up
public void caretUpdate (CaretEvent e) {
    int position = e.getDot ();
    try {
        selectPath (position);
    } catch (ConcurrentModificationException ex) {
    }
}
 
Example 19
Source Project: netbeans   Source File: SearchBar.java    License: Apache License 2.0 5 votes vote down vote up
private CaretListener createCaretListenerForComponent() {
    return new CaretListener() {

        @Override
        public void caretUpdate(CaretEvent e) {
            if (SearchBar.getInstance().isVisible()) {
                int num = SearchBar.getInstance().getNumOfMatches();
                SearchBar.getInstance().showNumberOfMatches(null, num);
            }
        }
    };
}
 
Example 20
Source Project: netbeans   Source File: SelectCodeElementAction.java    License: Apache License 2.0 5 votes vote down vote up
public @Override void caretUpdate(CaretEvent e) {
    if (!ignoreNextCaretUpdate) {
        synchronized (this) {
            selectionInfos = null;
            selIndex = -1;
        }
    }
    ignoreNextCaretUpdate = false;
}
 
Example 21
/**
 * Called when the caret position is updated.
 *
 * @param e the caret event
 */
public void caretUpdate( final CaretEvent e ) {
  if ( ignoreTextEvents ) {
    return;
  }

  editorModel.setCaretPosition( functionTextArea.getCaretPosition() );
  refreshInformationPanel();
  revalidateParameters( true );
}
 
Example 22
Source Project: netbeans   Source File: TaskPanel.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates new form TaskPanel
 */
public TaskPanel (LocalTask task) {
    this.task = task;
    initComponents();
    updateReadOnlyField(headerField);
    Font font = new JLabel().getFont();
    headerField.setFont(font.deriveFont((float) (font.getSize() * 1.7)));

    mainScrollPane.getVerticalScrollBar().setUnitIncrement(10);
    
    privateNotesField.addCaretListener(new CaretListener() {
        @Override
        public void caretUpdate (CaretEvent e) {
            makeCaretVisible(privateNotesField);
        }
    });
    // A11Y - Issues 163597 and 163598
    UIUtils.fixFocusTraversalKeys(privateNotesField);
    initSpellChecker();

    attachmentsPanel = new AttachmentsPanel(this);
    attachmentsSection.setContent(attachmentsPanel);
    
    GroupLayout layout = (GroupLayout) attributesPanel.getLayout();
    dueDatePicker = UIUtils.createDatePickerComponent();
    scheduleDatePicker = new SchedulePicker();
    layout.replace(dummyDueDateField, dueDatePicker.getComponent());
    dueDateLabel.setLabelFor(dueDatePicker.getComponent());
    layout.replace(dummyScheduleDateField, scheduleDatePicker.getComponent());
    scheduleDateLabel.setLabelFor(scheduleDatePicker.getComponent());
    attachListeners();
}
 
Example 23
Source Project: netbeans   Source File: EventSupport.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void caretUpdate(final CaretEvent event) {
    final JTextComponent lastEditor = lastEditorRef == null ? null : lastEditorRef.get();
    if (lastEditor != null) {
        Document doc = lastEditor.getDocument ();
        String mimeType = DocumentUtilities.getMimeType (doc);
        if (doc != null && mimeType != null) {
            Source source = Source.create(doc);
            if (source != null) {
                ((EventSupport)SourceEnvironment.forSource(source)).resetState(false, false, -1, -1, false);
            }
        }
    }
}
 
Example 24
Source Project: netbeans   Source File: AbbrevDetection.java    License: Apache License 2.0 5 votes vote down vote up
public void caretUpdate(CaretEvent evt) {
    if (evt.getDot() != evt.getMark()) {
        surroundsWithTimer.setInitialDelay(SURROUND_WITH_DELAY);
        surroundsWithTimer.restart();
    } else {
        surroundsWithTimer.stop();
        hideSurroundWithHint();
    }
}
 
Example 25
Source Project: chipster   Source File: ToolsInternalFrame.java    License: MIT License 5 votes vote down vote up
public void caretUpdate(CaretEvent e) {
	Object source = e.getSource();
	
	// Custom delimeter field
	if (source == customDelimField) {
		if (customDelimRadioButton.isSelected()) {
			String customDelim = customDelimField.getText();
			if (customDelim != null && customDelim.length() > 0) {
				this.setDelim(customDelim);
			} else {
				customDelimField.setForeground(Color.BLACK);
			}	
		}
	}
}
 
Example 26
Source Project: visualvm   Source File: TokenMarker.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void caretUpdate(CaretEvent e) {
    int pos = e.getDot();
    SyntaxDocument doc = ActionUtils.getSyntaxDocument(pane);
    Token token = doc.getTokenAt(pos);
    removeMarkers();
    if (token != null && tokenTypes.contains(token.type)) {
        addMarkers(token);
    }
}
 
Example 27
Source Project: visualvm   Source File: PairsMarker.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void caretUpdate(CaretEvent e) {
    removeMarkers();
    int pos = e.getDot();
    SyntaxDocument doc = ActionUtils.getSyntaxDocument(pane);
    Token token = doc.getTokenAt(pos);
    if (token != null && token.pairValue != 0) {
        Markers.markToken(pane, token, marker);
        Token other = doc.getPairFor(token);
        if (other != null) {
            Markers.markToken(pane, other, marker);
        }
    }
}
 
Example 28
Source Project: pumpernickel   Source File: TextComponentHighlighter.java    License: MIT License 5 votes vote down vote up
@Override
public void caretUpdate(CaretEvent e) {
	// see notes in the similar docListener
	synchronized (caretListener) {
		dirty = true;
		SwingUtilities.invokeLater(rehighlightRunnable);
	}
}
 
Example 29
Source Project: jpexs-decompiler   Source File: PairsMarker.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void caretUpdate(CaretEvent e) {
    removeMarkers();
    int pos = e.getDot();
    SyntaxDocument doc = ActionUtils.getSyntaxDocument(pane);
    Token token = doc.getTokenAt(pos);
    if (token != null && token.pairValue != 0) {
        Markers.markToken(pane, token, marker);
        Token other = doc.getPairFor(token);
        if (other != null) {
            Markers.markToken(pane, other, marker);
        }
    }
}
 
Example 30
Source Project: Digital   Source File: TextLineNumber.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void caretUpdate(CaretEvent e) {
    //  Get the line the caret is positioned on

    int caretPosition = component.getCaretPosition();
    Element root = component.getDocument().getDefaultRootElement();
    int currentLine = root.getElementIndex(caretPosition);

    //  Need to repaint so the correct line number can be highlighted

    if (lastLine != currentLine) {
        repaint();
        lastLine = currentLine;
    }
}