KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > ui > search > SearchControlPanel


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xam.ui.search;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Toolkit JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.swing.JPanel JavaDoc;
30 import org.netbeans.modules.xml.xam.Component;
31 import org.netbeans.modules.xml.xam.ui.category.SearchComponent;
32 import org.netbeans.modules.xml.xam.ui.highlight.Highlight;
33 import org.netbeans.modules.xml.xam.ui.highlight.HighlightGroup;
34 import org.netbeans.modules.xml.xam.ui.highlight.HighlightManager;
35 import org.openide.util.NbBundle;
36
37 /**
38  * Presents the search component for XAM-based editors.
39  *
40  * @author Nathan Fiedler
41  */

42 public abstract class SearchControlPanel extends JPanel JavaDoc
43         implements ActionListener JavaDoc, SearchComponent, SearchListener {
44     /** silence compiler warnings */
45     private static final long serialVersionUID = 1L;
46     /** The search input field. */
47     private SearchFieldPanel searchField;
48     /** If not null, the results of the most recent search. */
49     private List JavaDoc<Object JavaDoc> searchResults;
50     /** Offset into searchResults which is currently shown. */
51     private int searchResultIndex;
52
53     /**
54      * Creates new form SearchControlPanel.
55      */

56     public SearchControlPanel() {
57         initComponents();
58         nextButton.setEnabled(false);
59         prevButton.setEnabled(false);
60         closeButton.addActionListener(this);
61         nextButton.addActionListener(this);
62         prevButton.addActionListener(this);
63         resetButton.addActionListener(this);
64         searchField = new SearchFieldPanel();
65         searchField.addSearchListener(this);
66         fieldPanel.add(searchField, BorderLayout.CENTER);
67     }
68
69     public void actionPerformed(ActionEvent JavaDoc event) {
70         Object JavaDoc src = event.getSource();
71         if (src == closeButton) {
72             dismissSearch();
73             hideComponent();
74         } else if (src == resetButton) {
75             dismissSearch();
76             searchField.prepareForInput(true);
77         } else if (src == nextButton) {
78             searchResultIndex++;
79
80             if (searchResultIndex >= searchResults.size()) {
81                 searchResultIndex = 0;
82                 beep();
83             }
84             showSearchResult(searchResults.get(searchResultIndex));
85         } else if (src == prevButton) {
86             searchResultIndex--;
87           
88             if (searchResultIndex < 0) {
89                 searchResultIndex = searchResults.size() - 1;
90                 beep();
91             }
92             showSearchResult(searchResults.get(searchResultIndex));
93         }
94     }
95     
96     private void beep() {
97         Toolkit.getDefaultToolkit().beep();
98     }
99
100     /**
101      * Hide the results of the previous search, if any.
102      */

103     private void dismissSearch() {
104         hideResults();
105         // Clear the cached search results.
106
searchResults = null;
107         // No results, no navigation either.
108
nextButton.setEnabled(false);
109         prevButton.setEnabled(false);
110         resultsLabel.setText(" ");
111     }
112
113     protected void hideResults() {
114         HighlightManager hm = HighlightManager.getDefault();
115         List JavaDoc<HighlightGroup> groups = hm.getHighlightGroups(HighlightGroup.SEARCH);
116         if (groups != null) {
117             for (HighlightGroup group : groups) {
118                 hm.removeHighlightGroup(group);
119             }
120         }
121     }
122
123     public java.awt.Component JavaDoc getComponent() {
124         return this;
125     }
126
127     public void hideComponent() {
128         setVisible(false);
129     }
130
131     public void showComponent() {
132         setVisible(true);
133     }
134
135     public void setVisible(boolean visible) {
136         super.setVisible(visible);
137
138         if (visible) {
139             searchField.prepareForInput(false);
140         }
141         revalidate();
142         repaint();
143     }
144
145     public void searchCommenced(SearchEvent event) {
146         dismissSearch();
147     }
148
149     public void searchDismissed(SearchEvent event) {
150         dismissSearch();
151         hideComponent();
152     }
153
154     public void searchFailed(SearchEvent event) {
155         nextButton.setEnabled(false);
156         prevButton.setEnabled(false);
157         SearchException exc = event.getException();
158         String JavaDoc msg = null;
159         if (exc != null) {
160             msg = exc.getMessage();
161             if (msg == null || msg.length() == 0) {
162                 msg = exc.toString();
163             }
164         } else {
165             msg = NbBundle.getMessage(SearchControlPanel.class,
166                     "LBL_SearchControlPanel_Failed");
167         }
168         resultsLabel.setText(msg);
169     }
170
171     public void searchFinished(SearchEvent event) {
172         searchResults = event.getResults();
173         if (searchResults.isEmpty()) {
174             nextButton.setEnabled(false);
175             prevButton.setEnabled(false);
176             resultsLabel.setText(NbBundle.getMessage(SearchControlPanel.class,
177                     "LBL_SearchControlPanel_NoResults"));
178         } else {
179             nextButton.setEnabled(true);
180             prevButton.setEnabled(true);
181             // Generate Highlight instances for each matching result, and its
182
// parents, grand parents, and so on, up to the root component.
183
HighlightManager hm = HighlightManager.getDefault();
184             HighlightGroup group = new HighlightGroup(HighlightGroup.SEARCH);
185             Iterator JavaDoc<Object JavaDoc> iter = searchResults.iterator();
186
187             while (iter.hasNext()) {
188                 Object JavaDoc object = iter.next();
189
190                 if ( !(object instanceof Component)) {
191                   continue;
192                 }
193                 Component comp = (Component) object;
194                 SearchHighlight h = new SearchHighlight(comp, Highlight.SEARCH_RESULT);
195                 group.addHighlight(h);
196                 Component parent = comp.getParent();
197                 
198                 while (parent != null) {
199                     h = new SearchHighlight(parent, Highlight.SEARCH_RESULT_PARENT);
200                     group.addHighlight(h);
201                     parent = parent.getParent();
202                 }
203             }
204             hm.addHighlightGroup(group);
205             // Show the first matching result.
206
searchResultIndex = 0;
207             showSearchResult(searchResults.get(searchResultIndex));
208             int count = searchResults.size();
209             if (count == 1) {
210                 resultsLabel.setText(NbBundle.getMessage(SearchControlPanel.class,
211                         "LBL_SearchControlPanel_OneResult"));
212             } else {
213                 resultsLabel.setText(NbBundle.getMessage(SearchControlPanel.class,
214                         "LBL_SearchControlPanel_MultipleResults", count));
215             }
216         }
217     }
218
219     /**
220      * Set the search providers to be made available in the search field.
221      *
222      * @param providers the available search providers (may be null).
223      */

224     public void setProviders(Collection JavaDoc providers) {
225         if (providers != null && providers.size() > 0) {
226             searchField.setProviders(providers);
227             searchField.setEnabled(true);
228         } else {
229             searchField.setEnabled(false);
230         }
231     }
232
233     /**
234      * Make the given search result visible in the editor.
235      *
236      * @param result search result to show.
237      */

238     protected abstract void showSearchResult(Object JavaDoc result);
239
240     /**
241      * Concrete implementation of a Highlight.
242      */

243     private static class SearchHighlight extends Highlight {
244
245         public SearchHighlight(Component comp, String JavaDoc searchResults) {
246             super(comp, searchResults);
247         }
248     }
249
250     /** This method is called from within the constructor to
251      * initialize the form.
252      * WARNING: Do NOT modify this code. The content of this method is
253      * always regenerated by the Form Editor.
254      */

255     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
256
private void initComponents() {
257         closeButton = new javax.swing.JButton JavaDoc();
258         fieldLabel = new javax.swing.JLabel JavaDoc();
259         fieldPanel = new javax.swing.JPanel JavaDoc();
260         nextButton = new javax.swing.JButton JavaDoc();
261         prevButton = new javax.swing.JButton JavaDoc();
262         resetButton = new javax.swing.JButton JavaDoc();
263         resultsLabel = new javax.swing.JLabel JavaDoc();
264
265         setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createMatteBorder(1, 0, 0, 0, new java.awt.Color JavaDoc(0, 0, 0)), javax.swing.BorderFactory.createEmptyBorder(2, 2, 2, 2)));
266         closeButton.setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/org/netbeans/modules/xml/xam/ui/search/search_close.png")));
267         closeButton.setToolTipText(java.util.ResourceBundle.getBundle("org/netbeans/modules/xml/xam/ui/search/Bundle").getString("HINT_SearchControlPanel_Close"));
268         closeButton.setBorderPainted(false);
269         closeButton.setContentAreaFilled(false);
270         closeButton.setMargin(new java.awt.Insets JavaDoc(2, 2, 2, 2));
271         closeButton.setRolloverIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/org/netbeans/modules/xml/xam/ui/search/search_close_light.png")));
272
273         fieldLabel.setLabelFor(fieldPanel);
274         fieldLabel.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/xml/xam/ui/search/Bundle").getString("LBL_SearchControlPanel_Find"));
275
276         fieldPanel.setLayout(new java.awt.BorderLayout JavaDoc());
277
278         nextButton.setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/xml/xam/ui/search/Bundle").getString("KEY_SearchControlPanel_FindNext").charAt(0));
279         nextButton.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/xml/xam/ui/search/Bundle").getString("LBL_SearchControlPanel_FindNext"));
280         nextButton.setToolTipText(java.util.ResourceBundle.getBundle("org/netbeans/modules/xml/xam/ui/search/Bundle").getString("HINT_SearchControlPanel_FindNext"));
281
282         prevButton.setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/xml/xam/ui/search/Bundle").getString("KEY_SearchControlPanel_FindPrevious").charAt(0));
283         prevButton.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/xml/xam/ui/search/Bundle").getString("LBL_SearchControlPanel_FindPrevious"));
284         prevButton.setToolTipText(java.util.ResourceBundle.getBundle("org/netbeans/modules/xml/xam/ui/search/Bundle").getString("HINT_SearchControlPanel_FindPrevious"));
285
286         resetButton.setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/xml/xam/ui/search/Bundle").getString("KEY_SearchControlPanel_Reset").charAt(0));
287         resetButton.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/xml/xam/ui/search/Bundle").getString("LBL_SearchControlPanel_Reset"));
288         resetButton.setToolTipText(java.util.ResourceBundle.getBundle("org/netbeans/modules/xml/xam/ui/search/Bundle").getString("TIP_SearchControlPanel_Reset"));
289
290         resultsLabel.setText(" ");
291
292         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
293         this.setLayout(layout);
294         layout.setHorizontalGroup(
295             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
296             .add(layout.createSequentialGroup()
297                 .add(closeButton)
298                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
299                 .add(fieldLabel)
300                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
301                 .add(fieldPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
302                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
303                 .add(nextButton)
304                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
305                 .add(prevButton)
306                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
307                 .add(resetButton)
308                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
309                 .add(resultsLabel)
310                 .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
311         );
312         layout.setVerticalGroup(
313             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
314             .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
315                 .add(closeButton)
316                 .add(fieldLabel)
317                 .add(nextButton)
318                 .add(prevButton)
319                 .add(resetButton)
320                 .add(resultsLabel))
321             .add(fieldPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
322         );
323     }// </editor-fold>//GEN-END:initComponents
324

325     // Variables declaration - do not modify//GEN-BEGIN:variables
326
private javax.swing.JButton JavaDoc closeButton;
327     private javax.swing.JLabel JavaDoc fieldLabel;
328     private javax.swing.JPanel JavaDoc fieldPanel;
329     private javax.swing.JButton JavaDoc nextButton;
330     private javax.swing.JButton JavaDoc prevButton;
331     private javax.swing.JButton JavaDoc resetButton;
332     private javax.swing.JLabel JavaDoc resultsLabel;
333     // End of variables declaration//GEN-END:variables
334
}
335
Popular Tags