KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output > FindDialogPanel


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.core.output;
21
22 import java.awt.event.KeyAdapter JavaDoc;
23 import java.awt.event.KeyEvent JavaDoc;
24 import java.util.Vector JavaDoc;
25 import javax.swing.DefaultComboBoxModel JavaDoc;
26 import javax.swing.JButton JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
28 import org.openide.DialogDescriptor;
29 import org.openide.util.NbBundle;
30
31 /**
32  *
33  * @author Ales Kemr
34  * @version 1.0
35  */

36 public class FindDialogPanel extends javax.swing.JPanel JavaDoc {
37
38     static final long serialVersionUID =5048678953767663114L;
39
40     private static FindDialogPanel findPanel;
41     private static java.awt.Dialog JavaDoc dialog = null;
42     private static boolean accepted = false;
43     private static JButton JavaDoc acceptButton;
44     private Vector JavaDoc history = new Vector JavaDoc();
45     
46     /** Initializes the Form */
47     public FindDialogPanel() {
48         initComponents ();
49         getAccessibleContext().setAccessibleName(NbBundle.getBundle(FindDialogPanel.class).getString("ACSN_Find"));
50         getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(FindDialogPanel.class).getString("ACSD_Find"));
51         findWhat.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(FindDialogPanel.class).getString("ACSD_Find_What"));
52         findWhat.getEditor().getEditorComponent().addKeyListener( new KeyAdapter JavaDoc() {
53             public void keyPressed(KeyEvent JavaDoc evt) {
54                 if ( evt.getKeyCode() == KeyEvent.VK_ESCAPE ) {
55                     findWhat.setPopupVisible( false );
56                     dialog.setVisible( false );
57                 }
58             }
59         });
60         findWhat.getEditor().addActionListener( new java.awt.event.ActionListener JavaDoc() {
61                public void actionPerformed( java.awt.event.ActionEvent JavaDoc evt ) {
62                    acceptButton.requestFocus();
63                    acceptButton.doClick();
64                }
65            });
66        bwdSearch.getAccessibleContext().setAccessibleDescription(
67            NbBundle.getMessage(FindDialogPanel.class, "ACSD_BackwardSearch"));
68        matchCase.getAccessibleContext().setAccessibleDescription(
69            NbBundle.getMessage(FindDialogPanel.class, "ACSD_MatchCase"));
70        wholeWords.getAccessibleContext().setAccessibleDescription(
71            NbBundle.getMessage(FindDialogPanel.class, "ACSD_MatchWhole"));
72     }
73
74     /** This method is called from within the constructor to
75      * initialize the form.
76      * WARNING: Do NOT modify this code. The content of this method is
77      * always regenerated by the FormEditor.
78      */

79     private void initComponents() {//GEN-BEGIN:initComponents
80
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
81
82         findWhatLabel = new javax.swing.JLabel JavaDoc();
83         findWhat = new javax.swing.JComboBox JavaDoc();
84         matchCase = new javax.swing.JCheckBox JavaDoc();
85         wholeWords = new javax.swing.JCheckBox JavaDoc();
86         bwdSearch = new javax.swing.JCheckBox JavaDoc();
87
88         setLayout(new java.awt.GridBagLayout JavaDoc());
89
90         findWhatLabel.setDisplayedMnemonic(NbBundle.getBundle(FindDialogPanel.class).getString("LBL_Find_What_Mnemonic").charAt(0));
91         findWhatLabel.setLabelFor(findWhat);
92         findWhatLabel.setText(org.openide.util.NbBundle.getBundle(FindDialogPanel.class).getString("LBL_Find_What"));
93         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
94         gridBagConstraints.gridx = 0;
95         gridBagConstraints.gridy = 1;
96         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
97         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 5, 0);
98         add(findWhatLabel, gridBagConstraints);
99
100         findWhat.setEditable(true);
101         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
102         gridBagConstraints.gridx = 1;
103         gridBagConstraints.gridy = 1;
104         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
105         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
106         gridBagConstraints.weightx = 1.0;
107         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 11, 9, 10);
108         add(findWhat, gridBagConstraints);
109
110         matchCase.setMnemonic(NbBundle.getBundle(FindDialogPanel.class).getString("LBL_Find_Case_Mnemonic").charAt(0));
111         matchCase.setText(org.openide.util.NbBundle.getBundle(FindDialogPanel.class).getString("LBL_Find_Case"));
112         matchCase.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(1, 1, 1, 1)));
113         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
114         gridBagConstraints.gridx = 1;
115         gridBagConstraints.gridy = 3;
116         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
117         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 11, 0, 0);
118         add(matchCase, gridBagConstraints);
119
120         wholeWords.setMnemonic(NbBundle.getBundle(FindDialogPanel.class).getString("LBL_Find_Whole_Mnemonic").charAt(0));
121         wholeWords.setText(org.openide.util.NbBundle.getBundle(FindDialogPanel.class).getString("LBL_Find_Whole"));
122         wholeWords.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(1, 1, 1, 1)));
123         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
124         gridBagConstraints.gridx = 1;
125         gridBagConstraints.gridy = 4;
126         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 11, 0, 0);
127         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
128         add(wholeWords, gridBagConstraints);
129
130         bwdSearch.setMnemonic(NbBundle.getBundle(FindDialogPanel.class).getString("LBL_Find_Back_Mnemonic").charAt(0));
131         bwdSearch.setText(org.openide.util.NbBundle.getBundle(FindDialogPanel.class).getString("LBL_Find_Back"));
132         bwdSearch.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(1, 1, 1, 1)));
133         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
134         gridBagConstraints.gridx = 2;
135         gridBagConstraints.gridy = 3;
136         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 11, 0, 10);
137         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
138         add(bwdSearch, gridBagConstraints);
139
140     }//GEN-END:initComponents
141

142
143     // Variables declaration - do not modify//GEN-BEGIN:variables
144
protected javax.swing.JComboBox JavaDoc findWhat;
145     protected javax.swing.JCheckBox JavaDoc bwdSearch;
146     protected javax.swing.JCheckBox JavaDoc matchCase;
147     protected javax.swing.JCheckBox JavaDoc wholeWords;
148     protected javax.swing.JLabel JavaDoc findWhatLabel;
149     // End of variables declaration//GEN-END:variables
150

151
152     static FindDialogPanel showFindDialog() {
153         if ( dialog == null )
154             createDialog();
155
156         accepted = false;
157         findPanel.init();
158         dialog.show();
159
160         return findPanel;
161     }
162     
163     private void init() {
164         findWhat.setModel( new DefaultComboBoxModel JavaDoc( history ) );
165         
166         SwingUtilities.invokeLater( new Runnable JavaDoc() {
167                 public void run() {
168                     findWhat.getEditor().getEditorComponent().requestFocus();
169                     findWhat.getEditor().selectAll();
170                 }
171         });
172     }
173     
174     boolean isAccepted() {
175         return accepted;
176     }
177     
178     static String JavaDoc getPattern() {
179         return findPanel != null ? (String JavaDoc) findPanel.findWhat.getSelectedItem() : null;
180     }
181     
182     static boolean isMatchCase() {
183         return findPanel != null ? findPanel.matchCase.isSelected() : false;
184     }
185     
186     static boolean isWholeWordsOnly() {
187         return findPanel != null ? findPanel.wholeWords.isSelected() : false;
188     }
189     
190     static boolean isBackwardSearch() {
191         return findPanel != null ? findPanel.bwdSearch.isSelected() : false;
192     }
193
194     private void updateHistory() {
195         Object JavaDoc pattern = findWhat.getEditor().getItem();
196
197         history.add( 0, pattern );
198         for ( int i = history.size() - 1; i > 0; i-- ) {
199             if ( history.get( i ).equals( pattern ) ) {
200                 history.remove( i );
201                 break;
202             }
203         }
204     }
205
206     private static void createDialog() {
207         findPanel = new FindDialogPanel();
208
209         acceptButton = new JButton JavaDoc( NbBundle.getBundle(FindDialogPanel.class).getString("BTN_Find") );
210         acceptButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(FindDialogPanel.class).getString("ACSD_FindBTN"));
211
212         DialogDescriptor dialogDescriptor = new DialogDescriptor(
213                                findPanel,
214                                NbBundle.getBundle(FindDialogPanel.class).getString("LBL_Find_Title"),
215                                true, // Modal
216
new Object JavaDoc[] { acceptButton, DialogDescriptor.CANCEL_OPTION }, // Option list
217
acceptButton, // Default
218
DialogDescriptor.RIGHT_ALIGN, // Align
219
null, // Help
220
new java.awt.event.ActionListener JavaDoc() {
221                                    public void actionPerformed( java.awt.event.ActionEvent JavaDoc evt ) {
222                                        if ( evt.getSource() == acceptButton ) {
223                                            accepted = true;
224                                            findPanel.updateHistory();
225                                        }
226                                        else
227                                            accepted = false;
228
229                                        dialog.setVisible( false );
230                                    }
231                                });
232
233         dialog = org.openide.DialogDisplayer.getDefault().createDialog( dialogDescriptor );
234         dialog.getAccessibleContext().setAccessibleName(
235             NbBundle.getMessage(FindDialogPanel.class, "ACSN_Find")); //NOI18N
236
dialog.getAccessibleContext().setAccessibleDescription(
237             NbBundle.getMessage(FindDialogPanel.class, "ACSD_Find")); //NOI18N
238
}
239     
240 }
241
Popular Tags