Advertisement
MacSG

Frame.java

Mar 4th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 24.13 KB | None | 0 0
  1. package editor;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Font;
  6. import java.awt.Toolkit;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.InputEvent;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.event.WindowEvent;
  12. import java.io.BufferedWriter;
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FileNotFoundException;
  16. import java.io.FileWriter;
  17. import java.util.Scanner;
  18.  
  19. import javax.swing.Action;
  20. import javax.swing.ActionMap;
  21. import javax.swing.ImageIcon;
  22. import javax.swing.JButton;
  23. import javax.swing.JFileChooser;
  24. import javax.swing.JFrame;
  25. import javax.swing.JLabel;
  26. import javax.swing.JMenu;
  27. import javax.swing.JMenuBar;
  28. import javax.swing.JMenuItem;
  29. import javax.swing.JOptionPane;
  30. import javax.swing.JScrollPane;
  31. import javax.swing.JSplitPane;
  32. import javax.swing.JTabbedPane;
  33. import javax.swing.JTextArea;
  34. import javax.swing.JToolBar;
  35. import javax.swing.KeyStroke;
  36. import javax.swing.event.DocumentEvent;
  37. import javax.swing.event.DocumentListener;
  38. import javax.swing.text.DefaultEditorKit;
  39. import javax.swing.text.Element;
  40.  
  41. public class Frame extends JFrame {
  42.  
  43.         public JFrame mainFrame;
  44.         public JMenuBar menu;
  45.  
  46.         public JMenu file;
  47.         public JMenu function;
  48.         public JMenu edit;
  49.         public JMenu font;
  50.         public JMenu fontDecor;
  51.         public JMenu fontSize;
  52.         public JMenu help;
  53.  
  54.         public JMenuItem fileNew;
  55.         public JMenuItem fileOpen;
  56.         public JMenuItem fileSave;
  57.         public JMenuItem fileSaveAs;
  58.         public JMenuItem fileExit;
  59.        
  60.         public JMenuItem functionTokenize;
  61.         public JMenuItem functionCompile;
  62.         public JMenuItem functionExecute;
  63.  
  64.         public JMenuItem editToggleEditable;
  65.         public JMenuItem animation;
  66.  
  67.         public JMenuItem fontArial;
  68.         public JMenuItem fontSerif;
  69.         public JMenuItem fontComicSansMS;
  70.         public JMenuItem fontConsolas;
  71.  
  72.         public JMenuItem fontDecorPlain;
  73.         public JMenuItem fontDecorBold;
  74.         public JMenuItem fontDecorItalic;
  75.         public JMenuItem fontDecorBoldItalic;
  76.  
  77.         public JMenuItem fontSizeSmall;
  78.         public JMenuItem fontSizeNormal;
  79.         public JMenuItem fontSizeBig;
  80.         public JMenuItem fontSizeHuge;
  81.  
  82.         public JMenuItem helpAbout;
  83.         public JMenuItem helpHelp;
  84.  
  85.         public JTextArea field;
  86.         public JTextArea lineSet;
  87.  
  88.         public JScrollPane scroller;
  89.         public JScrollPane scroll;
  90.        
  91.         public JLabel status;
  92.  
  93.         public String fontF = "Arial";
  94.         public int fontD = Font.PLAIN;
  95.         public int fontS = 14;
  96.  
  97.         public Font origFont;
  98.  
  99.         public JFileChooser fileSelect;
  100.  
  101.         public File currentFile;
  102.  
  103.         public JFileChooser dialog = new JFileChooser(System.getProperty("user.dir"));
  104.         public Frame() {           
  105.                
  106.                 menu = new JMenuBar();
  107.                 setJMenuBar(menu);
  108.                 field = new JTextArea();
  109.                 status = new JLabel("Welcome to Text Editor!");
  110.                 fileSelect = new JFileChooser();
  111.  
  112.                 origFont = (new Font(fontF, fontD, fontS));
  113.                 field.setFont(origFont);
  114.                                        
  115.                 file = new JMenu("File");
  116.                 function = new JMenu("Function");
  117.                 edit = new JMenu("Edit");
  118.                 font = new JMenu("Font");
  119.                 fontDecor = new JMenu("Font Decorations");
  120.                 fontSize = new JMenu("Font Size");
  121.                 help = new JMenu("Help");
  122.                
  123.                
  124.                 ActionMap m = field.getActionMap();
  125.                 Action Cut = m.get(DefaultEditorKit.cutAction);
  126.                 Action Copy = m.get(DefaultEditorKit.copyAction);
  127.                 Action Paste = m.get(DefaultEditorKit.pasteAction);
  128.                
  129.                 edit.add(Cut);edit.add(Copy);edit.add(Paste);
  130.  
  131.                 edit.getItem(0).setText("Cut");
  132.                 edit.getItem(1).setText("Copy");
  133.                 edit.getItem(2).setText("Paste");
  134.                
  135.  
  136.                 fileNew = new JMenuItem("New");
  137.                 fileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
  138.                
  139.                 fileOpen = new JMenuItem("Open...");
  140.                 fileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
  141.                
  142.                 fileSave = new JMenuItem("Save");
  143.                 fileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
  144.                
  145.                 fileSaveAs = new JMenuItem("Save As...");
  146.                 fileSaveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK+InputEvent.ALT_MASK));
  147.                
  148.                 fileExit = new JMenuItem("Exit");
  149.                 fileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,InputEvent.ALT_MASK));
  150.  
  151.                 file.add(fileNew);
  152.                 file.add(fileOpen);
  153.                 file.add(fileSave);
  154.                 file.add(fileSaveAs);
  155.                 file.add(fileExit);
  156.                
  157.                 functionTokenize = new JMenuItem("Tokenize");
  158.                 functionCompile = new JMenuItem("Compile");
  159.                 functionExecute = new JMenuItem("Execute");
  160.                
  161.                 function.add(functionTokenize);
  162.                 function.add(functionCompile);
  163.                 function.add(functionExecute);
  164.  
  165.                 editToggleEditable = new JMenuItem("Toggle Editable...");
  166.                 animation = new JMenuItem("View Animation");
  167.                 edit.add(editToggleEditable);
  168.                 edit.add(animation);
  169.                
  170.  
  171.                 fontArial = new JMenuItem("Arial");
  172.                 fontSerif = new JMenuItem("Serif");
  173.                 fontComicSansMS = new JMenuItem("ComicSansMS");
  174.                 fontConsolas = new JMenuItem("Consolas");
  175.  
  176.                 font.add(fontArial);
  177.                 font.add(fontSerif);
  178.                 font.add(fontComicSansMS);
  179.                 font.add(fontConsolas);
  180.  
  181.                 fontDecorPlain = new JMenuItem("Plain");
  182.                 fontDecorBold = new JMenuItem("Bold");
  183.                 fontDecorItalic = new JMenuItem("Italic");
  184.                 fontDecorBoldItalic = new JMenuItem("Bold & Italic");
  185.  
  186.                 fontDecor.add(fontDecorPlain);
  187.                 fontDecor.add(fontDecorBold);
  188.                 fontDecor.add(fontDecorItalic);
  189.                 fontDecor.add(fontDecorBoldItalic);
  190.  
  191.                 fontSizeSmall = new JMenuItem("Small");
  192.                 fontSizeNormal = new JMenuItem("Normal");
  193.                 fontSizeBig = new JMenuItem("Big");
  194.                 fontSizeHuge = new JMenuItem("Huge");
  195.  
  196.                 fontSize.add(fontSizeSmall);
  197.                 fontSize.add(fontSizeNormal);
  198.                 fontSize.add(fontSizeBig);
  199.                 fontSize.add(fontSizeHuge);
  200.  
  201.                 helpAbout = new JMenuItem("About...");
  202.                 helpHelp = new JMenuItem("Help...");
  203.  
  204.                 help.add(helpAbout);
  205.                 help.add(helpHelp);
  206.  
  207.                 menu.add(file);
  208.                 menu.add(function);
  209.                 menu.add(edit);
  210.                 menu.add(font);
  211.                 menu.add(fontDecor);
  212.                 menu.add(fontSize);
  213.                 menu.add(help);
  214.                
  215.                 field.setLineWrap(true);
  216.                 field.setWrapStyleWord(true);
  217.                 scroller = new JScrollPane(field);
  218.                 add(scroller, BorderLayout.CENTER);
  219.                 add(status, BorderLayout.SOUTH);
  220.                
  221.                 JToolBar tool = new JToolBar();
  222.                 add(tool,BorderLayout.NORTH);
  223.                
  224.                 JButton cut = tool.add(Cut), copy = tool.add(Copy),paste = tool.add(Paste);
  225.                
  226.                 cut.setText(null); cut.setIcon(new ImageIcon(getClass().getResource("/images/cut.png")));  
  227.                 cut.setToolTipText("Cut");cut.setSize(100, 100);
  228.                
  229.                 copy.setText(null); copy.setIcon(new ImageIcon(getClass().getResource("/images/copy.png")));   
  230.                 copy.setToolTipText("Copy");copy.setSize(100, 100);
  231.                
  232.                 paste.setText(null); paste.setIcon(new ImageIcon(getClass().getResource("/images/paste.png")));
  233.                 paste.setToolTipText("Paste");paste.setSize(100, 100);
  234.                
  235.                 tool.addSeparator();
  236.                
  237.                 fileNew = new JMenuItem("New");
  238.                 fileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
  239.                
  240.                 fileOpen = new JMenuItem("Open...");
  241.                 fileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
  242.                
  243.                 fileSave = new JMenuItem("Save");
  244.                 fileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
  245.                
  246.                 animation = new JMenuItem("Animation");                
  247.                
  248.                 tool.add(fileNew);tool.add(fileOpen);tool.add(fileSave);tool.add(animation);
  249.                
  250.                
  251.                 createLineNumTop();
  252.                 splitWindow();
  253.                
  254.                 fileNew.addActionListener(new ActionListener() {
  255.                
  256.                         public void actionPerformed(ActionEvent e) {
  257.                                 if (JOptionPane.showConfirmDialog(null, "Are you sure you want to create a new file?") == 0) {
  258.                                         field.setText("");
  259.                                         status.setText("Generated New File...");
  260.                                 }
  261.                         }
  262.                 });
  263.  
  264.                 fileOpen.addActionListener(new ActionListener() {
  265.                         public void actionPerformed(ActionEvent e) {
  266.                                 if (JOptionPane.showConfirmDialog(null,
  267.                                                 "Are you sure you want to delete the current file?") == 0) {
  268.                                         int openResult = fileSelect.showOpenDialog(null);
  269.                                         if (openResult == fileSelect.APPROVE_OPTION) {
  270.                                                 openFile(fileSelect.getSelectedFile());
  271.                                                 status.setText("Opened File...");
  272.                                         }
  273.                                 }
  274.                         }
  275.                 });
  276.                
  277.                 fileSave.addActionListener(new ActionListener(){
  278.                         public void actionPerformed(ActionEvent e){
  279.                                 if(currentFile == null){
  280.                                         int saveResult = fileSelect.showSaveDialog(null);
  281.                                         if(saveResult == fileSelect.APPROVE_OPTION){
  282.                                                 saveFile(fileSelect.getSelectedFile(), field.getText());
  283.                                         }
  284.                                 } else {
  285.                                         saveFile(currentFile, field.getText());
  286.                                 }
  287.                                 status.setText("File Saved...");
  288.                         }
  289.                 });
  290.                
  291.                 fileSaveAs.addActionListener(new ActionListener(){
  292.                         public void actionPerformed(ActionEvent e){
  293.                                 int saveResult = fileSelect.showSaveDialog(null);
  294.                                 saveFile(fileSelect.getSelectedFile(), field.getText());
  295.                                 status.setText("File Saved...");
  296.                         }
  297.                 });
  298.                
  299.                 fileExit.addActionListener(new ActionListener(){
  300.                         public void actionPerformed(ActionEvent e){
  301.                                 if(JOptionPane.showConfirmDialog(null, "Are you sure you wish to exit without saving?") == 0){
  302.                                         closeWindow();
  303.                                 }
  304.                         }
  305.                 });
  306.                
  307.                 editToggleEditable.addActionListener(new ActionListener(){
  308.                         public void actionPerformed(ActionEvent e){
  309.                                 if(field.isEditable()){
  310.                                         field.setEditable(false);
  311.                                         status.setText("Field is No Longer Editable...");
  312.                                 } else if(!field.isEditable()){
  313.                                         field.setEditable(true);
  314.                                         status.setText("Field is Now Editable...");
  315.                                 }
  316.                         }
  317.                 });
  318.                
  319.                 animation.addActionListener(new ActionListener(){
  320.                         public void actionPerformed(ActionEvent e){
  321.                                 JFrame animation = new JFrame("Animation");
  322.                                 animation.pack();
  323.                                 animation.setVisible(true);
  324.                                 animation.setSize(800, 600);
  325.                                 animation.setBackground(Color.WHITE);
  326.                                 animation.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  327.                             }
  328.                     }
  329.                 );
  330.                
  331.                 fontArial.addActionListener(new ActionListener(){
  332.                         public void actionPerformed(ActionEvent e){
  333.                                 fontF = "Arial";
  334.                                 field.setFont(new Font(fontF, fontD, fontS));
  335.                                 status.setText("Font Changed to: Arial");
  336.                         }
  337.                 });
  338.                
  339.                 fontSerif.addActionListener(new ActionListener(){
  340.                         public void actionPerformed(ActionEvent e){
  341.                                 fontF = "Serif";
  342.                                 field.setFont(new Font(fontF, fontD, fontS));
  343.                                 status.setText("Font Changed to: Serif");
  344.                         }
  345.                 });
  346.                
  347.                 fontComicSansMS.addActionListener(new ActionListener(){
  348.                         public void actionPerformed(ActionEvent e){
  349.                                 fontF = "ComicSansMS";
  350.                                 field.setFont(new Font(fontF, fontD, fontS));
  351.                                 status.setText("Font Changed to: ComicSansMS");
  352.                         }
  353.                 });
  354.                
  355.                 fontConsolas.addActionListener(new ActionListener(){
  356.                         public void actionPerformed(ActionEvent e){
  357.                                 fontF = "Consolas";
  358.                                 field.setFont(new Font(fontF, fontD, fontS));
  359.                                 status.setText("Font Changed to: Consolas");
  360.                         }
  361.                 });
  362.  
  363.                 fontDecorPlain.addActionListener(new ActionListener(){
  364.                         public void actionPerformed(ActionEvent e){
  365.                                 fontD = Font.PLAIN;
  366.                                 field.setFont(new Font(fontF, fontD, fontS));
  367.                                 status.setText("Font Decoration Set to Plain...");
  368.                         }
  369.                 });
  370.                
  371.                 fontDecorBold.addActionListener(new ActionListener(){
  372.                         public void actionPerformed(ActionEvent e){
  373.                                 fontD = Font.BOLD;
  374.                                 field.setFont(new Font(fontF, fontD, fontS));
  375.                                 status.setText("Font Decoration Set to Bold...");
  376.                         }
  377.                 });
  378.                
  379.                 fontDecorItalic.addActionListener(new ActionListener(){
  380.                         public void actionPerformed(ActionEvent e){
  381.                                 fontD = Font.ITALIC;
  382.                                 field.setFont(new Font(fontF, fontD, fontS));
  383.                                 status.setText("Font Decoration Set to Italic...");
  384.                         }
  385.                 });
  386.                
  387.                 fontDecorBoldItalic.addActionListener(new ActionListener(){
  388.                         public void actionPerformed(ActionEvent e){
  389.                                 fontD = Font.BOLD + Font.ITALIC;
  390.                                 field.setFont(new Font(fontF, fontD, fontS));
  391.                                 status.setText("Font Decoration Set to Bold & Italic...");
  392.                         }
  393.                 });
  394.                
  395.  
  396.                 fontSizeSmall.addActionListener(new ActionListener(){
  397.                         public void actionPerformed(ActionEvent e){
  398.                                 fontS = 14;
  399.                                 field.setFont(new Font(fontF, fontD, fontS));
  400.                                 status.setText("Font Size Set to SMALL...");
  401.                         }
  402.                 });
  403.                
  404.                 fontSizeNormal.addActionListener(new ActionListener(){
  405.                         public void actionPerformed(ActionEvent e){
  406.                                 fontS = 18;
  407.                                 field.setFont(new Font(fontF, fontD, fontS));
  408.                                 status.setText("Font Size Set to NORMAL...");
  409.                         }
  410.                 });
  411.                
  412.                 fontSizeBig.addActionListener(new ActionListener(){
  413.                         public void actionPerformed(ActionEvent e){
  414.                                 fontS = 24;
  415.                                 field.setFont(new Font(fontF, fontD, fontS));
  416.                                 status.setText("Font Size Set to BIG...");
  417.                         }
  418.                 });
  419.                
  420.                 fontSizeHuge.addActionListener(new ActionListener(){
  421.                         public void actionPerformed(ActionEvent e){
  422.                                 fontS = 36;
  423.                                 field.setFont(new Font(fontF, fontD, fontS));
  424.                                 status.setText("Font Size Set to HUGE...");
  425.                         }
  426.                 });
  427.                
  428.                 helpAbout.addActionListener(new ActionListener(){
  429.                         public void actionPerformed(ActionEvent e){
  430.                             JOptionPane.showMessageDialog(null, "Tanatip Iamjunchai 5510405627");
  431.                         }
  432.                 });
  433.                
  434.                 helpHelp.addActionListener(new ActionListener(){
  435.                         public void actionPerformed(ActionEvent e){
  436.                             JOptionPane.showMessageDialog(null, "Help is coming soon. . .","HELP", JOptionPane.WARNING_MESSAGE);
  437. //                                helpF = new HelpFrame();
  438. //                              
  439. //                                helpF.setSize(300, 100);
  440. //                                helpF.setVisible(true);
  441. //                                helpF.setResizable(false);
  442. //                                helpF.setLocationRelativeTo(null);
  443.                         }
  444.                 });
  445.         }
  446.        
  447.         public void createLineNumTop(){
  448.             //field = new JTextArea();
  449.             add(field);
  450.             scroll = new JScrollPane(field);
  451.             add(scroll);
  452.             lineSet = new JTextArea("1");
  453.             lineSet.setBackground(Color.lightGray);
  454.             lineSet.setColumns(3);
  455.             lineSet.setForeground(Color.BLACK);
  456.             lineSet.setEditable(false);
  457.             field.getDocument().addDocumentListener(new DocumentListener(){
  458.                 public String getText(){
  459.                     int caretPosition = field.getDocument().getLength();
  460.                     Element root = field.getDocument().getDefaultRootElement();
  461.                     String text = "1" + System.getProperty("line.separator");
  462.                     for(int i = 2; i < root.getElementIndex( caretPosition ) + 2; i++){
  463.                         text += i + System.getProperty("line.separator");
  464.                     }
  465.                     return text;
  466.                 }
  467.                 @Override
  468.                 public void changedUpdate(DocumentEvent de) {
  469.                     lineSet.setText(getText());
  470.                 }
  471.      
  472.                 @Override
  473.                 public void insertUpdate(DocumentEvent de) {
  474.                     lineSet.setText(getText());
  475.                 }
  476.      
  477.                 @Override
  478.                 public void removeUpdate(DocumentEvent de) {
  479.                     lineSet.setText(getText());
  480.                 }
  481.      
  482.             });
  483.             scroll.setRowHeaderView(lineSet);
  484.             scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  485.         }
  486.        
  487.         public JTabbedPane createTabbedPane(){
  488.             JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT);
  489.            
  490.             JTextArea error = new JTextArea();
  491.             error.setBackground(new Color(255,102,102));
  492.             error.setEditable(false);
  493.             JScrollPane errorScroll = new JScrollPane(error);
  494.             tabbedPane.addTab("Error",errorScroll);
  495.            
  496.             JTextArea symbol = new JTextArea();
  497.             symbol.setBackground(new Color(255,204,0));
  498.             symbol.setEditable(false);
  499.             JScrollPane symbolScroll = new JScrollPane(symbol);
  500.             tabbedPane.addTab("Symbol",symbolScroll);
  501.            
  502.             JTextArea compile = new JTextArea();
  503.             compile.setBackground(new Color(153,255,102));
  504.             compile.setEditable(false);
  505.             JScrollPane compileScroll = new JScrollPane(compile);
  506.             tabbedPane.addTab("Compile",compileScroll);
  507.            
  508.             JTextArea execute = new JTextArea();
  509.             execute.setBackground(new Color(102,102,255));
  510.             execute.setEditable(false);
  511.             JScrollPane executeScroll = new JScrollPane(execute);
  512.            
  513.             tabbedPane.addTab("Execute",executeScroll);
  514.            
  515.             return tabbedPane;
  516.         }
  517.        
  518.        
  519.         public void splitWindow(){
  520.             JSplitPane spliter = new JSplitPane(JSplitPane.VERTICAL_SPLIT,scroll,createTabbedPane());
  521.             spliter.setDividerLocation(500);
  522.             spliter.setBackground(Color.WHITE);
  523.             spliter.setOneTouchExpandable(true);
  524.             add(spliter);
  525.         }  
  526.        
  527.         public void openFile(File file) {
  528.                 if (file.canRead()) {
  529.  
  530.                         String filePath = file.getPath();
  531.                         String fileContents = "";
  532.  
  533.                         if (filePath.endsWith(".txt")) {
  534.                                 try {
  535.                                         Scanner scan = new Scanner(new FileInputStream(file));
  536.                                         while (scan.hasNextLine()) {
  537.                                                 fileContents += scan.nextLine();
  538.                                         }
  539.  
  540.                                         scan.close();
  541.                                 } catch (FileNotFoundException e) {
  542.  
  543.                                 }
  544.  
  545.                                 field.setText(fileContents);
  546.                                 setTitle("Text Editor" + filePath);
  547.                                 currentFile = file;
  548.                         } else {
  549.                                 JOptionPane.showMessageDialog(null,"That file type is not supported!\nOnly .txt files are supported.");
  550.  
  551.                         }
  552.                 } else JOptionPane.showMessageDialog(null, "Could not open file...");
  553.         }
  554.        
  555.         public void saveFile(File file, String contents){
  556.                 BufferedWriter writer = null;
  557.                 String filePath = file.getPath();
  558.                 if(!filePath.endsWith(".txt")){
  559.                         filePath += ".txt";
  560.                 }
  561.                
  562.                 try{
  563.                         writer = new BufferedWriter(new FileWriter(filePath));
  564.                         writer.write(contents);
  565.                         writer.close();
  566.                         field.setText(contents);
  567.                         setTitle("Text Editor" + filePath);
  568.                         currentFile = file;
  569.                 } catch(Exception e) {
  570.                        
  571.                 }
  572.         }
  573.         public void closeWindow(){
  574.                 WindowEvent close = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
  575.                 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(close);
  576.         }
  577.        
  578. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement