collapse collapse

* Who's Online

  • Dot Guests: 68
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.

* Board Stats

  • stats Total Members: 88
  • stats Total Posts: 11163
  • stats Total Topics: 1699
  • stats Total Categories: 4
  • stats Total Boards: 76
  • stats Most Online: 248

Author Topic: [TUT] advanced java text editor [TUT]  (Read 3844 times)

0 Members and 1 Guest are viewing this topic.

Offline Legion

  • Newbie
  • *
  • Posts: 11
  • Reputation 23
    • View Profile
[TUT] advanced java text editor [TUT]
« on: August 09, 2012, 07:49:19 pm »
Hello Everybody! Here is a tutorial on an advanced text editor made in java!

Editor.java
Code: Java
  1. /* Imports the java.net package class. */
  2. import java.net.URL;
  3. /* Imports javax.swing.undo package classes. */
  4. import javax.swing.undo.UndoManager;
  5. import javax.swing.undo.CannotUndoException;
  6. import javax.swing.undo.CannotRedoException;
  7. /* Imports javax.swing package classes. */
  8. import javax.swing.JColorChooser;
  9. import javax.swing.JFrame;
  10. import javax.swing.JMenu;
  11. import javax.swing.JMenuBar;
  12. import javax.swing.JMenuItem;
  13. import javax.swing.JTextArea;
  14. import javax.swing.JPanel;
  15. import javax.swing.JScrollPane;
  16. import javax.swing.JCheckBoxMenuItem;
  17. import javax.swing.UIManager;
  18. import javax.swing.Action;
  19. import javax.swing.KeyStroke;
  20. import javax.swing.AbstractAction;
  21. /* Imports java.awt package class. */
  22. import java.awt.BorderLayout;
  23. /* Imports java.awt.event package classes. */
  24. import java.awt.event.WindowAdapter;
  25. import java.awt.event.WindowEvent;
  26. import java.awt.event.ActionEvent;
  27. import java.awt.event.ActionListener;
  28. import java.awt.event.ActionEvent;
  29. import java.awt.event.KeyEvent;
  30. /* Imports javax.swing.event package classes. */
  31. import javax.swing.event.UndoableEditListener;
  32. import javax.swing.event.UndoableEditEvent;
  33. /*
  34. Class Editor - This class is the main class of the application. This class initializes
  35. the interface and loads all components like text area and menu bar on the main window.
  36. Fields:
  37. menu - Creates the menu bar.
  38. file - Creates the File menu.
  39. edit - Creates the Edit menu.
  40. format - Creates the Format menu.
  41. lock - Creates the Lock menu.
  42. help - Creates the Help menu.
  43. newfile - Creates the New menu item.
  44. open - Creates the Open menu item.
  45. print - Creates the Print menu item.
  46. save - Creates the Save menu item.
  47. saveas - Creates the Save As menu item.
  48. exit - Creates the Open Exit item.
  49. cut - Creates the Cut menu item.
  50. copy - Creates the Copy menu item.
  51. paste - Creates the Paste menu item.
  52. find - Creates the Find menu item.
  53. selectAll - Creates the Select All menu tem.
  54. datetime - Creates the Date/Time menu item.
  55. font - Creates the Open Font item.
  56. color - Creates the Open Color item.
  57. about - Creates the Open Help item.
  58. area - Creates an object the JTextArea class that contains the file contents.
  59. panel - Creates the panel menu item that contains all the AWT and swing components.
  60. scrollpane - Creates an object of the JScrollPane class that is added to the text area.
  61. str - Creates the String object.
  62. wordWrap - Creates the Word Wrap menu item.
  63. exclusiveLock - Creates the Exclusive menu item.
  64. shareLock - Creates the Share menu item.
  65. Methods:
  66. addWindowListener() - This method is called when end user clicks the window close button.
  67. actionPerformed() - This method is invoked when an end user selects any command from the menu bar.
  68. undoableEditHappened() - This method is called when an end user performs undo and redo operation.
  69. main() - This method creates the main window of the application and displays it.
  70. */
  71. public class Editor extends JFrame implements ActionListener, UndoableEditListener, Runnable
  72. {
  73. /* Creates an object of the ActionPerform class. */
  74. public ActionPerform action = new ActionPerform(this);
  75. /* Creates an object of the UndoManager class. */
  76. UndoManager undoManager = new UndoManager();
  77. /* Creates an instance of the UndoAction class. */
  78. UndoAction undoAction = new UndoAction();
  79. /* Creates an instance of the RedoAction class. */
  80. RedoAction redoAction = new RedoAction();
  81. /* Declares an object of the JMenuBar class. */
  82. JMenuBar menu;
  83. /* Declares objects of the JMenu class. */
  84. JMenu file;
  85. JMenu edit;
  86. JMenu format;
  87. JMenu lock;
  88. JMenu help;
  89. /* Declares objects of the JMenuItem class. */
  90. JMenuItem newfile;
  91. JMenuItem open;
  92. JMenuItem print;
  93. JMenuItem save;
  94. JMenuItem saveas;
  95. JMenuItem exit;
  96. JMenuItem copy;
  97. JMenuItem paste;
  98. JMenuItem find;
  99. JMenuItem selectAll;
  100. JMenuItem datetime;
  101. JMenuItem font;
  102. JMenuItem color;
  103. JMenuItem about;
  104. /* Declares an object of the JTextArea class. */
  105. JTextArea area;
  106. /* Declares an object of the JPanel class. */
  107. JPanel panel;
  108. /* Declares an object of the JScrollPane class. */
  109. JScrollPane scrollpane;
  110. /* Declares an object of the String class. */
  111. String str;
  112. /* Declares objects of the JCheckBoxMenuItem class. */
  113. JCheckBoxMenuItem exclusiveLock;
  114. /* Declares objects of the Thread class. */
  115. Thread t1;
  116. Thread t2;
  117. Thread t3;
  118. int act;
  119. /* Defines the constructor of the Editor class. */
  120. public Editor()
  121. {
  122.      /*
  123.      Initializes the menu bar and sets the menu bar to the frame.
  124.      */
  125.      menu = new JMenuBar();
  126.      setJMenuBar(menu);
  127.      setTitle("Text2Text by LEGION");
  128.      /*
  129.      Initializes the menu and adds the menus to the menu bar.
  130.      */
  131.      file = new JMenu("File");
  132.      menu.add(file);
  133.      edit = new JMenu("Edit");
  134.      menu.add(edit);
  135.      format = new JMenu("Format");
  136.      menu.add(format);
  137.      lock = new JMenu("Locks");
  138.      menu.add(lock);
  139.      help = new JMenu("Help");
  140.      menu.add(help);
  141.      /* Sets the mnemonic to the menu. */
  142.      file.setMnemonic('f');
  143.      edit.setMnemonic('e');
  144.      lock.setMnemonic('l');
  145.      format.setMnemonic('o');
  146.      help.setMnemonic('h');
  147.      /*
  148.      Initializes the menu items.
  149.      Adds the menu item to the particular menu.
  150.      Adds the action listener with each menu item.
  151.      Sets accelerator to each menu item.
  152.      */
  153.      /* File->New */
  154.      newfile = new JMenuItem("New");
  155.      newfile.addActionListener(this);
  156.      newfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
  157.      file.add(newfile);
  158.      /* File->Open */
  159.      open = new JMenuItem("Open...");
  160.      open.addActionListener(this);
  161.      open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
  162.      file.add(open);
  163.      /* File->Save */
  164.      save = new JMenuItem("Save");
  165.      save.addActionListener(this);
  166.      save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
  167.      file.add(save);
  168.      /* File->Save As */
  169.      saveas = new JMenuItem("Save As...");
  170.      saveas.addActionListener(this);
  171.      file.add(saveas);
  172.      /* File->Print */
  173.      print = new JMenuItem("Print");
  174.      print.addActionListener(this);
  175.      print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));
  176.      file.add(print);
  177.      /* File->Exit */
  178.      exit = new JMenuItem("Exit");
  179.      exit.addActionListener(this);
  180.      exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.CTRL_MASK));
  181.      file.add(exit);
  182.      /* Inserts separators at the 4th and 6th positions. */
  183.      file.insertSeparator(4);
  184.      file.insertSeparator(6);
  185.      /* Edit->Undo */
  186.      edit.add(undoAction);
  187.      /* Edit->Redo */
  188.      edit.add(redoAction);
  189.      /* Edit->Cut */
  190.      cut = new JMenuItem("Cut");
  191.      edit.add(cut);
  192.      cut.addActionListener(this);
  193.      cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
  194.      /* Edit->Copy */
  195.      copy = new JMenuItem("Copy");
  196.      edit.add(copy);
  197.      copy.addActionListener(this);
  198.      copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
  199.      /* Edit->Paste */
  200.      paste = new JMenuItem("Paste");
  201.      edit.add(paste);
  202.      paste.addActionListener(this);
  203.      paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
  204.      /* Edit->Find */
  205.      find = new JMenuItem("Find");
  206.      edit.add(find);
  207.      find.addActionListener(this);
  208.      find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK));
  209.      /* Edit->Select All */
  210.      selectAll = new JMenuItem("Select All");
  211.      edit.add(selectAll);
  212.      selectAll.addActionListener(this);
  213.      selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
  214.      /* Edit->Date/Time */
  215.      datetime = new JMenuItem("Date/Time");
  216.      edit.add(datetime);
  217.      datetime.addActionListener(this);
  218.      datetime.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, ActionEvent.CTRL_MASK));
  219.      /* Inserts separators at the 2nd, 6th, and 9th positions. */
  220.      edit.insertSeparator(2);
  221.      edit.insertSeparator(6);
  222.      edit.insertSeparator(9);
  223.      /* Format->Font */
  224.      font = new JMenuItem("Font");
  225.      format.add(font);
  226.      font.addActionListener(this);
  227.      /* Format->Color */
  228.      color = new JMenuItem("Color");
  229.      format.add(color);
  230.      color.addActionListener(this);
  231.      /* Format->Word Wrap */
  232.      wordWrap = new JCheckBoxMenuItem("Word Wrap");
  233.      format.add(wordWrap);
  234.      wordWrap.addActionListener(this);
  235.      /* Lock->Exclusive Lock */
  236.      exclusiveLock = new JCheckBoxMenuItem("Exclusive Lock");
  237.      exclusiveLock.setEnabled(false);
  238.      lock.add(exclusiveLock);
  239.      exclusiveLock.addActionListener(this);
  240.      /* Lock->Share Lock */
  241.      shareLock = new JCheckBoxMenuItem("Share Lock");
  242.      shareLock.setEnabled(false);
  243.      lock.add(shareLock);
  244.      shareLock.addActionListener(this);
  245.      /* Help->Help Topics */
  246.      about = new JMenuItem("Help Topics");
  247.      help.add(about);
  248.      about.addActionListener(this);
  249.      /* Initializes the panel. */
  250.      panel = new JPanel();
  251.      /* Sets the panel layout as BorderLayout. */
  252.      panel.setLayout(new BorderLayout());
  253.      /* Adds the panel to the frame. */
  254.      getContentPane().add(panel, BorderLayout.CENTER);
  255.      /* Initializes the object of text area */
  256.      area = new JTextArea(25, 65);
  257.      /*
  258.      Adds UndoableEditListener to the area for undo and redo operations.
  259.      */
  260.      area.getDocument().addUndoableEditListener(this);
  261.      /* Sets line wrap to false. */
  262.      area.setLineWrap(false);
  263.      /* Sets word wrap style to false. */
  264.      area.setWrapStyleWord(false);
  265.      /*
  266.      Initializes the object of the Scrollpane class and adds the text area to the scroll pane.
  267.      */
  268.      scrollpane = new JScrollPane(area);
  269.      /* Adds the scroll pane to the panel. */
  270.      panel.add(scrollpane);
  271.      /* Sets default close operation to false. */
  272.      setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  273.      /*
  274.      addWindowListener - It contains a windowClosing() method.
  275.      windowClosing: It is called when the user clicks the cancel button of the window.
  276.      It closes the main window.
  277.      Parameter: we- Represents an object of the WindowEvent class.
  278.      Return Value: NA
  279.      */
  280.      addWindowListener(new WindowAdapter()
  281.      {
  282.          public void windowClosing(WindowEvent we)
  283.          {
  284.              System.exit(0);
  285.          }
  286.      });
  287.      }
  288.      /*
  289.      undoableEditHappened() - This method is called when the user wants to perform undo/redo.
  290.      Parameters: e Represents an object of UndoableEditEvent class that contains
  291.      the details of the undo/redo events.
  292.      Return Value: NA
  293.      */
  294.      public void undoableEditHappened(UndoableEditEvent e)
  295.      {
  296.          /*
  297.          Inserts an Edit at indexOfNextAdd and removes any old edits that were at indexOfNextAdd or later.
  298.          */
  299.          undoManager.addEdit(e.getEdit());
  300.          /* Calls the update() method to perform undo operations. */
  301.          undoAction.update();
  302.          /* Calls the update() method to perform redo operations. */
  303.          redoAction.update();
  304.      }
  305.      /*
  306.      actionPerformed() - This method is called when the end user selects any menu item from the menu bar.
  307.      Parameters: ae Represents an object of the ActionEvent class that contains the details of the event.
  308.      Return Value: NA
  309.      */
  310.      public void actionPerformed(ActionEvent ae)
  311.      {
  312.          /*
  313.          This section is executed when the end user selects the File->New command to the menu bar.
  314.          */
  315.          if(ae.getSource() == newfile)
  316.          {
  317.          /* Calls the newFile() method. */
  318.              exclusiveLock.setEnabled(false);
  319.              shareLock.setEnabled(false);
  320.              action.newFile();
  321.          }
  322.          /*
  323.          This section is executed when the end user selects the File->Open command to the menu bar.
  324.          */
  325.          else if(ae.getSource() == open)
  326.          {
  327.          /* Calls the openFile() method. */
  328.              action.openFile();
  329.          }
  330.    
  331.          /*
  332.          This section is executed when the end user selects the File->Save command to the menu bar.
  333.          */
  334.          else if(ae.getSource() == save)
  335.          {
  336.          /* Calls the saveFile() method. */
  337.              action.saveFile();
  338.          }
  339.          /*
  340.          This section is executed when the end user selects the File->Save As command to the menu bar.
  341.          */
  342.          else if(ae.getSource() == saveas)
  343.          {
  344.          /* Calls the saveAsFile() method. */
  345.              action.saveAsFile();
  346.          }
  347.          /*
  348.          This section is executed when the end user selects the File->Print command to the menu bar.
  349.          */
  350.          else if(ae.getSource() == print)
  351.          {
  352.          /* Calls the printFile() method. */
  353.              act = 1;
  354.              t1 = new Thread(this);
  355.              t1.start();
  356.          }
  357.          /*
  358.          This section is executed when the end user selects the Edit->Exit command to the menu bar.
  359.          */
  360.          else if(ae.getSource() == exit)
  361.          {
  362.          /* Calls the exitFile() method. */
  363.              action.exitFile();
  364.          }
  365.          /*
  366.          This section is executed when the end user selects the Edit->Cut command to the menu bar.
  367.          */
  368.          else if(ae.getSource() == cut)
  369.          {
  370.          /* Calls the cutFile() method. */
  371.              action.cutFile();
  372.          }
  373.          /*
  374.          This section is executed when the end user selects the Edit->Copy command to the menu bar.
  375.          */
  376.          else if(ae.getSource() == copy)
  377.          {
  378.          /* Calls the copyFile() method. */
  379.              action.copyFile();
  380.          }
  381.          /*
  382.          This section is executed when the end user selects the Edit->Paste command to the menu bar.
  383.          */
  384.          else if(ae.getSource() == paste)
  385.          {
  386.              /* Calls the pasteFile() method. */
  387.              action.pasteFile();
  388.          }
  389.          /*
  390.          This section is executed when the end user selects the Edit->Date/Time command to the menu bar.
  391.          */
  392.          else if(ae.getSource() == datetime)
  393.          {
  394.          /* Calls the dateTime() method. */
  395.              action.dateTime();
  396.          }
  397.          /*
  398.          This section is executed when the end user selects the Edit->Find command to the menu bar.
  399.          */
  400.          else if(ae.getSource() == find)
  401.          {
  402.          /* Calls the findFile() method. */
  403.              action.findFile();
  404.          }
  405.          /*
  406.          This section is executed when the end user selects the Edit->Select All command to the menu bar.
  407.          */
  408.          else if(ae.getSource() == selectAll)
  409.          {
  410.          /* Calls the selectAllFile() method. */
  411.              action.selectAllFile();
  412.          }
  413.          /*
  414.          This section is executed when the end user selects the Format->Font command to the menu bar.
  415.          */
  416.          else if(ae.getSource() == font)
  417.          {
  418.          /* Calls the fontFile() method. */
  419.              action.fontFile();
  420.          }
  421.          /*
  422.          This section is executed when the end user selects the Format->Color command to the menu bar.
  423.          */
  424.          else if(ae.getSource() == color)
  425.          {
  426.          /* Calls the colorFile() method. */
  427.              action.colorFile();
  428.          }
  429.          /*
  430.          This section is executed when the end user selects the Format->Word Wrap command to the menu bar.
  431.          */
  432.          else if(ae.getSource() == wordWrap)
  433.          {
  434.          /* Calls the warpFile() method. */
  435.              action.wrapFile();
  436.          }
  437.          /*
  438.          This section is executed when the end user selects the Lock->Exclusive command to the menu bar.
  439.          */
  440.          else if(ae.getSource() == exclusiveLock)
  441.          {
  442.              act = 2;
  443.              t2 = new Thread(this);
  444.              t2.start();
  445.          }
  446.          /*
  447.          This section is executed when the end user selects the Lock->Share command to the menu bar.
  448.          */
  449.          else if(ae.getSource() == shareLock)
  450.          {
  451.              act = 3;
  452.              t3 = new Thread(this);
  453.              t3.start();
  454.          }
  455.          /*
  456.          This section is executed when the end user selects the Help->Help Topics command to the menu bar.
  457.          */
  458.          else if(ae.getSource() == about)
  459.          {
  460.          /* Calls the aboutFile() method. */
  461.              action.aboutFile();
  462.          }
  463.      }
  464.      public void run()
  465.      {
  466.          if(act==1)
  467.          {
  468.              action.printFile();
  469.      }
  470.      else if(act==2)
  471.      {
  472.          /* Calls the exLockFile() method. */
  473.          action.exLockFile();
  474.      }
  475.      else if(act==3)
  476.      {
  477.          /* Calls the shLockFile() method. */
  478.          action.shLockFile();
  479.      }
  480. }
  481. /*
  482. main() - This method creates the main window of the user interface and displays it.
  483. Parameters:
  484. args[] - Contains any command line arguments passed.
  485. Return Value: NA
  486. */
  487. public static void main(String args[])
  488. {
  489.      try
  490.      {
  491.          /*
  492.          Initializes and sets the look and feel of the application to Windows look and feel.
  493.          */
  494.          UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  495.      }
  496.      catch(Exception e)
  497.      {
  498.          System.out.println("Unknown Look and Feel." + e);
  499.      }
  500.      /* Creates an object of the Editor class. */
  501.      Editor ed = new Editor();
  502.      /* Sets the size of the main window. */
  503.      ed.setSize(725, 460);
  504.      /* Sets the visibility of the window to TRUE. */
  505.      ed.setVisible(true);
  506.      /* Displays the main window. */
  507.      ed.show();
  508. }
  509. /*
  510. Class UndoAction - Extends the AbstractAction class to perform undo operations.
  511. Method:
  512. UndoAction() - Implements the default constructor of the UndoClass.
  513. actionPerformed() - Performs the action event.
  514. update() - Updates the change made by the end user. */
  515. class UndoAction extends AbstractAction
  516. {
  517.      /* Defines the default constructor. */
  518.      public UndoAction()
  519. {
  520.      super("Undo");
  521.      setEnabled(false);
  522. }
  523. /*
  524. actionPerformed() - This method is called when the end user select the Edit->Undo command from the menu bar.
  525. Parameters: e Represent an object of the ActionEvent class that contains details of the event.
  526. Return Value: NA
  527. */
  528. public void actionPerformed(ActionEvent e)
  529. {
  530.      try
  531.      {
  532.          undoManager.undo();
  533.      }
  534.      catch (Exception ex)
  535.      {
  536.          System.out.println("Error: " + ex);
  537.      }
  538.      update();
  539.      redoAction.update();
  540. }
  541. /*
  542. update() - This method performs undo operations.
  543. Parameters: NA
  544. Return Value: NA
  545. */
  546. protected void update()
  547. {
  548.      if(undoManager.canUndo())
  549.      {
  550.          setEnabled(true);
  551.          putValue("Undo", undoManager.getUndoPresentationName());
  552.      }
  553.      else
  554.      {
  555.          setEnabled(false);
  556.          putValue(Action.NAME, "Undo");
  557.      }
  558. }
  559. }
  560. /*
  561. Class UndoAction - Extends the AbstractAction class to perform redo operations.
  562. Method:
  563. RedoAction() - Implements the default constructor of UndoClass.
  564. actionPerformed() - Performs the action event.
  565. update() - Updates the changes made by the end user.
  566. */
  567. class RedoAction extends AbstractAction
  568. {
  569. /* Defines the default constructor. */
  570. public RedoAction()
  571. {
  572.      super("Redo");
  573.      setEnabled(false);
  574. }
  575.      /*
  576.      actionPerformed() - This method is called when the end user selects the
  577.      Edit->Redo command from the menu bar.
  578.      Parameters: e Represents an object of the ActionEvent class that contains
  579.      the details of the event.
  580.      Return Value: NA
  581.      */
  582.      public void actionPerformed(ActionEvent e)
  583.      {
  584.          try
  585.          {
  586.              undoManager.redo();
  587.          }
  588.          catch (Exception ex)
  589.          {
  590.          System.out.println("Error: " + ex);
  591.          }
  592.          update();
  593.          undoAction.update();
  594.      }
  595.      /*
  596.      update() - This method performs the redo operations.
  597.      Parameters: NA
  598.      Return Value: NA
  599.      */
  600.      protected void update()
  601.      {
  602.          if(undoManager.canRedo())
  603.          {
  604.              setEnabled(true);
  605.              putValue("Redo", undoManager.getRedoPresentationName());
  606.          }
  607.          else
  608.          {
  609.              setEnabled(false);
  610.          putValue(Action.NAME, "Redo");
  611.          }
  612.      }
  613. }
  614. }
  615.  

printClass.java
Code: Java
  1. /* Imports java.awt.print package class. */
  2. import java.awt.print.PrinterJob;
  3. import java.awt.print.PrinterException;
  4. import java.awt.print.PageFormat;
  5. import java.awt.print.Printable;
  6. /* Imports java.awt package classes. */
  7. import java.awt.Component;
  8. import java.awt.Graphics;
  9. import java.awt.Graphics2D;
  10. /* Imports javax.swing package class. */
  11. import javax.swing.RepaintManager;
  12. /*
  13. class PrintClass - This class implements Printable interface and enable the end user to print the document.
  14. Fields:
  15. compPrint - Contains the document that to be printed.
  16. Methods:
  17. print() - This method prints the current document opened in the text editor window
  18. */
  19. public class PrintClass implements Printable
  20. {
  21. /* Declares the object of Component class. */
  22. private Component compPrint;
  23. public Editor editor;
  24. /* Defines the default constructor. */
  25. public PrintClass(Component compPrint, Editor editor)
  26. {
  27.      this.compPrint = compPrint;
  28.      this.editor = editor;
  29. }
  30. /*
  31. print() - This method prints the document
  32. Parameters: lse - NA
  33. Return Value: NA
  34. */
  35. public void print()
  36. {
  37.      /* Creates an object of the PrinterJob class */
  38.      PrinterJob printer = PrinterJob.getPrinterJob();
  39.      /* Sets the object of PrinterJob class to printable */
  40.      printer.setPrintable(this);
  41.      if(printer.printDialog())
  42.      {
  43.          try
  44.          {
  45.          /* Prints the document */
  46.          printer.print();
  47.          }
  48.          catch(PrinterException pe)
  49.          {
  50.          System.out.println("Error: " + pe);
  51.          }
  52.      }
  53.      editor.show();
  54. }
  55. /*
  56. print() - This method opens a Print dialog box
  57. Parameters:
  58. g - Represents the object of Graphics class
  59. format - Represents the object of PageFormat class
  60. index - Represents an index of page
  61. Return Value: int PAGE_EXIST
  62. */
  63. public int print(Graphics g, PageFormat format, int index)
  64. {
  65.      if(index > 0)
  66.      {
  67.          return(NO_SUCH_PAGE);
  68.      }
  69.      else
  70.      {
  71.          /*
  72.          Creates an object of the Graphics2D class and converts simple graphics to 2D graphics
  73.          */
  74.          Graphics2D g2d = (Graphics2D)g;
  75.          /*
  76.          Translates the origin of the Graphics2D context to the point (x, y)
  77.          in the current coordinate system
  78.          */
  79.          g2d.translate(format.getImageableX(), format.getImageableY());
  80.          /* Creates the object of the RepaintManager class */
  81.          RepaintManager manager1 = RepaintManager.currentManager(compPrint);
  82.          /* Sets the double buffer to FALSE */
  83.          manager1.setDoubleBufferingEnabled(false);
  84.          /* Paints the component */
  85.          compPrint.paint(g2d);
  86.          /* Creates the object of RepaintManager class */
  87.          RepaintManager manager2 = RepaintManager.currentManager(compPrint);
  88.          /* Sets the double buffer to TRUE */
  89.          manager2.setDoubleBufferingEnabled(true);
  90.          /* Returns the PAGE_EXIST value */
  91.          return(PAGE_EXISTS);
  92.      }
  93. }
  94. }
  95.  

fontClass.java

Code: Java
  1. /* Imports javax.swing package classes. */
  2. import javax.swing.JPanel;
  3. import javax.swing.JButton;
  4. import javax.swing.JLabel;
  5. import javax.swing.JTextField;
  6. import javax.swing.JList;
  7. import javax.swing.JScrollPane;
  8. import javax.swing.JDialog;
  9. import javax.swing.BorderFactory;
  10. /* Imports java.awt package classes. */
  11. import java.awt.GraphicsEnvironment;
  12. import java.awt.GridBagConstraints;
  13. import java.awt.GridBagLayout;
  14. import java.awt.FlowLayout;
  15. import java.awt.Font;
  16. /* Imports javax.swing.event package classes. */
  17. import javax.swing.event.ListSelectionListener;
  18. import javax.swing.event.ListSelectionEvent;
  19. /*
  20. class FontClass - This class creates a Font dialog box that enables the
  21. end user to change the font, size, and type of the text.
  22. Fields:
  23. fontLabel - Contains the content of Font label.
  24. sizeLabel - Contains the content of Size label.
  25. typeLabel - Contains the content of Type label.
  26. previewLabel - Contains the content of preview.
  27. label - Contains the preview contents.
  28. fontText - Contains the selected font name.
  29. typeText - Contains the selected font type name.
  30. sizeText - Contains the selected font size name.
  31. fontScroll - Contains the Font list.
  32. typeScroll - Contains the Type list.
  33. sizeScroll - Contains the Size list.
  34. fontList - Contains all the available font.
  35. typeList - Contains all the available types of font style.
  36. sizeList - Contains all the available font size.
  37. ok - Creates an OK button.
  38. cancel - Creates a cancel button.
  39. Methods:
  40. getOK() - This method returns the OK button object
  41. getCancel() - This method returns the Cancel button object
  42. valueChanged() - This method is invoked when an end user select the item from the List box.
  43. font() - This method returns the font
  44. */
  45. public class FontClass extends JDialog implements ListSelectionListener
  46. {
  47. /* Declares the objects of the JPanel class. */
  48. JPanel pan1;
  49. JPanel pan2;
  50. JPanel pan3;
  51. /* Declares the objects of the JLabel class. */
  52. JLabel fontLabel;
  53. JLabel sizeLabel;
  54. JLabel typeLabel;
  55. JLabel previewLabel;
  56. /* Declares the objects of the JTextField class. */
  57. JTextField label;
  58. JTextField fontText;
  59. JTextField sizeText;
  60. JTextField typeText;
  61. /* Declares the objects of the JScrollPane class. */
  62. JScrollPane fontScroll;
  63. JScrollPane typeScroll;
  64. JScrollPane sizeScroll;
  65. /* Declares the objects of the JList class. */
  66. JList fontList;
  67. JList sizeList;
  68. JList typeList;
  69. /* Declares the objects of the JButton class. */
  70. JButton cancel;
  71. /* Defines the default constructor. */
  72. public FontClass()
  73. {
  74.      /* Sets the title of the Font dialog. */
  75.      setTitle("Font Dialog");
  76.      /* Sets the size of Font dialog. */
  77.      setSize(300, 400);
  78.      /* Sets resizable button to false. */
  79.      setResizable(false);
  80.      /* Initializes the object of the GridBagLayout class. */
  81.      gbl = new GridBagLayout();
  82.      /* Sets the Layout. */
  83.      getContentPane().setLayout(gbl);
  84.      /* Creates an object of GridBagConstraints class. */
  85.      gbc = new GridBagConstraints();
  86.      /*
  87.      Initializes the Font label object and add it to the 1, 1, 1, 1 positions with WEST alignment.
  88.      */
  89.      gbc.gridx = 1;
  90.      gbc.gridy = 1;
  91.      gbc.gridwidth = 1;
  92.      gbc.gridheight = 1;
  93.      gbc.anchor = GridBagConstraints.WEST;
  94.      fontLabel = new JLabel("Fonts: ");
  95.      getContentPane().add(fontLabel, gbc);
  96.      /*
  97.      Initializes the Size label object and adds it to the 2, 1, 1, 1 positions with WEST alignment.
  98.      */
  99.      gbc.gridx = 2;
  100.      gbc.gridy = 1;
  101.      gbc.gridwidth = 1;
  102.      gbc.gridheight = 1;
  103.      gbc.anchor = GridBagConstraints.WEST;
  104.      sizeLabel = new JLabel("Sizes: ");
  105.      getContentPane().add(sizeLabel, gbc);
  106.      /*
  107.      Initializes the Types label object and adds it to the 3, 1, 1, 1 positions with WEST alignment.
  108.      */
  109.      gbc.gridx = 3;
  110.      gbc.gridy = 1;
  111.      gbc.gridwidth = 1;
  112.      gbc.gridheight = 1;
  113.      gbc.anchor = GridBagConstraints.WEST;
  114.      typeLabel = new JLabel("Types: ");
  115.      getContentPane().add(typeLabel, gbc);
  116.      /*
  117.      Initializes the Font text field object and adds it to the 1, 2, 1, 1 positions with WEST alignment.
  118.      */
  119.      gbc.gridx = 1;
  120.      gbc.gridy = 2;
  121.      gbc.gridwidth = 1;
  122.      gbc.gridheight = 1;
  123.      gbc.anchor = GridBagConstraints.WEST;
  124.      fontText = new JTextField("Arial", 12);
  125.      getContentPane().add(fontText, gbc);
  126.      /*
  127.      Initializes the Size text field object and adds it to the 2, 2, 1, 1 positions with WEST alignment.
  128.      */
  129.      gbc.gridx = 2;
  130.      gbc.gridy = 2;
  131.      gbc.gridwidth = 1;
  132.      gbc.gridheight = 1;
  133.      gbc.anchor = GridBagConstraints.WEST;
  134.      sizeText = new JTextField("8", 4);
  135.      getContentPane().add(sizeText, gbc);
  136.      /*
  137.      Initializes the Types text field object and adds it to the 3, 2, 1, 1 positions with WEST alignment.
  138.      */
  139.      gbc.gridx = 3;
  140.      gbc.gridy = 2;
  141.      gbc.gridwidth = 1;
  142.      gbc.gridheight = 1;
  143.      gbc.anchor = GridBagConstraints.WEST;
  144.      typeText = new JTextField("Regular", 6);
  145.      getContentPane().add(typeText, gbc);
  146.      /*
  147.      Initializes the Font list object and add it to the Font scroll pane object.
  148.      Adds this scroll pane object to 1, 3, 1, 1 positions with WEST alignment
  149.      */
  150.      gbc.gridx = 1;
  151.      gbc.gridy = 3;
  152.      gbc.gridwidth = 1;
  153.      gbc.gridheight = 1;
  154.      gbc.anchor = GridBagConstraints.WEST;
  155.      String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
  156.      fontList = new JList(fonts);
  157.      fontList.setFixedCellWidth(110);
  158.      fontList.addListSelectionListener(this);
  159.      fontList.setSelectedIndex(0);
  160.      fontScroll = new JScrollPane(fontList);
  161.      getContentPane().add(fontScroll, gbc);
  162.      /*
  163.      Initializes the Size list object and add it to the Size scroll pane object.
  164.      Adds this scroll pane object to 2, 3, 1, 1 positions with WEST alignment
  165.      */
  166.      gbc.gridx = 2;
  167.      gbc.gridy = 3;
  168.      gbc.gridwidth = 1;
  169.      gbc.gridheight = 1;
  170.      gbc.anchor = GridBagConstraints.WEST;
  171.      String[] sizes = {"8","10","12","14","16","18","20","24","28","32","48","72"};
  172.      sizeList = new JList(sizes);
  173.      sizeList.setSelectedIndex(0);
  174.      sizeList.setFixedCellWidth(20);
  175.      sizeList.addListSelectionListener(this);
  176.      sizeScroll = new JScrollPane(sizeList);
  177.      getContentPane().add(sizeScroll, gbc);
  178.      /*
  179.      Initializes the Types list object and adds it to the Types scroll pane object.
  180.      Next, adds this scroll pane object to 3, 3, 1, 1 positions with WEST alignment
  181.      */
  182.      gbc.gridx = 3;
  183.      gbc.gridy = 3;
  184.      gbc.gridwidth = 1;
  185.      gbc.gridheight = 1;
  186.      gbc.anchor = GridBagConstraints.WEST;
  187.      String[] types = {"Regular", "Bold", "Italic", "Bold Italic"};
  188.      typeList = new JList(types);
  189.      typeList.setFixedCellWidth(60);
  190.      typeList.addListSelectionListener(this);
  191.      typeList.setSelectedIndex(0);
  192.      typeScroll = new JScrollPane(typeList);
  193.      getContentPane().add(typeScroll, gbc);
  194.      /*
  195.      Initializes the preview label and adds it to 1,4,3,1 positions with CENTER alignment.
  196.      */
  197.      gbc.gridx = 1;
  198.      gbc.gridy = 4;
  199.      gbc.gridwidth = 3;
  200.      gbc.gridheight = 1;
  201.      gbc.anchor = GridBagConstraints.CENTER;
  202.      pan1 = new JPanel();
  203.      pan1.setLayout(new FlowLayout());
  204.      previewLabel = new JLabel("Preview:");
  205.      pan1.add(previewLabel);
  206.      getContentPane().add(pan1, gbc);
  207.      /*
  208.      Initializes the preview text field and adds it to 1,5,3,1 positions with CENTER alignment.
  209.      */
  210.      gbc.gridx = 1;
  211.      gbc.gridy = 5;
  212.      gbc.gridwidth = 3;
  213.      gbc.gridheight = 1;
  214.      gbc.anchor = GridBagConstraints.CENTER;
  215.      pan2 = new JPanel();
  216.      pan2.setLayout(new FlowLayout());
  217.      label = new JTextField("AaBaCcDdeEfFgGhHjJ", 15);
  218.      label.setEditable(false);
  219.      label.setBorder(BorderFactory.createEtchedBorder());
  220.      label.setFont(new Font("Arial",Font.PLAIN,20));
  221.      pan2.add(label);
  222.      getContentPane().add(pan2, gbc);
  223.      /*
  224.      Initializes the OK and Cancel button and adds these two buttons to the panel.
  225.      Sets layout of the panel to FlowLayout. Now add this panel to the 1, 6, 4, 1
  226.      positions with CENTER alignment.
  227.      */
  228.      gbc.gridx = 1;
  229.      gbc.gridy = 6;
  230.      gbc.gridwidth = 3;
  231.      gbc.gridheight = 1;
  232.      gbc.anchor = GridBagConstraints.CENTER;
  233.      pan3 = new JPanel();
  234.      pan3.setLayout(new FlowLayout());
  235.      ok = new JButton("OK");
  236.      cancel = new JButton("Cancel");
  237.      pan3.add(ok);
  238.      pan3.add(cancel);
  239.      getContentPane().add(pan3, gbc);
  240. }
  241. /*
  242. valueChanged() - This method is called when the user selects any menu item from the menu bar.
  243. Parameters: lse - a ListSelectionEvent object containing details of the event.
  244. Return Value: NA
  245. */
  246. public void valueChanged(ListSelectionEvent lse)
  247. {
  248.      try
  249.      {
  250.          /*
  251.          This section is executed, when end user selects the item from Font list.
  252.          */
  253.          if(lse.getSource() == fontList)
  254.          {
  255.          Font f1 = new Font(String.valueOf(fontList.getSelectedValue()),typeList.getSelectedIndex(),
  256.          Integer.parseInt(String.valueOf(sizeList.getSelectedValue())));
  257.          fontText.setText(String.valueOf(fontList.getSelectedValue()));
  258.          label.setFont(f1);
  259.          }
  260.          /*
  261.          This section is executed, when end user selects the item from Size list.
  262.          */
  263.          else if(lse.getSource() == sizeList)
  264.          {
  265.          Font f2 = new Font(String.valueOf(fontList.getSelectedValue()),typeList.getSelectedIndex(),
  266.          Integer.parseInt(String.valueOf(sizeList.getSelectedValue())));
  267.          sizeText.setText(String.valueOf(sizeList.getSelectedValue()));
  268.          label.setFont(f2);
  269.          }
  270.          /*
  271.          This section is executed, when end user selects the item from Type list.
  272.          */
  273.          else if(lse.getSource() == typeList)
  274.          {
  275.          Font f2 = new Font(String.valueOf(fontList.getSelectedValue()),typeList.getSelectedIndex(),
  276.          Integer.parseInt(String.valueOf(sizeList.getSelectedValue())));
  277.          typeText.setText(String.valueOf(typeList.getSelectedValue()));
  278.          label.setFont(f2);
  279.          }
  280.      }
  281.      catch(Exception nfe){}
  282. }
  283. /*
  284. font() - This method is set the font of the file text.
  285. Parameters: NA
  286. Return Value: font
  287. */
  288. public Font font()
  289. {
  290.      /* Creates an object of the Font class. */
  291.      Font font = new Font(String.valueOf(fontList.getSelectedValue()), typeList.getSelectedIndex(),
  292.      Integer.parseInt(String.valueOf(sizeList.getSelectedValue())));
  293.      /* Returns the font object */
  294.      return font;
  295. }
  296. /*
  297. getOk() method - This method is invoked when end user click the OK button of the Font dialog box
  298. parameter - NA
  299. return value - ok
  300. */
  301. public JButton getOk()
  302. {
  303.      return ok;
  304. }
  305. /*
  306. getCancel() method - This method is invoked when end user click the Cancel button of the Font dialog box.
  307. parameter - NA
  308. return value - cancel
  309. */
  310. public JButton getCancel()
  311. {
  312.      return cancel;
  313. }
  314. }
  315.  

colorClass.java
Code: Java
  1. /* Imports javax.swing package classes. */
  2. import javax.swing.JPanel;
  3. import javax.swing.JButton;
  4. import javax.swing.JLabel;
  5. import javax.swing.JTextField;
  6. import javax.swing.JSlider;
  7. import javax.swing.JDialog;
  8. import javax.swing.BorderFactory;
  9. /* Imports java.awt package classes. */
  10. import java.awt.GridLayout;
  11. import java.awt.Font;
  12. import java.awt.Color;
  13. /* Imports javax.swing.event package classes. */
  14. import javax.swing.event.ChangeListener;
  15. import javax.swing.event.ChangeEvent;
  16. /*
  17. class ColorClass - This class creates a Color dialog box that enables the end user to
  18. change the color of the file text.
  19. Fields:
  20. panel - Contains all the components of the Color dialog.
  21. redLabel - Contains the content of Red label.
  22. greenLabel - Contains the content of Green label.
  23. blueLabel - Contains the content of Blue label.
  24. previewLabel - Contains the content of Preview label.
  25. redSlider - Enables the end user to select the red value.
  26. greenSlider - Enables the end user to select the green value.
  27. blueSlider - Enables the end user to select the blue value.
  28. labelText - Contains the content of preview text.
  29. ok - Creates an OK button.
  30. cancel - Creates a cancel button.
  31. r - Stores the Red value.
  32. g - Stores the Green value.
  33. b - Stores the Blue value.
  34. Methods:
  35. getOK() - This method returns the OK button object.
  36. getCancel() - This method returns the Cancel button object.
  37. stateChanged() - This method is invoked when an end user slide slider.
  38. color() - This method returns the color.
  39. */
  40. public class ColorClass extends JDialog implements ChangeListener
  41. {
  42. /* Declares the object of the JPanel class. */
  43. JPanel panel;
  44. /* Declares the objects of the JLabel class. */
  45. JLabel redLabel;
  46. JLabel greenLabel;
  47. JLabel blueLabel;
  48. JLabel previewLabel;
  49. /* Declares the objects of the JSlider class */
  50. JSlider redSlider;
  51. JSlider greenSlider;
  52. JSlider blueSlider;
  53. /* Declares the object of the JTextField class */
  54. JTextField labelText;
  55. /* Declares the objects of the JButton class */
  56. JButton cancel;
  57. /* Declares the integer for storing the RGB values */
  58. int r = 0;
  59. int g = 0;
  60. int b = 0;
  61. public Editor ed;
  62. /* Defines the default constructor of the ColorClass class. */
  63. public ColorClass()
  64. {
  65.      /* Sets the title of the Font dialog. */
  66.      setTitle("Color Dialog");
  67.      /* Sets resizable button to false. */
  68.      setResizable(false);
  69.      /* Initializes the object of the JPanel class. */
  70.      panel = new JPanel();
  71.      /* Sets the Layout as GridLayout.*/
  72.      panel.setLayout(new GridLayout(5,2,1,1));
  73.      /* Adds the panel to Color dialog frame */
  74.      getContentPane().add(panel);
  75.      /* Initializes and adds Red label to the panel. */
  76.      redLabel = new JLabel("Red: ");
  77.      panel.add(redLabel);
  78.      /* Initializes and adds Red slider to the panel. */
  79.      redSlider = new JSlider(0, 255, 1);
  80.      panel.add(redSlider);
  81.      /* Sets Border to the Red slider. */
  82.      redSlider.setBorder(BorderFactory.createEtchedBorder());
  83.      /* Adds state change listener to Red slider. */
  84.      redSlider.addChangeListener(this);
  85.      /* Initializes and adds Green label to the panel. */
  86.      greenLabel = new JLabel("Green: ");
  87.      panel.add(greenLabel);
  88.      /* Initializes and adds Green slider to the panel. */
  89.      greenSlider = new JSlider(0, 255, 1);
  90.      panel.add(greenSlider);
  91.      /* Sets Border to the Green slider. */
  92.      greenSlider.setBorder(BorderFactory.createEtchedBorder());
  93.      /* Adds state change listener to Green slider. */
  94.      greenSlider.addChangeListener(this);
  95.      /* Initializes and adds Blue label to the panel. */
  96.      blueLabel = new JLabel("Blue: ");
  97.      panel.add(blueLabel);
  98.      /* Initializes and adds Blue slider to the panel. */
  99.      blueSlider = new JSlider(0, 255, 1);
  100.      panel.add(blueSlider);
  101.      /* Sets Border to the Blue slider. */
  102.      blueSlider.setBorder(BorderFactory.createEtchedBorder());
  103.      /* Adds state change listener to Blue slider. */
  104.      blueSlider.addChangeListener(this);
  105.      /* Initializes and add Preview label to the panel. */
  106.      previewLabel = new JLabel("Preview: ");
  107.      panel.add(previewLabel);
  108.      /*
  109.      Initializes and adds Preview text field to the panel.
  110.      */
  111.      labelText = new JTextField(10);
  112.      panel.add(labelText);
  113.      /* Initializes and adds OK button to the panel. */
  114.      ok = new JButton("OK");
  115.      panel.add(ok);
  116.      /* Initializes and adds Cancel button to the panel. */
  117.      cancel = new JButton("Cancel");
  118.      panel.add(cancel);
  119. }
  120. /*
  121. stateChanged() - This method is called when the user slides any slider of the Color dialog box.
  122. Parameters: ce Represents an object of the ChangeEvent class that contains the details of the event.
  123. Return Value: NA
  124. */
  125. public void stateChanged(ChangeEvent ce)
  126. {
  127.      /*
  128.      This section is executed when end user slides the Red slider.
  129.      */
  130.      if(ce.getSource() == redSlider)
  131.      {
  132.          r = redSlider.getValue();
  133.      }
  134.      /*
  135.      This section is executed, when end user slides the Green slider.
  136.      */
  137.      else if(ce.getSource() == greenSlider)
  138.      {
  139.          g = greenSlider.getValue();
  140.      }
  141.      /*
  142.      This section is executed, when end user slides the Blue slider.
  143.      */
  144.      else if(ce.getSource() == blueSlider)
  145.      {
  146.          b = blueSlider.getValue();
  147.      }
  148.      /* Creates the object of the Color class. */
  149.      Color c = new Color(r, g, B);
  150.      /*
  151.      Sets the background color of the preview text field.
  152.      */
  153.      labelText.setBackground(c);
  154. }
  155. /*
  156. color() - This method is set color of the file text.
  157. Parameters: NA
  158. Return Value: color
  159. */
  160. public Color color()
  161. {
  162.      Color color = new Color(redSlider.getValue(), greenSlider.getValue(), blueSlider.getValue());
  163.      return color;
  164. }
  165. /*
  166. getOk() method - This method is invoked when end user click the OK button of the Color dialog box.
  167. parameter - NA
  168. return value - ok
  169. */
  170. public JButton getOk()
  171. {
  172.      return ok;
  173. }
  174. /*
  175. getCancel() method - This method is invoked when end user click the Cancel
  176. button of the Color dialog box.
  177. parameter - NA
  178. return value - cancel
  179. */
  180. public JButton getCancel()
  181. {
  182.      return cancel;
  183. }
  184. }
  185.  

actionperfromed.java

[code=java]
/* Imports the java.util package classes. */
import java.util.Date;
import java.util.StringTokenizer;
/* Imports the java.io package classes. */
import java.io.File;
import java.io.RandomAccessFile;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
/* Imports the javax.swing package classes. */
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.awt.Font;
/* Imports the java.awt.event package classes. */
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/* Imports the java.nio package classes. */
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
/*
The class ActionPerform - This class implements the core functionality of the Editor.
This class provides the implementation of methods that are called in the Editor class.
Fields:
value - Contains the return value of the showDialog() method.
option - Contains 0 or 1 value.
content - Contains the content of a file.
name - Contains the file name.
word - Contains the word that to be searched.
str - Represents a string variable.
Methods:
newFile() - This method opens a new file.
openFile() - This method calls the method to open an existing file.
saveFile() - This method calls the method to save the existing file.
saveAsFile() - This method calls the method to save the new file.
printFile() - This method enables you to print the file.
exitFile() - This method closes the application.
cutFile() - This method cuts the selected text.
copyFile() - This method copies the selected text.
pasteFile() - This method pastes the cut or copied text.
dateTime() - This method inserts date and time in the file.
exLockFile() - This method locks the file with exclusive lock.
shLockFile() - This method locks the file with share lock.
findFile() - This method finds a specified word in the file.
wrapFile() - This method wraps the text line in the file.
selectAllFile() - This method selects all the contents of the file.
aboutFile() -This method opens the help file.
colorFile() - This method opens a Color dialog box.
fontFile() - This method opens a Font dialog box.
open() - This method opens a new file.
save() - This method saves the existing file.
saveAs() - This method saves the newly created file.
*/
public class ActionPerform
{
int value;
int option;
String content = null;
String name = null;
String word;
String str;
/* Creates an instance of the JFileChooser() class. */
JFileChooser jfc = new JFileChooser(".");
/* Creates an instance of the Editor class. */
public Editor ed;
/* Creates an object of the FontClass() method. */
public FontClass font = new FontClass();
/* Creates an object of the ColorClass() method. */
public ColorClass cc = new ColorClass();
/* Creates an object of the Help() method. */
public Help h = new Help();
/* Creates an object of the PrintClass() method. */
public PrintClass pc;
/*
Defines default constructor of the ActionPerform class.
*/
public ActionPerform(Editor ed)
{
     this.ed = ed;
}
/* Implementation of the newFile() method. */
public void newFile()
{
     if(!ed.area.getText().equals("") && !ed.area.getText().equals(content))
     {
         if(name == null)
         {
         /* Shows a Confirm dialog box. */
         option = JOptionPane.showConfirmDialog(null,"Do you want to save the file?");
         if(option == 0)
         {
             /* Calls the saveAs() method. */
             saveAs();
             /* Sets the text area to be NULL. */
             ed.area.setText("");
             }
       
             if(option == 1)
             {
             ed.area.setText("");
             }
         }
         else
         {
         /* Shows a Confirm dialog box. */
         option = JOptionPane.showConfirmDialog(null,"Do you want to save the file?");
         if(option == 0)
         {
             /* Calls the save() method. */
             save();
             ed.area.setText("");
             }
             if(option == 1)
             {
             ed.area.setText("");
             }
         }
     }
     else
     {
         ed.area.setText("");
     }
     /* Sets the title of the main window. */
     ed.setTitle("Untitled - Text2Text by LEGION");
     }
     /* Implementation of openFile() method. */
     public void openFile()
     {
     if(!ed.area.getText().equals("") && !ed.area.getText().equals(content))
     {
         if(name == null)
         {
         /* Shows a Confirm dialog box. */
         option = JOptionPane.showConfirmDialog(null,"Do you want to save the file?");
         if(option == 0)
         {
             /* Calls the saveAs() method. */
             saveAs();
             open();
         }
         if(option == 1)
         {
             open();
         }
         }
         else
         {
         /* Shows a Confirm dialog box. */
         option = JOptionPane.showConfirmDialog(null,"Do you want to save the file?");
         if(option == 0)
         {
             /* Calls the save() method. */
             save();
         

 

Donate


* Search


* Recent Posts

Image Comparison by Shishka
[May 15, 2017, 01:18:02 pm]


Re: srchub - free source code hosting by Nathan
[December 14, 2015, 11:37:02 pm]


Re: srchub - free source code hosting by Celestialkey
[November 27, 2015, 08:51:42 am]


Updates by Nathan
[October 30, 2015, 08:27:36 pm]


Re: Client-Server Messaging by Nathan
[October 25, 2015, 05:48:57 pm]