home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 February / VPR9802A.ISO / APP_DEMO / VC / SAMPLES.BIN / JavaPad.java < prev    next >
Text File  |  1997-10-27  |  11KB  |  384 lines

  1. /*
  2.     This simple extension of the java.awt.Frame class
  3.     contains all the elements necessary to act as the
  4.     main window of an application.
  5.  */
  6.  
  7. import java.awt.*;
  8. import java.io.*;
  9. import java.util.*;
  10. import java.awt.event.*;
  11.  
  12.  
  13. public class JavaPad extends Frame
  14. {
  15.     void SelectAll_Action(java.awt.event.ActionEvent event) {
  16.         // to do: place event handler code here.
  17.         textArea1.select(0, textArea1.getText().length());
  18.     }
  19.  
  20.     void Paste_Action(java.awt.event.ActionEvent event) {
  21.         // to do: place event handler code here.
  22.         textArea1. replaceText(m_Clipboard, textArea1.getSelectionStart(), textArea1.getSelectionEnd());
  23.     }
  24.  
  25.     void Copy_Action(java.awt.event.ActionEvent event) {
  26.         // to do: place event handler code here.
  27.         m_Clipboard = textArea1.getSelectedText();
  28.     }
  29.  
  30.     void Cut_Action(java.awt.event.ActionEvent event) {
  31.         // to do: place event handler code here.
  32.         m_Clipboard = textArea1.getSelectedText();
  33.         textArea1. replaceText("", textArea1.getSelectionStart(), textArea1.getSelectionEnd());
  34.     }
  35.  
  36.     void SaveAs_Action(java.awt.event.ActionEvent event) {
  37.         // to do: place event handler code here.
  38.         saveFileDialog1.show();
  39.         m_fileName = saveFileDialog1.getFile();
  40.         m_dirName = saveFileDialog1.getDirectory();
  41.  
  42.         if (m_fileName!=null)
  43.         {
  44.             try {
  45.                 FileOutputStream foS = new FileOutputStream( m_dirName + m_fileSeparator + m_fileName );
  46.                 String text = textArea1.getText();
  47.                 byte[] byteBuffer = text.getBytes();
  48.                 foS.write(byteBuffer);
  49.                 foS.close();
  50.             }
  51.             catch (IOException e)
  52.             {
  53.                 System.out.print("Error reading file");
  54.             }
  55.         }
  56.         setTitle("JavaPad - " + m_fileName);
  57.     }
  58.  
  59.     void New_Action(java.awt.event.ActionEvent event) {
  60.         // to do: place event handler code here.
  61.         m_fileName = "Untitled";
  62.         textArea1.setText("");
  63.         setTitle("JavaPad - " + m_fileName);
  64.     }
  65.  
  66.     void Save_Action(java.awt.event.ActionEvent event) {
  67.             
  68.         if (m_fileName == "Untitled")
  69.         {
  70.             // Create a new file and then save it's contents.
  71.             saveFileDialog1.show();
  72.             m_fileName = saveFileDialog1.getFile();
  73.             m_dirName = saveFileDialog1.getDirectory();
  74.         }
  75.  
  76.         if (m_fileName!=null)
  77.         {
  78.             try {
  79.                 FileOutputStream foS = new FileOutputStream( m_dirName + m_fileSeparator + m_fileName );
  80.                 String text = textArea1.getText();
  81.                 byte[] byteBuffer = text.getBytes();
  82.                 foS.write(byteBuffer);
  83.                 foS.close();
  84.             }
  85.             catch (IOException e)
  86.             {
  87.                 System.out.print("Error reading file");
  88.             }
  89.         }
  90.     }
  91.  
  92.     void Open_Action(java.awt.event.ActionEvent event) {
  93.         //{{CONNECTION
  94.         // Action from Open... Show the OpenFileDialog
  95.         openFileDialog1.show();
  96.         m_fileName = openFileDialog1.getFile();
  97.         m_dirName = openFileDialog1.getDirectory();
  98.         if (openFileDialog1.getFile()!=null)
  99.         {
  100.             try {
  101.                 BufferedReader fiS = new BufferedReader(new FileReader(  m_dirName + m_fileSeparator + m_fileName  ));
  102.                 textArea1.setText("");
  103.  
  104.                 String s = new String("");
  105.                 while ((s = fiS.readLine()) != null)
  106.                 {
  107.                     textArea1.appendText(s + "\n");
  108.                 }
  109.                 textArea1.select(0,0);
  110.                 setTitle("JavaPad - " + m_fileName);
  111.                 fiS.close();
  112.             }
  113.             catch (IOException e)
  114.             {
  115.                 System.out.print("Error reading file");
  116.             }
  117.         }
  118.         //}}
  119.     }
  120.  
  121.     void About_Action(java.awt.event.ActionEvent event) {
  122.         //{{CONNECTION
  123.         // Action from About Create and show as modal
  124.         (new AboutDialog(this, true)).show();
  125.         //}}
  126.     }
  127.  
  128.     void Exit_Action(java.awt.event.ActionEvent event) {
  129.         //{{CONNECTION
  130.         // Action from Exit Create and show as modal
  131.         (new QuitDialog(this, true)).show();
  132.         //}}
  133.     }
  134.  
  135.     public JavaPad() {
  136.         
  137.         Properties prop = new Properties(System.getProperties());
  138.         m_fileSeparator = prop.getProperty("file.separator");
  139.         
  140.         //{{INIT_CONTROLS
  141.         setLayout(new GridLayout(0,1,0,0));
  142.         setVisible(false);
  143.         setSize(insets().left + insets().right + 400,insets().top + insets().bottom + 300);
  144.         saveFileDialog1 = new java.awt.FileDialog(this);
  145.         saveFileDialog1.setMode(FileDialog.SAVE);
  146.         saveFileDialog1.setTitle("Save");
  147.         //$$ saveFileDialog1.move(0,0);
  148.         openFileDialog1 = new java.awt.FileDialog(this);
  149.         openFileDialog1.setMode(FileDialog.LOAD);
  150.         openFileDialog1.setTitle("Open");
  151.         //$$ openFileDialog1.move(36,0);
  152.         textArea1 = new java.awt.TextArea();
  153.         textArea1.setBounds(insets().left + 0,insets().top + 0,400,300);
  154.         add(textArea1);
  155.         setTitle("JavaPad - Untitled");
  156.         //}}
  157.  
  158.            m_findReplace = new FindReplace("",textArea1);
  159.  
  160.         //{{INIT_MENUS
  161.         mainMenuBar = new java.awt.MenuBar();
  162.         menu1 = new java.awt.Menu("File");
  163.         miNew = new java.awt.MenuItem("New");
  164.         menu1.add(miNew);
  165.         miOpen = new java.awt.MenuItem("Open...");
  166.         menu1.add(miOpen);
  167.         miSave = new java.awt.MenuItem("Save");
  168.         menu1.add(miSave);
  169.         miSaveAs = new java.awt.MenuItem("Save As...");
  170.         menu1.add(miSaveAs);
  171.         menu1.addSeparator();
  172.         miExit = new java.awt.MenuItem("Exit");
  173.         menu1.add(miExit);
  174.         mainMenuBar.add(menu1);
  175.         menu2 = new java.awt.Menu("Edit");
  176.         miCut = new java.awt.MenuItem("Cut");
  177.         menu2.add(miCut);
  178.         miCopy = new java.awt.MenuItem("Copy");
  179.         menu2.add(miCopy);
  180.         miPaste = new java.awt.MenuItem("Paste");
  181.         menu2.add(miPaste);
  182.         menu2.addSeparator();
  183.         miSelectAll = new java.awt.MenuItem("Select All");
  184.         menu2.add(miSelectAll);
  185.         mainMenuBar.add(menu2);
  186.         menu4 = new java.awt.Menu("Search");
  187.         miFind = new java.awt.MenuItem("Find ...");
  188.         menu4.add(miFind);
  189.         miFindAgain = new java.awt.MenuItem("Find Again");
  190.         menu4.add(miFindAgain);
  191.         miReplace = new java.awt.MenuItem("Replace ...");
  192.         menu4.add(miReplace);
  193.         mainMenuBar.add(menu4);
  194.         menu3 = new java.awt.Menu("Help");
  195.         mainMenuBar.setHelpMenu(menu3);
  196.         miAbout = new java.awt.MenuItem("About");
  197.         menu3.add(miAbout);
  198.         mainMenuBar.add(menu3);
  199.         setMenuBar(mainMenuBar);
  200.         //$$ mainMenuBar.move(0,0);
  201.         //}}
  202.     
  203.         //{{REGISTER_LISTENERS
  204.         Action lAction = new Action();
  205.         miSelectAll.addActionListener(lAction);
  206.         miPaste.addActionListener(lAction);
  207.         miCopy.addActionListener(lAction);
  208.         miCut.addActionListener(lAction);
  209.         miSaveAs.addActionListener(lAction);
  210.         miNew.addActionListener(lAction);
  211.         miSave.addActionListener(lAction);
  212.         miOpen.addActionListener(lAction);
  213.         miAbout.addActionListener(lAction);
  214.         miExit.addActionListener(lAction);
  215.         miFind.addActionListener(lAction);
  216.         miFindAgain.addActionListener(lAction);
  217.         SymKey lSymKey = new SymKey();
  218.         textArea1.addKeyListener(lSymKey);
  219.         miReplace.addActionListener(lAction);
  220.         SymWindow aSymWindow = new SymWindow();
  221.         addWindowListener(aSymWindow);
  222.         //}}
  223.     }
  224.  
  225.     public JavaPad(String title) {
  226.         this();
  227.         setTitle(title);
  228.     }
  229.  
  230.     public synchronized void show() {
  231.         move(50, 50);
  232.         super.show();
  233.     }
  234.     
  235.  
  236.     static public void main(String args[]) {
  237.         (new JavaPad()).show();
  238.     }
  239.     
  240.     String m_fileSeparator;
  241.     String m_dirName;
  242.     String m_fileName;
  243.     String m_Clipboard;
  244.  
  245.     //{{DECLARE_CONTROLS
  246.     java.awt.FileDialog saveFileDialog1;
  247.     java.awt.FileDialog openFileDialog1;
  248.     java.awt.TextArea textArea1;
  249.     //}}
  250.     FindDialog m_findDialog;
  251.     ReplaceDialog m_replaceDialog;
  252.  
  253.     //{{DECLARE_MENUS
  254.     java.awt.MenuBar mainMenuBar;
  255.     java.awt.Menu menu1;
  256.     java.awt.MenuItem miNew;
  257.     java.awt.MenuItem miOpen;
  258.     java.awt.MenuItem miSave;
  259.     java.awt.MenuItem miSaveAs;
  260.     java.awt.MenuItem miExit;
  261.     java.awt.Menu menu2;
  262.     java.awt.MenuItem miCut;
  263.     java.awt.MenuItem miCopy;
  264.     java.awt.MenuItem miPaste;
  265.     java.awt.MenuItem miSelectAll;
  266.     java.awt.Menu menu4;
  267.     java.awt.MenuItem miFind;
  268.     java.awt.MenuItem miFindAgain;
  269.     java.awt.MenuItem miReplace;
  270.     java.awt.Menu menu3;
  271.     java.awt.MenuItem miAbout;
  272.     //}}
  273.     FindReplace m_findReplace;
  274.     
  275.     class Action implements java.awt.event.ActionListener
  276.     {
  277.         public void actionPerformed(java.awt.event.ActionEvent event)
  278.         {
  279.             Object object = event.getSource();
  280.             if (object == miSelectAll)
  281.                 SelectAll_Action(event);
  282.             else if (object == miPaste)
  283.                 Paste_Action(event);
  284.             else if (object == miCopy)
  285.                 Copy_Action(event);
  286.             else if (object == miCut)
  287.                 Cut_Action(event);
  288.             else if (object == miSaveAs)
  289.                 SaveAs_Action(event);
  290.             else if (object == miNew)
  291.                 New_Action(event);
  292.             else if (object == miSave)
  293.                 Save_Action(event);
  294.             else if (object == miOpen)
  295.                 Open_Action(event);
  296.             else if (object == miAbout)
  297.                 About_Action(event);
  298.             else if (object == miExit)
  299.                 Exit_Action(event);
  300.             else if (object == miFind)
  301.                 miFind_Action(event);
  302.             else if (object == miFindAgain)
  303.                 miFindAgain_Action(event);
  304.             else if (object == miReplace)
  305.                 miReplace_Action(event);
  306.         }
  307.     }
  308.  
  309.     class SymWindow extends java.awt.event.WindowAdapter
  310.     {
  311.         public void windowClosing(java.awt.event.WindowEvent event)
  312.         {
  313.             Object object = event.getSource();
  314.             if (object == JavaPad.this)
  315.                 Frame1_WindowClosing(event);
  316.         }
  317.     }
  318.  
  319.     void Frame1_WindowClosing(java.awt.event.WindowEvent event)
  320.     {
  321.         hide();         // hide the Frame
  322.         dispose();      // free the system resources
  323.         System.exit(0); // close the application
  324.     }
  325.  
  326.     void miFind_Action(java.awt.event.ActionEvent event)
  327.     {   
  328.         int select_start;
  329.         int select_end;
  330.         int select_diff;
  331.         int start_index;
  332.         String select_body;
  333.         
  334.         //automatically put in selection in FindDialog field
  335.         select_start = textArea1.getSelectionStart();
  336.         select_end = textArea1.getSelectionEnd();
  337.         select_diff = select_end - select_start;
  338.         //set find field if selected text not too big
  339.         if(select_diff > 0 && select_diff < 20) {  
  340.             m_findReplace.setFind(textArea1.getSelectedText());
  341.             start_index = textArea1.getSelectionStart() + select_diff;
  342.             m_findReplace.setIndex(start_index);
  343.         }
  344.         else {
  345.             m_findReplace.setFind("");
  346.             m_findReplace.setIndex(textArea1.getSelectionStart());
  347.         }
  348.         m_findReplace.setOccur(0);
  349.                      
  350.         (new FindDialog(this, "Find", true, m_findReplace)).show();
  351.     }
  352.  
  353.     void miFindAgain_Action(java.awt.event.ActionEvent event)
  354.     {
  355.         FindReplaceEngine fre;
  356.         fre = new FindReplaceEngine(m_findReplace);
  357.         fre.FindIt();
  358.         if(!m_findReplace.isFoundIt()) this.getToolkit().beep();
  359.     }
  360.  
  361.     class SymKey extends java.awt.event.KeyAdapter
  362.     {
  363.         public void keyPressed(java.awt.event.KeyEvent event)
  364.         {
  365.             Object object = event.getSource();
  366.             if (object == textArea1)
  367.                 textArea1_KeyPress(event);
  368.         }
  369.     }
  370.  
  371.     void textArea1_KeyPress(java.awt.event.KeyEvent event)
  372.     {   
  373.         int keypressed;
  374.         keypressed = event.getKeyCode();
  375.         //114 is F3 on my keyboard, although java.awt.Event.F3 is 1010
  376.         if(keypressed == 114) { miFindAgain_Action(new ActionEvent(this,0,"")); }
  377.     }
  378.  
  379.     void miReplace_Action(java.awt.event.ActionEvent event)
  380.     {
  381.     //(new ReplaceDialog(this, "Replace", true, m_findReplace)).show();
  382.     }
  383. }
  384.