KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > sortabletable > UnicodeViewer


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 /**
14
15   ftp://ftp.unicode.org/Public/UNIDATA/unicodeData.txt
16
17 */

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 JavaDoc[]{"32768", "256", "1024", "2048", "4096", "8192", "16384", "32768", "65536"});
27   private int charDisplayLimit = 8192;
28   StringBuffer JavaDoc allChars = new StringBuffer JavaDoc();
29   final private JDialog previewDialog;
30   final private JTextField previewLabel = new JTextField( 20 );
31
32   public UnicodeViewer()
33   {
34      super("");
35      //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
36

37      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
38      stm.installGUI(table);
39      getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
40
41      table.setDefaultRenderer(Object JavaDoc.class, new UniversalTableCellRenderer(stm));
42      table.setDefaultRenderer(Integer JavaDoc.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      // initialize combobox for charsets
52
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 JavaDoc limS = (String JavaDoc) displayLimitCB.getSelectedItem();
72          basicTableModel.setDisplayLimit( Integer.parseInt(limS) );
73
74        }
75      });
76
77      // only few !
78
//final JComboBox fontCB = new JComboBox(Toolkit.getDefaultToolkit().getFontList());
79
final String JavaDoc[] 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 JavaDoc fn = (String JavaDoc) 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 JavaDoc 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      //System.out.println((int) Character.MAX_VALUE+" ");
133
} // Constructor
134

135
136   private Vector<Charset> collect_limited_Charsets()
137   {
138      SortedMap<String JavaDoc, 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 /*ACCEPT ALL
143        Charset cs = (Charset) items.elementAt(i);
144        if(cs.newEncoder().averageBytesPerChar()!=1.0f)
145        {
146          items.remove(cs);
147        }*/

148      }
149      return items;
150   }
151
152
153
154
155
156   final private Hashtable<Integer JavaDoc,String JavaDoc> names = new Hashtable<Integer JavaDoc,String JavaDoc>();
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 JavaDoc line = null;
167      while( (line=br.readLine())!=null)
168      {
169        try
170        {
171         // 0041 ; LATIN CAPITAL LETTER A ; Lu;0;L;;;;;N;;;;0061;
172
StringTokenizer tokenizer = new StringTokenizer(line, ";");
173         String JavaDoc n = tokenizer.nextToken(); // 0041
174
String JavaDoc name = tokenizer.nextToken(); // name
175

176         int code = Integer.parseInt(n, 16);
177         names.put(code, name);
178        }
179        catch(Exception JavaDoc e)
180        {
181          System.out.println("Cannot parse "+line);
182        }
183      }
184    }
185    catch(Exception JavaDoc e)
186    {
187     //e.printStackTrace();
188
System.out.println("Cannot read unicode data: "+e.getMessage());
189    }
190    finally
191    {
192      try{fr.close();} catch(Exception JavaDoc e) {}
193    }
194   }
195
196
197   public static void main(String JavaDoc[] a)
198   {
199      EventQueue.invokeLater(new Runnable JavaDoc()
200      { public void run()
201        {
202          new UnicodeViewer();
203        }
204      });
205   }
206
207
208 public static void mainTEST(String JavaDoc[] 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 JavaDoc[] COLUMN_NAMES = new String JavaDoc[]{
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 JavaDoc 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        //System.out.println("set limit to "+lim);
253
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 JavaDoc 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 JavaDoc 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 JavaDoc n = names.get(row); //Character.toString(c);
299
if(n==null) return "";
300          return ""+n;
301        }
302        if(col==5)
303        {
304          Character.UnicodeBlock JavaDoc ub = Character.UnicodeBlock.of(c); // in 1.5, can be applied to INT
305
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 /*
322      public Class getColumnClass(int col)
323      {
324        if(col==0) return Integer.TYPE;
325        return String.class;
326      } */

327   }
328
329
330 } // UnicodeViewer
Popular Tags