KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > texteditor > Finder


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 examples.texteditor;
21
22 /** Finder dialog is used to allow the user to search for given string.
23  */

24 public class Finder extends javax.swing.JDialog JavaDoc {
25
26     /** Finder constructor.
27      * It creates modal dialog and displays it.
28      */

29     public Finder(java.awt.Frame JavaDoc parent, javax.swing.JTextArea JavaDoc textEditor) {
30         super(parent, true);
31         this.textEditor = textEditor;
32         initComponents();
33         pack();
34         setLocationRelativeTo(parent);
35         findField.requestFocus();
36     }
37
38     /** This method is called from within the constructor to
39      * initialize the form.
40      * WARNING: Do NOT modify this code. The content of this method is
41      * always regenerated by the FormEditor.
42      */

43     private void initComponents() {//GEN-BEGIN:initComponents
44
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
45
46         findPanel = new javax.swing.JPanel JavaDoc();
47         findLabel = new javax.swing.JLabel JavaDoc();
48         findField = new javax.swing.JTextField JavaDoc();
49         buttonPanel = new javax.swing.JPanel JavaDoc();
50         findButton = new javax.swing.JButton JavaDoc();
51         closeButton = new javax.swing.JButton JavaDoc();
52
53         getContentPane().setLayout(new java.awt.GridBagLayout JavaDoc());
54
55         setTitle("Find");
56         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
57             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
58                 closeDialog(evt);
59             }
60         });
61
62         getAccessibleContext().setAccessibleName("Find Dialog");
63         getAccessibleContext().setAccessibleDescription("Find dialog.");
64         findPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
65
66         findLabel.setLabelFor(findField);
67         findLabel.setText("Find text:");
68         findPanel.add(findLabel, new java.awt.GridBagConstraints JavaDoc());
69         findLabel.getAccessibleContext().setAccessibleDescription("Find text.");
70
71         findField.addActionListener(new java.awt.event.ActionListener JavaDoc() {
72             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
73                 findFieldActionPerformed(evt);
74             }
75         });
76
77         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
78         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
79         gridBagConstraints.weightx = 1.0;
80         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 0, 0);
81         findPanel.add(findField, gridBagConstraints);
82         findField.getAccessibleContext().setAccessibleName("Find Field");
83         findField.getAccessibleContext().setAccessibleDescription("Find field.");
84
85         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
86         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
87         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 12, 0, 11);
88         getContentPane().add(findPanel, gridBagConstraints);
89
90         buttonPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
91
92         findButton.setMnemonic('f');
93         findButton.setText("Find");
94         findButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
95             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
96                 findButtonActionPerformed(evt);
97             }
98         });
99
100         buttonPanel.add(findButton, new java.awt.GridBagConstraints JavaDoc());
101
102         closeButton.setMnemonic('c');
103         closeButton.setText("Close");
104         closeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
105             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
106                 closeButtonActionPerformed(evt);
107             }
108         });
109
110         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
111         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 0, 0);
112         buttonPanel.add(closeButton, gridBagConstraints);
113
114         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
115         gridBagConstraints.gridx = 0;
116         gridBagConstraints.gridy = 1;
117         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
118         gridBagConstraints.insets = new java.awt.Insets JavaDoc(17, 12, 11, 11);
119         getContentPane().add(buttonPanel, gridBagConstraints);
120
121     }//GEN-END:initComponents
122

123     /** This method is called when ENTER is pressed in Find text field.
124      * If the field contains some text, it invokes Find button action, otherwise does nothing.
125      * @param evt ActionEvent instance passed from actionPerformed event.
126      */

127     private void findFieldActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_findFieldActionPerformed
128
if (findField.getText().trim().length() > 0)
129             findButton.doClick();
130     }//GEN-LAST:event_findFieldActionPerformed
131

132     /** This method is called when Find button is pressed.
133      * If the field contains some text, it sets the caret position to the searched word, otherwise does nothing.
134      * @param evt ActionEvent instance passed from actionPerformed event.
135      */

136     private void findButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_findButtonActionPerformed
137
// Add your handling code here:
138
String JavaDoc text = textEditor.getText();
139         String JavaDoc textToFind = findField.getText();
140         if (!"".equals(textToFind)) {
141             int index = text.indexOf(textToFind);
142             if (index != -1) {
143                 textEditor.setCaretPosition(index);
144                 closeDialog(null);
145             } else {
146                 java.awt.Toolkit.getDefaultToolkit().beep();
147             }
148         }
149     }//GEN-LAST:event_findButtonActionPerformed
150

151     /** This method is called when Close button is pressed.
152      * It closes the Finder dialog.
153      * @param evt ActionEvent instance passed from actionPerformed event.
154      */

155     private void closeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_closeButtonActionPerformed
156
closeDialog(null);
157     }//GEN-LAST:event_closeButtonActionPerformed
158

159     /** This method is called when the dialog is closed.
160      * @param evt WindowEvent instance passed from windowClosing event.
161      */

162     private void closeDialog(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_closeDialog
163
setVisible(false);
164         dispose();
165     }//GEN-LAST:event_closeDialog
166

167
168     // Variables declaration - do not modify//GEN-BEGIN:variables
169
private javax.swing.JPanel JavaDoc buttonPanel;
170     private javax.swing.JButton JavaDoc closeButton;
171     private javax.swing.JButton JavaDoc findButton;
172     private javax.swing.JTextField JavaDoc findField;
173     private javax.swing.JLabel JavaDoc findLabel;
174     private javax.swing.JPanel JavaDoc findPanel;
175     // End of variables declaration//GEN-END:variables
176

177     private javax.swing.JTextArea JavaDoc textEditor;
178
179 }
180
Popular Tags