KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > texteditor > Ted


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 import java.awt.FileDialog JavaDoc;
23 import java.io.*;
24
25 public class Ted extends javax.swing.JFrame JavaDoc {
26
27     /** Initializes the Form */
28     public Ted() {
29         initComponents ();
30         setSize(500,300);
31     }
32
33     /** This method is called from within the constructor to
34      * initialize the form.
35      * WARNING: Do NOT modify this code. The content of this method is
36      * always regenerated by the FormEditor.
37      */

38     private void initComponents() {//GEN-BEGIN:initComponents
39
jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
40         textBox = new javax.swing.JTextArea JavaDoc();
41         jMenuBar1 = new javax.swing.JMenuBar JavaDoc();
42         jMenu1 = new javax.swing.JMenu JavaDoc();
43         jMenuItem1 = new javax.swing.JMenuItem JavaDoc();
44         jMenuItem4 = new javax.swing.JMenuItem JavaDoc();
45         jMenuItem5 = new javax.swing.JMenuItem JavaDoc();
46         jMenuItem6 = new javax.swing.JMenuItem JavaDoc();
47         jMenuItem7 = new javax.swing.JMenuItem JavaDoc();
48         jMenu2 = new javax.swing.JMenu JavaDoc();
49         jMenuItem2 = new javax.swing.JMenuItem JavaDoc();
50         jMenu3 = new javax.swing.JMenu JavaDoc();
51         jMenuItem3 = new javax.swing.JMenuItem JavaDoc();
52
53         setTitle("Ted");
54         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
55             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
56                 exitForm(evt);
57             }
58         });
59
60         jScrollPane1.setViewportView(textBox);
61
62         getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
63
64         jMenu1.setText("File");
65         jMenuItem1.setText("New");
66         jMenuItem1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
67             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
68                 jMenuItem1ActionPerformed(evt);
69             }
70         });
71
72         jMenu1.add(jMenuItem1);
73         jMenuItem4.setText("Open ...");
74         jMenuItem4.addActionListener(new java.awt.event.ActionListener JavaDoc() {
75             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
76                 jMenuItem4ActionPerformed(evt);
77             }
78         });
79
80         jMenu1.add(jMenuItem4);
81         jMenuItem5.setText("Save");
82         jMenuItem5.addActionListener(new java.awt.event.ActionListener JavaDoc() {
83             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
84                 jMenuItem5ActionPerformed(evt);
85             }
86         });
87
88         jMenu1.add(jMenuItem5);
89         jMenuItem6.setText("Save As ...");
90         jMenuItem6.addActionListener(new java.awt.event.ActionListener JavaDoc() {
91             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
92                 jMenuItem6ActionPerformed(evt);
93             }
94         });
95
96         jMenu1.add(jMenuItem6);
97         jMenuItem7.setText("Exit");
98         jMenuItem7.addActionListener(new java.awt.event.ActionListener JavaDoc() {
99             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
100                 jMenuItem7ActionPerformed(evt);
101             }
102         });
103
104         jMenu1.add(jMenuItem7);
105         jMenuBar1.add(jMenu1);
106         jMenu2.setText("Edit");
107         jMenuItem2.setText("Find ...");
108         jMenuItem2.addActionListener(new java.awt.event.ActionListener JavaDoc() {
109             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
110                 jMenuItem2ActionPerformed(evt);
111             }
112         });
113
114         jMenu2.add(jMenuItem2);
115         jMenuBar1.add(jMenu2);
116         jMenu3.setText("Help");
117         jMenuItem3.setText("About ...");
118         jMenuItem3.addActionListener(new java.awt.event.ActionListener JavaDoc() {
119             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
120                 jMenuItem3ActionPerformed(evt);
121             }
122         });
123
124         jMenu3.add(jMenuItem3);
125         jMenuBar1.add(jMenu3);
126         setJMenuBar(jMenuBar1);
127
128     }//GEN-END:initComponents
129

130     private void jMenuItem5ActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
131
// Add your handling code here:
132
if ("".equals(fileName))
133             doSaveAs();
134         else
135             doSave(fileName);
136     }//GEN-LAST:event_jMenuItem5ActionPerformed
137

138     private void jMenuItem7ActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
139
// Add your handling code here:
140
System.exit(0);
141     }//GEN-LAST:event_jMenuItem7ActionPerformed
142

143     private void jMenuItem2ActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
144
// Add your handling code here:
145
new Finder (this, textBox).show();
146     }//GEN-LAST:event_jMenuItem2ActionPerformed
147

148     private void jMenuItem3ActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
149
// Add your handling code here:
150
new About(this). show();
151     }//GEN-LAST:event_jMenuItem3ActionPerformed
152

153     private void jMenuItem6ActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
154
// Add your handling code here:
155
doSaveAs();
156     }//GEN-LAST:event_jMenuItem6ActionPerformed
157

158     private void jMenuItem4ActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
159
// Add your handling code here:
160
FileDialog JavaDoc fileDialog = new FileDialog JavaDoc (this, "Open...", FileDialog.LOAD);
161         fileDialog.show ();
162         if (fileDialog.getFile () == null)
163             return;
164         fileName = fileDialog.getDirectory () + File.separator + fileDialog.getFile ();
165
166         FileInputStream fis = null;
167         String JavaDoc str = null;
168         try {
169             fis = new FileInputStream (fileName);
170             int size = fis.available ();
171             byte[] bytes = new byte [size];
172             fis.read (bytes);
173             str = new String JavaDoc (bytes);
174         } catch (IOException e) {
175         } finally {
176             try {
177                 fis.close ();
178             } catch (IOException e2) {
179             }
180         }
181
182         if (str != null)
183             textBox.setText (str);
184     }//GEN-LAST:event_jMenuItem4ActionPerformed
185

186     private void jMenuItem1ActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
187
// Add your handling code here:
188
fileName = "";
189         textBox.setText ("");
190     }//GEN-LAST:event_jMenuItem1ActionPerformed
191

192     /** Exit the Application */
193     private void exitForm(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_exitForm
194
System.exit (0);
195     }//GEN-LAST:event_exitForm
196

197     private void doSave (String JavaDoc fileName) {
198         FileOutputStream fos = null;
199         String JavaDoc str = textBox.getText ();
200         try {
201             fos = new FileOutputStream (fileName);
202             fos.write (str.getBytes ());
203         } catch (IOException e) {
204         } finally {
205             try {
206                 fos.close ();
207             } catch (IOException e2) {
208             }
209         }
210     }
211
212     private void doSaveAs () {
213         FileDialog JavaDoc fileDialog = new FileDialog JavaDoc (this, "Save As...", FileDialog.SAVE);
214         fileDialog.show ();
215         if (fileDialog.getFile () == null)
216             return;
217         fileName = fileDialog.getDirectory () + File.separator + fileDialog.getFile ();
218
219         doSave (fileName);
220     }
221
222     // Variables declaration - do not modify//GEN-BEGIN:variables
223
private javax.swing.JMenuItem JavaDoc jMenuItem7;
224     private javax.swing.JMenuItem JavaDoc jMenuItem6;
225     private javax.swing.JMenuItem JavaDoc jMenuItem5;
226     private javax.swing.JMenuItem JavaDoc jMenuItem4;
227     private javax.swing.JMenuItem JavaDoc jMenuItem3;
228     private javax.swing.JMenuItem JavaDoc jMenuItem2;
229     private javax.swing.JMenuItem JavaDoc jMenuItem1;
230     private javax.swing.JScrollPane JavaDoc jScrollPane1;
231     private javax.swing.JTextArea JavaDoc textBox;
232     private javax.swing.JMenu JavaDoc jMenu3;
233     private javax.swing.JMenu JavaDoc jMenu2;
234     private javax.swing.JMenuBar JavaDoc jMenuBar1;
235     private javax.swing.JMenu JavaDoc jMenu1;
236     // End of variables declaration//GEN-END:variables
237

238
239     public static void main(java.lang.String JavaDoc[] args) {
240         new Ted ().show ();
241     }
242
243     private String JavaDoc fileName = "";
244 }
245
Popular Tags