/* * Character_Info.bsh -- Shows you the codepoint, hexvalue and classification * of the characters selected in the textarea. Takes a long time if the * selection is large. * * @author Delvin Johnson (delvinj@gmail.com) * @version 1.0 */ import javax.swing.table.*; void getCharacterInfo() { String text = textArea.getSelectedText(); if(text == null) return; DefaultTableModel model = new DefaultTableModel(); model.setColumnCount(4); HashSet set = new HashSet(); char[] chars = text.toCharArray(); for(int j=0, rows=chars.length; j < rows; j++) { int cp = Character.codePointAt(chars, j); int type = Character.getType(cp); if(!set.add(Integer.valueOf(cp))) continue; String typeStr; switch(type) { case Character.UNASSIGNED : typeStr = "UNASSIGNED"; break; case Character.UPPERCASE_LETTER : typeStr = "UPPERCASE LETTER"; break; case Character.LOWERCASE_LETTER : typeStr = "LOWERCASE LETTER"; break; case Character.TITLECASE_LETTER : typeStr = "TITLECASE LETTER"; break; case Character.MODIFIER_LETTER : typeStr = "MODIFIER LETTER"; break; case Character.OTHER_LETTER : typeStr = "OTHER LETTER"; break; case Character.NON_SPACING_MARK : typeStr = "NON SPACING MARK"; break; case Character.ENCLOSING_MARK : typeStr = "ENCLOSING MARK"; break; case Character.COMBINING_SPACING_MARK : typeStr = "COMBINING SPACING MARK"; break; case Character.DECIMAL_DIGIT_NUMBER : typeStr = "DECIMAL DIGIT NUMBER"; break; case Character.LETTER_NUMBER : typeStr = "LETTER NUMBER"; break; case Character.OTHER_NUMBER : typeStr = "OTHER NUMBER"; break; case Character.SPACE_SEPARATOR : typeStr = "SPACE SEPARATOR"; break; case Character.LINE_SEPARATOR : typeStr = "LINE SEPARATOR"; break; case Character.PARAGRAPH_SEPARATOR : typeStr = "PARAGRAPH SEPARATOR"; break; case Character.CONTROL : typeStr = "CONTROL"; break; case Character.FORMAT : typeStr = "FORMAT"; break; case Character.PRIVATE_USE : typeStr = "PRIVATE USE"; break; case Character.SURROGATE : typeStr = "SURROGATE"; break; case Character.DASH_PUNCTUATION : typeStr = "DASH PUNCTUATION"; break; case Character.START_PUNCTUATION : typeStr = "START PUNCTUATION"; break; case Character.END_PUNCTUATION : typeStr = "END PUNCTUATION"; break; case Character.CONNECTOR_PUNCTUATION : typeStr = "CONNECTOR PUNCTUATION"; break; case Character.OTHER_PUNCTUATION : typeStr = "OTHER PUNCTUATION"; break; case Character.MATH_SYMBOL : typeStr = "MATH SYMBOL"; break; case Character.CURRENCY_SYMBOL : typeStr = "CURRENCY SYMBOL"; break; case Character.MODIFIER_SYMBOL : typeStr = "MODIFIER SYMBOL"; break; case Character.OTHER_SYMBOL : typeStr = "OTHER SYMBOL"; break; case Character.INITIAL_QUOTE_PUNCTUATION : typeStr = "INITIAL QUOTE PUNCTUATION"; break; case Character.FINAL_QUOTE_PUNCTUATION : typeStr = "FINAL QUOTE PUNCTUATION"; break; default: typeStr = "Foo"; } model.addRow(new Object[] { Character.toString(chars[j]), Integer.toString(cp), "0x" + (Integer.toHexString(cp).toUpperCase()), typeStr }); } model.setColumnIdentifiers(new String[] { "Value", "Codepoint", "Hex", "Type" }); JTable table = new JTable(); table.setModel(model); table.setCellSelectionEnabled(true); table.getColumnModel().getColumn(0).setPreferredWidth(40); table.getColumnModel().getColumn(1).setPreferredWidth(70); table.getColumnModel().getColumn(2).setPreferredWidth(50); table.getColumnModel().getColumn(3).setPreferredWidth(200); JDialog dialog = new JDialog(view, "Dialog", false); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); KeyStroke esc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); Action action = new AbstractAction() { public void actionPerformed(ActionEvent e) { dialog.setVisible(false); dialog.dispose(); }; }; Object QUIT = "QUIT"; table.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(esc, QUIT); table.getActionMap().put(QUIT, action); JScrollPane sp = new JScrollPane(table); sp.setPreferredSize(new Dimension(420, 300)); JPanel panel = new JPanel(new BorderLayout(8,8)); panel.setBorder(BorderFactory.createEmptyBorder(6,6,6,6)); panel.add(sp, BorderLayout.CENTER); dialog.setContentPane(panel); dialog.pack(); dialog.setLocationRelativeTo(null); dialog.setVisible(true); } getCharacterInfo();