KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > search > SearchPanel


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
21 package org.netbeans.modules.search;
22
23
24 import java.awt.Component JavaDoc;
25 import java.awt.Dialog JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.awt.event.FocusAdapter JavaDoc;
29 import java.awt.event.FocusEvent JavaDoc;
30 import java.awt.event.FocusListener JavaDoc;
31 import java.beans.Customizer JavaDoc;
32 import java.beans.PropertyChangeEvent JavaDoc;
33 import java.beans.PropertyChangeListener JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Collection JavaDoc;
36 import java.util.Collections JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40 import javax.swing.JButton JavaDoc;
41 import javax.swing.JPanel JavaDoc;
42 import javax.swing.event.ChangeEvent JavaDoc;
43 import javax.swing.event.ChangeListener JavaDoc;
44
45 import org.openide.DialogDescriptor;
46 import org.openide.DialogDisplayer;
47 import org.openide.awt.Mnemonics;
48 import org.openide.util.HelpCtx;
49 import org.openide.util.NbBundle;
50 import org.openidex.search.SearchType;
51
52
53 /**
54  * Panel which shows all enabled search types for user allowing them to
55  * select appropriate criteria for a new search.
56  *
57  * @author Peter Zavadsky
58  * @author Marian Petras
59  * @see SearchTypePanel
60  */

61 public final class SearchPanel extends JPanel JavaDoc
62                                implements PropertyChangeListener JavaDoc,
63                                           FocusListener JavaDoc,
64                                           ChangeListener JavaDoc,
65                                           ActionListener JavaDoc {
66     
67     /** */
68     public static final String JavaDoc PROP_DIALOG_TITLE
69                                = "Find Files dialog title"; //NOI18N
70

71     /** Return status code - returned if Cancel button has been pressed. */
72     public static final int RET_CANCEL = 0;
73     
74     /** Return status code - returned if OK button has been pressed. */
75     public static final int RET_OK = 1;
76
77     /** Dialog descriptor. */
78     private DialogDescriptor dialogDescriptor;
79
80     /** OK button. */
81     private final JButton JavaDoc okButton;
82     
83     /** Cancel button. */
84     private final JButton JavaDoc cancelButton;
85
86     /** Java equivalent. */
87     private Dialog JavaDoc dialog;
88
89     /** Return status. */
90     private int returnStatus = RET_CANCEL;
91
92     /** Ordered list of <code>SearchTypePanel</code>'s. */
93     private List JavaDoc orderedSearchTypePanels;
94
95     /** Whether some criterion is customized. */
96     private boolean customized;
97     
98     
99     /**
100      * Creates a new <code>SearchPanel</code>.
101      *
102      * @param searchTypeList list of <code>SearchType</code>s to use
103      */

104     public SearchPanel(List JavaDoc searchTypeList) {
105         this(searchTypeList, false);
106     }
107
108     /**
109      * Creates a new <code>SearchPanel</code>.
110      *
111      * @param searchTypeList list of <code>SearchType</code>s to use
112      * @param isCustomized sets customized flag indicating there is at least
113      * one from <code>SearchType</code>s already set and
114      * search - okButton should be enabled
115      */

116     public SearchPanel(List JavaDoc searchTypeList, boolean activateWithPreviousValues) {
117         this.orderedSearchTypePanels = new ArrayList JavaDoc(searchTypeList.size());
118
119         // Default values of criteria.
120
Iterator JavaDoc it;
121         
122         /* Create search type panels: */
123         Map JavaDoc sortedCriteria;
124         {
125             SearchCriterion[] allCriteria = SearchProjectSettings.getInstance()
126                                             .getSearchCriteria();
127             sortedCriteria = Utils.sortCriteriaBySearchType(allCriteria);
128         }
129         Collection JavaDoc processedClassNames = new ArrayList JavaDoc();
130         for (it = searchTypeList.iterator(); it.hasNext(); ) {
131             SearchType searchType = (SearchType) it.next();
132             String JavaDoc className = searchType.getClass().getName();
133             if (processedClassNames.contains(className)) {
134                 continue;
135             }
136             processedClassNames.add(className);
137
138             /*
139              * we will use activateWithPreviousValues for the decision
140              * whether to pre-fill the search pattern
141              * (with the last entry in the history) or not.
142              */

143             final boolean initPanelFromHistory =
144                    !activateWithPreviousValues
145                    && FindDialogMemory.getDefault()
146                       .wasSearchTypeUsed(searchType.getClass().getName());
147             SearchTypePanel newPanel = new SearchTypePanel(searchType,
148                                                            initPanelFromHistory);
149             Collection JavaDoc savedCriteria = (sortedCriteria == null)
150                     ? null
151                     : (Collection JavaDoc) sortedCriteria.get(className);
152             
153             int index = orderedSearchTypePanels.indexOf(newPanel);
154             if (savedCriteria != null) {
155                 SearchTypePanel targetPanel = (index == -1)
156                         ? newPanel
157                         : (SearchTypePanel) orderedSearchTypePanels.get(index);
158                 targetPanel.addSavedCriteria(
159                         Collections.unmodifiableCollection(savedCriteria));
160             }
161             if (index != -1) {
162                 continue;
163             }
164             orderedSearchTypePanels.add(newPanel);
165             newPanel.addPropertyChangeListener(this);
166         }
167         
168         initComponents();
169
170         // For each search type create one tab as its search type panel.
171
for (it = orderedSearchTypePanels.iterator(); it.hasNext(); ) {
172             tabbedPane.add((Component JavaDoc) it.next());
173         }
174               
175         // initial selection
176
int index = 0; //prevents bug #43843 ("AIOOBE after push button Modify Search")
177
/*
178          * we will use activateWithPreviousValues for the decision
179          * whether to pre-select the last selected tab
180          * (with the last used SearchType) or not.
181          */

182         if(activateWithPreviousValues){
183             index = getIndexOfSearchType(FindDialogMemory.getDefault().getLastSearchType());
184             if(index<0){
185                 //prevents bug #43843 ("AIOOBE after push button Modify Search")
186
index=0;
187             }
188         }
189         tabbedPane.setSelectedIndex(index);
190
191         setName(NbBundle.getBundle(SearchPanel.class)
192                 .getString("TEXT_TITLE_CUSTOMIZE")); //NOI18N
193

194         okButton = new JButton JavaDoc(NbBundle.getBundle(SearchPanel.class)
195                                .getString("TEXT_BUTTON_SEARCH")); //NOI18N
196
updateIsCustomized();
197
198         Mnemonics.setLocalizedText(cancelButton = new JButton JavaDoc(),
199                                    NbBundle.getBundle(SearchPanel.class)
200                                    .getString("TEXT_BUTTON_CANCEL")); //NOI18N
201

202         Object JavaDoc options[] = new Object JavaDoc[] {okButton, cancelButton};
203
204         initAccessibility();
205         
206         // Creates representing dialog descriptor.
207
dialogDescriptor = new DialogDescriptor(
208             this,
209             getName(),
210             true,
211             options,
212             options[0],
213             DialogDescriptor.BOTTOM_ALIGN,
214             null, //<null> HelpCtx - no help
215
this);
216     }
217         
218     /**
219      * This method is called when the Search or Close button is pressed.
220      * It closes the Find dialog, cleans up the individual panels
221      * and sets the return status.
222      *
223      * @see #getReturnStatus
224      */

225     public void actionPerformed(final ActionEvent JavaDoc evt) {
226         doClose(evt.getSource() == okButton ? RET_OK : RET_CANCEL);
227     }
228     
229     void setTitle(String JavaDoc title) {
230         dialogDescriptor.setTitle(title);
231     }
232
233     private void initAccessibility() {
234         this.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(SearchPanel.class).getString("ACS_SearchPanel")); // NOI18N
235
tabbedPane.getAccessibleContext().setAccessibleName(NbBundle.getBundle(SearchPanel.class).getString("ACSN_Tabs")); // NOI18N
236
tabbedPane.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(SearchPanel.class).getString("ACSD_Tabs")); // NOI18N
237
okButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(SearchPanel.class).getString("ACS_TEXT_BUTTON_SEARCH")); // NOI18N
238
cancelButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(SearchPanel.class).getString("ACS_TEXT_BUTTON_CANCEL")); // NOI18N
239
}
240     
241     /** This method is called from within the constructor to
242      * initialize the form.
243      * WARNING: Do NOT modify this code. The content of this method is
244      * always regenerated by the Form Editor.
245      */

246     private void initComponents() {//GEN-BEGIN:initComponents
247
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
248
249         tabbedPane = new javax.swing.JTabbedPane JavaDoc();
250
251         setLayout(new java.awt.GridBagLayout JavaDoc());
252
253         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
254         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
255         gridBagConstraints.weightx = 1.0;
256         gridBagConstraints.weighty = 1.0;
257         add(tabbedPane, gridBagConstraints);
258
259     }//GEN-END:initComponents
260

261
262     // Variables declaration - do not modify//GEN-BEGIN:variables
263
private javax.swing.JTabbedPane JavaDoc tabbedPane;
264     // End of variables declaration//GEN-END:variables
265

266     /** @return true if some criterion customized. */
267     public boolean isCustomized() {
268         return customized;
269     }
270     
271     /**
272      * Gets ordered criterion panels.
273      *
274      * @return iterator over properly ordered <code>SearchTypePanel</code>'s.
275      */

276     private List JavaDoc getOrderedSearchTypePanels() {
277         return new ArrayList JavaDoc(orderedSearchTypePanels);
278     }
279
280     /** @return name of criterion at index is modified. */
281     private String JavaDoc getTabText(int index) {
282         try {
283             return ((SearchTypePanel)getOrderedSearchTypePanels().get(index)).getName();
284         } catch (ArrayIndexOutOfBoundsException JavaDoc ex) {
285             return null;
286         }
287     }
288
289     /**
290      * Gets array of customized search types.
291      *
292      * @return current state of customized search types.
293      */

294     public SearchType[] getCustomizedSearchTypes() {
295         
296         List JavaDoc searchTypeList = new ArrayList JavaDoc(orderedSearchTypePanels.size());
297         
298         for (Iterator JavaDoc it = orderedSearchTypePanels.iterator(); it.hasNext(); ) {
299             SearchTypePanel searchTypePanel = (SearchTypePanel) it.next();
300             if (searchTypePanel.isCustomized()) {
301                 searchTypeList.add(searchTypePanel.getSearchType());
302             }
303         }
304         
305         return (SearchType[]) searchTypeList.toArray(
306                 new SearchType[searchTypeList.size()]);
307     }
308     
309     /**
310      * Getter for return status property.
311      *
312      * @return the return status of this dialog - one of RET_OK or RET_CANCEL
313      */

314     public int getReturnStatus () {
315         return returnStatus;
316     }
317
318     /** Closes dialog. */
319     private void doClose(int returnStatus) {
320
321         Iterator JavaDoc it = orderedSearchTypePanels.iterator();
322         while (it.hasNext()) {
323             SearchTypePanel panel = (SearchTypePanel) it.next();
324             panel.removePropertyChangeListener(this);
325         }
326         
327         if (returnStatus == RET_OK) {
328             FindDialogMemory.getDefault().clearSearchTypesUsed();
329         }
330         
331         int selectedIndex = tabbedPane.getSelectedIndex();
332         if (selectedIndex >= 0) {
333             SearchTypePanel panel = getSearchTypePanel(selectedIndex);
334             if (returnStatus == RET_OK){
335                 FindDialogMemory.getDefault().setLastUsedSearchType(panel.getSearchType());
336                 panel.onOk();
337             }
338             else {
339                 panel.onCancel();
340             }
341         }
342                           
343         this.returnStatus = returnStatus;
344
345         dialog.setVisible(false);
346         dialog.dispose();
347     }
348
349     /** Shows dialog created from <code>DialogDescriptor</code> which wraps this instance. */
350     public void showDialog() {
351         dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
352         dialog.setModal(true);
353         tabbedPane.addFocusListener(this);
354         
355         dialog.pack();
356         dialog.setVisible(true);
357     }
358     
359     /**
360      * This method is called when the tabbed pane gets focus after the Find
361      * dialog is displayed.
362      * It lets the first tab to initialize focus.
363      */

364     public void focusGained(FocusEvent JavaDoc e) {
365         tabbedPane.removeFocusListener(this);
366
367         int selectedIndex = tabbedPane.getSelectedIndex();
368         if (selectedIndex >= 0) {
369             SearchTypePanel panel = getSearchTypePanel(selectedIndex);
370             if ((panel != null) && (panel.customizerComponent != null)) {
371                 panel.customizerComponent.requestFocus();
372             }
373         }
374
375         tabbedPane.addChangeListener(this);
376     }
377     
378     /**
379      * This method is called when the tabbed pane looses focus.
380      * It does nothing and it is here just because this class declares that
381      * it implements the <code>FocusListener</code> interface.
382      *
383      * @see #focusGained(FocusEvent)
384      */

385     public void focusLost(FocusEvent JavaDoc e) {
386         //does nothing
387
}
388             
389     /**
390      * This method is called when tab selection changes.
391      * It initializes the customizer below the selected tab.
392      */

393     public void stateChanged(ChangeEvent JavaDoc e) {
394         int selectedIndex = tabbedPane.getSelectedIndex();
395         if (selectedIndex >= 0) {
396             SearchTypePanel panel = getSearchTypePanel(selectedIndex);
397             if (panel != null) {
398                 panel.initializeWithObject();
399             }
400         }
401     }
402
403     /** Implements <code>PropertyChangeListener</code> interface. */
404     public void propertyChange(PropertyChangeEvent JavaDoc event) {
405         if(SearchTypePanel.PROP_CUSTOMIZED.equals(event.getPropertyName())) {
406             updateIsCustomized();
407         }
408
409         for(int i = 0; i < tabbedPane.getTabCount(); i++) {
410             tabbedPane.setTitleAt(i, getTabText(i));
411             tabbedPane.setIconAt(i, null);
412         }
413     }
414     /**
415      */

416     private void updateIsCustomized() {
417         customized = getCustomizedSearchTypes().length != 0;
418
419         okButton.setEnabled(isCustomized());
420     }
421
422    
423     /**
424      * Gets a <code>SearchTypePanel</code> for the given tab index.
425      *
426      * @param index index of the tab to get the panel from
427      * @return <code>SearchTypePanel</code> at the given tab;
428      * or <code>null</code> if there is none at the given tab index
429      */

430     private SearchTypePanel getSearchTypePanel(int index) {
431         SearchTypePanel searchTypePanel = null;
432         
433         Iterator JavaDoc it = getOrderedSearchTypePanels().iterator();
434         while(index >= 0 && it.hasNext()) {
435             searchTypePanel = (SearchTypePanel)it.next();
436             index--;
437         }
438         
439         return searchTypePanel;
440     }
441     
442     /**
443      * Gets the index for the the given <code>SearchType</code>
444      *
445      * @param searchTypeToFind <code>SearchType</code> to get the index for.
446      * @return index of the given <code>SearchType</code>
447      */

448     private int getIndexOfSearchType(SearchType searchTypeToFind) {
449         
450         if(searchTypeToFind==null){
451             return -1;
452         }
453         
454         SearchTypePanel searchTypePanel = null;
455         Iterator JavaDoc it = getOrderedSearchTypePanels().iterator();
456         
457         int index = -1;
458         while(it.hasNext()) {
459             index++;
460             searchTypePanel = (SearchTypePanel) it.next();
461             
462             if(searchTypePanel.getSearchType().getClass() == searchTypeToFind.getClass()){
463                 return index;
464             }
465         }
466         
467         return -1;
468     }
469     
470 }
471
Popular Tags