Example usage for javax.swing JTextPane getSelectionEnd

List of usage examples for javax.swing JTextPane getSelectionEnd

Introduction

In this page you can find the example usage for javax.swing JTextPane getSelectionEnd.

Prototype

@Transient
public int getSelectionEnd() 

Source Link

Document

Returns the selected text's end position.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JTextPane textPane = new JTextPane();

    JButton button = new JButton("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel.setLayout(new BorderLayout());
    panel.setPreferredSize(new Dimension(200, 200));

    panel.add(textPane, BorderLayout.CENTER);
    panel.add(button, BorderLayout.SOUTH);
    textPane.addStyle("myStyle", null);

    button.addActionListener(e -> {/*from  w w w. ja  v  a2s  .  c  o  m*/
        StyledDocument doc = textPane.getStyledDocument();
        int start = textPane.getSelectionStart();
        int end = textPane.getSelectionEnd();
        if (start == end) {
            return;
        }
        if (start > end) {
            int life = start;
            start = end;
            end = life;
        }
        Style style = textPane.getStyle("myStyle");

        if (StyleConstants.isBold(style)) {
            StyleConstants.setBold(style, false);
        } else {
            StyleConstants.setBold(style, true);
        }
        doc.setCharacterAttributes(start, end - start, style, false);
    });

    frame.add(panel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:DocumentModel.java

public static void main(String[] args) {
    final StyledDocument doc;
    final JTextPane textpane;

    JFrame f = new JFrame();

    f.setTitle("Document Model");

    JToolBar toolbar = new JToolBar();
    JButton boldb = new JButton("bold");
    JButton italb = new JButton("italic");
    JButton strib = new JButton("strike");
    JButton undeb = new JButton("underline");

    toolbar.add(boldb);//  w w  w .  j  av a 2 s  . c o m
    toolbar.add(italb);
    toolbar.add(strib);
    toolbar.add(undeb);

    f.add(toolbar, BorderLayout.NORTH);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    JScrollPane pane = new JScrollPane();
    textpane = new JTextPane();
    textpane.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));

    doc = textpane.getStyledDocument();

    Style style = textpane.addStyle("Bold", null);
    StyleConstants.setBold(style, true);

    style = textpane.addStyle("Italic", null);
    StyleConstants.setItalic(style, true);

    style = textpane.addStyle("Underline", null);
    StyleConstants.setUnderline(style, true);

    style = textpane.addStyle("Strike", null);
    StyleConstants.setStrikeThrough(style, true);

    boldb.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doc.setCharacterAttributes(textpane.getSelectionStart(),
                    textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Bold"),
                    false);
        }
    });

    italb.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doc.setCharacterAttributes(textpane.getSelectionStart(),
                    textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Italic"),
                    false);
        }

    });

    strib.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doc.setCharacterAttributes(textpane.getSelectionStart(),
                    textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Strike"),
                    false);
        }

    });

    undeb.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doc.setCharacterAttributes(textpane.getSelectionStart(),
                    textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Underline"),
                    false);
        }
    });

    pane.getViewport().add(textpane);
    panel.add(pane);

    f.add(panel);

    f.setSize(new Dimension(380, 320));
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

}

From source file:com.hexidec.ekit.component.HTMLUtilities.java

public void delete() throws BadLocationException, IOException {
    JTextPane jtpMain = parent.getTextPane();
    JTextArea jtpSource = parent.getSourcePane();
    ExtendedHTMLDocument htmlDoc = parent.getExtendedHtmlDoc();
    int selStart = jtpMain.getSelectionStart();
    int selEnd = jtpMain.getSelectionEnd();
    String[] posStrings = getUniString(2);
    if (posStrings == null) {
        return;//  ww w. j a va  2  s . c om
    }
    htmlDoc.insertString(selStart, posStrings[0], null);
    htmlDoc.insertString(selEnd + posStrings[0].length(), posStrings[1], null);
    parent.refreshOnUpdate();
    int start = jtpSource.getText().indexOf(posStrings[0]);
    int end = jtpSource.getText().indexOf(posStrings[1]);
    if (start == -1 || end == -1) {
        return;
    }
    String htmlString = new String();
    htmlString += jtpSource.getText().substring(0, start);
    htmlString += jtpSource.getText().substring(start + posStrings[0].length(), end);
    htmlString += jtpSource.getText().substring(end + posStrings[1].length(), jtpSource.getText().length());
    String source = htmlString;
    end = end - posStrings[0].length();
    htmlString = new String();
    htmlString += source.substring(0, start);
    htmlString += getAllTableTags(source.substring(start, end));
    htmlString += source.substring(end, source.length());
    parent.getTextPane().setText(htmlString);
    parent.refreshOnUpdate();
}

From source file:com.hp.alm.ali.idea.ui.editor.field.HTMLAreaField.java

public static void enableCapability(final JTextPane desc, Project project, String value, boolean editable,
        boolean navigation) {
    value = removeSmallFont(value);//  www.ja va  2s. com
    HTMLEditorKit kit = new HTMLLetterWrappingEditorKit();
    desc.setEditorKit(kit);
    desc.setDocument(kit.createDefaultDocument());
    if (!editable && navigation) {
        value = NavigationDecorator.explodeHtml(project, value);
    }
    desc.setText(value);
    if (!editable) {
        desc.setCaret(new NonAdjustingCaret());
    }
    desc.addCaretListener(new BodyLimitCaretListener(desc));
    if (editable) {
        String element = checkElements(desc.getDocument().getDefaultRootElement());
        if (element != null) {
            desc.setToolTipText("Found unsupported element '" + element + "', editing is disabled.");
            editable = false;
        }
    }
    desc.setEditable(editable);

    if (editable && SpellCheckerManager.isAvailable()
            && ApplicationManager.getApplication().getComponent(AliConfiguration.class).spellChecker) {
        desc.getDocument().addDocumentListener(new SpellCheckDocumentListener(project, desc));
    }

    Font font = UIManager.getFont("Label.font");
    String bodyRule = "body { font-family: " + font.getFamily() + "; " + "font-size: " + font.getSize()
            + "pt; }";
    ((HTMLDocument) desc.getDocument()).getStyleSheet().addRule(bodyRule);

    // AGM uses plain "p" to create lines, we need to avoid excessive spacing this by default creates
    String paragraphRule = "p { margin-top: 0px; }";
    ((HTMLDocument) desc.getDocument()).getStyleSheet().addRule(paragraphRule);

    Keymap keymap = KeymapManager.getInstance().getActiveKeymap();
    new AnAction() {
        public void actionPerformed(AnActionEvent e) {
            // following is needed to make copy work in the IDE
            try {
                StringSelection selection = new StringSelection(desc.getText(desc.getSelectionStart(),
                        desc.getSelectionEnd() - desc.getSelectionStart()));
                CopyPasteManager.getInstance().setContents(selection);
            } catch (Exception ex) {
                // no clipboard, so what
            }
        }
    }.registerCustomShortcutSet(new CustomShortcutSet(keymap.getShortcuts(IdeActions.ACTION_COPY)), desc);
    new AnAction() {
        public void actionPerformed(AnActionEvent e) {
            // avoid pasting non-supported HTML markup by always converting to plain text
            Transferable contents = CopyPasteManager.getInstance().getContents();
            try {
                desc.getActionMap().get(DefaultEditorKit.cutAction).actionPerformed(null);
                desc.getDocument().insertString(desc.getSelectionStart(),
                        (String) contents.getTransferData(DataFlavor.stringFlavor), null);
            } catch (Exception ex) {
                // no clipboard, so what
            }
        }
    }.registerCustomShortcutSet(new CustomShortcutSet(keymap.getShortcuts(IdeActions.ACTION_PASTE)), desc);
    installNavigationShortCuts(desc);

}