KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javadoc > search > IndexSearch


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.javadoc.search;
21
22 import java.io.Externalizable JavaDoc;
23 import java.io.ObjectStreamException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.lang.ref.Reference JavaDoc;
27 import java.lang.ref.SoftReference JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29 import javax.swing.ComboBoxModel JavaDoc;
30 import javax.swing.DefaultListModel JavaDoc;
31 import javax.swing.ImageIcon JavaDoc;
32 import javax.swing.JSplitPane JavaDoc;
33 import org.netbeans.api.java.source.ElementHandle;
34 import org.netbeans.api.java.source.UiUtils;
35 import org.netbeans.api.javahelp.Help;
36 import org.netbeans.modules.javadoc.settings.DocumentationSettings;
37 import org.openide.awt.HtmlBrowser;
38 import org.openide.windows.TopComponent;
39 import org.openide.util.RequestProcessor;
40 import org.openide.NotifyDescriptor;
41 import org.openide.DialogDisplayer;
42 import org.openide.filesystems.FileObject;
43 import org.openide.util.HelpCtx;
44 import org.openide.util.Lookup;
45 import org.openide.util.NbBundle;
46 import org.openide.util.Utilities;
47
48 /** Main window for documentation index search
49  *
50  * @author Petr Hrebejk, Petr Suchomel
51  */

52 public final class IndexSearch
53             extends TopComponent
54     implements Externalizable JavaDoc {
55
56     private static final String JavaDoc INDEX_SEARCH_HELP_CTX_KEY = "javadoc.search.window"; //NOI18N
57

58     private static final java.awt.Dimension JavaDoc PREFFERED_SIZE = new java.awt.Dimension JavaDoc( 580, 430 );
59
60     static final long serialVersionUID =1200348578933093459L;
61
62     /** The only instance allowed in system */
63     private static Reference JavaDoc refIndexSearch;
64     
65     /** cache of previously searched strings */
66     private static Object JavaDoc[] MRU = new Object JavaDoc[0];
67
68     /** Search engine */
69     private JavadocSearchEngine searchEngine = null;
70
71     /** The state of the window is stored in hidden options of DocumentationSettings */
72     private DocumentationSettings ds = DocumentationSettings.getDefault();
73
74     private String JavaDoc quickFind;
75
76     /* Current sort mode */
77     private String JavaDoc currentSort = "A"; // NOI18N
78

79     /* Hand made components */
80     private javax.swing.JScrollPane JavaDoc resultsScrollPane;
81     private javax.swing.JList JavaDoc resultsList;
82     //private HtmlBrowser.BrowserComponent quickBrowser;
83
private HtmlBrowser quickBrowser;
84     private JSplitPane JavaDoc splitPanel;
85
86     /** Button titles */
87
88     private static final String JavaDoc STR_FIND = NbBundle.getMessage(IndexSearch.class, "CTL_SEARCH_ButtonFind"); //NOI18N
89
private static final String JavaDoc STR_STOP = NbBundle.getMessage(IndexSearch.class, "CTL_SEARCH_ButtonStop"); //NOI18N
90

91     /** List models for different sorts */
92     private ArrayList JavaDoc results = new ArrayList JavaDoc();
93
94     private DefaultListModel JavaDoc referenceModel = null;
95     private DefaultListModel JavaDoc typeModel = null;
96     private DefaultListModel JavaDoc alphaModel = null;
97
98     /* Holds split position if the quick view is disabled */
99     private int oldSplit = DocumentationSettings.getDefault().getIdxSearchSplit();
100
101     private final DefaultListModel JavaDoc waitModel = new DefaultListModel JavaDoc();
102     private final DefaultListModel JavaDoc notModel = new DefaultListModel JavaDoc();
103     private boolean setDividerLocation;
104
105     /** Initializes the Form */
106     public IndexSearch() {
107         ResourceBundle JavaDoc b = NbBundle.getBundle(IndexSearch.class);
108         DocIndexItem dii = new DocIndexItem( b.getString("CTL_SEARCH_Wait" ), "", null, "" ); //NOI18N
109
dii.setIconIndex( DocSearchIcons.ICON_WAIT );
110         waitModel.addElement( dii );
111
112         DocIndexItem diin = new DocIndexItem( b.getString("CTL_SEARCH_NotFound" ), "", null, "" ); //NOI18N
113
diin.setIconIndex( DocSearchIcons.ICON_NOT_FOUND );
114         notModel.addElement( diin );
115         
116         initComponents ();
117         
118         // Force winsys to not show tab when this comp is alone
119
putClientProperty("TabPolicy", "HideWhenAlone"); // NOI18N
120

121         javax.swing.ComboBoxEditor JavaDoc editor = searchComboBox.getEditor();
122         editor.addActionListener (new java.awt.event.ActionListener JavaDoc () {
123                                       public void actionPerformed (java.awt.event.ActionEvent JavaDoc evt) {
124                                           if ( searchEngine == null )
125                                               searchButtonActionPerformed( evt );
126                                       }
127                                   }
128                                  );
129
130         // Split panel
131
splitPanel = new JSplitPane JavaDoc (JSplitPane.VERTICAL_SPLIT);
132         splitPanel.setPreferredSize(PREFFERED_SIZE);
133         
134         splitPanel.setDividerLocation(oldSplit / 100.0);
135         //previous line does not work
136
//setDividerLocation must be set in open
137
setDividerLocation = true;
138         
139
140         java.awt.GridBagConstraints JavaDoc gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
141         gridBagConstraints1.gridwidth = 0;
142         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
143         gridBagConstraints1.insets = new java.awt.Insets JavaDoc (5, 0, 0, 0);
144         gridBagConstraints1.weightx = 1.0;
145         gridBagConstraints1.weighty = 1.0;
146         add (splitPanel, gridBagConstraints1);
147
148         // Results - SrollPane & JList
149
resultsScrollPane = new javax.swing.JScrollPane JavaDoc ();
150
151         resultsList = new javax.swing.JList JavaDoc ();
152         resultsList.setSelectionMode (javax.swing.ListSelectionModel.SINGLE_SELECTION );
153         resultsList.addKeyListener (new java.awt.event.KeyAdapter JavaDoc () {
154                                         public void keyPressed (java.awt.event.KeyEvent JavaDoc evt) {
155                                             resultsListKeyPressed (evt);
156                                         }
157                                     }
158                                    );
159         resultsList.addMouseListener (new java.awt.event.MouseAdapter JavaDoc () {
160                                           public void mouseClicked (java.awt.event.MouseEvent JavaDoc evt) {
161                                               resultsListMouseClicked (evt);
162                                           }
163                                       }
164                                      );
165
166         resultsScrollPane.setViewportView (resultsList);
167
168         splitPanel.setTopComponent(resultsScrollPane);
169
170         // Quick browser component
171
quickBrowser = new HtmlBrowser( true, false );//.BrowserComponent( true, false );
172
quickBrowser.setEnableLocation( false );
173         quickBrowser.setEnableHome( false );
174         //browser buttons without border are too top
175
quickBrowser.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(8, 0, 0, 0)));
176         splitPanel.setBottomComponent(quickBrowser);
177
178         DefaultListModel JavaDoc listModel = new DefaultListModel JavaDoc(); // PENDING: Change to SortedArrayList
179
resultsList.setModel( listModel );
180
181         IndexListCellRenderer cr = new IndexListCellRenderer();
182         resultsList.setCellRenderer( cr );
183
184         resultsList.getSelectionModel().addListSelectionListener(
185             new javax.swing.event.ListSelectionListener JavaDoc() {
186                 public void valueChanged( javax.swing.event.ListSelectionEvent JavaDoc evt ) {
187                     showHelp( true );
188                 }
189             });
190         resultsScrollPane.validate();
191
192         searchButton.setText( STR_FIND );
193
194         
195         sourceButton.setIcon(new ImageIcon JavaDoc(Utilities.loadImage("org/netbeans/modules/javadoc/resources/showSource.gif"))); // NOI18N
196
byReferenceButton.setIcon(new ImageIcon JavaDoc(Utilities.loadImage("org/netbeans/modules/javadoc/resources/refSort.gif"))); // NOI18N
197
byTypeButton.setIcon(new ImageIcon JavaDoc(Utilities.loadImage("org/netbeans/modules/javadoc/resources/typeSort.gif"))); // NOI18N
198
byNameButton.setIcon(new ImageIcon JavaDoc(Utilities.loadImage("org/netbeans/modules/javadoc/resources/alphaSort.gif"))); // NOI18N
199
quickViewButton.setIcon(new ImageIcon JavaDoc(Utilities.loadImage("org/netbeans/modules/javadoc/resources/list_html.gif"))); // NOI18N
200
quickViewButton.setSelectedIcon(new ImageIcon JavaDoc(Utilities.loadImage("org/netbeans/modules/javadoc/resources/list_only.gif"))); // NOI18N
201

202         javax.swing.ButtonGroup JavaDoc bg = new javax.swing.ButtonGroup JavaDoc();
203         bg.add( byReferenceButton );
204         bg.add( byTypeButton );
205         bg.add( byNameButton );
206
207         // Adding ToolTips
208

209         searchButton.setToolTipText(b.getString( "CTL_SEARCH_search_ToolTip" )); //NOI18N
210
byReferenceButton.setToolTipText(b.getString( "CTL_SEARCH_byReference_ToolTip" )); //NOI18N
211
byReferenceButton.setMnemonic(b.getString("CTL_SEARCH_byReference_Mnemonic").charAt(0)); // NOI18N
212
byTypeButton.setToolTipText(b.getString( "CTL_SEARCH_byType_ToolTip" )); //NOI18N
213
byTypeButton.setMnemonic(b.getString("CTL_SEARCH_byType_Mnemonic").charAt(0)); // NOI18N
214
byNameButton.setToolTipText(b.getString( "CTL_SEARCH_byName_ToolTip" )); //NOI18N
215
byNameButton.setMnemonic(b.getString("CTL_SEARCH_byName_Mnemonic").charAt(0)); // NOI18N
216
quickViewButton.setToolTipText(b.getString( "CTL_SEARCH_quickView_ToolTip" )); //NOI18N
217
quickViewButton.setMnemonic(b.getString("CTL_SEARCH_quickView_Mnemonic").charAt(0)); // NOI18N
218
sourceButton.setToolTipText(b.getString( "CTL_SEARCH_showSource_ToolTip" )); //NOI18N
219
sourceButton.setMnemonic(b.getString("CTL_SEARCH_showSource_Mnemonic").charAt(0)); // NOI18N
220
searchComboBox.setToolTipText(b.getString( "ACS_SEARCH_SearchComboBoxA11yDesc" )); //NOI18N
221
resultsList.setToolTipText(b.getString( "ACS_SEARCH_ResultsListA11yDesc" )); //NOI18N
222
quickBrowser.setToolTipText(b.getString( "ACS_SEARCH_QuickBrowserA11yDesc" )); //NOI18N
223

224         searchButton.setMnemonic(b.getString("CTL_SEARCH_ButtonFind_Mnemonic").charAt(0)); // NOI18N
225
helpButton.setMnemonic(b.getString("CTL_SEARCH_ButtonHelp_Mnemonic").charAt(0)); // NOI18N
226

227         initAccessibility();
228     }
229     
230     public int getPersistenceType() {
231         return TopComponent.PERSISTENCE_NEVER;
232     }
233     
234     protected String JavaDoc preferredID() {
235         return "JavaDocIndexSearch"; // NOI18N
236
}
237     
238     public HelpCtx getHelpCtx() {
239         return new HelpCtx(INDEX_SEARCH_HELP_CTX_KEY);
240     }
241     
242     private void initAccessibility() {
243         ResourceBundle JavaDoc b = NbBundle.getBundle(IndexSearch.class);
244         getAccessibleContext().setAccessibleName(b.getString("ACS_SEARCH_PanelA11yName")); // NOI18N
245
getAccessibleContext().setAccessibleDescription(b.getString("ACS_SEARCH_PanelA11yDesc")); // NOI18N
246
searchComboBox.getAccessibleContext().setAccessibleName(b.getString("ACS_SEARCH_SearchComboBoxA11yName")); // NOI18N
247
searchComboBox.getAccessibleContext().setAccessibleDescription(b.getString("ACS_SEARCH_SearchComboBoxA11yDesc")); // NOI18N
248
resultsList.getAccessibleContext().setAccessibleName(b.getString("ACS_SEARCH_ResultsListA11yName")); // NOI18N
249
resultsList.getAccessibleContext().setAccessibleDescription(b.getString("ACS_SEARCH_ResultsListA11yDesc")); // NOI18N
250
quickBrowser.getAccessibleContext().setAccessibleName(b.getString("ACS_SEARCH_QuickBrowserA11yName")); // NOI18N
251
quickBrowser.getAccessibleContext().setAccessibleDescription(b.getString("ACS_SEARCH_QuickBrowserA11yDesc")); // NOI18N
252
}
253     
254     /** This method is called from within the constructor to
255      * initialize the form.
256      * WARNING: Do NOT modify this code. The content of this method is
257      * always regenerated by the FormEditor.
258      */

259     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
260
private void initComponents() {
261         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
262
263         jPanel1 = new javax.swing.JPanel JavaDoc();
264         searchComboBox = new javax.swing.JComboBox JavaDoc(MRU);
265         searchButton = new javax.swing.JButton JavaDoc();
266         sourceButton = new javax.swing.JButton JavaDoc();
267         byNameButton = new javax.swing.JToggleButton JavaDoc();
268         byReferenceButton = new javax.swing.JToggleButton JavaDoc();
269         byTypeButton = new javax.swing.JToggleButton JavaDoc();
270         quickViewButton = new javax.swing.JToggleButton JavaDoc();
271         helpButton = new javax.swing.JButton JavaDoc();
272
273         FormListener formListener = new FormListener();
274
275         setLayout(new java.awt.GridBagLayout JavaDoc());
276
277         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(8, 8, 8, 8)));
278         jPanel1.setLayout(new java.awt.GridBagLayout JavaDoc());
279
280         searchComboBox.setEditable(true);
281         searchComboBox.addActionListener(formListener);
282
283         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
284         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
285         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
286         gridBagConstraints.weightx = 1.0;
287         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 8);
288         jPanel1.add(searchComboBox, gridBagConstraints);
289
290         searchButton.setText(org.openide.util.NbBundle.getBundle(IndexSearch.class).getString("IndexSearch.searchButton.text"));
291         searchButton.addActionListener(formListener);
292
293         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
294         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
295         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 3);
296         jPanel1.add(searchButton, gridBagConstraints);
297
298         sourceButton.setPreferredSize(new java.awt.Dimension JavaDoc(25, 25));
299         sourceButton.setMinimumSize(new java.awt.Dimension JavaDoc(25, 25));
300         sourceButton.addActionListener(formListener);
301
302         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
303         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 3);
304         jPanel1.add(sourceButton, gridBagConstraints);
305
306         byNameButton.setSelected(true);
307         byNameButton.setPreferredSize(new java.awt.Dimension JavaDoc(25, 25));
308         byNameButton.setActionCommand("A");
309         byNameButton.setMinimumSize(new java.awt.Dimension JavaDoc(25, 25));
310         byNameButton.setRequestFocusEnabled(false);
311         byNameButton.addActionListener(formListener);
312
313         jPanel1.add(byNameButton, new java.awt.GridBagConstraints JavaDoc());
314
315         byReferenceButton.setPreferredSize(new java.awt.Dimension JavaDoc(25, 25));
316         byReferenceButton.setActionCommand("R");
317         byReferenceButton.setMinimumSize(new java.awt.Dimension JavaDoc(25, 25));
318         byReferenceButton.setRequestFocusEnabled(false);
319         byReferenceButton.addActionListener(formListener);
320
321         jPanel1.add(byReferenceButton, new java.awt.GridBagConstraints JavaDoc());
322
323         byTypeButton.setPreferredSize(new java.awt.Dimension JavaDoc(25, 25));
324         byTypeButton.setActionCommand("T");
325         byTypeButton.setMinimumSize(new java.awt.Dimension JavaDoc(25, 25));
326         byTypeButton.setRequestFocusEnabled(false);
327         byTypeButton.addActionListener(formListener);
328
329         jPanel1.add(byTypeButton, new java.awt.GridBagConstraints JavaDoc());
330
331         quickViewButton.setSelected(true);
332         quickViewButton.setRequestFocusEnabled(false);
333         quickViewButton.addActionListener(formListener);
334
335         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
336         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 0, 0);
337         jPanel1.add(quickViewButton, gridBagConstraints);
338
339         helpButton.setToolTipText(org.openide.util.NbBundle.getBundle(IndexSearch.class).getString("CTL_SEARCH_ButtonHelp_tooltip"));
340         helpButton.setText(org.openide.util.NbBundle.getBundle(IndexSearch.class).getString("CTL_SEARCH_ButtonHelp"));
341         helpButton.addActionListener(formListener);
342
343         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
344         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
345         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 0, 0);
346         jPanel1.add(helpButton, gridBagConstraints);
347
348         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
349         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
350         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
351         gridBagConstraints.weightx = 1.0;
352         add(jPanel1, gridBagConstraints);
353
354     }
355
356     // Code for dispatching events from components to event handlers.
357

358     private class FormListener implements java.awt.event.ActionListener JavaDoc {
359         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
360             if (evt.getSource() == searchComboBox) {
361                 IndexSearch.this.searchComboBoxActionPerformed(evt);
362             }
363             else if (evt.getSource() == searchButton) {
364                 IndexSearch.this.searchButtonActionPerformed(evt);
365             }
366             else if (evt.getSource() == sourceButton) {
367                 IndexSearch.this.showSource(evt);
368             }
369             else if (evt.getSource() == byNameButton) {
370                 IndexSearch.this.sortButtonActionPerformed(evt);
371             }
372             else if (evt.getSource() == byReferenceButton) {
373                 IndexSearch.this.sortButtonActionPerformed(evt);
374             }
375             else if (evt.getSource() == byTypeButton) {
376                 IndexSearch.this.sortButtonActionPerformed(evt);
377             }
378             else if (evt.getSource() == quickViewButton) {
379                 IndexSearch.this.quickViewButtonActionPerformed(evt);
380             }
381             else if (evt.getSource() == helpButton) {
382                 IndexSearch.this.helpButtonActionPerformed(evt);
383             }
384         }
385     }
386     // </editor-fold>//GEN-END:initComponents
387

388     private void helpButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_helpButtonActionPerformed
389
Help help=(Help)Lookup.getDefault().lookup(Help.class);
390         
391         help.showHelp(getHelpCtx());
392     }//GEN-LAST:event_helpButtonActionPerformed
393

394     private void showSource (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_showSource
395
showSource();
396     }//GEN-LAST:event_showSource
397

398     private void sortButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_sortButtonActionPerformed
399

400         currentSort = evt.getActionCommand();
401         ds.setIdxSearchSort( currentSort );
402         sortResults();
403
404     }//GEN-LAST:event_sortButtonActionPerformed
405

406     private void quickViewButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_quickViewButtonActionPerformed
407
if ( quickViewButton.isSelected() ) {
408             splitPanel.setDividerLocation( oldSplit == 100 ? 0.5 : oldSplit / 100.0 );
409             ds.setIdxSearchSplit( oldSplit == 100 ? 50 : oldSplit );
410             ds.setIdxSearchNoHtml( false );
411             showHelp( true );
412         }
413         else {
414             oldSplit = (int) (splitPanel.getDividerLocation() / splitPanel.getSize().getHeight() * 100);
415             splitPanel.setDividerLocation( 1.0 );
416             ds.setIdxSearchSplit( 100 );
417             ds.setIdxSearchNoHtml( true );
418         }
419     }//GEN-LAST:event_quickViewButtonActionPerformed
420

421     private void resultsListKeyPressed (java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_resultsListKeyPressed
422
// Add your handling code here:
423
if ( evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER ||
424                 evt.getKeyCode() == java.awt.event.KeyEvent.VK_SPACE ) {
425             /*
426             if ( evt.isShiftDown() ) {
427               showSource();
428               evt.consume();
429         }
430             else
431             */

432             showHelp();
433         }
434     }//GEN-LAST:event_resultsListKeyPressed
435

436     private void resultsListMouseClicked (java.awt.event.MouseEvent JavaDoc evt) {//GEN-FIRST:event_resultsListMouseClicked
437
// Add your handling code here:
438
if ( evt.getClickCount() == 2 ) {
439             if ( evt.isShiftDown() ) {
440                 showSource();
441                 evt.consume();
442             }
443             else
444                 showHelp();
445         }
446     }//GEN-LAST:event_resultsListMouseClicked
447

448     private void showHelp( ) {
449         showHelp( false );
450     }
451
452     RequestProcessor.Task task=null;
453     /** Invokes the browser with help */
454     private void showHelp( boolean quick ) {
455
456         if (quick && splitPanel.getDividerLocation() == 100 )
457             return;
458
459         if ( resultsList.getMinSelectionIndex() < 0 )
460             return;
461
462         
463         DocIndexItem dii = (DocIndexItem)resultsList.getModel().getElementAt( resultsList.getMinSelectionIndex() );
464
465         try {
466             URL JavaDoc url = dii.getURL();
467
468             if ( url == null )
469                 return;
470
471             // Workaround for bug in FileSystems
472
String JavaDoc strUrl = url.toString();
473
474             if ( strUrl.startsWith( "nbfs:" ) && strUrl.charAt( 5 ) != '/' ){ // NOI18N
475
url = new URL JavaDoc( "nbfs:/" + strUrl.substring( 5 ) ); // NOI18N
476
}
477             
478             if ( quick ){
479                 final URL JavaDoc furl = url;
480                 if( task != null )
481                     task.cancel();
482                 task = RequestProcessor.getDefault().post( new Runnable JavaDoc(){
483                     public void run(){
484                         quickBrowser.setURL( furl );
485                     }
486                 }, 100 );
487             }
488             else
489                 HtmlBrowser.URLDisplayer.getDefault().showURL( url );
490         }
491         catch ( java.net.MalformedURLException JavaDoc ex ) {
492             // Do nothing if the URL isn't O.K.
493
}
494     }
495
496     /** Tryies to find source code for the selected item in repository. If the
497      * is foun opens the source
498      */

499     private void showSource( ) {
500
501         if ( resultsList.getMinSelectionIndex() < 0 ) {
502             return;
503         }
504
505         DocIndexItem dii = (DocIndexItem)resultsList.getModel().getElementAt( resultsList.getMinSelectionIndex() );
506
507         try {
508             Object JavaDoc[] e = SrcFinder.findSource( dii.getPackage(), dii.getURL() );
509
510             if ( e != null ) {
511                 FileObject toOpen = (FileObject) e[0];
512                 ElementHandle eh = (ElementHandle) e[1];
513                 UiUtils.open(toOpen, eh);
514             }
515             else {
516                 NotifyDescriptor.Message nd = new NotifyDescriptor.Message( NbBundle.getMessage(IndexSearch.class, "MSG_SEARCH_SrcNotFound" ) ); //NOI18N
517
DialogDisplayer.getDefault().notify( nd );
518             }
519
520         }
521         catch ( java.net.MalformedURLException JavaDoc e ) {
522             //System.out.println( e );
523
}
524     }
525
526     private void searchComboBoxActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_searchComboBoxActionPerformed
527
/*if ( searchEngine == null )
528           searchButtonActionPerformed( evt );*/

529     }//GEN-LAST:event_searchComboBoxActionPerformed
530

531     private void searchButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_searchButtonActionPerformed
532
if ( searchEngine == null ) {
533             if ( searchComboBox.getEditor().getItem().toString() != null &&
534                     searchComboBox.getEditor().getItem().toString().length() > 0 ) {
535                 searchEngine = JavadocSearchEngine.getDefault();
536                 go();
537             }
538         }
539         else {
540             searchEngine.stop();
541             searchEngine = null;
542         }
543     }//GEN-LAST:event_searchButtonActionPerformed
544

545
546     // Variables declaration - do not modify//GEN-BEGIN:variables
547
private javax.swing.JToggleButton JavaDoc byNameButton;
548     private javax.swing.JToggleButton JavaDoc byReferenceButton;
549     private javax.swing.JToggleButton JavaDoc byTypeButton;
550     private javax.swing.JButton JavaDoc helpButton;
551     private javax.swing.JPanel JavaDoc jPanel1;
552     private javax.swing.JToggleButton JavaDoc quickViewButton;
553     private javax.swing.JButton JavaDoc searchButton;
554     private javax.swing.JComboBox JavaDoc searchComboBox;
555     private javax.swing.JButton JavaDoc sourceButton;
556     // End of variables declaration//GEN-END:variables
557

558
559     private void searchStoped() {
560         searchEngine = null;
561         javax.swing.SwingUtilities.invokeLater( new Runnable JavaDoc() {
562                                                     public void run() {
563                                                         searchButton.setText( STR_FIND );
564                                                         searchButton.setMnemonic(NbBundle.getMessage(IndexSearch.class,"CTL_SEARCH_ButtonFind_Mnemonic").charAt(0)); // NOI18N
565
if ( resultsList.getModel().getSize() > 0 ) {
566                                                             resultsList.setSelectedIndex( 0 );
567                                                             resultsList.grabFocus();
568                                                         }
569                                                         referenceModel = typeModel = alphaModel = null;
570                                                         sortResults();
571                                                     }
572                                                 } );
573     }
574
575     void setTextToFind( String JavaDoc toFind ) {
576         quickFind = toFind;
577         /*
578         if ( toFind != null ) {
579           quickFind = toFind;
580     }
581         */

582     }
583
584
585     public void open() {
586         super.open();
587
588         if ( quickFind != null ) {
589             searchComboBox.getEditor().setItem( quickFind );
590             if ( searchEngine == null ) {
591                 searchButtonActionPerformed( null );
592             }
593         }
594
595         quickFind = null;
596         searchComboBox.getEditor().selectAll();
597         
598         if (setDividerLocation) {
599             splitPanel.setDividerLocation(oldSplit / 100.0);
600             setDividerLocation = false;
601         }
602     }
603
604     java.awt.Dimension JavaDoc getPrefferedSize() {
605         return PREFFERED_SIZE;
606     }
607
608     public static IndexSearch getDefault() {
609         IndexSearch indexSearch;
610         if (refIndexSearch == null || null == (indexSearch = (IndexSearch) refIndexSearch.get())) {
611             indexSearch = new IndexSearch ();
612             refIndexSearch = new SoftReference JavaDoc(indexSearch);
613
614             indexSearch.setName( NbBundle.getMessage(IndexSearch.class, "CTL_SEARCH_WindowTitle") ); //NOI18N
615
indexSearch.setIcon(Utilities.loadImage("org/netbeans/modules/javadoc/resources/searchDoc.gif")); // NOI18N
616
}
617         return indexSearch;
618     }
619
620     public void resolveButtonState() {
621
622         final String JavaDoc sort = ds.getIdxSearchSort();
623         final boolean noHtml = ds.isIdxSearchNoHtml();
624         final int split = ds.getIdxSearchSplit();
625
626         currentSort = sort;
627
628         javax.swing.SwingUtilities.invokeLater( new Runnable JavaDoc() {
629                                                     public void run() {
630                                                         byNameButton.setSelected( sort.equals( "A" ) ); // NOI18N
631
byReferenceButton.setSelected( sort.equals( "R" ) ); // NOI18N
632
byTypeButton.setSelected( sort.equals( "T" ) ); // NOI18N
633

634                                                         quickViewButton.setSelected( !noHtml );
635
636                                                         splitPanel.setDividerLocation(split / 100.0);
637                                                     }
638                                                 } );
639     }
640     
641     /**
642      * Replaces previously stored instances with the default one. Just due to
643      * backward compatibility.
644      * @return the default instance
645      * @throws ObjectStreamException
646      */

647     private Object JavaDoc readResolve() throws ObjectStreamException JavaDoc {
648         return getDefault();
649     }
650
651     void go() {
652         String JavaDoc toFind = searchComboBox.getEditor().getItem().toString().trim();
653
654         // Alocate array for results
655
results = new ArrayList JavaDoc();
656
657         //Clear all models
658
referenceModel = null;
659         typeModel = null;
660         alphaModel = null;
661
662         // Try to find this string in Combo
663

664         for ( int i = 0; i < searchComboBox.getItemCount(); i++ ) {
665             if ( searchComboBox.getItemAt( i ).toString().equals( toFind ) || i >= 10 ) {
666                 searchComboBox.removeItemAt( i );
667             }
668         }
669
670         searchComboBox.insertItemAt( toFind, 0 );
671         mirrorMRUStrings();
672         searchComboBox.getEditor().setItem( toFind );
673
674         resultsList.setModel( waitModel );
675
676         try {
677             searchEngine.search(new String JavaDoc[]{toFind}, new JavadocSearchEngine.SearchEngineCallback(){
678                 public void finished(){
679                     searchStoped();
680                 }
681                 public void addItem(DocIndexItem item){
682                     results.add(item);
683                 }
684             });
685         }
686         catch(NoJavadocException noJdc){
687             DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message( noJdc.getMessage() ) ); //NOI18N
688
searchStoped();
689             return;
690         }
691         
692         searchButton.setText( STR_STOP );
693         searchButton.setMnemonic(NbBundle.getMessage(IndexSearch.class,"CTL_SEARCH_ButtonStop_Mnemonic").charAt(0)); // NOI18N
694
}
695     
696     private void mirrorMRUStrings() {
697         ComboBoxModel JavaDoc model = searchComboBox.getModel();
698         int size = model.getSize();
699         MRU = new Object JavaDoc[size];
700         for (int i = 0; i < size; i++) {
701             MRU[i] = model.getElementAt(i);
702         }
703     }
704
705     DefaultListModel JavaDoc generateModel( java.util.Comparator JavaDoc comp ) {
706         DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
707
708         java.util.Collections.sort( results, comp );
709         java.util.Iterator JavaDoc it = results.iterator();
710
711         String JavaDoc pckg = null;
712
713         while ( it.hasNext() ) {
714             DocIndexItem dii = (DocIndexItem)it.next();
715             if ( comp == DocIndexItem.REFERENCE_COMPARATOR &&
716                     !dii.getPackage().equals( pckg ) &&
717                     dii.getIconIndex() != DocSearchIcons.ICON_PACKAGE ) {
718                 DocIndexItem ndii = new DocIndexItem( "PACKAGE ", dii.getPackage(), null, "" ); // NOI18N
719
ndii.setIconIndex( DocSearchIcons.ICON_PACKAGE );
720                 model.addElement( ndii );
721                 pckg = dii.getPackage();
722             }
723
724             model.addElement( dii );
725         }
726         return model;
727     }
728
729     void sortResults() {
730
731         if ( results.size() < 1 ) {
732             resultsList.setModel( notModel );
733         }
734         else if ( currentSort.equals( "R" ) ) { // NOI18N
735
if ( referenceModel == null ) {
736                 resultsList.setModel( waitModel );
737                 resultsList.invalidate();
738                 resultsList.revalidate();
739                 referenceModel = generateModel( DocIndexItem.REFERENCE_COMPARATOR );
740             }
741             resultsList.setModel( referenceModel );
742         }
743         else if ( currentSort.equals( "T" ) ) { // NOI18N
744
if ( typeModel == null ) {
745                 resultsList.setModel( waitModel );
746                 resultsList.invalidate();
747                 resultsList.revalidate();
748                 typeModel = generateModel( DocIndexItem.TYPE_COMPARATOR );
749             }
750             resultsList.setModel( typeModel );
751         }
752         else if ( currentSort.equals( "A" ) ) { // NOI18N
753
if ( alphaModel == null ) {
754                 resultsList.setModel( waitModel );
755                 resultsList.invalidate();
756                 resultsList.revalidate();
757                 alphaModel = generateModel( DocIndexItem.ALPHA_COMPARATOR );
758             }
759             resultsList.setModel( alphaModel );
760         }
761
762         resultsList.invalidate();
763         resultsList.revalidate();
764         resultsList.repaint();
765     }
766 }
767
Popular Tags