001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package examples.texteditor;
043:
044: /** Finder dialog is used to allow the user to search for given string.
045: */
046: public class Finder extends javax.swing.JDialog {
047:
048: /** Finder constructor.
049: * It creates modal dialog and displays it.
050: */
051: public Finder(java.awt.Frame parent,
052: javax.swing.JTextArea textEditor) {
053: super (parent, true);
054: this .textEditor = textEditor;
055: initComponents();
056: pack();
057: setLocationRelativeTo(parent);
058: findField.requestFocus();
059: }
060:
061: /** This method is called from within the constructor to
062: * initialize the form.
063: * WARNING: Do NOT modify this code. The content of this method is
064: * always regenerated by the FormEditor.
065: */
066: private void initComponents() {//GEN-BEGIN:initComponents
067: java.awt.GridBagConstraints gridBagConstraints;
068:
069: findPanel = new javax.swing.JPanel();
070: findLabel = new javax.swing.JLabel();
071: findField = new javax.swing.JTextField();
072: buttonPanel = new javax.swing.JPanel();
073: findButton = new javax.swing.JButton();
074: closeButton = new javax.swing.JButton();
075:
076: getContentPane().setLayout(new java.awt.GridBagLayout());
077:
078: setTitle("Find");
079: addWindowListener(new java.awt.event.WindowAdapter() {
080: public void windowClosing(java.awt.event.WindowEvent evt) {
081: closeDialog(evt);
082: }
083: });
084:
085: getAccessibleContext().setAccessibleName("Find Dialog");
086: getAccessibleContext().setAccessibleDescription("Find dialog.");
087: findPanel.setLayout(new java.awt.GridBagLayout());
088:
089: findLabel.setLabelFor(findField);
090: findLabel.setText("Find text:");
091: findPanel.add(findLabel, new java.awt.GridBagConstraints());
092: findLabel.getAccessibleContext().setAccessibleDescription(
093: "Find text.");
094:
095: findField
096: .addActionListener(new java.awt.event.ActionListener() {
097: public void actionPerformed(
098: java.awt.event.ActionEvent evt) {
099: findFieldActionPerformed(evt);
100: }
101: });
102:
103: gridBagConstraints = new java.awt.GridBagConstraints();
104: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
105: gridBagConstraints.weightx = 1.0;
106: gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
107: findPanel.add(findField, gridBagConstraints);
108: findField.getAccessibleContext()
109: .setAccessibleName("Find Field");
110: findField.getAccessibleContext().setAccessibleDescription(
111: "Find field.");
112:
113: gridBagConstraints = new java.awt.GridBagConstraints();
114: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
115: gridBagConstraints.insets = new java.awt.Insets(11, 12, 0, 11);
116: getContentPane().add(findPanel, gridBagConstraints);
117:
118: buttonPanel.setLayout(new java.awt.GridBagLayout());
119:
120: findButton.setMnemonic('f');
121: findButton.setText("Find");
122: findButton
123: .addActionListener(new java.awt.event.ActionListener() {
124: public void actionPerformed(
125: java.awt.event.ActionEvent evt) {
126: findButtonActionPerformed(evt);
127: }
128: });
129:
130: buttonPanel.add(findButton, new java.awt.GridBagConstraints());
131:
132: closeButton.setMnemonic('c');
133: closeButton.setText("Close");
134: closeButton
135: .addActionListener(new java.awt.event.ActionListener() {
136: public void actionPerformed(
137: java.awt.event.ActionEvent evt) {
138: closeButtonActionPerformed(evt);
139: }
140: });
141:
142: gridBagConstraints = new java.awt.GridBagConstraints();
143: gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
144: buttonPanel.add(closeButton, gridBagConstraints);
145:
146: gridBagConstraints = new java.awt.GridBagConstraints();
147: gridBagConstraints.gridx = 0;
148: gridBagConstraints.gridy = 1;
149: gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
150: gridBagConstraints.insets = new java.awt.Insets(17, 12, 11, 11);
151: getContentPane().add(buttonPanel, gridBagConstraints);
152:
153: }//GEN-END:initComponents
154:
155: /** This method is called when ENTER is pressed in Find text field.
156: * If the field contains some text, it invokes Find button action, otherwise does nothing.
157: * @param evt ActionEvent instance passed from actionPerformed event.
158: */
159: private void findFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findFieldActionPerformed
160: if (findField.getText().trim().length() > 0)
161: findButton.doClick();
162: }//GEN-LAST:event_findFieldActionPerformed
163:
164: /** This method is called when Find button is pressed.
165: * If the field contains some text, it sets the caret position to the searched word, otherwise does nothing.
166: * @param evt ActionEvent instance passed from actionPerformed event.
167: */
168: private void findButtonActionPerformed(
169: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findButtonActionPerformed
170: // Add your handling code here:
171: String text = textEditor.getText();
172: String textToFind = findField.getText();
173: if (!"".equals(textToFind)) {
174: int index = text.indexOf(textToFind);
175: if (index != -1) {
176: textEditor.setCaretPosition(index);
177: closeDialog(null);
178: } else {
179: java.awt.Toolkit.getDefaultToolkit().beep();
180: }
181: }
182: }//GEN-LAST:event_findButtonActionPerformed
183:
184: /** This method is called when Close button is pressed.
185: * It closes the Finder dialog.
186: * @param evt ActionEvent instance passed from actionPerformed event.
187: */
188: private void closeButtonActionPerformed(
189: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
190: closeDialog(null);
191: }//GEN-LAST:event_closeButtonActionPerformed
192:
193: /** This method is called when the dialog is closed.
194: * @param evt WindowEvent instance passed from windowClosing event.
195: */
196: private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
197: setVisible(false);
198: dispose();
199: }//GEN-LAST:event_closeDialog
200:
201: // Variables declaration - do not modify//GEN-BEGIN:variables
202: private javax.swing.JPanel buttonPanel;
203: private javax.swing.JButton closeButton;
204: private javax.swing.JButton findButton;
205: private javax.swing.JTextField findField;
206: private javax.swing.JLabel findLabel;
207: private javax.swing.JPanel findPanel;
208: // End of variables declaration//GEN-END:variables
209:
210: private javax.swing.JTextArea textEditor;
211:
212: }
|