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: public class Finder extends javax.swing.JDialog {
045:
046: /** Initializes the Form */
047: public Finder(java.awt.Frame parent,
048: javax.swing.JTextArea textEditor) {
049: super (parent, true);
050: this .textEditor = textEditor;
051: initComponents();
052: pack();
053: findField.requestFocus();
054: }
055:
056: /** This method is called from within the constructor to
057: * initialize the form.
058: * WARNING: Do NOT modify this code. The content of this method is
059: * always regenerated by the FormEditor.
060: */
061: private void initComponents() {//GEN-BEGIN:initComponents
062: findField = new javax.swing.JTextField();
063: jPanel1 = new javax.swing.JPanel();
064: findButton = new javax.swing.JButton();
065: closeButton = new javax.swing.JButton();
066:
067: setTitle("Find");
068: addWindowListener(new java.awt.event.WindowAdapter() {
069: public void windowClosing(java.awt.event.WindowEvent evt) {
070: closeDialog(evt);
071: }
072: });
073:
074: getContentPane().add(findField, java.awt.BorderLayout.CENTER);
075:
076: jPanel1.setLayout(new java.awt.FlowLayout(
077: java.awt.FlowLayout.RIGHT));
078:
079: findButton.setText("Find");
080: findButton
081: .addActionListener(new java.awt.event.ActionListener() {
082: public void actionPerformed(
083: java.awt.event.ActionEvent evt) {
084: findButtonActionPerformed(evt);
085: }
086: });
087:
088: jPanel1.add(findButton);
089:
090: closeButton.setText("Close");
091: closeButton
092: .addActionListener(new java.awt.event.ActionListener() {
093: public void actionPerformed(
094: java.awt.event.ActionEvent evt) {
095: closeButtonActionPerformed(evt);
096: }
097: });
098:
099: jPanel1.add(closeButton);
100:
101: getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);
102:
103: }//GEN-END:initComponents
104:
105: private void findButtonActionPerformed(
106: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findButtonActionPerformed
107: // Add your handling code here:
108: String text = textEditor.getText();
109: String textToFind = findField.getText();
110: if (!"".equals(textToFind)) {
111: int index = text.indexOf(textToFind);
112: if (index != -1) {
113: textEditor.setCaretPosition(index);
114: closeDialog(null);
115: } else {
116: java.awt.Toolkit.getDefaultToolkit().beep();
117: }
118: }
119: }//GEN-LAST:event_findButtonActionPerformed
120:
121: private void closeButtonActionPerformed(
122: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
123: closeDialog(null);
124: }//GEN-LAST:event_closeButtonActionPerformed
125:
126: /** Closes the dialog */
127: private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
128: setVisible(false);
129: dispose();
130: }//GEN-LAST:event_closeDialog
131:
132: // Variables declaration - do not modify//GEN-BEGIN:variables
133: private javax.swing.JButton findButton;
134: private javax.swing.JButton closeButton;
135: private javax.swing.JTextField findField;
136: private javax.swing.JPanel jPanel1;
137: // End of variables declaration//GEN-END:variables
138: private javax.swing.JTextArea textEditor;
139:
140: }
|