1 package info.rolandkrueger.demo.tstmap;
2
3 import info.rolandkrueger.roklib.ui.swing.rapidsuggest.SuggestionComboBox;
4 import info.rolandkrueger.roklib.ui.swing.rapidsuggest.SuggestionComboBoxModel;
5 import info.rolandkrueger.roklib.util.ITernarySearchTreeMap;
6 import info.rolandkrueger.roklib.util.TernarySearchTreeMap;
7
8 import java.awt.BorderLayout;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11 import java.io.BufferedReader;
12 import java.io.File;
13 import java.io.FileNotFoundException;
14 import java.io.FileReader;
15 import java.io.IOException;
16 import java.net.URL;
17 import java.net.URLConnection;
18 import java.util.Iterator;
19 import java.util.LinkedList;
20 import java.util.Map;
21 import java.util.StringTokenizer;
22
23 import javax.swing.JFrame;
24 import javax.swing.JPanel;
25 import javax.swing.JScrollPane;
26 import javax.swing.JTextArea;
27 import javax.swing.JTextField;
28 import javax.swing.event.CaretEvent;
29 import javax.swing.event.CaretListener;
30
31
32
33
34
35
36
37 public class TernarySearchTreeMapDemo extends JFrame implements ActionListener, CaretListener
38 {
39 private TernarySearchTreeMap<Integer> map;
40 private TernarySearchTreeMap<String> dictMap;
41 private SuggestionComboBox scb;
42 private SuggestionComboBoxModel scbm;
43 private JTextField tf1, tf2;
44 private JTextArea ta;
45 private JScrollPane sp;
46
47 int count = 0;
48
49 public static void main (String[] args) throws IOException
50 {
51 new TernarySearchTreeMapDemo ();
52 }
53
54 public TernarySearchTreeMapDemo () throws IOException
55 {
56 super ("Testframe for SuggestionComboBox");
57 setSize (350, 400);
58 this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
59
60 dictMap = new TernarySearchTreeMap<String> ();
61
62 scbm = new SuggestionComboBoxModel (dictMap);
63 scb = new SuggestionComboBox (scbm);
64 scb.addActionListener (this);
65 getContentPane ().setLayout (new BorderLayout (5, 5));
66
67 tf1 = new JTextField ();
68 tf2 = new JTextField ();
69 tf1.addCaretListener (this);
70 tf2.addCaretListener (this);
71 JPanel upP = new JPanel ();
72 upP.setLayout (new BorderLayout (5, 5));
73 upP.add (tf1, BorderLayout.NORTH);
74 upP.add (tf2, BorderLayout.SOUTH);
75 getContentPane ().add (upP, BorderLayout.NORTH);
76
77 ta = new JTextArea (50, 50);
78 ta.setEditable (false);
79 sp = new JScrollPane (ta);
80 getContentPane ().add (sp, BorderLayout.CENTER);
81
82
83 readDictionaries ();
84
85
86 for (Iterator it = dictMap.entrySet ().iterator (); it.hasNext ();)
87 ta.append (it.next ().toString () + "\n");
88
89 setVisible (true);
90 SelectFileDialog selectFile = new SelectFileDialog (this);
91 selectFile.setVisible (true);
92
93 }
94
95 public void readDictionaries () throws IOException
96 {
97 System.out.println ("Reading German...");
98 readDict (new File ("C:/workspace/RoKlib/dicts/de-DE.dic"), "de");
99 System.out.println ("Reading English...");
100 readDict (new File ("C:/workspace/RoKlib/dicts/en-GB.dic"), "en");
101 System.out.println ("Reading Swedish...");
102 readDict (new File ("C:/workspace/RoKlib/dicts/sv-SE.dic"), "sv");
103 System.out.println (dictMap.size () + " keys in map.");
104 }
105
106 public void readDict (File file, String lang) throws IOException
107 {
108 BufferedReader reader = new BufferedReader (new FileReader (file));
109 String line = reader.readLine ();
110 while (line != null)
111 {
112 String value = dictMap.get (line);
113 if (value == null) value = lang;
114 else value = value + " " + lang;
115 dictMap.put (line, value);
116 line = reader.readLine ();
117 }
118 }
119
120 public void readInWords ()
121 {
122 long time = System.currentTimeMillis ();
123
124 for (int x = 0; x < 1; ++x)
125 {
126 ITernarySearchTreeMap<Integer> tMap = new TernarySearchTreeMap<Integer> ();
127 URLConnection con;
128 URL url;
129 String line;
130 BufferedReader reader;
131 int count = 0;
132
133 try
134 {
135
136
137
138
139
140
141
142
143 reader = new BufferedReader (new FileReader (new File ("C:\\workspace\\RoKlib\\test.txt")));
144 while (true)
145 {
146 line = reader.readLine ();
147 if (line == null) break;
148 if (line.length () == 0) continue;
149 StringTokenizer st = new StringTokenizer (line);
150 while (st.hasMoreTokens ())
151 {
152 count++;
153 String token = st.nextToken ();
154 if (map.indexOf (token) == - 1)
155 map.put (token, new Integer (1));
156 else
157 {
158
159
160 Integer i = (Integer) map.get (token);
161 if (i == null) continue;
162 int intV = i.intValue ();
163 intV++;
164 map.put (token, new Integer (intV));
165 }
166 }
167 }
168 } catch (Exception e)
169 {
170 e.printStackTrace ();
171 }
172 }
173 System.out.println ("Count: " + count + " tokens in input file.");
174 time = System.currentTimeMillis () - time;
175 System.out.println ("Duration: " + time);
176
177 }
178
179 public void caretUpdate (CaretEvent e)
180 {
181 if (e.getSource () == tf1)
182 {
183 String prefix = tf1.getText ();
184 ta.setText ("");
185 if (prefix.equals ("")) return;
186 long time = System.currentTimeMillis ();
187 for (Iterator it = dictMap.getPrefixSubtreeIterator (prefix); it.hasNext ();)
188 {
189 Object o = it.next ();
190 ta.append (((Map.Entry) o).getKey ().toString () + " (" + ((Map.Entry) o).getValue ()
191 + ")\n");
192 }
193
194 System.out.println (System.currentTimeMillis () - time);
195 }
196
197 if (e.getSource () == tf2)
198 {
199 String text = tf2.getText ();
200 ta.setText ("");
201 for (Iterator it = dictMap.matchAlmost (text, 2, 0).iterator (); it.hasNext ();)
202 {
203 ta.append (it.next ().toString () + "\n");
204 }
205 }
206 }
207
208 public void actionPerformed (ActionEvent e)
209 {
210 System.out.println ("Action performed");
211 }
212 }