Example usage for javax.swing.event CaretEvent getDot

List of usage examples for javax.swing.event CaretEvent getDot

Introduction

In this page you can find the example usage for javax.swing.event CaretEvent getDot.

Prototype

public abstract int getDot();

Source Link

Document

Fetches the location of the caret.

Usage

From source file:Main.java

public static void main(String[] argv) {
    JTextComponent textComp = new JTextArea();
    textComp.addCaretListener(new CaretListener() {
        public void caretUpdate(CaretEvent e) {

            int dot = e.getDot();
            System.out.println("dot is the caret position:" + dot);

            int mark = e.getMark();
            System.out.println("mark is the non-caret end of the selection: " + mark);
        }//from  ww w . ja  v  a  2 s . c om
    });
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Caret Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    frame.add(scrollPane, BorderLayout.CENTER);

    CaretListener listener = new CaretListener() {
        public void caretUpdate(CaretEvent caretEvent) {
            System.out.println("dot:" + caretEvent.getDot());
            System.out.println("mark" + caretEvent.getMark());
        }//from   w w  w . j ava  2 s  .c o m
    };

    textArea.addCaretListener(listener);

    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Caret Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    frame.add(scrollPane, BorderLayout.CENTER);

    CaretListener listener = new CaretListener() {
        public void caretUpdate(CaretEvent caretEvent) {
            System.out.println("Dot: " + caretEvent.getDot());
            System.out.println("Mark: " + caretEvent.getMark());
        }//w w w  . j  a  va 2 s. co m
    };

    textArea.addCaretListener(listener);

    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:CaretSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Caret Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = frame.getContentPane();
    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    content.add(scrollPane, BorderLayout.CENTER);

    final JTextField dot = new JTextField();
    dot.setEditable(false);/* w  w w. j a  v a2 s  .  c  om*/
    JPanel dotPanel = new JPanel(new BorderLayout());
    dotPanel.add(new JLabel("Dot: "), BorderLayout.WEST);
    dotPanel.add(dot, BorderLayout.CENTER);
    content.add(dotPanel, BorderLayout.NORTH);

    final JTextField mark = new JTextField();
    mark.setEditable(false);
    JPanel markPanel = new JPanel(new BorderLayout());
    markPanel.add(new JLabel("Mark: "), BorderLayout.WEST);
    markPanel.add(mark, BorderLayout.CENTER);
    content.add(markPanel, BorderLayout.SOUTH);

    CaretListener listener = new CaretListener() {
        public void caretUpdate(CaretEvent caretEvent) {
            dot.setText("" + caretEvent.getDot());
            mark.setText("" + caretEvent.getMark());
        }
    };

    textArea.addCaretListener(listener);

    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:de.codesourcery.jasm16.ide.ui.views.SourceEditorView.java

protected void onCaretUpdate(CaretEvent e) {
    // if AST inspector is visible, make sure the current AST node is visible
    // (scroll there if it isn't)
    if (!isASTInspectorVisible()) {
        return;//w ww . j av a2 s . c o  m
    }

    final AST ast = getCurrentCompilationUnit() != null ? getCurrentCompilationUnit().getAST() : null;
    if (ast == null) {
        return;
    }

    final ASTNode n = ast.getNodeInRange(e.getDot());
    if (n != null) {
        TreePath path = new TreePath(n.getPathToRoot());
        astTree.setSelectionPath(path);
        astTree.scrollPathToVisible(path);
    }
}

From source file:com.hexidec.ekit.EkitCore.java

/**
 * Handles caret tracking and related events, such as displaying the current
 * style of the text under the caret/*  w w  w . j av a  2  s  . c o m*/
 */
private void handleCaretPositionChange(CaretEvent ce) {
    int caretPos = ce.getDot();
    Element element = htmlDoc.getCharacterElement(caretPos);
    /*
     * //---- TAG EXPLICATOR CODE
     * -------------------------------------------
     * javax.swing.text.ElementIterator ei = new
     * javax.swing.text.ElementIterator(htmlDoc); Element ele; while((ele =
     * ei.next()) != null) { log.debug("ELEMENT : " + ele.getName()); }
     * log.debug("ELEMENT:" + element.getName()); Element elementParent =
     * element.getParentElement(); log.debug("ATTRS:"); AttributeSet attribs
     * = elementParent.getAttributes(); for(Enumeration eAttrs =
     * attribs.getAttributeNames(); eAttrs.hasMoreElements();) {
     * log.debug("  " + eAttrs.nextElement().toString()); }
     * while(elementParent != null &&
     * !elementParent.getName().equals("body")) { String parentName =
     * elementParent.getName(); log.debug("PARENT:" + parentName);
     * log.debug("ATTRS:"); attribs = elementParent.getAttributes();
     * for(Enumeration eAttr = attribs.getAttributeNames();
     * eAttr.hasMoreElements();) { log.debug("  " +
     * eAttr.nextElement().toString()); } elementParent =
     * elementParent.getParentElement(); } //---- END
     * -------------------------------------------
     */
    if (jtpMain.hasFocus()) {
        if (element == null) {
            return;
        }
        String style = null;
        Vector<Element> vcStyles = new Vector<Element>();
        while (element != null) {
            if (style == null) {
                style = findStyle(element);
            }
            vcStyles.add(element);
            element = element.getParentElement();
        }
        int stylefound = -1;
        if (style != null) {
            for (int i = 0; i < jcmbStyleSelector.getItemCount(); i++) {
                String in = (String) (jcmbStyleSelector.getItemAt(i));
                if (in.equalsIgnoreCase(style)) {
                    stylefound = i;
                    break;
                }
            }
        }
        if (stylefound > -1) {
            jcmbStyleSelector.getAction().setEnabled(false);
            jcmbStyleSelector.setSelectedIndex(stylefound);
            jcmbStyleSelector.getAction().setEnabled(true);
        } else {
            jcmbStyleSelector.setSelectedIndex(0);
        }
        // see if current font face is set
        if (jcmbFontSelector != null && jcmbFontSelector.isVisible()) {
            AttributeSet mainAttrs = jtpMain.getCharacterAttributes();
            Enumeration e = mainAttrs.getAttributeNames();
            Object activeFontName = (Object) (Translatrix.getTranslationString("SelectorToolFontsDefaultFont"));
            while (e.hasMoreElements()) {
                Object nexte = e.nextElement();
                if (nexte.toString().toLowerCase().equals("face")
                        || nexte.toString().toLowerCase().equals("font-family")) {
                    activeFontName = mainAttrs.getAttribute(nexte);
                    break;
                }
            }
            jcmbFontSelector.getAction().setEnabled(false);
            jcmbFontSelector.getModel().setSelectedItem(activeFontName);
            jcmbFontSelector.getAction().setEnabled(true);
        }
    }
}

From source file:net.team2xh.crt.gui.editor.EditorTextPane.java

public EditorTextPane() {

    // Initialize highlighters
    lh = new LineHighlighter(Theme.getTheme().COLOR_13);
    ll = new LineHighlighter(Theme.getTheme().COLOR_14);
    wh = new WordHighlighter(Theme.getTheme().COLOR_11);
    eh = new ErrorHighlighter(Color.RED);

    // Initialise colors
    initAttributeSets();/*from   www.  ja v  a2  s .  c  o  m*/

    setOpaque(false); // Background will be drawn later on
    setFont(font);

    doc = (DefaultStyledDocument) getDocument();

    // Replace all tabs with four spaces
    // TODO: tab to next multiple of 4 column
    // TODO: tab whole selection
    // TODO: insert matching brace
    doc.setDocumentFilter(new DocumentFilter() {
        private String process(int offset, String text) {
            return text.replaceAll("\t", "    ").replaceAll("\r", "");
        }

        @Override
        public void insertString(FilterBypass fb, int offset, String text, AttributeSet attr)
                throws BadLocationException {
            super.insertString(fb, offset, process(offset, text), attr);
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attr)
                throws BadLocationException {
            super.replace(fb, offset, length, process(offset, text), attr);
        }
    });

    // Highlight text when text changes
    doc.addDocumentListener(new DocumentListener() {

        @Override
        public void removeUpdate(DocumentEvent e) {
            SwingUtilities.invokeLater(() -> highlightText());
            changed = true;
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            SwingUtilities.invokeLater(() -> highlightText());
            changed = true;
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
        }
    });

    addCaretListener(new CaretListener() {
        private boolean isAlphanum(char c) {
            return Character.isDigit(c) || Character.isLetter(c);
        }

        private boolean isAlphanum(String s) {
            for (char c : s.toCharArray()) // Allow dots in middle of words for floats
            {
                if (c != '.' && !isAlphanum(c)) {
                    return false;
                }
            }

            return true;
        }

        @Override
        public void caretUpdate(CaretEvent ce) {
            try {

                // Highlight current line
                highlightCurrentLine();

                // Clear previously highlighted occurrences
                for (Object o : occurrences) {
                    getHighlighter().removeHighlight(o);
                }
                repaint();
                occurrences.clear();

                // Get start and end offsets, swap them if necessary
                int s = ce.getDot();
                int e = ce.getMark();

                if (s > e) {
                    s = s + e;
                    e = s - e;
                    s = s - e;
                }

                // If there is a selection,
                if (s != e) {
                    // Check if the char on the left and on the right are not alphanums
                    char f = s == 0 ? ' ' : doc.getText(s - 1, 1).charAt(0);
                    char l = s == doc.getLength() - 1 ? ' ' : doc.getText(e, 1).charAt(0);
                    if (!isAlphanum(f) && !isAlphanum(l)) {
                        String word = doc.getText(s, e - s);
                        if (isAlphanum(word)) {
                            highlightOccurrences(word, s, e);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    setCaretColor(Theme.getTheme().COLOR_11);

    highlightText();
}

From source file:plugin.notes.gui.NotesView.java

private void editorCaretUpdate(CaretEvent evt) {
    //GEN-FIRST:event_editorCaretUpdate
    int dot = evt.getDot();
    updateButtons(editor, dot);//from   w  w w  . j ava  2s .  c  om

    Object obj = notesTree.getLastSelectedPathComponent();

    if (obj instanceof NotesTreeNode) {
        NotesTreeNode node = (NotesTreeNode) obj;

        if (node.isDirty()) {
            revertButton.setEnabled(true);
        } else {
            revertButton.setEnabled(false);
        }
    }
}