1 package snow.sortabletable; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import javax.swing.event.*; 7 import javax.swing.table.*; 8 import java.nio.charset.*; 9 import java.nio.*; 10 import java.util.*; 11 import java.io.*; 12 13 18 19 public final class UnicodeViewer extends JFrame 20 { 21 final private UnicodeTableModel basicTableModel = new UnicodeTableModel(); 22 final private SortableTableModel stm = new SortableTableModel(basicTableModel, 1, true); 23 final private JTable table = new JTable(stm); 24 25 private final JComboBox charsetCB; 26 private final JComboBox displayLimitCB = new JComboBox(new String []{"32768", "256", "1024", "2048", "4096", "8192", "16384", "32768", "65536"}); 27 private int charDisplayLimit = 8192; 28 StringBuffer allChars = new StringBuffer (); 29 final private JDialog previewDialog; 30 final private JTextField previewLabel = new JTextField( 20 ); 31 32 public UnicodeViewer() 33 { 34 super(""); 35 37 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 38 stm.installGUI(table); 39 getContentPane().add(new JScrollPane(table), BorderLayout.CENTER); 40 41 table.setDefaultRenderer(Object .class, new UniversalTableCellRenderer(stm)); 42 table.setDefaultRenderer(Integer .class, new UniversalTableCellRenderer(stm)); 43 44 table.setFont(new Font("Dialog", Font.PLAIN, 16)); 45 table.setRowHeight(21); 46 47 AdvancedSearchPanel searchPanel = new AdvancedSearchPanel("Search: ", null, stm, true); 48 searchPanel.setAdvancedMode(true); 49 getContentPane().add(searchPanel, BorderLayout.NORTH); 50 51 charsetCB = new JComboBox(collect_limited_Charsets()); 53 searchPanel.add(charsetCB); 54 charsetCB.setMaximumRowCount(30); 55 charsetCB.addActionListener(new ActionListener() 56 { 57 public void actionPerformed(ActionEvent ae) 58 { 59 Charset cs = (Charset) charsetCB.getSelectedItem(); 60 basicTableModel.setCharset(cs); 61 } 62 }); 63 64 searchPanel.add(new JLabel(" max showed element: ")); 65 searchPanel.add(displayLimitCB); 66 displayLimitCB.setMaximumRowCount(20); 67 displayLimitCB.addActionListener(new ActionListener() 68 { 69 public void actionPerformed(ActionEvent ae) 70 { 71 String limS = (String ) displayLimitCB.getSelectedItem(); 72 basicTableModel.setDisplayLimit( Integer.parseInt(limS) ); 73 74 } 75 }); 76 77 final String [] allFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); 80 final JComboBox fontCB = new JComboBox(allFonts); 81 82 searchPanel.add(fontCB); 83 fontCB.addActionListener(new ActionListener() 84 { 85 public void actionPerformed(ActionEvent ae) 86 { 87 String fn = (String ) fontCB.getSelectedItem(); 88 table.setFont(new Font(fn, Font.PLAIN, 16)); 89 previewLabel.setFont(new Font(fn, Font.PLAIN, 64)); 90 } 91 }); 92 93 parseUnicodeData(new File("e:/projects/unicodedata.txt")); 94 this.basicTableModel.setDisplayLimit(32768); 95 charsetCB.setSelectedIndex(0); 96 fontCB.setSelectedIndex(0); 97 98 setSize(800,600); 99 setLocation(0,150); 100 setVisible(true); 101 102 previewDialog = new JDialog(this, "Preview", false); 103 previewDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 104 previewDialog.getContentPane().setLayout(new BorderLayout()); 105 previewDialog.getContentPane().add(previewLabel, BorderLayout.CENTER); 106 previewDialog.setSize(1000,150); 107 previewDialog.setVisible(true); 108 109 110 table.getSelectionModel().addListSelectionListener(new ListSelectionListener() 111 { 112 public void valueChanged(ListSelectionEvent lse) 113 { 114 if(lse.getValueIsAdjusting()) return; 115 116 int sel = table.getSelectedRow(); 117 if(sel==-1) 118 { previewLabel.setText(""); return; } 119 120 int pos = stm.getIndexInUnsortedFromTablePos(sel); 121 if(pos==-1) 122 { previewLabel.setText(""); return; } 123 124 String nt = previewLabel.getText() + basicTableModel.getValueAt(pos,3); 125 if(nt.length()>20) nt = nt.substring(nt.length()-20, nt.length()); 126 previewLabel.setText(nt); 127 } 128 }); 129 130 131 132 } 135 136 private Vector<Charset> collect_limited_Charsets() 137 { 138 SortedMap<String , Charset> sm = Charset.availableCharsets(); 139 Vector<Charset> items = new Vector<Charset>(sm.values()); 140 for(int i=items.size()-1; i>=0; i--) 141 { 142 148 } 149 return items; 150 } 151 152 153 154 155 156 final private Hashtable<Integer ,String > names = new Hashtable<Integer ,String >(); 157 158 public void parseUnicodeData(File file) 159 { 160 FileReader fr = null; 161 162 try 163 { 164 fr = new FileReader(file); 165 BufferedReader br = new BufferedReader(fr); 166 String line = null; 167 while( (line=br.readLine())!=null) 168 { 169 try 170 { 171 StringTokenizer tokenizer = new StringTokenizer(line, ";"); 173 String n = tokenizer.nextToken(); String name = tokenizer.nextToken(); 176 int code = Integer.parseInt(n, 16); 177 names.put(code, name); 178 } 179 catch(Exception e) 180 { 181 System.out.println("Cannot parse "+line); 182 } 183 } 184 } 185 catch(Exception e) 186 { 187 System.out.println("Cannot read unicode data: "+e.getMessage()); 189 } 190 finally 191 { 192 try{fr.close();} catch(Exception e) {} 193 } 194 } 195 196 197 public static void main(String [] a) 198 { 199 EventQueue.invokeLater(new Runnable () 200 { public void run() 201 { 202 new UnicodeViewer(); 203 } 204 }); 205 } 206 207 208 public static void mainTEST(String [] aaa) 209 { 210 JFrame fr = new JFrame(); 211 fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 212 fr.setContentPane( 213 new JLabel(" thai digit one : \u0e51") ); 214 fr.pack(); 215 fr.setLocationRelativeTo(null); 216 fr.setVisible(true); 217 } 218 219 220 221 class UnicodeTableModel extends FineGrainTableModel 222 { 223 private char[] representableCharsetChars = null; 224 225 String [] COLUMN_NAMES = new String []{ 226 "representable", "hexcode", "intcode", 227 "char", "name", "unicode block", 228 "type", "lowercase", "uppercase", "titlecase" }; 229 230 int[] COLUMN_PREFERED_SIZES = new int[]{4, 8,8,8, 28,16, 4,4,4,4}; 231 public int getPreferredColumnWidth(int column) 232 { 233 if(column>=0 && column<COLUMN_PREFERED_SIZES.length) return COLUMN_PREFERED_SIZES[column]; 234 return -1; 235 } 236 237 public String getColumnName(int col) { return COLUMN_NAMES[col]; } 238 239 public int getColumnAlignment(int column) 240 { 241 if( column==3 || column==4) return JLabel.LEFT; 242 return JLabel.CENTER; 243 } 244 245 246 247 public int getColumnCount() { return COLUMN_NAMES.length; } 248 public int getRowCount() { return charDisplayLimit; } 249 250 public void setDisplayLimit(int lim) 251 { 252 fireTableModelWillChange(); 254 charDisplayLimit = lim; 255 fireTableDataChanged(); 256 fireTableModelHasChanged(); 257 } 258 259 public void setCharset(Charset cs) 260 { 261 fireTableModelWillChange(); 262 byte[] bb = new byte[256]; 263 for(int i=0; i<256; i++) 264 { 265 bb[i] = (byte) i; 266 } 267 268 representableCharsetChars = cs.decode(ByteBuffer.wrap(bb)).toString().toCharArray(); 269 Arrays.sort(representableCharsetChars); 270 fireTableDataChanged(); 271 fireTableModelHasChanged(); 272 } 273 274 275 public Object getValueAt(int row, int col) 276 { 277 char c = (char) row; 278 279 if(col==0) 280 { 281 if(representableCharsetChars==null) return Boolean.TRUE; 282 int pos = Arrays.binarySearch(representableCharsetChars, c); 283 return pos>=0 ; 284 } 285 286 if(col==1) 287 { 288 String hexCode = "000"+Integer.toHexString(row); 289 return hexCode.substring(hexCode.length()-4,hexCode.length()); 290 } 291 292 if(col==2) return row; 293 294 295 if(col==3) return ""+c ; 296 if(col==4) 297 { 298 Object n = names.get(row); if(n==null) return ""; 300 return ""+n; 301 } 302 if(col==5) 303 { 304 Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if(ub==null) return ""; 306 return ""+ub; 307 } 308 309 310 if(col==6) return Character.getType(c); 311 312 if(col==7) return Character.isLowerCase(c); 313 if(col==8) return Character.isUpperCase(c); 314 if(col==9) return Character.isTitleCase(c); 315 316 317 318 return "?"; 319 } 320 321 327 } 328 329 330 } | Popular Tags |