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: import java.awt.FileDialog;
045: import java.io.*;
046:
047: public class Ted extends javax.swing.JFrame {
048:
049: /** Initializes the Form */
050: public Ted() {
051: initComponents();
052: setSize(500, 300);
053: }
054:
055: /** This method is called from within the constructor to
056: * initialize the form.
057: * WARNING: Do NOT modify this code. The content of this method is
058: * always regenerated by the FormEditor.
059: */
060: private void initComponents() {//GEN-BEGIN:initComponents
061: jScrollPane1 = new javax.swing.JScrollPane();
062: textBox = new javax.swing.JTextArea();
063: jMenuBar1 = new javax.swing.JMenuBar();
064: jMenu1 = new javax.swing.JMenu();
065: jMenuItem1 = new javax.swing.JMenuItem();
066: jMenuItem4 = new javax.swing.JMenuItem();
067: jMenuItem5 = new javax.swing.JMenuItem();
068: jMenuItem6 = new javax.swing.JMenuItem();
069: jMenuItem7 = new javax.swing.JMenuItem();
070: jMenu2 = new javax.swing.JMenu();
071: jMenuItem2 = new javax.swing.JMenuItem();
072: jMenu3 = new javax.swing.JMenu();
073: jMenuItem3 = new javax.swing.JMenuItem();
074:
075: setTitle("Ted");
076: addWindowListener(new java.awt.event.WindowAdapter() {
077: public void windowClosing(java.awt.event.WindowEvent evt) {
078: exitForm(evt);
079: }
080: });
081:
082: jScrollPane1.setViewportView(textBox);
083:
084: getContentPane()
085: .add(jScrollPane1, java.awt.BorderLayout.CENTER);
086:
087: jMenu1.setText("File");
088: jMenuItem1.setText("New");
089: jMenuItem1
090: .addActionListener(new java.awt.event.ActionListener() {
091: public void actionPerformed(
092: java.awt.event.ActionEvent evt) {
093: jMenuItem1ActionPerformed(evt);
094: }
095: });
096:
097: jMenu1.add(jMenuItem1);
098: jMenuItem4.setText("Open ...");
099: jMenuItem4
100: .addActionListener(new java.awt.event.ActionListener() {
101: public void actionPerformed(
102: java.awt.event.ActionEvent evt) {
103: jMenuItem4ActionPerformed(evt);
104: }
105: });
106:
107: jMenu1.add(jMenuItem4);
108: jMenuItem5.setText("Save");
109: jMenuItem5
110: .addActionListener(new java.awt.event.ActionListener() {
111: public void actionPerformed(
112: java.awt.event.ActionEvent evt) {
113: jMenuItem5ActionPerformed(evt);
114: }
115: });
116:
117: jMenu1.add(jMenuItem5);
118: jMenuItem6.setText("Save As ...");
119: jMenuItem6
120: .addActionListener(new java.awt.event.ActionListener() {
121: public void actionPerformed(
122: java.awt.event.ActionEvent evt) {
123: jMenuItem6ActionPerformed(evt);
124: }
125: });
126:
127: jMenu1.add(jMenuItem6);
128: jMenuItem7.setText("Exit");
129: jMenuItem7
130: .addActionListener(new java.awt.event.ActionListener() {
131: public void actionPerformed(
132: java.awt.event.ActionEvent evt) {
133: jMenuItem7ActionPerformed(evt);
134: }
135: });
136:
137: jMenu1.add(jMenuItem7);
138: jMenuBar1.add(jMenu1);
139: jMenu2.setText("Edit");
140: jMenuItem2.setText("Find ...");
141: jMenuItem2
142: .addActionListener(new java.awt.event.ActionListener() {
143: public void actionPerformed(
144: java.awt.event.ActionEvent evt) {
145: jMenuItem2ActionPerformed(evt);
146: }
147: });
148:
149: jMenu2.add(jMenuItem2);
150: jMenuBar1.add(jMenu2);
151: jMenu3.setText("Help");
152: jMenuItem3.setText("About ...");
153: jMenuItem3
154: .addActionListener(new java.awt.event.ActionListener() {
155: public void actionPerformed(
156: java.awt.event.ActionEvent evt) {
157: jMenuItem3ActionPerformed(evt);
158: }
159: });
160:
161: jMenu3.add(jMenuItem3);
162: jMenuBar1.add(jMenu3);
163: setJMenuBar(jMenuBar1);
164:
165: }//GEN-END:initComponents
166:
167: private void jMenuItem5ActionPerformed(
168: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
169: // Add your handling code here:
170: if ("".equals(fileName))
171: doSaveAs();
172: else
173: doSave(fileName);
174: }//GEN-LAST:event_jMenuItem5ActionPerformed
175:
176: private void jMenuItem7ActionPerformed(
177: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
178: // Add your handling code here:
179: System.exit(0);
180: }//GEN-LAST:event_jMenuItem7ActionPerformed
181:
182: private void jMenuItem2ActionPerformed(
183: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
184: // Add your handling code here:
185: new Finder(this , textBox).show();
186: }//GEN-LAST:event_jMenuItem2ActionPerformed
187:
188: private void jMenuItem3ActionPerformed(
189: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
190: // Add your handling code here:
191: new About(this ).show();
192: }//GEN-LAST:event_jMenuItem3ActionPerformed
193:
194: private void jMenuItem6ActionPerformed(
195: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
196: // Add your handling code here:
197: doSaveAs();
198: }//GEN-LAST:event_jMenuItem6ActionPerformed
199:
200: private void jMenuItem4ActionPerformed(
201: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
202: // Add your handling code here:
203: FileDialog fileDialog = new FileDialog(this , "Open...",
204: FileDialog.LOAD);
205: fileDialog.show();
206: if (fileDialog.getFile() == null)
207: return;
208: fileName = fileDialog.getDirectory() + File.separator
209: + fileDialog.getFile();
210:
211: FileInputStream fis = null;
212: String str = null;
213: try {
214: fis = new FileInputStream(fileName);
215: int size = fis.available();
216: byte[] bytes = new byte[size];
217: fis.read(bytes);
218: str = new String(bytes);
219: } catch (IOException e) {
220: } finally {
221: try {
222: fis.close();
223: } catch (IOException e2) {
224: }
225: }
226:
227: if (str != null)
228: textBox.setText(str);
229: }//GEN-LAST:event_jMenuItem4ActionPerformed
230:
231: private void jMenuItem1ActionPerformed(
232: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
233: // Add your handling code here:
234: fileName = "";
235: textBox.setText("");
236: }//GEN-LAST:event_jMenuItem1ActionPerformed
237:
238: /** Exit the Application */
239: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
240: System.exit(0);
241: }//GEN-LAST:event_exitForm
242:
243: private void doSave(String fileName) {
244: FileOutputStream fos = null;
245: String str = textBox.getText();
246: try {
247: fos = new FileOutputStream(fileName);
248: fos.write(str.getBytes());
249: } catch (IOException e) {
250: } finally {
251: try {
252: fos.close();
253: } catch (IOException e2) {
254: }
255: }
256: }
257:
258: private void doSaveAs() {
259: FileDialog fileDialog = new FileDialog(this , "Save As...",
260: FileDialog.SAVE);
261: fileDialog.show();
262: if (fileDialog.getFile() == null)
263: return;
264: fileName = fileDialog.getDirectory() + File.separator
265: + fileDialog.getFile();
266:
267: doSave(fileName);
268: }
269:
270: // Variables declaration - do not modify//GEN-BEGIN:variables
271: private javax.swing.JMenuItem jMenuItem7;
272: private javax.swing.JMenuItem jMenuItem6;
273: private javax.swing.JMenuItem jMenuItem5;
274: private javax.swing.JMenuItem jMenuItem4;
275: private javax.swing.JMenuItem jMenuItem3;
276: private javax.swing.JMenuItem jMenuItem2;
277: private javax.swing.JMenuItem jMenuItem1;
278: private javax.swing.JScrollPane jScrollPane1;
279: private javax.swing.JTextArea textBox;
280: private javax.swing.JMenu jMenu3;
281: private javax.swing.JMenu jMenu2;
282: private javax.swing.JMenuBar jMenuBar1;
283: private javax.swing.JMenu jMenu1;
284:
285: // End of variables declaration//GEN-END:variables
286:
287: public static void main(java.lang.String[] args) {
288: new Ted().show();
289: }
290:
291: private String fileName = "";
292: }
|