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: /** This class is an entry point of the simple text editor.
048: * It creates and shows the main application frame.
049: */
050: public class Ted extends javax.swing.JFrame {
051:
052: /** Ted constructor.
053: * It initializes all GUI components [menu bar, menu items, editor pane, etc.].
054: */
055: public Ted() {
056: initComponents();
057: setSize(500, 300);
058: }
059:
060: /** This method is called from within the constructor to
061: * initialize the form.
062: * WARNING: Do NOT modify this code. The content of this method is
063: * always regenerated by the FormEditor.
064: */
065: private void initComponents() {//GEN-BEGIN:initComponents
066: textScrollPane = new javax.swing.JScrollPane();
067: textBox = new javax.swing.JTextArea();
068: tedMenuBar = new javax.swing.JMenuBar();
069: fileMenu = new javax.swing.JMenu();
070: newMenuItem = new javax.swing.JMenuItem();
071: jSeparator1 = new javax.swing.JSeparator();
072: openMenuItem = new javax.swing.JMenuItem();
073: saveMenuItem = new javax.swing.JMenuItem();
074: saveAsMenuItem = new javax.swing.JMenuItem();
075: jSeparator2 = new javax.swing.JSeparator();
076: exitMenuItem = new javax.swing.JMenuItem();
077: editMenu = new javax.swing.JMenu();
078: findMenuItem = new javax.swing.JMenuItem();
079: helpMenu = new javax.swing.JMenu();
080: aboutMenuItem = new javax.swing.JMenuItem();
081:
082: setTitle("Ted");
083: addWindowListener(new java.awt.event.WindowAdapter() {
084: public void windowClosing(java.awt.event.WindowEvent evt) {
085: exitForm(evt);
086: }
087: });
088:
089: getAccessibleContext().setAccessibleName("Ted Frame");
090: getAccessibleContext().setAccessibleDescription("Ted frame.");
091: textScrollPane.setViewportView(textBox);
092: textBox.getAccessibleContext().setAccessibleName("Text Box");
093: textBox.getAccessibleContext().setAccessibleDescription(
094: "Text box.");
095:
096: getContentPane().add(textScrollPane,
097: java.awt.BorderLayout.CENTER);
098:
099: fileMenu.setMnemonic('f');
100: fileMenu.setText("File");
101: newMenuItem.setMnemonic('n');
102: newMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
103: java.awt.event.KeyEvent.VK_N,
104: java.awt.event.InputEvent.CTRL_MASK));
105: newMenuItem.setText("New");
106: newMenuItem
107: .addActionListener(new java.awt.event.ActionListener() {
108: public void actionPerformed(
109: java.awt.event.ActionEvent evt) {
110: newMenuItemActionPerformed(evt);
111: }
112: });
113:
114: fileMenu.add(newMenuItem);
115: newMenuItem.getAccessibleContext().setAccessibleName(
116: "New Menu Item");
117: newMenuItem.getAccessibleContext().setAccessibleDescription(
118: "New menu item.");
119:
120: fileMenu.add(jSeparator1);
121:
122: openMenuItem.setMnemonic('o');
123: openMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
124: java.awt.event.KeyEvent.VK_O,
125: java.awt.event.InputEvent.CTRL_MASK));
126: openMenuItem.setText("Open ...");
127: openMenuItem
128: .addActionListener(new java.awt.event.ActionListener() {
129: public void actionPerformed(
130: java.awt.event.ActionEvent evt) {
131: openMenuItemActionPerformed(evt);
132: }
133: });
134:
135: fileMenu.add(openMenuItem);
136: openMenuItem.getAccessibleContext().setAccessibleName(
137: "Open Menu Item");
138: openMenuItem.getAccessibleContext().setAccessibleDescription(
139: "Open menu item.");
140:
141: saveMenuItem.setMnemonic('s');
142: saveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
143: java.awt.event.KeyEvent.VK_S,
144: java.awt.event.InputEvent.CTRL_MASK));
145: saveMenuItem.setText("Save");
146: saveMenuItem
147: .addActionListener(new java.awt.event.ActionListener() {
148: public void actionPerformed(
149: java.awt.event.ActionEvent evt) {
150: saveMenuItemActionPerformed(evt);
151: }
152: });
153:
154: fileMenu.add(saveMenuItem);
155: saveMenuItem.getAccessibleContext().setAccessibleName(
156: "Save Menu Item");
157: saveMenuItem.getAccessibleContext().setAccessibleDescription(
158: "Save menu item.");
159:
160: saveAsMenuItem.setMnemonic('a');
161: saveAsMenuItem.setText("Save As ...");
162: saveAsMenuItem
163: .addActionListener(new java.awt.event.ActionListener() {
164: public void actionPerformed(
165: java.awt.event.ActionEvent evt) {
166: saveAsMenuItemActionPerformed(evt);
167: }
168: });
169:
170: fileMenu.add(saveAsMenuItem);
171: saveAsMenuItem.getAccessibleContext().setAccessibleName(
172: "Save As Menu Item");
173: saveAsMenuItem.getAccessibleContext().setAccessibleDescription(
174: "Save As menu item.");
175:
176: fileMenu.add(jSeparator2);
177:
178: exitMenuItem.setMnemonic('x');
179: exitMenuItem.setText("Exit");
180: exitMenuItem
181: .addActionListener(new java.awt.event.ActionListener() {
182: public void actionPerformed(
183: java.awt.event.ActionEvent evt) {
184: exitMenuItemActionPerformed(evt);
185: }
186: });
187:
188: fileMenu.add(exitMenuItem);
189: exitMenuItem.getAccessibleContext().setAccessibleName(
190: "Exit Menu Item");
191: exitMenuItem.getAccessibleContext().setAccessibleDescription(
192: "Exit menu item.");
193:
194: tedMenuBar.add(fileMenu);
195: fileMenu.getAccessibleContext().setAccessibleName("File Menu");
196: fileMenu.getAccessibleContext().setAccessibleDescription(
197: "File menu.");
198:
199: editMenu.setMnemonic('e');
200: editMenu.setText("Edit");
201: findMenuItem.setMnemonic('f');
202: findMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
203: java.awt.event.KeyEvent.VK_F,
204: java.awt.event.InputEvent.CTRL_MASK));
205: findMenuItem.setText("Find ...");
206: findMenuItem
207: .addActionListener(new java.awt.event.ActionListener() {
208: public void actionPerformed(
209: java.awt.event.ActionEvent evt) {
210: findMenuItemActionPerformed(evt);
211: }
212: });
213:
214: editMenu.add(findMenuItem);
215: findMenuItem.getAccessibleContext().setAccessibleName(
216: "Find Menu Item");
217: findMenuItem.getAccessibleContext().setAccessibleDescription(
218: "Find menu item.");
219:
220: tedMenuBar.add(editMenu);
221: editMenu.getAccessibleContext().setAccessibleName("Edit Menu");
222: editMenu.getAccessibleContext().setAccessibleDescription(
223: "Edit menu.");
224:
225: helpMenu.setMnemonic('h');
226: helpMenu.setText("Help");
227: aboutMenuItem.setMnemonic('a');
228: aboutMenuItem.setText("About ...");
229: aboutMenuItem
230: .addActionListener(new java.awt.event.ActionListener() {
231: public void actionPerformed(
232: java.awt.event.ActionEvent evt) {
233: aboutMenuItemActionPerformed(evt);
234: }
235: });
236:
237: helpMenu.add(aboutMenuItem);
238: aboutMenuItem.getAccessibleContext().setAccessibleName(
239: "About Menu Item");
240: aboutMenuItem.getAccessibleContext().setAccessibleDescription(
241: "About menu item.");
242:
243: tedMenuBar.add(helpMenu);
244: helpMenu.getAccessibleContext().setAccessibleName("Help Menu");
245: helpMenu.getAccessibleContext().setAccessibleDescription(
246: "Help menu.");
247:
248: setJMenuBar(tedMenuBar);
249: tedMenuBar.getAccessibleContext().setAccessibleName(
250: "Ted Menu Bar");
251: tedMenuBar.getAccessibleContext().setAccessibleDescription(
252: "Ted menu bar.");
253:
254: }//GEN-END:initComponents
255:
256: /** This method is called when File -> Save menu item is invoked.
257: * It saves the current opened file.
258: * @param evt ActionEvent instance passed from actionPerformed event.
259: */
260: private void saveMenuItemActionPerformed(
261: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuItemActionPerformed
262: if ("".equals(fileName))
263: doSaveAs();
264: else
265: doSave(fileName);
266: }//GEN-LAST:event_saveMenuItemActionPerformed
267:
268: /** This method is called when File -> Exit menu item is invoked.
269: * It closes the application.
270: * @param evt ActionEvent instance passed from actionPerformed event.
271: */
272: private void exitMenuItemActionPerformed(
273: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
274: System.exit(0);
275: }//GEN-LAST:event_exitMenuItemActionPerformed
276:
277: /** This method is called when Edit -> Find menu item is invoked.
278: * It creates and shows the Finder frame to allow the user to search in the text.
279: * @param evt ActionEvent instance passed from actionPerformed event.
280: */
281: private void findMenuItemActionPerformed(
282: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findMenuItemActionPerformed
283: new Finder(this , textBox).show();
284: }//GEN-LAST:event_findMenuItemActionPerformed
285:
286: /** This method is called when Help -> About menu item is invoked.
287: * It creates and shows the About dialog.
288: * @param evt ActionEvent instance passed from actionPerformed event.
289: */
290: private void aboutMenuItemActionPerformed(
291: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
292: new About(this ).show();
293: }//GEN-LAST:event_aboutMenuItemActionPerformed
294:
295: /** This method is called when File -> Save as menu item is invoked.
296: * It asks for a new file name, then saves the file.
297: * @param evt ActionEvent instance passed from actionPerformed event.
298: */
299: private void saveAsMenuItemActionPerformed(
300: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsMenuItemActionPerformed
301: doSaveAs();
302: }//GEN-LAST:event_saveAsMenuItemActionPerformed
303:
304: /** This method is called when File -> Open menu item is invoked.
305: * It displays a dialog to choose the file to be opened and edited.
306: * @param evt ActionEvent instance passed from actionPerformed event.
307: */
308: private void openMenuItemActionPerformed(
309: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
310: FileDialog fileDialog = new FileDialog(this , "Open...",
311: FileDialog.LOAD);
312: fileDialog.show();
313: if (fileDialog.getFile() == null)
314: return;
315: fileName = fileDialog.getDirectory() + File.separator
316: + fileDialog.getFile();
317:
318: FileInputStream fis = null;
319: String str = null;
320: try {
321: fis = new FileInputStream(fileName);
322: int size = fis.available();
323: byte[] bytes = new byte[size];
324: fis.read(bytes);
325: str = new String(bytes);
326: } catch (IOException e) {
327: } finally {
328: try {
329: fis.close();
330: } catch (IOException e2) {
331: }
332: }
333:
334: if (str != null)
335: textBox.setText(str);
336: }//GEN-LAST:event_openMenuItemActionPerformed
337:
338: /** This method is called when File -> New menu item is invoked.
339: * It clears the editor pane.
340: * @param evt ActionEvent instance passed from actionPerformed event.
341: */
342: private void newMenuItemActionPerformed(
343: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMenuItemActionPerformed
344: fileName = "";
345: textBox.setText("");
346: }//GEN-LAST:event_newMenuItemActionPerformed
347:
348: /** This method is called when the application frame is closed.
349: * @param evt WindowEvent instance passed from windowClosing event.
350: */
351: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
352: System.exit(0);
353: }//GEN-LAST:event_exitForm
354:
355: /** Saves the current content of editor pane to the file.
356: * @param fileName Name of the file.
357: */
358: private void doSave(String fileName) {
359: FileOutputStream fos = null;
360: String str = textBox.getText();
361: try {
362: fos = new FileOutputStream(fileName);
363: fos.write(str.getBytes());
364: } catch (IOException e) {
365: } finally {
366: try {
367: fos.close();
368: } catch (IOException e2) {
369: }
370: }
371: }
372:
373: /** Asks for a file name. then saves the current content of editor pane to the file.
374: */
375: private void doSaveAs() {
376: FileDialog fileDialog = new FileDialog(this , "Save As...",
377: FileDialog.SAVE);
378: fileDialog.show();
379: if (fileDialog.getFile() == null)
380: return;
381: fileName = fileDialog.getDirectory() + File.separator
382: + fileDialog.getFile();
383:
384: doSave(fileName);
385: }
386:
387: // Variables declaration - do not modify//GEN-BEGIN:variables
388: private javax.swing.JMenuItem aboutMenuItem;
389: private javax.swing.JMenu editMenu;
390: private javax.swing.JMenuItem exitMenuItem;
391: private javax.swing.JMenu fileMenu;
392: private javax.swing.JMenuItem findMenuItem;
393: private javax.swing.JMenu helpMenu;
394: private javax.swing.JSeparator jSeparator1;
395: private javax.swing.JSeparator jSeparator2;
396: private javax.swing.JMenuItem newMenuItem;
397: private javax.swing.JMenuItem openMenuItem;
398: private javax.swing.JMenuItem saveAsMenuItem;
399: private javax.swing.JMenuItem saveMenuItem;
400: private javax.swing.JMenuBar tedMenuBar;
401: private javax.swing.JTextArea textBox;
402: private javax.swing.JScrollPane textScrollPane;
403: // End of variables declaration//GEN-END:variables
404:
405: private String fileName = "";
406:
407: /** Starts the application.
408: * @param args Application arguments.
409: */
410: public static void main(java.lang.String[] args) {
411: new Ted().show();
412: }
413:
414: }
|