KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > gsf > tools > lucene > IndexBrowserTopComponent


1 package org.netbeans.modules.gsf.tools.lucene;
2
3 import java.awt.Toolkit JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.Serializable JavaDoc;
7 import java.net.URL JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Collections JavaDoc;
10 import java.util.Comparator JavaDoc;
11 import java.util.EnumSet JavaDoc;
12 import java.util.Enumeration JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19 import javax.swing.JFileChooser JavaDoc;
20 import javax.swing.ListModel JavaDoc;
21 import javax.swing.ListSelectionModel JavaDoc;
22 import javax.swing.event.ListDataListener JavaDoc;
23 import javax.swing.event.ListSelectionEvent JavaDoc;
24 import javax.swing.event.ListSelectionListener JavaDoc;
25 import javax.swing.event.TableModelListener JavaDoc;
26 import javax.swing.table.TableModel JavaDoc;
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.document.Field;
29 import org.apache.lucene.index.IndexReader;
30 import org.netbeans.api.gsf.Index.SearchResult;
31 import org.netbeans.api.java.classpath.ClassPath;
32 import org.netbeans.api.retouche.source.ClassIndex;
33 import org.netbeans.modules.retouche.source.usages.ClassIndexImpl;
34 import org.netbeans.modules.retouche.source.usages.ClassIndexManager;
35 import org.netbeans.modules.retouche.source.usages.Index;
36 import org.openide.ErrorManager;
37 import org.openide.awt.StatusDisplayer;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.modules.InstalledFileLocator;
41 import org.openide.util.Exceptions;
42 import org.openide.util.NbBundle;
43 import org.openide.windows.TopComponent;
44 import org.openide.windows.WindowManager;
45 //import org.openide.util.Utilities;
46

47 /**
48  * Window for browsing a GSF index
49  *
50  * TODO: Query the set of available keys from Lucene
51  * @todo Set column model to get proportional widths (auto fit?)
52  * @todo Instead of showing the segment+docid, show the require-value in the match list
53  *
54  * @author Tor Norbye
55  */

56 final class IndexBrowserTopComponent extends TopComponent {
57     
58     private static IndexBrowserTopComponent instance;
59     /** path to the icon used by the component and its open action */
60     // static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
61

62     private static final String JavaDoc PREFERRED_ID = "IndexBrowserTopComponent";
63     
64     private File JavaDoc segmentFile;
65     
66     private IndexBrowserTopComponent() {
67         initComponents();
68         setName(NbBundle.getMessage(IndexBrowserTopComponent.class, "CTL_IndexBrowserTopComponent"));
69         setToolTipText(NbBundle.getMessage(IndexBrowserTopComponent.class, "HINT_IndexBrowserTopComponent"));
70         // setIcon(Utilities.loadImage(ICON_PATH, true));
71

72         
73         matchTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
74         //Ask to be notified of selection changes.
75
ListSelectionModel JavaDoc rowSM = matchTable.getSelectionModel();
76         rowSM.addListSelectionListener(new ListSelectionListener JavaDoc() {
77             public void valueChanged(ListSelectionEvent JavaDoc e) {
78                 //Ignore extra messages.
79
if (e.getValueIsAdjusting()) return;
80
81                 ListSelectionModel JavaDoc lsm =
82                     (ListSelectionModel JavaDoc)e.getSource();
83                 if (!lsm.isSelectionEmpty()) {
84                     int selectedRow = lsm.getMinSelectionIndex();
85                     if (matchTable.getModel() instanceof SearchMatchModel) {
86                         SearchMatchModel model = (SearchMatchModel)matchTable.getModel();
87                         SearchResult result = model.getSearchResultAt(selectedRow);
88                         show(result);
89                     }
90                 }
91             }
92         });
93         
94         updateIndices();
95         
96         indexList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
97         indexList.addListSelectionListener(new ListSelectionListener JavaDoc() {
98             public void valueChanged(ListSelectionEvent JavaDoc ev) {
99                 if (!ev.getValueIsAdjusting()) {
100                     int row = indexList.getSelectedIndex();
101                     if (row == -1) {
102                         indexLabel.setText("");
103                         segmentLabel.setText("");
104                         setSegment(null);
105                         return;
106                     }
107                     if (row >= indexList.getModel().getSize()) {
108                         return;
109                     }
110                     IndexEntry entry = ((IndexListModel)indexList.getModel()).getEntry(row);
111                     setSegment(entry.segment);
112                     String JavaDoc path = getSegmentLabel(entry.segment);
113                     segmentLabel.setText(path);
114                     indexLabel.setText(getUrlLabel(entry.url));
115                 }
116             }
117
118             
119         });
120     }
121     
122     public int getPersistenceType() {
123         return TopComponent.PERSISTENCE_NEVER;
124     }
125     
126     private void updateIndices() {
127         List JavaDoc<IndexEntry> list = new ArrayList JavaDoc<IndexEntry>();
128         if (allIndexButton.isSelected()) {
129             // Show all indices
130
// Initialize indices
131
Map JavaDoc<URL JavaDoc, ClassIndexImpl> map = ClassIndexManager.getDefault().getAllIndices();
132             for (URL JavaDoc url : map.keySet()) {
133                 ClassIndexImpl index = map.get(url);
134                 File JavaDoc segment = index.getSegment();
135                 list.add(new IndexEntry(url, index, segment));
136             }
137         } else {
138             assert indexForButton.isSelected();
139             
140             File JavaDoc file = getCurrentFile();
141             if (file == null) {
142                 Toolkit.getDefaultToolkit().beep();
143                 return;
144             }
145             
146             FileObject fo = FileUtil.toFileObject(file);
147             if (fo == null) {
148                 Toolkit.getDefaultToolkit().beep();
149                 return;
150             }
151             
152 // Project p = FileOwnerQuery.getOwner(fo);
153
// if (p == null) {
154
// Toolkit.getDefaultToolkit().beep();
155
// return;
156
// }
157

158             final ClassPath cp = ClassPath.getClassPath(fo, ClassPath.SOURCE);
159             Set JavaDoc<ClassIndexImpl> indices = new HashSet JavaDoc<ClassIndexImpl>();
160             ClassIndex.createQueriesForRoots(cp, true, indices);
161             for (ClassIndexImpl index : indices) {
162                 URL JavaDoc url = index.getRoot();
163                 File JavaDoc segment = index.getSegment();
164                 list.add(new IndexEntry(url, index, segment));
165             }
166         }
167         indexList.setModel(new IndexListModel(list));
168     }
169     
170     private String JavaDoc getUrlLabel(URL JavaDoc url) {
171         String JavaDoc path = url.toExternalForm();
172         int index = path.indexOf("jruby-0.9.8");
173         if (index != -1) {
174             path = path.substring(index+12);
175         }
176         return path;
177     }
178     
179     private String JavaDoc getSegmentLabel(File JavaDoc segment) {
180         String JavaDoc path = "";
181         if (segment != null) {
182             path = segment.getPath();
183             int index = path.indexOf("gsf-index");
184             if (index != -1) {
185                 path = path.substring(index+10);
186             }
187         }
188         
189         return path;
190     }
191     
192     private void setSegment(File JavaDoc segment) {
193         this.segmentFile = segment;
194         lukeButton.setEnabled(segment != null);
195     }
196     
197     private void show(SearchResult result) {
198         File JavaDoc segment = (File JavaDoc)result.getSegment();
199         setSegment(segment);
200         segmentLabel.setText(segment.getPath());
201         ClassIndexImpl index = (ClassIndexImpl)result.getIndex();
202         URL JavaDoc root = index.getRoot();
203         if (root != null) {
204             indexLabel.setText(getUrlLabel(root));
205         } else {
206             indexLabel.setText("");
207         }
208         docIdField.setText(Integer.toString(result.getDocumentNumber()));
209         
210         TableModel JavaDoc model = new SearchDocumentTableModel(result);
211         documentTable.setModel(model);
212         
213         // Temporary hack because the other search model doesn't work right -- duplicate entries
214
showDocument(result.getDocumentNumber());
215     }
216     
217     /** This method is called from within the constructor to
218      * initialize the form.
219      * WARNING: Do NOT modify this code. The content of this method is
220      * always regenerated by the Form Editor.
221      */

222     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
223
private void initComponents() {
224
225         jRadioButton3 = new javax.swing.JRadioButton JavaDoc();
226         indexGroup = new javax.swing.ButtonGroup JavaDoc();
227         jLabel1 = new javax.swing.JLabel JavaDoc();
228         keyCombo = new javax.swing.JComboBox JavaDoc();
229         jLabel2 = new javax.swing.JLabel JavaDoc();
230         nameField = new javax.swing.JTextField JavaDoc();
231         jLabel3 = new javax.swing.JLabel JavaDoc();
232         typeCombo = new javax.swing.JComboBox JavaDoc();
233         searchButton = new javax.swing.JButton JavaDoc();
234         jSplitPane1 = new javax.swing.JSplitPane JavaDoc();
235         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
236         matchTable = new javax.swing.JTable JavaDoc();
237         jPanel2 = new javax.swing.JPanel JavaDoc();
238         jScrollPane2 = new javax.swing.JScrollPane JavaDoc();
239         documentTable = new javax.swing.JTable JavaDoc();
240         jLabel5 = new javax.swing.JLabel JavaDoc();
241         docIdField = new javax.swing.JTextField JavaDoc();
242         prevButton = new javax.swing.JButton JavaDoc();
243         nextButton = new javax.swing.JButton JavaDoc();
244         jPanel1 = new javax.swing.JPanel JavaDoc();
245         allIndexButton = new javax.swing.JRadioButton JavaDoc();
246         indexForButton = new javax.swing.JRadioButton JavaDoc();
247         indexFileField = new javax.swing.JTextField JavaDoc();
248         indexFileButton = new javax.swing.JButton JavaDoc();
249         jScrollPane3 = new javax.swing.JScrollPane JavaDoc();
250         indexList = new javax.swing.JList JavaDoc();
251         jButton1 = new javax.swing.JButton JavaDoc();
252         indexLabel = new javax.swing.JLabel JavaDoc();
253         jLabel4 = new javax.swing.JLabel JavaDoc();
254         jLabel6 = new javax.swing.JLabel JavaDoc();
255         segmentLabel = new javax.swing.JLabel JavaDoc();
256         lukeButton = new javax.swing.JButton JavaDoc();
257
258         org.openide.awt.Mnemonics.setLocalizedText(jRadioButton3, "jRadioButton3");
259         jRadioButton3.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
260         jRadioButton3.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
261
262         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, "Key:");
263
264         keyCombo.setModel(new javax.swing.DefaultComboBoxModel JavaDoc(new String JavaDoc[] { "class", "method", "field", "attribute", "constant", "fqn", "file", "module", "extends", "require" }));
265
266         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, "Name:");
267
268         nameField.addActionListener(new java.awt.event.ActionListener JavaDoc() {
269             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
270                 search(evt);
271             }
272         });
273
274         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, "Type:");
275
276         typeCombo.setModel(new javax.swing.DefaultComboBoxModel JavaDoc(new String JavaDoc[] { "Prefix", "Exact", "Regexp" }));
277
278         org.openide.awt.Mnemonics.setLocalizedText(searchButton, "Search");
279         searchButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
280             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
281                 search(evt);
282             }
283         });
284
285         jSplitPane1.setBorder(null);
286         jSplitPane1.setResizeWeight(0.5);
287
288         matchTable.setModel(getEmptyMatchTableModel());
289         jScrollPane1.setViewportView(matchTable);
290
291         jSplitPane1.setLeftComponent(jScrollPane1);
292
293         documentTable.setModel(getEmptyDocumentTableModel());
294         jScrollPane2.setViewportView(documentTable);
295
296         org.openide.awt.Mnemonics.setLocalizedText(jLabel5, "Document:");
297
298         docIdField.setColumns(6);
299         docIdField.addActionListener(new java.awt.event.ActionListener JavaDoc() {
300             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
301                 docIdFieldActionPerformed(evt);
302             }
303         });
304
305         org.openide.awt.Mnemonics.setLocalizedText(prevButton, "Previous");
306         prevButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
307             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
308                 prevButtonActionPerformed(evt);
309             }
310         });
311
312         org.openide.awt.Mnemonics.setLocalizedText(nextButton, "Next");
313         nextButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
314             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
315                 nextButtonActionPerformed(evt);
316             }
317         });
318
319         org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
320         jPanel2.setLayout(jPanel2Layout);
321         jPanel2Layout.setHorizontalGroup(
322             jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
323             .add(jPanel2Layout.createSequentialGroup()
324                 .add(jLabel5)
325                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
326                 .add(docIdField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
327                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
328                 .add(prevButton)
329                 .add(6, 6, 6)
330                 .add(nextButton)
331                 .addContainerGap(174, Short.MAX_VALUE))
332             .add(jPanel2Layout.createSequentialGroup()
333                 .add(20, 20, 20)
334                 .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE))
335         );
336         jPanel2Layout.setVerticalGroup(
337             jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
338             .add(jPanel2Layout.createSequentialGroup()
339                 .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
340                     .add(jLabel5)
341                     .add(docIdField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
342                     .add(prevButton)
343                     .add(nextButton))
344                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
345                 .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE))
346         );
347
348         jSplitPane1.setRightComponent(jPanel2);
349
350         indexGroup.add(allIndexButton);
351         allIndexButton.setSelected(true);
352         org.openide.awt.Mnemonics.setLocalizedText(allIndexButton, "All Indices");
353         allIndexButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
354         allIndexButton.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
355         allIndexButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
356             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
357                 changeIndex(evt);
358             }
359         });
360
361         indexGroup.add(indexForButton);
362         org.openide.awt.Mnemonics.setLocalizedText(indexForButton, "Indices for:");
363         indexForButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
364         indexForButton.setEnabled(false);
365         indexForButton.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
366         indexForButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
367             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
368                 changeIndex(evt);
369             }
370         });
371
372         indexFileField.setColumns(8);
373         indexFileField.addActionListener(new java.awt.event.ActionListener JavaDoc() {
374             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
375                 changeIndex(evt);
376             }
377         });
378
379         org.openide.awt.Mnemonics.setLocalizedText(indexFileButton, "...");
380         indexFileButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
381             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
382                 browseIndexFile(evt);
383             }
384         });
385
386         indexList.setModel(getIndexList());
387         jScrollPane3.setViewportView(indexList);
388
389         org.openide.awt.Mnemonics.setLocalizedText(jButton1, "Update");
390         jButton1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
391             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
392                 updateIndices(evt);
393             }
394         });
395
396         org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
397         jPanel1.setLayout(jPanel1Layout);
398         jPanel1Layout.setHorizontalGroup(
399             jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
400             .add(jPanel1Layout.createSequentialGroup()
401                 .addContainerGap()
402                 .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
403                     .add(jPanel1Layout.createSequentialGroup()
404                         .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
405                             .add(indexForButton)
406                             .add(jPanel1Layout.createSequentialGroup()
407                                 .add(22, 22, 22)
408                                 .add(indexFileField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 82, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
409                                 .add(1, 1, 1)
410                                 .add(indexFileButton))
411                             .add(allIndexButton))
412                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
413                     .add(org.jdesktop.layout.GroupLayout.LEADING, jButton1))
414                 .add(8, 8, 8)
415                 .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE))
416         );
417         jPanel1Layout.setVerticalGroup(
418             jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
419             .add(jPanel1Layout.createSequentialGroup()
420                 .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
421                     .add(jPanel1Layout.createSequentialGroup()
422                         .add(allIndexButton)
423                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
424                         .add(indexForButton)
425                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
426                         .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
427                             .add(indexFileButton)
428                             .add(indexFileField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
429                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
430                         .add(jButton1))
431                     .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE))
432                 .addContainerGap())
433         );
434
435         org.openide.awt.Mnemonics.setLocalizedText(jLabel4, "Index:");
436
437         org.openide.awt.Mnemonics.setLocalizedText(jLabel6, "Segment:");
438
439         org.openide.awt.Mnemonics.setLocalizedText(lukeButton, "Open in Luke");
440         lukeButton.setEnabled(false);
441         lukeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
442             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
443                 openInLuke(evt);
444             }
445         });
446
447         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
448         this.setLayout(layout);
449         layout.setHorizontalGroup(
450             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
451             .add(layout.createSequentialGroup()
452                 .addContainerGap()
453                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
454                     .add(layout.createSequentialGroup()
455                         .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 691, Short.MAX_VALUE)
456                         .addContainerGap())
457                     .add(layout.createSequentialGroup()
458                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
459                             .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
460                                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
461                                     .add(layout.createSequentialGroup()
462                                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
463                                             .add(jLabel1)
464                                             .add(jLabel2)
465                                             .add(jLabel3))
466                                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
467                                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
468                                             .add(typeCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
469                                             .add(keyCombo, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
470                                             .add(nameField)))
471                                     .add(searchButton))
472                                 .add(13, 13, 13)
473                                 .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
474                                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
475                             .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
476                                 .add(jLabel6)
477                                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
478                                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
479                                     .add(indexLabel)
480                                     .add(layout.createSequentialGroup()
481                                         .add(segmentLabel)
482                                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 514, Short.MAX_VALUE)
483                                         .add(lukeButton))))
484                             .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel4))
485                         .add(20, 20, 20))))
486         );
487         layout.setVerticalGroup(
488             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
489             .add(layout.createSequentialGroup()
490                 .add(26, 26, 26)
491                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
492                     .add(layout.createSequentialGroup()
493                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
494                             .add(jLabel1)
495                             .add(keyCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
496                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
497                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
498                             .add(jLabel2)
499                             .add(nameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
500                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
501                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
502                             .add(jLabel3)
503                             .add(typeCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
504                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
505                         .add(searchButton))
506                     .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
507                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
508                 .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE)
509                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
510                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
511                     .add(jLabel4)
512                     .add(indexLabel))
513                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
514                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
515                     .add(jLabel6)
516                     .add(segmentLabel)
517                     .add(lukeButton))
518                 .addContainerGap())
519         );
520     }// </editor-fold>//GEN-END:initComponents
521

522 private void openInLuke(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_openInLuke
523
File JavaDoc f = null;
524     String JavaDoc l = System.getProperty("luke.dir");
525     if (l != null) {
526         f = new File JavaDoc(l);
527     } else {
528         f = InstalledFileLocator.getDefault().locate("modules/org-netbeans-modules-gsf-tools.jar", null, false);
529
530         if (f != null) {
531             f = new File JavaDoc(f.getParentFile().getAbsolutePath() + File.separator + "..");
532             try {
533                 f = f.getCanonicalFile();
534             } catch (IOException JavaDoc ioe) {
535                 ErrorManager.getDefault().notify(ioe);
536             }
537
538             f = new File JavaDoc(f, "lukeall.jar");
539         }
540     }
541     
542     try {
543         if (f == null || !f.exists()) {
544            StatusDisplayer.getDefault().setStatusText(f.getPath() + " not found. Install from http://www.getopt.org/luke/");
545             Toolkit.getDefaultToolkit().beep();
546             return;
547         }
548         Runtime.getRuntime().exec(new java.lang.String JavaDoc[]{ System.getProperty("java.home") + File.separator + "bin" + File.separator + "java",
549            "-jar", f.getPath(), "-index", segmentFile.getPath()+File.separator + "gsf"});
550     }
551     catch (IOException JavaDoc ex) {
552         Exceptions.printStackTrace(ex);
553     }
554 }//GEN-LAST:event_openInLuke
555

556 private void updateIndices(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_updateIndices
557
updateIndices();
558 }//GEN-LAST:event_updateIndices
559

560
561     private File JavaDoc getCurrentFile() {
562         String JavaDoc name = indexFileField.getText();
563
564         File JavaDoc f = new File JavaDoc(name);
565
566         if (f.exists()) {
567             return f;
568         }
569
570         return null;
571     }
572     
573 private void browseIndexFile(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseIndexFile
574
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
575     chooser.setDialogTitle("Choose a source file");
576     chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
577     chooser.setMultiSelectionEnabled(false);
578
579     File JavaDoc currentFile = getCurrentFile();
580
581     if (currentFile != null) {
582         if (!currentFile.isDirectory()) {
583             currentFile = currentFile.getParentFile();
584         }
585
586         if (currentFile.exists()) {
587             chooser.setCurrentDirectory(currentFile);
588         }
589     }
590
591     int returnVal = chooser.showOpenDialog(this);
592
593     if (returnVal == JFileChooser.APPROVE_OPTION) {
594         indexFileField.setText(chooser.getSelectedFile().getParent() + File.separator +
595             chooser.getSelectedFile().getName());
596     }
597 }//GEN-LAST:event_browseIndexFile
598

599 private void changeIndex(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_changeIndex
600
updateIndices();
601 }//GEN-LAST:event_changeIndex
602

603 private void docIdFieldActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_docIdFieldActionPerformed
604
int doc = Integer.parseInt(docIdField.getText().trim());
605     showDocument(doc);
606 }//GEN-LAST:event_docIdFieldActionPerformed
607

608 private void nextButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_nextButtonActionPerformed
609
String JavaDoc text = docIdField.getText().trim();
610     try {
611         int num = Integer.parseInt(text);
612         num++;
613         docIdField.setText(Integer.toString(num));
614         showDocument(num);
615     } catch (NumberFormatException JavaDoc nfe) {
616         return;
617     }
618 }//GEN-LAST:event_nextButtonActionPerformed
619

620 private void prevButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_prevButtonActionPerformed
621
String JavaDoc text = docIdField.getText().trim();
622     try {
623         int num = Integer.parseInt(text);
624         if (num == 0) {
625             return;
626         }
627         num--;
628         docIdField.setText(Integer.toString(num));
629         showDocument(num);
630     } catch (NumberFormatException JavaDoc nfe) {
631         return;
632     }
633 }//GEN-LAST:event_prevButtonActionPerformed
634

635 private void showDocument(int id) {
636     SearchDocumentTableModel model = (SearchDocumentTableModel)documentTable.getModel();
637     IndexReader reader = model.getIndexReader();
638     if (reader != null) {
639         if (id >= reader.maxDoc()) {
640             docIdField.setText(Integer.toString(reader.maxDoc()-1));
641             return;
642         }
643         try {
644             Document luceneDoc = reader.document(id);
645             if (luceneDoc != null) {
646                 documentTable.setModel(new SearchDocumentTableModel(luceneDoc, reader, id));
647             }
648         } catch (IOException JavaDoc ioe) {
649             Exceptions.printStackTrace(ioe);
650         }
651     }
652 }
653
654 private void search(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_search
655
// Perform the search
656
String JavaDoc name = nameField.getText().trim();
657     String JavaDoc type = typeCombo.getSelectedItem().toString();
658     String JavaDoc field = keyCombo.getSelectedItem().toString();
659     Set JavaDoc<Index.SearchScope> scope = EnumSet.allOf(Index.SearchScope.class);
660     
661     Index.NameKind kind = Index.NameKind.EXACT_NAME;
662     if (type.equals("Prefix")) {
663         kind = Index.NameKind.PREFIX;
664     }
665     if (type.equals("Regexp")) {
666         kind = Index.NameKind.REGEXP;
667     }
668     Map JavaDoc<URL JavaDoc, ClassIndexImpl> map = ClassIndexManager.getDefault().getAllIndices();
669     Set JavaDoc<SearchResult> result = new HashSet JavaDoc<SearchResult>();
670
671     try {
672         for (ClassIndexImpl index : map.values()) {
673             index.gsfSearch(field, name, kind, scope, result);
674         }
675         
676         TableModel JavaDoc model = new SearchMatchModel(result, field);
677         matchTable.setModel(model);
678         
679         // TODO: Clear out the document table
680

681     } catch (IOException JavaDoc ioe) {
682         Exceptions.printStackTrace(ioe);
683     }
684 }//GEN-LAST:event_search
685

686 private IndexReader indexReader;
687
688     
689     // Variables declaration - do not modify//GEN-BEGIN:variables
690
private javax.swing.JRadioButton JavaDoc allIndexButton;
691     private javax.swing.JTextField JavaDoc docIdField;
692     private javax.swing.JTable JavaDoc documentTable;
693     private javax.swing.JButton JavaDoc indexFileButton;
694     private javax.swing.JTextField JavaDoc indexFileField;
695     private javax.swing.JRadioButton JavaDoc indexForButton;
696     private javax.swing.ButtonGroup JavaDoc indexGroup;
697     private javax.swing.JLabel JavaDoc indexLabel;
698     private javax.swing.JList JavaDoc indexList;
699     private javax.swing.JButton JavaDoc jButton1;
700     private javax.swing.JLabel JavaDoc jLabel1;
701     private javax.swing.JLabel JavaDoc jLabel2;
702     private javax.swing.JLabel JavaDoc jLabel3;
703     private javax.swing.JLabel JavaDoc jLabel4;
704     private javax.swing.JLabel JavaDoc jLabel5;
705     private javax.swing.JLabel JavaDoc jLabel6;
706     private javax.swing.JPanel JavaDoc jPanel1;
707     private javax.swing.JPanel JavaDoc jPanel2;
708     private javax.swing.JRadioButton JavaDoc jRadioButton3;
709     private javax.swing.JScrollPane JavaDoc jScrollPane1;
710     private javax.swing.JScrollPane JavaDoc jScrollPane2;
711     private javax.swing.JScrollPane JavaDoc jScrollPane3;
712     private javax.swing.JSplitPane JavaDoc jSplitPane1;
713     private javax.swing.JComboBox JavaDoc keyCombo;
714     private javax.swing.JButton JavaDoc lukeButton;
715     private javax.swing.JTable JavaDoc matchTable;
716     private javax.swing.JTextField JavaDoc nameField;
717     private javax.swing.JButton JavaDoc nextButton;
718     private javax.swing.JButton JavaDoc prevButton;
719     private javax.swing.JButton JavaDoc searchButton;
720     private javax.swing.JLabel JavaDoc segmentLabel;
721     private javax.swing.JComboBox JavaDoc typeCombo;
722     // End of variables declaration//GEN-END:variables
723

724     /**
725      * Gets default instance. Do not use directly: reserved for *.settings files only,
726      * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
727      * To obtain the singleton instance, use {@link findInstance}.
728      */

729     public static synchronized IndexBrowserTopComponent getDefault() {
730         if (instance == null) {
731             instance = new IndexBrowserTopComponent();
732         }
733         return instance;
734     }
735     
736     /**
737      * Obtain the IndexBrowserTopComponent instance. Never call {@link #getDefault} directly!
738      */

739     public static synchronized IndexBrowserTopComponent findInstance() {
740         TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
741         if (win == null) {
742             ErrorManager.getDefault().log(ErrorManager.WARNING,
743                     "Cannot find MyWindow component. It will not be located properly in the window system.");
744             return getDefault();
745         }
746         if (win instanceof IndexBrowserTopComponent) {
747             return (IndexBrowserTopComponent)win;
748         }
749         ErrorManager.getDefault().log(ErrorManager.WARNING,
750                 "There seem to be multiple components with the '" + PREFERRED_ID +
751                 "' ID. That is a potential source of errors and unexpected behavior.");
752         return getDefault();
753     }
754         
755     public void componentOpened() {
756         // TODO add custom code on component opening
757
}
758     
759     public void componentClosed() {
760         // TODO add custom code on component closing
761
}
762     
763     /** replaces this in object stream */
764     public Object JavaDoc writeReplace() {
765         return new ResolvableHelper();
766     }
767     
768     protected String JavaDoc preferredID() {
769         return PREFERRED_ID;
770     }
771     
772     private ListModel JavaDoc getIndexList() {
773         return new IndexListModel();
774     }
775     
776     private TableModel JavaDoc getEmptyDocumentTableModel() {
777         return new SearchDocumentTableModel();
778     }
779
780     private TableModel JavaDoc getEmptyMatchTableModel() {
781         return new SearchMatchModel();
782     }
783     
784     final static class ResolvableHelper implements Serializable JavaDoc {
785         private static final long serialVersionUID = 1L;
786         public Object JavaDoc readResolve() {
787             return IndexBrowserTopComponent.getDefault();
788         }
789     }
790     
791     class SearchMatchModel implements TableModel JavaDoc {
792         private List JavaDoc<Map JavaDoc<String JavaDoc, SearchResult>> data;
793         private IndexReader indexReader;
794
795         SearchMatchModel() {
796             // Empty
797
}
798         
799         SearchMatchModel(Set JavaDoc<SearchResult> results, String JavaDoc key) {
800             data = new ArrayList JavaDoc<Map JavaDoc<String JavaDoc, SearchResult>>();
801             for (SearchResult result : results) {
802                 String JavaDoc[] matches = result.getValues(key);
803                 if (matches != null) {
804                     for (String JavaDoc match : matches) {
805                         Map JavaDoc<String JavaDoc,SearchResult> map = new HashMap JavaDoc<String JavaDoc,SearchResult>();
806                         map.put(match, result);
807                         data.add(map);
808                     }
809                 }
810
811                 IndexReader indexReader = (IndexReader)result.getIndexReader();
812                 if (indexReader != null) {
813                     this.indexReader = indexReader;
814                 }
815             }
816             
817             // Sort the data to be helpful
818
//final List<Map<String, SearchResult>> foo = new ArrayList<Map<String,SearchResult>>();
819
//Collections.sort(foo, new Comparator<Map<String,SearchResult>>() {
820
Collections.sort(data, new Comparator JavaDoc<Map JavaDoc<String JavaDoc,SearchResult>>() {
821                 public int compare(Map JavaDoc<String JavaDoc, SearchResult> m1,
822                                    Map JavaDoc<String JavaDoc, SearchResult> m2) {
823                     String JavaDoc key1 = m1.keySet().iterator().next();
824                     String JavaDoc key2 = m2.keySet().iterator().next();
825                     return key1.compareTo(key2);
826                 }
827             });
828         }
829         
830         public IndexReader getIndexReader() {
831             return indexReader;
832         }
833     
834         public int getRowCount() {
835             return data == null ? 0 : data.size();
836         }
837
838         public int getColumnCount() {
839             return 2;
840         }
841
842         public String JavaDoc getColumnName(int col) {
843             switch (col) {
844             case 0: return "Match";
845             case 1: return "Document";
846             default: throw new IllegalArgumentException JavaDoc();
847             }
848         }
849
850         public Class JavaDoc<?> getColumnClass(int arg0) {
851             return String JavaDoc.class;
852         }
853
854         public boolean isCellEditable(int arg0, int arg1) {
855             return false;
856         }
857
858         public Object JavaDoc getValueAt(int row, int col) {
859             Map JavaDoc<String JavaDoc,SearchResult> entry = data.get(row);
860             switch (col) {
861             case 0:
862                 return entry.keySet().iterator().next();
863             case 1: {
864                 SearchResult map = entry.values().iterator().next();
865                 //return getSegmentLabel((File)map.getSegment()) + ":" + Integer.toString(map.getDocumentNumber());
866
String JavaDoc value = map.getValue("require");
867                 if (value != null) {
868                     return value;
869                 } else {
870                     return "";
871                 }
872             }
873             default: throw new IllegalArgumentException JavaDoc();
874             }
875         }
876         
877         public SearchResult getSearchResultAt(int row) {
878             Map JavaDoc<String JavaDoc,SearchResult> entry = data.get(row);
879             return entry.values().iterator().next();
880         }
881
882         public void setValueAt(Object JavaDoc arg0, int arg1, int arg2) {
883             throw new IllegalArgumentException JavaDoc();
884         }
885
886         public void addTableModelListener(TableModelListener JavaDoc arg0) {
887             // For now, no changes occur - we just replace the whole model
888
}
889
890         public void removeTableModelListener(TableModelListener JavaDoc arg0) {
891         }
892     }
893  
894     class SearchDocumentTableModel implements TableModel JavaDoc {
895         private IndexReader indexReader;
896
897         private class Match {
898             private String JavaDoc key;
899             private String JavaDoc value;
900
901             Match(String JavaDoc key, String JavaDoc value) {
902                 this.key = key;
903                 this.value = value;
904             }
905         }
906
907         private String JavaDoc[] fields;
908         private List JavaDoc<Match> data;
909         
910         
911         SearchDocumentTableModel(Document luceneDoc, IndexReader reader, int docId) {
912             this.indexReader = reader;
913             data = new ArrayList JavaDoc<Match>();
914             initFromLuceneDoc(luceneDoc);
915         }
916         
917         SearchDocumentTableModel(SearchResult doc) {
918             data = new ArrayList JavaDoc<Match>();
919             Document luceneDoc = (Document)doc.getDocument();
920             IndexReader indexReader = (IndexReader)doc.getIndexReader();
921             if (indexReader != null) {
922                 this.indexReader = indexReader;
923             }
924             initFromLuceneDoc(luceneDoc);
925         }
926         
927         private void initFromLuceneDoc(Document luceneDoc) {
928             @SuppressWarnings JavaDoc("unchecked")
929             Enumeration JavaDoc<Field> en = luceneDoc.fields();
930             while (en.hasMoreElements()) {
931                 Field f = en.nextElement();
932                 String JavaDoc key = f.name();
933                 String JavaDoc value = f.stringValue();
934                 data.add(new Match(key, value));
935             }
936
937             // Sort the data to be helpful
938
Collections.sort(data, new Comparator JavaDoc<Match>() {
939                 public int compare(Match m1,
940                                    Match m2) {
941                     // Sort by key, then by value - except the "method" and "attribute" keys should go to the end
942
if (m1.key.equals(m2.key)) {
943                         return m1.value.compareTo(m2.value);
944                     }
945                     if (m1.key.equals("method")) {
946                         return 1;
947                     }
948                     if (m2.key.equals("method")) {
949                         return -1;
950                     }
951                     if (m1.key.equals("attribute")) {
952                         return 1;
953                     }
954                     if (m2.key.equals("attribute")) {
955                         return -1;
956                     }
957                     return m1.key.compareTo(m2.key);
958                 }
959             });
960         }
961
962         SearchDocumentTableModel() {
963             // Empty
964
}
965     
966         public IndexReader getIndexReader() {
967             return indexReader;
968         }
969     
970         public int getRowCount() {
971             return data == null ? 0 : data.size();
972         }
973
974         public int getColumnCount() {
975             return 2;
976         }
977
978         public String JavaDoc getColumnName(int col) {
979             switch (col) {
980             case 0: return "Key";
981             case 1: return "Value";
982             default: throw new IllegalArgumentException JavaDoc();
983             }
984         }
985
986         public Class JavaDoc<?> getColumnClass(int col) {
987             return String JavaDoc.class;
988         }
989
990         public boolean isCellEditable(int arg0, int arg1) {
991             return false;
992         }
993
994         public Object JavaDoc getValueAt(int row, int col) {
995             Match match = data.get(row);
996             
997             switch (col) {
998             case 0: return match.key;
999             case 1: {
1000                if ("source".equals(match.key)) {
1001                    // Reformat URLs since they are too long
1002
String JavaDoc name = match.value;
1003                    int index = name.lastIndexOf('/');
1004                    if (index != -1) {
1005                        return name.substring(index+1) + " : " + name;
1006                    }
1007                }
1008                return match.value;
1009            }
1010            default: throw new IllegalArgumentException JavaDoc();
1011            }
1012        }
1013
1014        public void setValueAt(Object JavaDoc arg0, int arg1, int arg2) {
1015            throw new IllegalArgumentException JavaDoc();
1016        }
1017
1018        public void addTableModelListener(TableModelListener JavaDoc arg0) {
1019            // For now, no changes occur - we just replace the whole model
1020
}
1021
1022        public void removeTableModelListener(TableModelListener JavaDoc arg0) {
1023        }
1024    }
1025    
1026    class IndexListModel implements ListModel JavaDoc {
1027        private List JavaDoc<IndexEntry> data;
1028        
1029        IndexListModel(List JavaDoc<IndexEntry> entries) {
1030            this.data = entries;
1031        }
1032        
1033        IndexListModel() {
1034            // Empty
1035
}
1036        
1037        IndexEntry getEntry(int row) {
1038            return data != null ? data.get(row) : null;
1039        }
1040    
1041        public int getSize() {
1042            return data != null ? data.size() : 0;
1043        }
1044
1045        public Object JavaDoc getElementAt(int row) {
1046            return getUrlLabel(data.get(row).url);
1047        }
1048
1049        public void addListDataListener(ListDataListener JavaDoc arg0) {
1050            // The list is fixed - we'll be replacing the model whenever
1051
// we change it
1052
}
1053
1054        public void removeListDataListener(ListDataListener JavaDoc arg0) {
1055        }
1056    }
1057    
1058    class IndexEntry {
1059        IndexEntry(URL JavaDoc url, ClassIndexImpl index, File JavaDoc segment) {
1060            this.url = url;
1061            this.index = index;
1062            this.segment = segment;
1063        }
1064        private URL JavaDoc url;
1065        private ClassIndexImpl index;
1066        private File JavaDoc segment;
1067    }
1068}
1069
Popular Tags