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