home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-12-08 | 7.3 KB | 286 lines |
- /*
- This simple extension of the java.awt.Frame class
- contains all the elements necessary to act as the
- main window of an application.
- */
-
- import java.awt.*;
- import java.io.*;
-
-
- public class JavaPad extends Frame
- {
- void SelectAll_Action(Event event) {
- // to do: place event handler code here.
- textArea1.select(0, textArea1.getText().length()-1);
- }
-
- void Paste_Action(Event event) {
- // to do: place event handler code here.
- textArea1. replaceText(m_Clipboard, textArea1.getSelectionStart(), textArea1.getSelectionEnd());
- }
-
- void Copy_Action(Event event) {
- // to do: place event handler code here.
- m_Clipboard = textArea1.getSelectedText();
- }
-
- void Cut_Action(Event event) {
- // to do: place event handler code here.
- m_Clipboard = textArea1.getSelectedText();
- textArea1. replaceText("", textArea1.getSelectionStart(), textArea1.getSelectionEnd());
- }
-
- void SaveAs_Action(Event event) {
- // to do: place event handler code here.
- saveFileDialog1.show();
- m_fileName = saveFileDialog1.getFile();
-
- if (m_fileName!=null)
- {
- try {
- FileOutputStream foS = new FileOutputStream( m_fileName );
-
- String text = textArea1.getText();
- int size = text.length();
- byte[] byteBuffer = new byte[size];
-
- text.getBytes(0, byteBuffer.length, byteBuffer, 0);
- foS.write(byteBuffer);
- foS.close();
- }
- catch (IOException e)
- {
- System.out.print("Error reading file");
- }
- }
- setTitle("JavaPad - " + m_fileName);
- }
-
- void New_Action(Event event) {
- // to do: place event handler code here.
- m_fileName = "Untitled";
- textArea1.setText("");
- setTitle("JavaPad - " + m_fileName);
- }
-
- void Save_Action(Event event) {
- // to do: place event handler code here.
- if (m_fileName == "Untitled")
- {
- // Create a new file and then save it's contents.
- saveFileDialog1.show();
- m_fileName = saveFileDialog1.getFile();
- }
-
- if (m_fileName!=null)
- {
- try {
- FileOutputStream foS = new FileOutputStream( m_fileName );
-
- String text = textArea1.getText();
- int size = text.length();
- byte[] byteBuffer = new byte[size];
-
- text.getBytes(0, byteBuffer.length, byteBuffer, 0);
- foS.write(byteBuffer);
- foS.close();
- }
- catch (IOException e)
- {
- System.out.print("Error reading file");
- }
- }
- }
-
- void Open_Action(Event event) {
- //{{CONNECTION
- // Action from Open... Show the OpenFileDialog
- openFileDialog1.show();
- m_fileName = openFileDialog1.getFile();
- if (openFileDialog1.getFile()!=null)
- {
- try {
- FileInputStream fiS = new FileInputStream( m_fileName );
- textArea1.setText("");
-
- int notAtEnd = 1;
- while (notAtEnd!=-1 && notAtEnd!=0)
- {
- int size = fiS.available();
- byte[] byteBuffer = new byte[size];
- notAtEnd = fiS.read(byteBuffer);
-
- // Convert to a String
- StringBuffer sb = new StringBuffer(size);
- int count = 0;
- while (count < size)
- {
- sb.append((char)byteBuffer[count]);
- count++;
- }
- textArea1.appendText(sb.toString());
- }
-
- textArea1.select(0,0);
- setTitle("JavaPad - " + m_fileName);
- fiS.close();
- }
- catch (IOException e)
- {
- System.out.print("Error reading file");
- }
- }
- //}}
- }
-
- void About_Action(Event event) {
- //{{CONNECTION
- // Action from About Create and show as modal
- (new AboutDialog(this, true)).show();
- //}}
- }
-
- void Exit_Action(Event event) {
- //{{CONNECTION
- // Action from Exit Create and show as modal
- (new QuitDialog(this, true)).show();
- //}}
- }
-
- public JavaPad() {
- m_FindIndex = 0;
-
- //{{INIT_CONTROLS
- setLayout(new GridLayout(0,1,0,0));
- addNotify();
- resize(insets().left + insets().right + 400,insets().top + insets().bottom + 300);
- openFileDialog1 = new java.awt.FileDialog(this, "Open",FileDialog.LOAD);
- //$$ openFileDialog1.move(0,0);
- saveFileDialog1 = new java.awt.FileDialog(this, "Save",FileDialog.SAVE);
- //$$ saveFileDialog1.move(0,0);
- textArea1 = new java.awt.TextArea();
- textArea1.reshape(insets().left + 0,insets().top + 0,400,300);
- add(textArea1);
- setTitle("JavaPad - Untitled");
- //}}
-
- //{{INIT_MENUS
- mainMenuBar = new java.awt.MenuBar();
-
- menu1 = new java.awt.Menu("File");
- menu1.add("New");
- menu1.add("Open...");
- menu1.add("Save");
- menu1.add("Save As...");
- menu1.addSeparator();
- menu1.add("Exit");
- mainMenuBar.add(menu1);
-
- menu2 = new java.awt.Menu("Edit");
- menu2.add("Cut");
- menu2.add("Copy");
- menu2.add("Paste");
- menu2.addSeparator();
- menu2.add("Select All");
- mainMenuBar.add(menu2);
-
- menu3 = new java.awt.Menu("Help");
- mainMenuBar.setHelpMenu(menu3);
- menu3.add("About");
- mainMenuBar.add(menu3);
- setMenuBar(mainMenuBar);
- //}}
- }
-
- public JavaPad(String title) {
- this();
- setTitle(title);
- }
-
- public synchronized void show() {
- move(50, 50);
- super.show();
- }
-
- public boolean handleEvent(Event event) {
- if (event.id == Event.WINDOW_DESTROY) {
- hide(); // hide the Frame
- dispose(); // free the system resources
- System.exit(0); // close the application
- return true;
- }
- return super.handleEvent(event);
- }
-
- public boolean action(Event event, Object arg) {
- if (event.target instanceof MenuItem) {
- String label = (String) arg;
- if (label.equalsIgnoreCase("Select All")) {
- SelectAll_Action(event);
- return true;
- } else
- if (label.equalsIgnoreCase("Paste")) {
- Paste_Action(event);
- return true;
- } else
- if (label.equalsIgnoreCase("Copy")) {
- Copy_Action(event);
- return true;
- } else
- if (label.equalsIgnoreCase("Cut")) {
- Cut_Action(event);
- return true;
- } else
- if (label.equalsIgnoreCase("Save As...")) {
- SaveAs_Action(event);
- return true;
- } else
- if (label.equalsIgnoreCase("New")) {
- New_Action(event);
- return true;
- } else
- if (label.equalsIgnoreCase("Save")) {
- Save_Action(event);
- return true;
- } else
- if (label.equalsIgnoreCase("Open...")) {
- Open_Action(event);
- return true;
- } else
- if (label.equalsIgnoreCase("About")) {
- About_Action(event);
- return true;
- } else
- if (label.equalsIgnoreCase("Exit")) {
- Exit_Action(event);
- return true;
- }
- }
- return super.action(event, arg);
- }
-
- static public void main(String args[]) {
- (new JavaPad()).show();
- }
-
- String m_fileName;
- String m_Clipboard;
- String m_FindString;
- String m_ReplaceString;
- int m_FindIndex; // Where to start searching.
-
- //{{DECLARE_CONTROLS
- java.awt.FileDialog openFileDialog1;
- java.awt.FileDialog saveFileDialog1;
- java.awt.TextArea textArea1;
- //}}
-
- //{{DECLARE_MENUS
- java.awt.MenuBar mainMenuBar;
- java.awt.Menu menu1;
- java.awt.Menu menu2;
- java.awt.Menu menu3;
- //}}
- }
-