Java JTextComponent Select toggleSelectionUpperCase(JTextComponent textPane)

Here you can find the source of toggleSelectionUpperCase(JTextComponent textPane)

Description

toggle Selection Upper Case

License

Apache License

Declaration

public static void toggleSelectionUpperCase(JTextComponent textPane) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.swing.text.JTextComponent;

public class Main {
    public static void toggleSelectionUpperCase(JTextComponent textPane) {
        if (isSelectionUpperCase(textPane)) {
            selectionToLowerCase(textPane);
        } else {/*from  w  ww  .j a  va  2s  . c  o  m*/
            selectionToUpperCase(textPane);
        }
    }

    public static boolean isSelectionUpperCase(JTextComponent textPane) {
        String text = textPane.getSelectedText();
        byte[] bytes = text.getBytes();

        for (int i = 0; i < bytes.length; i++) {
            if (Character.isLowerCase((char) bytes[i])) {
                return false;
            }
        }

        return true;
    }

    public static void selectionToLowerCase(JTextComponent textPane) {
        int start = textPane.getSelectionStart();
        int end = textPane.getSelectionEnd();

        if (Math.abs(start - end) > 0) {
            String lower = textPane.getSelectedText().toLowerCase();
            textPane.replaceSelection(lower);

            textPane.setCaretPosition(start);
            textPane.moveCaretPosition(end);
        }
    }

    public static void selectionToUpperCase(JTextComponent textPane) {
        int start = textPane.getSelectionStart();
        int end = textPane.getSelectionEnd();

        if (Math.abs(start - end) > 0) {
            String upper = textPane.getSelectedText().toUpperCase();
            textPane.replaceSelection(upper);

            textPane.setCaretPosition(start);
            textPane.moveCaretPosition(end);
        }
    }
}

Related

  1. selectAllOnFocus(JTextComponent textComponent)
  2. selectAndRequestFocus(JTextComponent editor)
  3. selectFirstArg(String strText, int initialSelectionStart, JTextComponent editor)
  4. selectionToUpperCase(JTextComponent textPane)
  5. selectLines(JTextComponent target)
  6. useSelectedTextColor(Highlighter.Highlight h, JTextComponent c)