1 package org.netbeans.modules.gsf.tools.lucene; 2 3 import java.awt.Toolkit ; 4 import java.io.File ; 5 import java.io.IOException ; 6 import java.io.Serializable ; 7 import java.net.URL ; 8 import java.util.ArrayList ; 9 import java.util.Collections ; 10 import java.util.Comparator ; 11 import java.util.EnumSet ; 12 import java.util.Enumeration ; 13 import java.util.HashMap ; 14 import java.util.HashSet ; 15 import java.util.List ; 16 import java.util.Map ; 17 import java.util.Map ; 18 import java.util.Set ; 19 import javax.swing.JFileChooser ; 20 import javax.swing.ListModel ; 21 import javax.swing.ListSelectionModel ; 22 import javax.swing.event.ListDataListener ; 23 import javax.swing.event.ListSelectionEvent ; 24 import javax.swing.event.ListSelectionListener ; 25 import javax.swing.event.TableModelListener ; 26 import javax.swing.table.TableModel ; 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 47 56 final class IndexBrowserTopComponent extends TopComponent { 57 58 private static IndexBrowserTopComponent instance; 59 60 62 private static final String PREFERRED_ID = "IndexBrowserTopComponent"; 63 64 private File segmentFile; 65 66 private IndexBrowserTopComponent() { 67 initComponents(); 68 setName(NbBundle.getMessage(IndexBrowserTopComponent.class, "CTL_IndexBrowserTopComponent")); 69 setToolTipText(NbBundle.getMessage(IndexBrowserTopComponent.class, "HINT_IndexBrowserTopComponent")); 70 72 73 matchTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 74 ListSelectionModel rowSM = matchTable.getSelectionModel(); 76 rowSM.addListSelectionListener(new ListSelectionListener () { 77 public void valueChanged(ListSelectionEvent e) { 78 if (e.getValueIsAdjusting()) return; 80 81 ListSelectionModel lsm = 82 (ListSelectionModel )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 () { 98 public void valueChanged(ListSelectionEvent 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 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 <IndexEntry> list = new ArrayList <IndexEntry>(); 128 if (allIndexButton.isSelected()) { 129 Map <URL , ClassIndexImpl> map = ClassIndexManager.getDefault().getAllIndices(); 132 for (URL url : map.keySet()) { 133 ClassIndexImpl index = map.get(url); 134 File segment = index.getSegment(); 135 list.add(new IndexEntry(url, index, segment)); 136 } 137 } else { 138 assert indexForButton.isSelected(); 139 140 File 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 158 final ClassPath cp = ClassPath.getClassPath(fo, ClassPath.SOURCE); 159 Set <ClassIndexImpl> indices = new HashSet <ClassIndexImpl>(); 160 ClassIndex.createQueriesForRoots(cp, true, indices); 161 for (ClassIndexImpl index : indices) { 162 URL url = index.getRoot(); 163 File segment = index.getSegment(); 164 list.add(new IndexEntry(url, index, segment)); 165 } 166 } 167 indexList.setModel(new IndexListModel(list)); 168 } 169 170 private String getUrlLabel(URL url) { 171 String 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 getSegmentLabel(File segment) { 180 String 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 segment) { 193 this.segmentFile = segment; 194 lukeButton.setEnabled(segment != null); 195 } 196 197 private void show(SearchResult result) { 198 File segment = (File )result.getSegment(); 199 setSegment(segment); 200 segmentLabel.setText(segment.getPath()); 201 ClassIndexImpl index = (ClassIndexImpl)result.getIndex(); 202 URL 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 model = new SearchDocumentTableModel(result); 211 documentTable.setModel(model); 212 213 showDocument(result.getDocumentNumber()); 215 } 216 217 222 private void initComponents() { 224 225 jRadioButton3 = new javax.swing.JRadioButton (); 226 indexGroup = new javax.swing.ButtonGroup (); 227 jLabel1 = new javax.swing.JLabel (); 228 keyCombo = new javax.swing.JComboBox (); 229 jLabel2 = new javax.swing.JLabel (); 230 nameField = new javax.swing.JTextField (); 231 jLabel3 = new javax.swing.JLabel (); 232 typeCombo = new javax.swing.JComboBox (); 233 searchButton = new javax.swing.JButton (); 234 jSplitPane1 = new javax.swing.JSplitPane (); 235 jScrollPane1 = new javax.swing.JScrollPane (); 236 matchTable = new javax.swing.JTable (); 237 jPanel2 = new javax.swing.JPanel (); 238 jScrollPane2 = new javax.swing.JScrollPane (); 239 documentTable = new javax.swing.JTable (); 240 jLabel5 = new javax.swing.JLabel (); 241 docIdField = new javax.swing.JTextField (); 242 prevButton = new javax.swing.JButton (); 243 nextButton = new javax.swing.JButton (); 244 jPanel1 = new javax.swing.JPanel (); 245 allIndexButton = new javax.swing.JRadioButton (); 246 indexForButton = new javax.swing.JRadioButton (); 247 indexFileField = new javax.swing.JTextField (); 248 indexFileButton = new javax.swing.JButton (); 249 jScrollPane3 = new javax.swing.JScrollPane (); 250 indexList = new javax.swing.JList (); 251 jButton1 = new javax.swing.JButton (); 252 indexLabel = new javax.swing.JLabel (); 253 jLabel4 = new javax.swing.JLabel (); 254 jLabel6 = new javax.swing.JLabel (); 255 segmentLabel = new javax.swing.JLabel (); 256 lukeButton = new javax.swing.JButton (); 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 (0, 0, 0, 0)); 261 262 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, "Key:"); 263 264 keyCombo.setModel(new javax.swing.DefaultComboBoxModel (new String [] { "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 () { 269 public void actionPerformed(java.awt.event.ActionEvent evt) { 270 search(evt); 271 } 272 }); 273 274 org.openide.awt.Mnemonics.setLocalizedText(jLabel3, "Type:"); 275 276 typeCombo.setModel(new javax.swing.DefaultComboBoxModel (new String [] { "Prefix", "Exact", "Regexp" })); 277 278 org.openide.awt.Mnemonics.setLocalizedText(searchButton, "Search"); 279 searchButton.addActionListener(new java.awt.event.ActionListener () { 280 public void actionPerformed(java.awt.event.ActionEvent 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 () { 300 public void actionPerformed(java.awt.event.ActionEvent evt) { 301 docIdFieldActionPerformed(evt); 302 } 303 }); 304 305 org.openide.awt.Mnemonics.setLocalizedText(prevButton, "Previous"); 306 prevButton.addActionListener(new java.awt.event.ActionListener () { 307 public void actionPerformed(java.awt.event.ActionEvent evt) { 308 prevButtonActionPerformed(evt); 309 } 310 }); 311 312 org.openide.awt.Mnemonics.setLocalizedText(nextButton, "Next"); 313 nextButton.addActionListener(new java.awt.event.ActionListener () { 314 public void actionPerformed(java.awt.event.ActionEvent 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 (0, 0, 0, 0)); 355 allIndexButton.addActionListener(new java.awt.event.ActionListener () { 356 public void actionPerformed(java.awt.event.ActionEvent 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 (0, 0, 0, 0)); 366 indexForButton.addActionListener(new java.awt.event.ActionListener () { 367 public void actionPerformed(java.awt.event.ActionEvent evt) { 368 changeIndex(evt); 369 } 370 }); 371 372 indexFileField.setColumns(8); 373 indexFileField.addActionListener(new java.awt.event.ActionListener () { 374 public void actionPerformed(java.awt.event.ActionEvent evt) { 375 changeIndex(evt); 376 } 377 }); 378 379 org.openide.awt.Mnemonics.setLocalizedText(indexFileButton, "..."); 380 indexFileButton.addActionListener(new java.awt.event.ActionListener () { 381 public void actionPerformed(java.awt.event.ActionEvent 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 () { 391 public void actionPerformed(java.awt.event.ActionEvent 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 () { 442 public void actionPerformed(java.awt.event.ActionEvent 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 } 522 private void openInLuke(java.awt.event.ActionEvent evt) { File f = null; 524 String l = System.getProperty("luke.dir"); 525 if (l != null) { 526 f = new File (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 (f.getParentFile().getAbsolutePath() + File.separator + ".."); 532 try { 533 f = f.getCanonicalFile(); 534 } catch (IOException ioe) { 535 ErrorManager.getDefault().notify(ioe); 536 } 537 538 f = new File (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 []{ System.getProperty("java.home") + File.separator + "bin" + File.separator + "java", 549 "-jar", f.getPath(), "-index", segmentFile.getPath()+File.separator + "gsf"}); 550 } 551 catch (IOException ex) { 552 Exceptions.printStackTrace(ex); 553 } 554 } 556 private void updateIndices(java.awt.event.ActionEvent evt) { updateIndices(); 558 } 560 561 private File getCurrentFile() { 562 String name = indexFileField.getText(); 563 564 File f = new File (name); 565 566 if (f.exists()) { 567 return f; 568 } 569 570 return null; 571 } 572 573 private void browseIndexFile(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 575 chooser.setDialogTitle("Choose a source file"); 576 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 577 chooser.setMultiSelectionEnabled(false); 578 579 File 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 } 599 private void changeIndex(java.awt.event.ActionEvent evt) { updateIndices(); 601 } 603 private void docIdFieldActionPerformed(java.awt.event.ActionEvent evt) { int doc = Integer.parseInt(docIdField.getText().trim()); 605 showDocument(doc); 606 } 608 private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) { String 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 nfe) { 616 return; 617 } 618 } 620 private void prevButtonActionPerformed(java.awt.event.ActionEvent evt) { String 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 nfe) { 631 return; 632 } 633 } 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 ioe) { 649 Exceptions.printStackTrace(ioe); 650 } 651 } 652 } 653 654 private void search(java.awt.event.ActionEvent evt) { String name = nameField.getText().trim(); 657 String type = typeCombo.getSelectedItem().toString(); 658 String field = keyCombo.getSelectedItem().toString(); 659 Set <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 <URL , ClassIndexImpl> map = ClassIndexManager.getDefault().getAllIndices(); 669 Set <SearchResult> result = new HashSet <SearchResult>(); 670 671 try { 672 for (ClassIndexImpl index : map.values()) { 673 index.gsfSearch(field, name, kind, scope, result); 674 } 675 676 TableModel model = new SearchMatchModel(result, field); 677 matchTable.setModel(model); 678 679 681 } catch (IOException ioe) { 682 Exceptions.printStackTrace(ioe); 683 } 684 } 686 private IndexReader indexReader; 687 688 689 private javax.swing.JRadioButton allIndexButton; 691 private javax.swing.JTextField docIdField; 692 private javax.swing.JTable documentTable; 693 private javax.swing.JButton indexFileButton; 694 private javax.swing.JTextField indexFileField; 695 private javax.swing.JRadioButton indexForButton; 696 private javax.swing.ButtonGroup indexGroup; 697 private javax.swing.JLabel indexLabel; 698 private javax.swing.JList indexList; 699 private javax.swing.JButton jButton1; 700 private javax.swing.JLabel jLabel1; 701 private javax.swing.JLabel jLabel2; 702 private javax.swing.JLabel jLabel3; 703 private javax.swing.JLabel jLabel4; 704 private javax.swing.JLabel jLabel5; 705 private javax.swing.JLabel jLabel6; 706 private javax.swing.JPanel jPanel1; 707 private javax.swing.JPanel jPanel2; 708 private javax.swing.JRadioButton jRadioButton3; 709 private javax.swing.JScrollPane jScrollPane1; 710 private javax.swing.JScrollPane jScrollPane2; 711 private javax.swing.JScrollPane jScrollPane3; 712 private javax.swing.JSplitPane jSplitPane1; 713 private javax.swing.JComboBox keyCombo; 714 private javax.swing.JButton lukeButton; 715 private javax.swing.JTable matchTable; 716 private javax.swing.JTextField nameField; 717 private javax.swing.JButton nextButton; 718 private javax.swing.JButton prevButton; 719 private javax.swing.JButton searchButton; 720 private javax.swing.JLabel segmentLabel; 721 private javax.swing.JComboBox typeCombo; 722 724 729 public static synchronized IndexBrowserTopComponent getDefault() { 730 if (instance == null) { 731 instance = new IndexBrowserTopComponent(); 732 } 733 return instance; 734 } 735 736 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 } 758 759 public void componentClosed() { 760 } 762 763 764 public Object writeReplace() { 765 return new ResolvableHelper(); 766 } 767 768 protected String preferredID() { 769 return PREFERRED_ID; 770 } 771 772 private ListModel getIndexList() { 773 return new IndexListModel(); 774 } 775 776 private TableModel getEmptyDocumentTableModel() { 777 return new SearchDocumentTableModel(); 778 } 779 780 private TableModel getEmptyMatchTableModel() { 781 return new SearchMatchModel(); 782 } 783 784 final static class ResolvableHelper implements Serializable { 785 private static final long serialVersionUID = 1L; 786 public Object readResolve() { 787 return IndexBrowserTopComponent.getDefault(); 788 } 789 } 790 791 class SearchMatchModel implements TableModel { 792 private List <Map <String , SearchResult>> data; 793 private IndexReader indexReader; 794 795 SearchMatchModel() { 796 } 798 799 SearchMatchModel(Set <SearchResult> results, String key) { 800 data = new ArrayList <Map <String , SearchResult>>(); 801 for (SearchResult result : results) { 802 String [] matches = result.getValues(key); 803 if (matches != null) { 804 for (String match : matches) { 805 Map <String ,SearchResult> map = new HashMap <String ,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 Collections.sort(data, new Comparator <Map <String ,SearchResult>>() { 821 public int compare(Map <String , SearchResult> m1, 822 Map <String , SearchResult> m2) { 823 String key1 = m1.keySet().iterator().next(); 824 String 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 getColumnName(int col) { 843 switch (col) { 844 case 0: return "Match"; 845 case 1: return "Document"; 846 default: throw new IllegalArgumentException (); 847 } 848 } 849 850 public Class <?> getColumnClass(int arg0) { 851 return String .class; 852 } 853 854 public boolean isCellEditable(int arg0, int arg1) { 855 return false; 856 } 857 858 public Object getValueAt(int row, int col) { 859 Map <String ,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 String value = map.getValue("require"); 867 if (value != null) { 868 return value; 869 } else { 870 return ""; 871 } 872 } 873 default: throw new IllegalArgumentException (); 874 } 875 } 876 877 public SearchResult getSearchResultAt(int row) { 878 Map <String ,SearchResult> entry = data.get(row); 879 return entry.values().iterator().next(); 880 } 881 882 public void setValueAt(Object arg0, int arg1, int arg2) { 883 throw new IllegalArgumentException (); 884 } 885 886 public void addTableModelListener(TableModelListener arg0) { 887 } 889 890 public void removeTableModelListener(TableModelListener arg0) { 891 } 892 } 893 894 class SearchDocumentTableModel implements TableModel { 895 private IndexReader indexReader; 896 897 private class Match { 898 private String key; 899 private String value; 900 901 Match(String key, String value) { 902 this.key = key; 903 this.value = value; 904 } 905 } 906 907 private String [] fields; 908 private List <Match> data; 909 910 911 SearchDocumentTableModel(Document luceneDoc, IndexReader reader, int docId) { 912 this.indexReader = reader; 913 data = new ArrayList <Match>(); 914 initFromLuceneDoc(luceneDoc); 915 } 916 917 SearchDocumentTableModel(SearchResult doc) { 918 data = new ArrayList <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 ("unchecked") 929 Enumeration <Field> en = luceneDoc.fields(); 930 while (en.hasMoreElements()) { 931 Field f = en.nextElement(); 932 String key = f.name(); 933 String value = f.stringValue(); 934 data.add(new Match(key, value)); 935 } 936 937 Collections.sort(data, new Comparator <Match>() { 939 public int compare(Match m1, 940 Match m2) { 941 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 } 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 getColumnName(int col) { 979 switch (col) { 980 case 0: return "Key"; 981 case 1: return "Value"; 982 default: throw new IllegalArgumentException (); 983 } 984 } 985 986 public Class <?> getColumnClass(int col) { 987 return String .class; 988 } 989 990 public boolean isCellEditable(int arg0, int arg1) { 991 return false; 992 } 993 994 public Object 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 String 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 (); 1011 } 1012 } 1013 1014 public void setValueAt(Object arg0, int arg1, int arg2) { 1015 throw new IllegalArgumentException (); 1016 } 1017 1018 public void addTableModelListener(TableModelListener arg0) { 1019 } 1021 1022 public void removeTableModelListener(TableModelListener arg0) { 1023 } 1024 } 1025 1026 class IndexListModel implements ListModel { 1027 private List <IndexEntry> data; 1028 1029 IndexListModel(List <IndexEntry> entries) { 1030 this.data = entries; 1031 } 1032 1033 IndexListModel() { 1034 } 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 getElementAt(int row) { 1046 return getUrlLabel(data.get(row).url); 1047 } 1048 1049 public void addListDataListener(ListDataListener arg0) { 1050 } 1053 1054 public void removeListDataListener(ListDataListener arg0) { 1055 } 1056 } 1057 1058 class IndexEntry { 1059 IndexEntry(URL url, ClassIndexImpl index, File segment) { 1060 this.url = url; 1061 this.index = index; 1062 this.segment = segment; 1063 } 1064 private URL url; 1065 private ClassIndexImpl index; 1066 private File segment; 1067 } 1068} 1069 | Popular Tags |