Example usage for javax.swing.text Utilities getWordStart

List of usage examples for javax.swing.text Utilities getWordStart

Introduction

In this page you can find the example usage for javax.swing.text Utilities getWordStart.

Prototype

public static final int getWordStart(JTextComponent c, int offs) throws BadLocationException 

Source Link

Document

Determines the start of a word for the given model location.

Usage

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/*  w w  w.j  a va  2s.c  om*/
        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:Main.java

public void actionPerformed(ActionEvent e) {
    JTextComponent comp = getTextComponent(e);
    if (comp == null)
        return;//from  w  w w  .  ja va2 s . c  o 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 (Exception ble) {
        return;
    }
}

From source file:Main.java

public void actionPerformed(ActionEvent e) {
    JTextComponent comp = getTextComponent(e);
    if (comp == null)
        return;/* ww w. ja v a  2  s  . c  o 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;
    }
}